javacommons/gcfprotocols/http/src.s60/nativehttptraansaction.cpp
changeset 21 2a9601315dfc
child 23 98ccebc37403
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  NativeHttpTransaction
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32def.h>
       
    20 #include <http/rhttpresponse.h>
       
    21 #include <http/rhttpheaders.h>
       
    22 #include <httpstringconstants.h>
       
    23 #include <http/thttphdrval.h>
       
    24 #include <stringpool.h>
       
    25 #include <e32svr.h>
       
    26 #include <x509cert.h>
       
    27 //#include <jdebug.h>
       
    28 
       
    29 #include <jni.h>
       
    30 
       
    31 #include "chttpscertinfo.h"
       
    32 #include "nativehttptransaction.h"
       
    33 #include "nativehttpsession.h"
       
    34 #include "chttptransactionclient.h"
       
    35 
       
    36 #include "javajniutils.h"
       
    37 #include "s60commonutils.h"
       
    38 #include "jstringutils.h"
       
    39 
       
    40 #include "fs_methodcall.h"
       
    41 
       
    42 using namespace java::util;
       
    43 
       
    44 const TInt KResponseGranularity=10;
       
    45 
       
    46 void CleanUpResetAndDestroy(TAny *aArray)
       
    47 {
       
    48     if (aArray)
       
    49     {
       
    50         RPointerArray<HBufC>* array=(RPointerArray<HBufC>*)aArray;
       
    51         array->ResetAndDestroy();
       
    52         array->Close();
       
    53     }
       
    54 }
       
    55 
       
    56 
       
    57 NativeHttpTransaction::NativeHttpTransaction(HttpSessionClient& aSession, FunctionServer* aFuncServer)
       
    58         :iSessionClient(aSession) //, java::util::FunctionServer("MyhttpServer2")
       
    59 {
       
    60     //  createServerToNewThread();
       
    61     iFuncServer = aFuncServer;
       
    62 }
       
    63 
       
    64 
       
    65 /**
       
    66  * This uses the TConstructor class to ensure safe construction of the CJavaEventSource
       
    67  * subclass, and then returns the Java handle to the object directly, hence the
       
    68  * somewhat different NewL method.
       
    69  */
       
    70 TInt NativeHttpTransaction::NewL(JNIEnv &aJni, jobject aPeer, HttpSessionClient& aSession,
       
    71                                  const TDesC* aUri, const TDesC* aRequestMethod,java::util::FunctionServer* aFuncServer)
       
    72 {
       
    73     NativeHttpTransaction* self =   new(ELeave) NativeHttpTransaction(aSession,aFuncServer);
       
    74 
       
    75     // Cleanup the event source object if a leave occurs during construction:
       
    76     //TConstructor selfCleanup(self, aJni);
       
    77     self->ConstructL(aJni, aPeer, /*aServer, */ aUri, aRequestMethod);
       
    78 
       
    79 
       
    80     // Pop the cleanup of the object and create a handle:
       
    81     return reinterpret_cast<int>(self); //selfCleanup.GetHandle();
       
    82 }
       
    83 
       
    84 void NativeHttpTransaction::ConstructL(JNIEnv& aJni, jobject aPeer,/* TJavaEventServer aServer,*/
       
    85                                        const TDesC* aUri, const TDesC* aRequestMethod)
       
    86 {
       
    87 
       
    88 
       
    89     //  iJniPeer = aJni.NewWeakGlobalRef(aPeer);
       
    90     iJniPeer = aJni.NewWeakGlobalRef(aPeer);
       
    91 //  CJavaEventSourceBase::ConstructL(aJni, aPeer, aServer);
       
    92     //Set up the Java Call Backs                                           HttpConnectionNative
       
    93     /*
       
    94     jclass httpNativeClass = aJni.FindClass( "com/nokia/mj/impl/http/HttpConnectionNative" );
       
    95     if ( httpNativeClass == NULL )
       
    96         {
       
    97         User::Leave( KErrGeneral );
       
    98         }
       
    99     iTransactionCallbackMethod   = aJni.GetMethodID( httpNativeClass, "transactionSubmitCallback", "(I)V" );
       
   100     if ( iTransactionCallbackMethod  == NULL )
       
   101         {
       
   102         User::Leave( KErrGeneral );
       
   103         }
       
   104 
       
   105     iNotifyDataReadyForReadMethod = aJni.GetMethodID( httpNativeClass, "dataReadyForReadCallBack", "(I)V" );
       
   106     if ( iNotifyDataReadyForReadMethod == NULL )
       
   107         {
       
   108         User::Leave( KErrGeneral );
       
   109         }
       
   110         */
       
   111     // iFuncServer->attachToVm(aJni, aPeer);
       
   112     int handle = reinterpret_cast<int>(this);
       
   113     int urihandle = reinterpret_cast<int>(aUri);
       
   114     int methodhandle = reinterpret_cast<int>(aRequestMethod);
       
   115     //open the transaction
       
   116     //User::LeaveIfError(ExecuteTrap(&NativeHttpTransaction::ExecuteCreateTransactionL, this , aUri , aRequestMethod));
       
   117     CallMethodL(this, &NativeHttpTransaction::ExecuteCreateTransactionL,handle,urihandle , methodhandle, iFuncServer);
       
   118     //ExecuteCreateTransactionL( this , aUri , aRequestMethod);
       
   119 }
       
   120 
       
   121 void NativeHttpTransaction::ExecuteCreateTransactionL(int aSelfhandle, int aUrihandle, int aMethodhandle)
       
   122 {
       
   123     NativeHttpTransaction* aSelf = reinterpret_cast<NativeHttpTransaction*>(aSelfhandle);
       
   124     HBufC* aUri = reinterpret_cast<HBufC*>(aUrihandle);
       
   125     HBufC* aRequestMethod = reinterpret_cast<HBufC*>(aMethodhandle);
       
   126 
       
   127     aSelf->iTransactionClient = CHttpTransactionClient::NewL(aSelf->iSessionClient, aSelf, aUri, aRequestMethod);
       
   128 }
       
   129 
       
   130 void NativeHttpTransaction::SubmitL(JNIEnv* aJni, jobject* /*aPeer*/,const jobjectArray aHeaders, const jbyteArray aPostData, const jint aPostDataLength,
       
   131                                     int aResponseTimeout)
       
   132 {
       
   133 
       
   134     //retrieve the headers
       
   135     RPointerArray<HBufC8> rawHeaderArray;
       
   136     CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,&rawHeaderArray));
       
   137     iJniObject = aJni;
       
   138     //iJniPeer = aPeer;
       
   139     //iFuncServer->attachToVm(*aJni, *aPeer);
       
   140 
       
   141     if (aHeaders!=NULL)
       
   142     {
       
   143         TInt count = aJni->GetArrayLength(aHeaders);
       
   144         if (count>KErrNone)
       
   145         {
       
   146             jstring jniString = NULL;
       
   147             HBufC8* narrowBuffer=NULL;
       
   148             TInt length=0;
       
   149             for (TInt ii=0; ii<count; ++ii)
       
   150             {
       
   151                 jniString = (jstring)aJni->GetObjectArrayElement(aHeaders, ii);
       
   152                 {
       
   153                     JStringUtils rawHeader(*aJni,jniString);
       
   154                     length=rawHeader.Length();
       
   155 
       
   156                     narrowBuffer = HBufC8::NewLC(length);
       
   157                     TPtr8 narrowPtr = narrowBuffer->Des();
       
   158                     narrowPtr.Copy(rawHeader);
       
   159                     rawHeaderArray.Append(narrowBuffer);
       
   160                     CleanupStack::Pop(narrowBuffer);
       
   161                 }
       
   162                 aJni->DeleteLocalRef(jniString);
       
   163             }
       
   164         }
       
   165     }
       
   166     //Convert the post data to native
       
   167     TInt offSet=0;
       
   168     HBufC8* postBuf=NULL;
       
   169     if (aPostDataLength>KErrNone)
       
   170     {
       
   171         postBuf=HBufC8::NewLC(aPostDataLength);
       
   172         TPtr8 bufPtr = postBuf->Des();
       
   173         User::LeaveIfError(S60CommonUtils::CopyToNative(*aJni , aPostData , offSet , aPostDataLength , bufPtr));
       
   174         CleanupStack::Pop(postBuf);
       
   175     }
       
   176     //Submit the transaction
       
   177 
       
   178     int handle = reinterpret_cast<int>(this);
       
   179 
       
   180     int arrayhandle = reinterpret_cast<int>(&rawHeaderArray);
       
   181 
       
   182     int hbufhandle = reinterpret_cast<int>(postBuf);
       
   183 
       
   184     CallMethodL(this, &NativeHttpTransaction::ExecuteSubmitL,handle,arrayhandle , hbufhandle, aResponseTimeout, iFuncServer);
       
   185     if (postBuf!=NULL)
       
   186     {
       
   187         delete postBuf;
       
   188         postBuf = NULL;
       
   189     }
       
   190 
       
   191 //  ExecuteSubmitL( this, &rawHeaderArray , postBuf);
       
   192     CleanupStack::PopAndDestroy();//rawHeaderArray;
       
   193 }
       
   194 
       
   195 void NativeHttpTransaction::ExecuteSubmitL(int aSelfhandle , int aRawHeadershandle , int aPostBufhandle, int aResponseTimeout)
       
   196 {
       
   197     //RPointerArray<HBufC8>* aRawHeaders;
       
   198     NativeHttpTransaction *aSelf = reinterpret_cast<NativeHttpTransaction*>(aSelfhandle);
       
   199     RPointerArray<HBufC8>* aRawHeaders = reinterpret_cast<RPointerArray<HBufC8>*>(aRawHeadershandle);
       
   200     HBufC8* aPostBuf = reinterpret_cast<HBufC8*>(aPostBufhandle);
       
   201 
       
   202     aSelf->iTransactionClient->SubmitL(aRawHeaders, aPostBuf, aResponseTimeout);
       
   203 }
       
   204 
       
   205 jobjectArray NativeHttpTransaction::GetResponseL(JNIEnv* aJni)
       
   206 {
       
   207     jobjectArray objArray=NULL;
       
   208     RPointerArray<HBufC8> rawHeaders(KResponseGranularity);
       
   209     CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,&rawHeaders));
       
   210     //User::LeaveIfError(ExecuteTrap(&NativeHttpTransaction::ExecuteGetResponseL, this, &rawHeaders));
       
   211     int handle = reinterpret_cast<int>(this);
       
   212 
       
   213     int arrayhandle = reinterpret_cast<int>(&rawHeaders);
       
   214     CallMethodL(this, &NativeHttpTransaction::ExecuteGetResponseL,handle,arrayhandle , iFuncServer);
       
   215     //ExecuteGetResponseL( this, &rawHeaders);
       
   216     const TInt headerCount = rawHeaders.Count();
       
   217     if (headerCount>KErrNone)
       
   218     {
       
   219         jclass stringClass = aJni->FindClass("java/lang/String");
       
   220         User::LeaveIfNull(stringClass);
       
   221         objArray = aJni->NewObjectArray(headerCount, stringClass, NULL);
       
   222         User::LeaveIfNull(objArray);
       
   223 
       
   224         HBufC* stringBuf = NULL;
       
   225         HBufC8* rawHeader = NULL;
       
   226         for (TInt i=0; i<headerCount; i++)
       
   227         {
       
   228             rawHeader= (rawHeaders)[i];
       
   229             stringBuf = HBufC::NewLC(rawHeader->Length());
       
   230             TPtr stringBufPtr = stringBuf->Des();
       
   231             stringBufPtr.Copy(*rawHeader);
       
   232             jstring string = S60CommonUtils::NativeToJavaString(*aJni, *stringBuf);
       
   233             aJni->SetObjectArrayElement(objArray, i, string);
       
   234             aJni->DeleteLocalRef(string);
       
   235             CleanupStack::PopAndDestroy(stringBuf);
       
   236         }
       
   237     }
       
   238     CleanupStack::PopAndDestroy();//rawHeaders
       
   239     return  objArray;
       
   240 }
       
   241 
       
   242 
       
   243 void NativeHttpTransaction::ExecuteGetResponseL(int aSelfhandle, int aRawHeadershandle)
       
   244 {
       
   245     NativeHttpTransaction *aSelf = reinterpret_cast<NativeHttpTransaction*>(aSelfhandle);
       
   246     RPointerArray<HBufC8>* aRawHeaders = reinterpret_cast<RPointerArray<HBufC8>*>(aRawHeadershandle);
       
   247     aSelf->iTransactionClient->GetResponseL(aRawHeaders);
       
   248 }
       
   249 /*
       
   250 * Returns the total number of bytes read into the buffer,
       
   251 * or -1 if there is no more data because the end of the stream
       
   252 * has been reached.
       
   253 */
       
   254 TInt NativeHttpTransaction::ReadBytes(TUint8* aBytes, TInt aLength)
       
   255 {
       
   256     //return    Execute(&NativeHttpTransaction::ExecuteReadBytes, this, aBytes , aLength);
       
   257     int handle = reinterpret_cast<int>(this);
       
   258     int uinthandle = reinterpret_cast<int>(aBytes);
       
   259 
       
   260     int ret = 0;
       
   261     CallMethod(ret,this, &NativeHttpTransaction::ExecuteReadBytes,handle,uinthandle,aLength,iFuncServer);
       
   262 
       
   263     return ret;
       
   264     //return    ExecuteReadBytes( this, aBytes , aLength);
       
   265 }
       
   266 
       
   267 TInt NativeHttpTransaction::ExecuteReadBytes(int aSelfhandle, int aByteshandle, TInt aLength)
       
   268 {
       
   269     NativeHttpTransaction *aSelf = reinterpret_cast<NativeHttpTransaction*>(aSelfhandle);
       
   270     TUint8* aBytes = reinterpret_cast<TUint8*>(aByteshandle);
       
   271 
       
   272     TPtr8 bufferPtr(aBytes, aLength);
       
   273     return  aSelf->iTransactionClient->ReadBytes(bufferPtr);
       
   274 }
       
   275 
       
   276 void NativeHttpTransaction::SubmitComplete(TInt aStatus)
       
   277 {
       
   278     LOG(ESOCKET,EInfo,"+NativeHttpTransaction::SubmitComplete");
       
   279 
       
   280 //     jclass httpNativeClass = NULL;
       
   281 //     httpNativeClass = iJniObject->FindClass("com/nokia/mj/impl/http/HttpConnectionNative");
       
   282 //     jmethodID iMethodID  = NULL;
       
   283 //     iMethodID = iJniObject->GetMethodID( httpNativeClass, "transactionSubmitCallback", "(I)V" );
       
   284 //     if ( httpNativeClass == NULL )
       
   285 //       {
       
   286 //          LOG(ESOCKET,EInfo,"+httpNativeClass is null");
       
   287 //       }
       
   288 //     if ( iMethodID == NULL )
       
   289 //       {
       
   290 //          LOG(ESOCKET,EInfo,"+iMethodID is NULL");
       
   291 //       }
       
   292     /*   if ( iJniPeer == NULL )
       
   293          {
       
   294             LOG(ESOCKET,EInfo,"+iJniPeer is null");
       
   295          }*/
       
   296     NativeHttpSession* session = reinterpret_cast<NativeHttpSession*>(iFuncServer);
       
   297     session->doSubmitCallback(aStatus,iJniPeer);
       
   298 
       
   299     // PostEvent(new (ELeave) CHttpTransactionEvent(iTransactionCallbackMethod, aStatus ),CJavaEventBase::ENotifyPriority);
       
   300     LOG(ESOCKET,EInfo,"-NativeHttpTransaction::SubmitComplete");
       
   301 }
       
   302 
       
   303 void NativeHttpTransaction::DataReadyForRead(TInt aStatus)
       
   304 {
       
   305     LOG(ESOCKET,EInfo,"+NativeHttpTransaction::DataReadyForRead");
       
   306 
       
   307 //       jclass httpNativeClass = NULL;
       
   308 //     httpNativeClass = iJniObject->FindClass("com/nokia/mj/impl/http/HttpConnectionNative");
       
   309 //     jmethodID iMethodReadCallBackID  = NULL;
       
   310 //     iMethodReadCallBackID = iJniObject->GetMethodID( httpNativeClass, "dataReadyForReadCallBack", "(I)V" );
       
   311 //
       
   312 //       iFuncServer->mJniEnv->CallVoidMethod(mJavaPeerObject,iMethodReadCallBackID,1);
       
   313 
       
   314     NativeHttpSession* session = reinterpret_cast<NativeHttpSession*>(iFuncServer);
       
   315     session->doReadCallback(aStatus,iJniPeer);
       
   316 
       
   317     //PostEvent(new (ELeave) CHttpTransactionEvent(iNotifyDataReadyForReadMethod, aStatus ),CJavaEventBase::ENotifyPriority);
       
   318     LOG(ESOCKET,EInfo,"-NativeHttpTransaction::DataReadyForRead");
       
   319 }
       
   320 
       
   321 void NativeHttpTransaction::ExecuteCloseTransaction(int aSelfhandle)
       
   322 {
       
   323     NativeHttpTransaction *aSelf = reinterpret_cast<NativeHttpTransaction*>(aSelfhandle);
       
   324     aSelf->iTransactionClient->CloseTransaction();
       
   325 }
       
   326 
       
   327 void NativeHttpTransaction::Dispose()
       
   328 {
       
   329     LOG(ESOCKET,EInfo,"+NativeHttpTransaction::Dispose()");
       
   330     //iFuncServer->stopServer();
       
   331     CloseTransaction();
       
   332     delete iTransactionClient;
       
   333     iTransactionClient=NULL;
       
   334 //  iFuncServer->detachFromVm();
       
   335 
       
   336     LOG(ESOCKET,EInfo,"-NativeHttpTransaction::Dispose()");
       
   337 }
       
   338 
       
   339 
       
   340 void NativeHttpTransaction::CloseTransaction()
       
   341 {
       
   342     //ExecuteV(&NativeHttpTransaction::ExecuteCloseTransaction, this);
       
   343     int handle = reinterpret_cast<int>(this);
       
   344     CallMethod(this, &NativeHttpTransaction::ExecuteCloseTransaction,handle,iFuncServer);
       
   345     //ExecuteCloseTransaction( this);
       
   346 }
       
   347 
       
   348 
       
   349 
       
   350 OS_EXPORT TInt NativeHttpTransaction::GetSecurityInfo()
       
   351 {
       
   352     TInt handle=0;
       
   353     MNativeSecureConnectionInformation* security = iTransactionClient->GetSecurityInfo();
       
   354     if (security)
       
   355     {
       
   356         handle = reinterpret_cast<int>(security);
       
   357     }
       
   358     return handle;
       
   359 }
       
   360 
       
   361 /*
       
   362 * Returns the amount of bytes avaible for read in our buffer.
       
   363 */
       
   364 TInt NativeHttpTransaction::Available()
       
   365 {
       
   366     return iTransactionClient->Available();
       
   367 }
       
   368 
       
   369 
       
   370