diff -r f5050f1da672 -r 04becd199f91 javacommons/gcfprotocols/http/src.s60/nativehttptraansaction.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javacommons/gcfprotocols/http/src.s60/nativehttptraansaction.cpp Tue Apr 27 16:30:29 2010 +0300 @@ -0,0 +1,321 @@ +/* +* Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: NativeHttpTransaction +* +*/ + + +#include +#include +#include +#include +#include +#include +#include +#include +//#include + +#include + +#include "chttpscertinfo.h" +#include "nativehttptransaction.h" +#include "nativehttpsession.h" +#include "chttptransactionclient.h" + +#include "javajniutils.h" +#include "s60commonutils.h" +#include "jstringutils.h" + +#include "fs_methodcall.h" + +using namespace java::util; + +const TInt KResponseGranularity=10; + +void CleanUpResetAndDestroy(TAny *aArray) +{ + if (aArray) + { + RPointerArray* array=(RPointerArray*)aArray; + array->ResetAndDestroy(); + array->Close(); + } +} + + +NativeHttpTransaction::NativeHttpTransaction(HttpSessionClient& aSession, FunctionServer* aFuncServer) + :iSessionClient(aSession) //, java::util::FunctionServer("MyhttpServer2") +{ + + iFuncServer = aFuncServer; +} + + +/** + * This uses the TConstructor class to ensure safe construction of the CJavaEventSource + * subclass, and then returns the Java handle to the object directly, hence the + * somewhat different NewL method. + */ +TInt NativeHttpTransaction::NewL(JNIEnv &aJni, jobject aPeer, HttpSessionClient& aSession, + const TDesC* aUri, const TDesC* aRequestMethod,java::util::FunctionServer* aFuncServer) +{ + NativeHttpTransaction* self = new(ELeave) NativeHttpTransaction(aSession,aFuncServer); + + + self->ConstructL(aJni, aPeer, /*aServer, */ aUri, aRequestMethod); + + + // Pop the cleanup of the object and create a handle: + return reinterpret_cast(self); //selfCleanup.GetHandle(); +} + +void NativeHttpTransaction::ConstructL(JNIEnv& aJni, jobject aPeer,/* TJavaEventServer aServer,*/ + const TDesC* aUri, const TDesC* aRequestMethod) +{ + + + iJniPeer = aJni.NewWeakGlobalRef(aPeer); + + + // iFuncServer->attachToVm(aJni, aPeer); + int handle = reinterpret_cast(this); + int urihandle = reinterpret_cast(aUri); + int methodhandle = reinterpret_cast(aRequestMethod); + //open the transaction + + CallMethodL(this, &NativeHttpTransaction::ExecuteCreateTransactionL,handle,urihandle , methodhandle, iFuncServer); + +} + +void NativeHttpTransaction::ExecuteCreateTransactionL(int aSelfhandle, int aUrihandle, int aMethodhandle) +{ + NativeHttpTransaction* aSelf = reinterpret_cast(aSelfhandle); + HBufC* aUri = reinterpret_cast(aUrihandle); + HBufC* aRequestMethod = reinterpret_cast(aMethodhandle); + + aSelf->iTransactionClient = CHttpTransactionClient::NewL(aSelf->iSessionClient, aSelf, aUri, aRequestMethod); +} + +void NativeHttpTransaction::SubmitL(JNIEnv* aJni, jobject* /*aPeer*/,const jobjectArray aHeaders, const jbyteArray aPostData, const jint aPostDataLength, + int aResponseTimeout) +{ + + //retrieve the headers + RPointerArray rawHeaderArray; + CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,&rawHeaderArray)); + iJniObject = aJni; + + + if (aHeaders!=NULL) + { + TInt count = aJni->GetArrayLength(aHeaders); + if (count>KErrNone) + { + jstring jniString = NULL; + HBufC8* narrowBuffer=NULL; + TInt length=0; + for (TInt ii=0; iiGetObjectArrayElement(aHeaders, ii); + { + JStringUtils rawHeader(*aJni,jniString); + length=rawHeader.Length(); + + narrowBuffer = HBufC8::NewLC(length); + TPtr8 narrowPtr = narrowBuffer->Des(); + narrowPtr.Copy(rawHeader); + rawHeaderArray.Append(narrowBuffer); + CleanupStack::Pop(narrowBuffer); + } + aJni->DeleteLocalRef(jniString); + } + } + } + //Convert the post data to native + TInt offSet=0; + HBufC8* postBuf=NULL; + if (aPostDataLength>KErrNone) + { + postBuf=HBufC8::NewLC(aPostDataLength); + TPtr8 bufPtr = postBuf->Des(); + User::LeaveIfError(S60CommonUtils::CopyToNative(*aJni , aPostData , offSet , aPostDataLength , bufPtr)); + CleanupStack::Pop(postBuf); + } + //Submit the transaction + + int handle = reinterpret_cast(this); + + int arrayhandle = reinterpret_cast(&rawHeaderArray); + + int hbufhandle = reinterpret_cast(postBuf); + + CallMethodL(this, &NativeHttpTransaction::ExecuteSubmitL,handle,arrayhandle , hbufhandle, aResponseTimeout, iFuncServer); + if (postBuf!=NULL) + { + delete postBuf; + postBuf = NULL; + } + + + CleanupStack::PopAndDestroy();//rawHeaderArray; +} + +void NativeHttpTransaction::ExecuteSubmitL(int aSelfhandle , int aRawHeadershandle , int aPostBufhandle, int aResponseTimeout) +{ + + NativeHttpTransaction *aSelf = reinterpret_cast(aSelfhandle); + RPointerArray* aRawHeaders = reinterpret_cast*>(aRawHeadershandle); + HBufC8* aPostBuf = reinterpret_cast(aPostBufhandle); + + aSelf->iTransactionClient->SubmitL(aRawHeaders, aPostBuf, aResponseTimeout); +} + +jobjectArray NativeHttpTransaction::GetResponseL(JNIEnv* aJni) +{ + jobjectArray objArray=NULL; + RPointerArray rawHeaders(KResponseGranularity); + CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,&rawHeaders)); + + int handle = reinterpret_cast(this); + + int arrayhandle = reinterpret_cast(&rawHeaders); + CallMethodL(this, &NativeHttpTransaction::ExecuteGetResponseL,handle,arrayhandle , iFuncServer); + + const TInt headerCount = rawHeaders.Count(); + if (headerCount>KErrNone) + { + jclass stringClass = aJni->FindClass("java/lang/String"); + User::LeaveIfNull(stringClass); + objArray = aJni->NewObjectArray(headerCount, stringClass, NULL); + User::LeaveIfNull(objArray); + + HBufC* stringBuf = NULL; + HBufC8* rawHeader = NULL; + for (TInt i=0; iLength()); + TPtr stringBufPtr = stringBuf->Des(); + stringBufPtr.Copy(*rawHeader); + jstring string = S60CommonUtils::NativeToJavaString(*aJni, *stringBuf); + aJni->SetObjectArrayElement(objArray, i, string); + aJni->DeleteLocalRef(string); + CleanupStack::PopAndDestroy(stringBuf); + } + } + CleanupStack::PopAndDestroy();//rawHeaders + return objArray; +} + + +void NativeHttpTransaction::ExecuteGetResponseL(int aSelfhandle, int aRawHeadershandle) +{ + NativeHttpTransaction *aSelf = reinterpret_cast(aSelfhandle); + RPointerArray* aRawHeaders = reinterpret_cast*>(aRawHeadershandle); + aSelf->iTransactionClient->GetResponseL(aRawHeaders); +} +/* +* Returns the total number of bytes read into the buffer, +* or -1 if there is no more data because the end of the stream +* has been reached. +*/ +TInt NativeHttpTransaction::ReadBytes(TUint8* aBytes, TInt aLength) +{ + + int handle = reinterpret_cast(this); + int uinthandle = reinterpret_cast(aBytes); + + int ret = 0; + CallMethod(ret,this, &NativeHttpTransaction::ExecuteReadBytes,handle,uinthandle,aLength,iFuncServer); + + return ret; + +} + +TInt NativeHttpTransaction::ExecuteReadBytes(int aSelfhandle, int aByteshandle, TInt aLength) +{ + NativeHttpTransaction *aSelf = reinterpret_cast(aSelfhandle); + TUint8* aBytes = reinterpret_cast(aByteshandle); + + TPtr8 bufferPtr(aBytes, aLength); + return aSelf->iTransactionClient->ReadBytes(bufferPtr); +} + +void NativeHttpTransaction::SubmitComplete(TInt aStatus) +{ + LOG(ESOCKET,EInfo,"+NativeHttpTransaction::SubmitComplete"); + + + NativeHttpSession* session = reinterpret_cast(iFuncServer); + session->doSubmitCallback(aStatus,iJniPeer); + + LOG(ESOCKET,EInfo,"-NativeHttpTransaction::SubmitComplete"); +} + +void NativeHttpTransaction::DataReadyForRead(TInt aStatus) +{ + LOG(ESOCKET,EInfo,"+NativeHttpTransaction::DataReadyForRead"); + + NativeHttpSession* session = reinterpret_cast(iFuncServer); + session->doReadCallback(aStatus,iJniPeer); + + LOG(ESOCKET,EInfo,"-NativeHttpTransaction::DataReadyForRead"); +} + +void NativeHttpTransaction::ExecuteCloseTransaction(int aSelfhandle) +{ + NativeHttpTransaction *aSelf = reinterpret_cast(aSelfhandle); + aSelf->iTransactionClient->CloseTransaction(); +} + +void NativeHttpTransaction::Dispose() +{ + LOG(ESOCKET,EInfo,"+NativeHttpTransaction::Dispose()"); + CloseTransaction(); + delete iTransactionClient; + iTransactionClient=NULL; + + LOG(ESOCKET,EInfo,"-NativeHttpTransaction::Dispose()"); +} + + +void NativeHttpTransaction::CloseTransaction() +{ + int handle = reinterpret_cast(this); + CallMethod(this, &NativeHttpTransaction::ExecuteCloseTransaction,handle,iFuncServer); +} + + + +OS_EXPORT TInt NativeHttpTransaction::GetSecurityInfo() +{ + TInt handle=0; + MNativeSecureConnectionInformation* security = iTransactionClient->GetSecurityInfo(); + if (security) + { + handle = reinterpret_cast(security); + } + return handle; +} + +/* +* Returns the amount of bytes avaible for read in our buffer. +*/ +TInt NativeHttpTransaction::Available() +{ + return iTransactionClient->Available(); +} + + +