javacommons/gcfprotocols/http/src.s60/nativehttptraansaction.cpp
branchRCL_3
changeset 14 04becd199f91
child 23 e5618cc85d74
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     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   
       
    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     
       
    76     self->ConstructL(aJni, aPeer, /*aServer, */ aUri, aRequestMethod);
       
    77 
       
    78 
       
    79     // Pop the cleanup of the object and create a handle:
       
    80     return reinterpret_cast<int>(self); //selfCleanup.GetHandle();
       
    81 }
       
    82 
       
    83 void NativeHttpTransaction::ConstructL(JNIEnv& aJni, jobject aPeer,/* TJavaEventServer aServer,*/
       
    84                                        const TDesC* aUri, const TDesC* aRequestMethod)
       
    85 {
       
    86 
       
    87 
       
    88     iJniPeer = aJni.NewWeakGlobalRef(aPeer);
       
    89 
       
    90 
       
    91     // iFuncServer->attachToVm(aJni, aPeer);
       
    92     int handle = reinterpret_cast<int>(this);
       
    93     int urihandle = reinterpret_cast<int>(aUri);
       
    94     int methodhandle = reinterpret_cast<int>(aRequestMethod);
       
    95     //open the transaction
       
    96     
       
    97     CallMethodL(this, &NativeHttpTransaction::ExecuteCreateTransactionL,handle,urihandle , methodhandle, iFuncServer);
       
    98     
       
    99 }
       
   100 
       
   101 void NativeHttpTransaction::ExecuteCreateTransactionL(int aSelfhandle, int aUrihandle, int aMethodhandle)
       
   102 {
       
   103     NativeHttpTransaction* aSelf = reinterpret_cast<NativeHttpTransaction*>(aSelfhandle);
       
   104     HBufC* aUri = reinterpret_cast<HBufC*>(aUrihandle);
       
   105     HBufC* aRequestMethod = reinterpret_cast<HBufC*>(aMethodhandle);
       
   106 
       
   107     aSelf->iTransactionClient = CHttpTransactionClient::NewL(aSelf->iSessionClient, aSelf, aUri, aRequestMethod);
       
   108 }
       
   109 
       
   110 void NativeHttpTransaction::SubmitL(JNIEnv* aJni, jobject* /*aPeer*/,const jobjectArray aHeaders, const jbyteArray aPostData, const jint aPostDataLength,
       
   111                                     int aResponseTimeout)
       
   112 {
       
   113 
       
   114     //retrieve the headers
       
   115     RPointerArray<HBufC8> rawHeaderArray;
       
   116     CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,&rawHeaderArray));
       
   117     iJniObject = aJni;
       
   118     
       
   119 
       
   120     if (aHeaders!=NULL)
       
   121     {
       
   122         TInt count = aJni->GetArrayLength(aHeaders);
       
   123         if (count>KErrNone)
       
   124         {
       
   125             jstring jniString = NULL;
       
   126             HBufC8* narrowBuffer=NULL;
       
   127             TInt length=0;
       
   128             for (TInt ii=0; ii<count; ++ii)
       
   129             {
       
   130                 jniString = (jstring)aJni->GetObjectArrayElement(aHeaders, ii);
       
   131                 {
       
   132                     JStringUtils rawHeader(*aJni,jniString);
       
   133                     length=rawHeader.Length();
       
   134 
       
   135                     narrowBuffer = HBufC8::NewLC(length);
       
   136                     TPtr8 narrowPtr = narrowBuffer->Des();
       
   137                     narrowPtr.Copy(rawHeader);
       
   138                     rawHeaderArray.Append(narrowBuffer);
       
   139                     CleanupStack::Pop(narrowBuffer);
       
   140                 }
       
   141                 aJni->DeleteLocalRef(jniString);
       
   142             }
       
   143         }
       
   144     }
       
   145     //Convert the post data to native
       
   146     TInt offSet=0;
       
   147     HBufC8* postBuf=NULL;
       
   148     if (aPostDataLength>KErrNone)
       
   149     {
       
   150         postBuf=HBufC8::NewLC(aPostDataLength);
       
   151         TPtr8 bufPtr = postBuf->Des();
       
   152         User::LeaveIfError(S60CommonUtils::CopyToNative(*aJni , aPostData , offSet , aPostDataLength , bufPtr));
       
   153         CleanupStack::Pop(postBuf);
       
   154     }
       
   155     //Submit the transaction
       
   156 
       
   157     int handle = reinterpret_cast<int>(this);
       
   158 
       
   159     int arrayhandle = reinterpret_cast<int>(&rawHeaderArray);
       
   160 
       
   161     int hbufhandle = reinterpret_cast<int>(postBuf);
       
   162 
       
   163     CallMethodL(this, &NativeHttpTransaction::ExecuteSubmitL,handle,arrayhandle , hbufhandle, aResponseTimeout, iFuncServer);
       
   164     if (postBuf!=NULL)
       
   165     {
       
   166         delete postBuf;
       
   167         postBuf = NULL;
       
   168     }
       
   169 
       
   170 
       
   171     CleanupStack::PopAndDestroy();//rawHeaderArray;
       
   172 }
       
   173 
       
   174 void NativeHttpTransaction::ExecuteSubmitL(int aSelfhandle , int aRawHeadershandle , int aPostBufhandle, int aResponseTimeout)
       
   175 {
       
   176     
       
   177     NativeHttpTransaction *aSelf = reinterpret_cast<NativeHttpTransaction*>(aSelfhandle);
       
   178     RPointerArray<HBufC8>* aRawHeaders = reinterpret_cast<RPointerArray<HBufC8>*>(aRawHeadershandle);
       
   179     HBufC8* aPostBuf = reinterpret_cast<HBufC8*>(aPostBufhandle);
       
   180 
       
   181     aSelf->iTransactionClient->SubmitL(aRawHeaders, aPostBuf, aResponseTimeout);
       
   182 }
       
   183 
       
   184 jobjectArray NativeHttpTransaction::GetResponseL(JNIEnv* aJni)
       
   185 {
       
   186     jobjectArray objArray=NULL;
       
   187     RPointerArray<HBufC8> rawHeaders(KResponseGranularity);
       
   188     CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,&rawHeaders));
       
   189     
       
   190     int handle = reinterpret_cast<int>(this);
       
   191 
       
   192     int arrayhandle = reinterpret_cast<int>(&rawHeaders);
       
   193     CallMethodL(this, &NativeHttpTransaction::ExecuteGetResponseL,handle,arrayhandle , iFuncServer);
       
   194     
       
   195     const TInt headerCount = rawHeaders.Count();
       
   196     if (headerCount>KErrNone)
       
   197     {
       
   198         jclass stringClass = aJni->FindClass("java/lang/String");
       
   199         User::LeaveIfNull(stringClass);
       
   200         objArray = aJni->NewObjectArray(headerCount, stringClass, NULL);
       
   201         User::LeaveIfNull(objArray);
       
   202 
       
   203         HBufC* stringBuf = NULL;
       
   204         HBufC8* rawHeader = NULL;
       
   205         for (TInt i=0; i<headerCount; i++)
       
   206         {
       
   207             rawHeader= (rawHeaders)[i];
       
   208             stringBuf = HBufC::NewLC(rawHeader->Length());
       
   209             TPtr stringBufPtr = stringBuf->Des();
       
   210             stringBufPtr.Copy(*rawHeader);
       
   211             jstring string = S60CommonUtils::NativeToJavaString(*aJni, *stringBuf);
       
   212             aJni->SetObjectArrayElement(objArray, i, string);
       
   213             aJni->DeleteLocalRef(string);
       
   214             CleanupStack::PopAndDestroy(stringBuf);
       
   215         }
       
   216     }
       
   217     CleanupStack::PopAndDestroy();//rawHeaders
       
   218     return  objArray;
       
   219 }
       
   220 
       
   221 
       
   222 void NativeHttpTransaction::ExecuteGetResponseL(int aSelfhandle, int aRawHeadershandle)
       
   223 {
       
   224     NativeHttpTransaction *aSelf = reinterpret_cast<NativeHttpTransaction*>(aSelfhandle);
       
   225     RPointerArray<HBufC8>* aRawHeaders = reinterpret_cast<RPointerArray<HBufC8>*>(aRawHeadershandle);
       
   226     aSelf->iTransactionClient->GetResponseL(aRawHeaders);
       
   227 }
       
   228 /*
       
   229 * Returns the total number of bytes read into the buffer,
       
   230 * or -1 if there is no more data because the end of the stream
       
   231 * has been reached.
       
   232 */
       
   233 TInt NativeHttpTransaction::ReadBytes(TUint8* aBytes, TInt aLength)
       
   234 {
       
   235     
       
   236     int handle = reinterpret_cast<int>(this);
       
   237     int uinthandle = reinterpret_cast<int>(aBytes);
       
   238 
       
   239     int ret = 0;
       
   240     CallMethod(ret,this, &NativeHttpTransaction::ExecuteReadBytes,handle,uinthandle,aLength,iFuncServer);
       
   241 
       
   242     return ret;
       
   243     
       
   244 }
       
   245 
       
   246 TInt NativeHttpTransaction::ExecuteReadBytes(int aSelfhandle, int aByteshandle, TInt aLength)
       
   247 {
       
   248     NativeHttpTransaction *aSelf = reinterpret_cast<NativeHttpTransaction*>(aSelfhandle);
       
   249     TUint8* aBytes = reinterpret_cast<TUint8*>(aByteshandle);
       
   250 
       
   251     TPtr8 bufferPtr(aBytes, aLength);
       
   252     return  aSelf->iTransactionClient->ReadBytes(bufferPtr);
       
   253 }
       
   254 
       
   255 void NativeHttpTransaction::SubmitComplete(TInt aStatus)
       
   256 {
       
   257     LOG(ESOCKET,EInfo,"+NativeHttpTransaction::SubmitComplete");
       
   258 
       
   259 
       
   260     NativeHttpSession* session = reinterpret_cast<NativeHttpSession*>(iFuncServer);
       
   261     session->doSubmitCallback(aStatus,iJniPeer);
       
   262 
       
   263     LOG(ESOCKET,EInfo,"-NativeHttpTransaction::SubmitComplete");
       
   264 }
       
   265 
       
   266 void NativeHttpTransaction::DataReadyForRead(TInt aStatus)
       
   267 {
       
   268     LOG(ESOCKET,EInfo,"+NativeHttpTransaction::DataReadyForRead");
       
   269 
       
   270     NativeHttpSession* session = reinterpret_cast<NativeHttpSession*>(iFuncServer);
       
   271     session->doReadCallback(aStatus,iJniPeer);
       
   272 
       
   273     LOG(ESOCKET,EInfo,"-NativeHttpTransaction::DataReadyForRead");
       
   274 }
       
   275 
       
   276 void NativeHttpTransaction::ExecuteCloseTransaction(int aSelfhandle)
       
   277 {
       
   278     NativeHttpTransaction *aSelf = reinterpret_cast<NativeHttpTransaction*>(aSelfhandle);
       
   279     aSelf->iTransactionClient->CloseTransaction();
       
   280 }
       
   281 
       
   282 void NativeHttpTransaction::Dispose()
       
   283 {
       
   284     LOG(ESOCKET,EInfo,"+NativeHttpTransaction::Dispose()");
       
   285     CloseTransaction();
       
   286     delete iTransactionClient;
       
   287     iTransactionClient=NULL;
       
   288 
       
   289     LOG(ESOCKET,EInfo,"-NativeHttpTransaction::Dispose()");
       
   290 }
       
   291 
       
   292 
       
   293 void NativeHttpTransaction::CloseTransaction()
       
   294 {
       
   295     int handle = reinterpret_cast<int>(this);
       
   296     CallMethod(this, &NativeHttpTransaction::ExecuteCloseTransaction,handle,iFuncServer);
       
   297 }
       
   298 
       
   299 
       
   300 
       
   301 OS_EXPORT TInt NativeHttpTransaction::GetSecurityInfo()
       
   302 {
       
   303     TInt handle=0;
       
   304     MNativeSecureConnectionInformation* security = iTransactionClient->GetSecurityInfo();
       
   305     if (security)
       
   306     {
       
   307         handle = reinterpret_cast<int>(security);
       
   308     }
       
   309     return handle;
       
   310 }
       
   311 
       
   312 /*
       
   313 * Returns the amount of bytes avaible for read in our buffer.
       
   314 */
       
   315 TInt NativeHttpTransaction::Available()
       
   316 {
       
   317     return iTransactionClient->Available();
       
   318 }
       
   319 
       
   320 
       
   321