javacommons/gcfprotocols/http/src.s60/nativetransaction.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:  NativeTransaction - all jni calls made through here
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32def.h>
       
    20 #include <centralrepository.h>
       
    21 #include <cuseragent.h>
       
    22 #include "com_nokia_mj_impl_http_HttpConnectionNative.h"
       
    23 #include "nativehttptransaction.h"
       
    24 #include "nativehttpsession.h"
       
    25 #include "monitor.h"
       
    26 #include "logger.h"
       
    27 #include "s60commonutils.h"
       
    28 
       
    29 using namespace java::util;
       
    30 
       
    31 // Constants for default User-Agent Header
       
    32 const TUid KCRUidJ2MEConfiguration = {0x102823D3};
       
    33 const TUint KHTTPUserAgentHeader = 2;
       
    34 const TUint KHTTPUserAgentHeaderValue = 3;
       
    35 const TInt KHTTPUserAgentS60Header = 0;
       
    36 const TInt KHTTPUserAgentBrowserHeader = 1;
       
    37 const TInt KHTTPUserAgentDefinedHeader = 2;
       
    38 const TInt KMaxLength = 1024;
       
    39 
       
    40 // Forward declaration
       
    41 jstring GetUserAgentL(JNIEnv *aJni, jboolean aMidpRuntime);
       
    42 
       
    43 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1createHttpSession(JNIEnv* aJni, jobject aPeer, jint /*aCommsServerHandle*/, jint aType, jint aAPNId, jintArray  aReturnValue)
       
    44 {
       
    45     TInt err = -1, apnerr = 0;
       
    46 
       
    47     TRAPD(handle,   handle = NativeHttpSession::NewL(*aJni, aPeer,  aType, aAPNId, &err, &apnerr));
       
    48     ILOG1(ESOCKET, "createsession returned %d ",err);
       
    49     ILOG1(ESOCKET, "apn setting returned %d ",apnerr);
       
    50     aJni->SetIntArrayRegion(aReturnValue,0,1,&err);
       
    51     aJni->SetIntArrayRegion(aReturnValue,1,1,&apnerr);
       
    52 
       
    53     //DEBUGCOMP_INT( KLogFileHttp ,"Java_com_symbian_midp_io_protocol_http_Protocol__1CreateHttpSession: handle = %d", handle );
       
    54     return handle;
       
    55 }
       
    56 
       
    57 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1dispose(
       
    58     JNIEnv*,
       
    59     jobject,
       
    60     jint aNativeHttpSessionHandle)
       
    61 {
       
    62     LOG(ESOCKET,EInfo,"http jni _dispose()");
       
    63     NativeHttpSession* session = reinterpret_cast<NativeHttpSession*>(aNativeHttpSessionHandle);
       
    64     delete session;
       
    65 }
       
    66 
       
    67 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1closeTransaction(
       
    68     JNIEnv* aJni,
       
    69     jobject aPeer,
       
    70     jint aNativeHttpTransaction)
       
    71 {
       
    72 
       
    73     NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction);
       
    74     tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer);
       
    75 
       
    76     tran->Dispose();
       
    77     delete tran;
       
    78 }
       
    79 
       
    80 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1createNativeTransaction(
       
    81     JNIEnv* aJni,
       
    82     jobject aPeer,
       
    83     jint aNativeHttpSession,
       
    84     jstring aUri,
       
    85     jstring aRequestMethod)
       
    86 {
       
    87     NativeHttpSession* session = reinterpret_cast<NativeHttpSession*>(aNativeHttpSession);
       
    88     //tran->iJniPeer = aJni->NewGlobalRef(aPeer);
       
    89     TRAPD(handle,   handle = session->CreateTransactionL(aJni, aPeer , aUri, aRequestMethod););
       
    90     return handle;
       
    91 }
       
    92 
       
    93 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1submitTransaction(
       
    94     JNIEnv* aJni,
       
    95     jobject aPeer,
       
    96     jint aNativeHttpTransaction,
       
    97     jobjectArray aHeaders,
       
    98     jbyteArray aPostData,
       
    99     jint aPostDataLength,
       
   100     jint aResponseTimeout)
       
   101 {
       
   102     NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction);
       
   103     int respTimeOut = aResponseTimeout;
       
   104     tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer);
       
   105     TRAPD(err,tran->SubmitL(aJni, &aPeer,aHeaders, aPostData, aPostDataLength, respTimeOut));
       
   106     return err;
       
   107 }
       
   108 
       
   109 JNIEXPORT jobjectArray JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1getResponse(
       
   110     JNIEnv* aJni,
       
   111     jobject aPeer,
       
   112     jint aNativeHttpTransaction)
       
   113 {
       
   114     jobjectArray rawHeaders=NULL;
       
   115 //          java::util::Monitor *iMonitor2;
       
   116 //  iMonitor2 = java::util::Monitor::createMonitor();
       
   117 //  iMonitor2->wait();
       
   118 
       
   119     NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction);
       
   120     tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer);
       
   121     TRAPD(err, rawHeaders =  tran->GetResponseL(aJni));
       
   122     if (err!=KErrNone)
       
   123     {
       
   124         rawHeaders=NULL;
       
   125     }
       
   126     return rawHeaders;
       
   127 }
       
   128 
       
   129 
       
   130 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1getBytes(
       
   131     JNIEnv*     aEnv,
       
   132     jobject aPeer,
       
   133     jint        aNativeHttpTransaction,
       
   134     jbyteArray  aBytes,
       
   135     jint        aLength)
       
   136 {
       
   137     jbyte* bytes = aEnv->GetByteArrayElements(aBytes, NULL);
       
   138 
       
   139 
       
   140     if (bytes == NULL)
       
   141     {
       
   142         return -1;
       
   143     }
       
   144 
       
   145     NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction);
       
   146     tran->iJniPeer = aEnv->NewWeakGlobalRef(aPeer);
       
   147     TInt length = tran->ReadBytes(reinterpret_cast<TUint8*>(bytes), aLength);
       
   148 
       
   149     aEnv->ReleaseByteArrayElements(aBytes, bytes, NULL);
       
   150     return length;
       
   151 }
       
   152 
       
   153 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1available(
       
   154     JNIEnv* aJni,
       
   155     jobject aPeer,
       
   156     jint aNativeHttpTransaction)
       
   157 {
       
   158     NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction);
       
   159     tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer);
       
   160     return tran->Available();
       
   161 }
       
   162 
       
   163 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1getUserAgentHeaderValue(
       
   164     JNIEnv *aJni,
       
   165     jobject, jboolean aMidpRuntime)
       
   166 {
       
   167     jstring header = NULL;
       
   168     TRAPD(err, header = GetUserAgentL(aJni, aMidpRuntime));
       
   169     if (err != KErrNone)
       
   170     {
       
   171         header = NULL;
       
   172     }
       
   173     return header;
       
   174 }
       
   175 
       
   176 jstring GetUserAgentL(JNIEnv *aJni, jboolean aMidpRuntime)
       
   177 {
       
   178 
       
   179     jstring header = NULL;
       
   180 
       
   181     if (aMidpRuntime == false)
       
   182     {
       
   183         CUserAgent* userAgent = CUserAgent::NewL();
       
   184         CleanupStack::PushL(userAgent);
       
   185 
       
   186         HBufC8* agent8 = userAgent->UserAgentL();
       
   187         CleanupStack::PushL(agent8);
       
   188         HBufC* agent = HBufC::NewMaxLC(agent8->Length());
       
   189         agent->Des().Copy(*agent8);
       
   190         header = S60CommonUtils::NativeToJavaString(*aJni, agent->Des());
       
   191 
       
   192         CleanupStack::PopAndDestroy(agent);
       
   193         CleanupStack::PopAndDestroy(agent8);
       
   194         CleanupStack::PopAndDestroy(userAgent);
       
   195 
       
   196         return header;
       
   197     }
       
   198 
       
   199     CRepository* repository = NULL;
       
   200 
       
   201     repository = CRepository::NewL(KCRUidJ2MEConfiguration);
       
   202 
       
   203     if (repository)
       
   204     {
       
   205         CleanupStack::PushL(repository);
       
   206 
       
   207         TInt headerType;
       
   208         TInt ret = repository->Get(KHTTPUserAgentHeader, headerType);
       
   209         if (ret == KErrNone)
       
   210         {
       
   211             switch (headerType)
       
   212             {
       
   213             case KHTTPUserAgentBrowserHeader:
       
   214             {
       
   215                 CUserAgent* userAgent = CUserAgent::NewL();
       
   216                 CleanupStack::PushL(userAgent);
       
   217 
       
   218                 HBufC8* agent8 = userAgent->UserAgentL();
       
   219                 CleanupStack::PushL(agent8);
       
   220                 HBufC* agent = HBufC::NewMaxLC(agent8->Length());
       
   221                 agent->Des().Copy(*agent8);
       
   222                 header = S60CommonUtils::NativeToJavaString(*aJni, agent->Des());
       
   223 
       
   224                 CleanupStack::PopAndDestroy(agent);
       
   225                 CleanupStack::PopAndDestroy(agent8);
       
   226                 CleanupStack::PopAndDestroy(userAgent);
       
   227             }
       
   228             break;
       
   229 
       
   230             case KHTTPUserAgentDefinedHeader:
       
   231             {
       
   232                 HBufC* headerValue = HBufC::NewLC(KMaxLength);
       
   233                 TPtr ptr(headerValue->Des());
       
   234                 ret = repository->Get(KHTTPUserAgentHeaderValue, ptr);
       
   235                 header = S60CommonUtils::NativeToJavaString(*aJni, headerValue->Des());
       
   236                 CleanupStack::PopAndDestroy(headerValue);
       
   237             }
       
   238             break;
       
   239 
       
   240             case KHTTPUserAgentS60Header:
       
   241             default:
       
   242                 header = NULL;
       
   243                 break;
       
   244             }
       
   245         }
       
   246 
       
   247         CleanupStack::PopAndDestroy(repository);
       
   248     }
       
   249 
       
   250     return header;
       
   251 }