javacommons/gcfprotocols/http/src.s60/nativetransaction.cpp
branchRCL_3
changeset 19 04becd199f91
child 24 0fd27995241b
equal deleted inserted replaced
16:f5050f1da672 19: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:  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__1closeTransaction(
       
    58     JNIEnv* aJni,
       
    59     jobject aPeer,
       
    60     jint aNativeHttpTransaction)
       
    61 {
       
    62 
       
    63     NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction);
       
    64     tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer);
       
    65     try
       
    66 		{
       
    67         tran->Dispose();
       
    68    	}
       
    69    	catch(...)
       
    70    	{
       
    71    		  // function server usage may throw an exception.   		
       
    72    		  // ignore, called when transcation is closed
       
    73    	    ELOG(ESOCKET,"Http JNI Error, exception caught!: _closeTransaction");    	
       
    74    	}
       
    75     delete tran;
       
    76 }
       
    77 
       
    78 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1createNativeTransaction(
       
    79     JNIEnv* aJni,
       
    80     jobject aPeer,
       
    81     jint aNativeHttpSession,
       
    82     jstring aUri,
       
    83     jstring aRequestMethod)
       
    84 {
       
    85 		LOG(ESOCKET,EInfo,"http jni _createNativeTransaction()");
       
    86     NativeHttpSession* session = reinterpret_cast<NativeHttpSession*>(aNativeHttpSession);
       
    87     //tran->iJniPeer = aJni->NewGlobalRef(aPeer);
       
    88     try
       
    89     {
       
    90         TRAPD(handle,   handle = session->CreateTransactionL(aJni, aPeer , aUri, aRequestMethod););
       
    91         return handle;
       
    92     }
       
    93     catch(...)
       
    94     {
       
    95         // function server usage may throw an exception.   	
       
    96         ELOG(ESOCKET,"Http JNI Error, exception caught!: _createTransaction");    	
       
    97         return -1;    	
       
    98     }
       
    99     
       
   100 }
       
   101 
       
   102 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1submitTransaction(
       
   103     JNIEnv* aJni,
       
   104     jobject aPeer,
       
   105     jint aNativeHttpTransaction,
       
   106     jobjectArray aHeaders,
       
   107     jbyteArray aPostData,
       
   108     jint aPostDataLength,
       
   109     jint aResponseTimeout)
       
   110 {
       
   111 		LOG(ESOCKET,EInfo,"http jni _submitTransaction");
       
   112     NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction);
       
   113     int respTimeOut = aResponseTimeout;
       
   114     tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer);
       
   115     try
       
   116     {
       
   117         TRAPD(err,tran->SubmitL(aJni, &aPeer,aHeaders, aPostData, aPostDataLength, respTimeOut));
       
   118         return err;
       
   119     }
       
   120     catch(...)
       
   121     {
       
   122         ELOG(ESOCKET,"Http JNI Error, exception caught!: _submitTransaction");    	
       
   123         return -1;	    	
       
   124     }    
       
   125 }
       
   126 
       
   127 JNIEXPORT jobjectArray JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1getResponse(
       
   128     JNIEnv* aJni,
       
   129     jobject aPeer,
       
   130     jint aNativeHttpTransaction)
       
   131 {
       
   132 		LOG(ESOCKET,EInfo,"http jni _getResponse");
       
   133     jobjectArray rawHeaders=NULL;
       
   134     NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction);
       
   135     tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer);
       
   136     try
       
   137     {
       
   138 		    TRAPD(err, rawHeaders =  tran->GetResponseL(aJni));
       
   139 		    if (err!=KErrNone)
       
   140 		    {
       
   141 		        rawHeaders=NULL;
       
   142 		    }
       
   143 		}
       
   144 		catch(...)
       
   145 		{
       
   146 				rawHeaders=NULL;
       
   147 				ELOG(ESOCKET,"Http JNI Error, exception caught!: _getResponse");    	
       
   148 			
       
   149 		}
       
   150     return rawHeaders;
       
   151 }
       
   152 
       
   153 
       
   154 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1getBytes(
       
   155     JNIEnv*     aEnv,
       
   156     jobject aPeer,
       
   157     jint        aNativeHttpTransaction,
       
   158     jbyteArray  aBytes,
       
   159     jint        aLength)
       
   160 {
       
   161 		LOG(ESOCKET,EInfo,"http jni _getBytes");
       
   162     jbyte* bytes = aEnv->GetByteArrayElements(aBytes, NULL);
       
   163 
       
   164     if (bytes == NULL)
       
   165     {
       
   166         return -1;
       
   167     }
       
   168 		try
       
   169 		{
       
   170 		    NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction);
       
   171 		    tran->iJniPeer = aEnv->NewWeakGlobalRef(aPeer);
       
   172 		    TInt length = tran->ReadBytes(reinterpret_cast<TUint8*>(bytes), aLength);
       
   173 		
       
   174 		    aEnv->ReleaseByteArrayElements(aBytes, bytes, NULL);
       
   175 		    return length;
       
   176 		}
       
   177 		catch(...)
       
   178 		{
       
   179 				ELOG(ESOCKET,"Http JNI Error, exception caught!: _getBytes");    	
       
   180 				return -1;					
       
   181 		}
       
   182 }
       
   183 
       
   184 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1available(
       
   185     JNIEnv* aJni,
       
   186     jobject aPeer,
       
   187     jint aNativeHttpTransaction)
       
   188 {
       
   189     NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction);
       
   190     tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer);
       
   191     try
       
   192     {
       
   193     		return tran->Available();
       
   194     }
       
   195     catch(...)
       
   196     {
       
   197     		ELOG(ESOCKET,"Http JNI Error, exception caught!: _available");    	
       
   198     		return -1;    	
       
   199     }
       
   200 }
       
   201 
       
   202 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1getUserAgentHeaderValue(
       
   203     JNIEnv *aJni,
       
   204     jobject, jboolean aMidpRuntime)
       
   205 {
       
   206     jstring header = NULL;
       
   207     TRAPD(err, header = GetUserAgentL(aJni, aMidpRuntime));
       
   208     if (err != KErrNone)
       
   209     {
       
   210         header = NULL;
       
   211     }
       
   212     return header;
       
   213 }
       
   214 
       
   215 jstring GetUserAgentL(JNIEnv *aJni, jboolean aMidpRuntime)
       
   216 {
       
   217 
       
   218     jstring header = NULL;
       
   219 
       
   220     if (aMidpRuntime == false)
       
   221     {
       
   222         CUserAgent* userAgent = CUserAgent::NewL();
       
   223         CleanupStack::PushL(userAgent);
       
   224 
       
   225         HBufC8* agent8 = userAgent->UserAgentL();
       
   226         CleanupStack::PushL(agent8);
       
   227         HBufC* agent = HBufC::NewMaxLC(agent8->Length());
       
   228         agent->Des().Copy(*agent8);
       
   229         header = S60CommonUtils::NativeToJavaString(*aJni, agent->Des());
       
   230 
       
   231         CleanupStack::PopAndDestroy(agent);
       
   232         CleanupStack::PopAndDestroy(agent8);
       
   233         CleanupStack::PopAndDestroy(userAgent);
       
   234 
       
   235         return header;
       
   236     }
       
   237 
       
   238     CRepository* repository = NULL;
       
   239 
       
   240     repository = CRepository::NewL(KCRUidJ2MEConfiguration);
       
   241 
       
   242     if (repository)
       
   243     {
       
   244         CleanupStack::PushL(repository);
       
   245 
       
   246         TInt headerType;
       
   247         TInt ret = repository->Get(KHTTPUserAgentHeader, headerType);
       
   248         if (ret == KErrNone)
       
   249         {
       
   250             switch (headerType)
       
   251             {
       
   252             case KHTTPUserAgentBrowserHeader:
       
   253             {
       
   254                 CUserAgent* userAgent = CUserAgent::NewL();
       
   255                 CleanupStack::PushL(userAgent);
       
   256 
       
   257                 HBufC8* agent8 = userAgent->UserAgentL();
       
   258                 CleanupStack::PushL(agent8);
       
   259                 HBufC* agent = HBufC::NewMaxLC(agent8->Length());
       
   260                 agent->Des().Copy(*agent8);
       
   261                 header = S60CommonUtils::NativeToJavaString(*aJni, agent->Des());
       
   262 
       
   263                 CleanupStack::PopAndDestroy(agent);
       
   264                 CleanupStack::PopAndDestroy(agent8);
       
   265                 CleanupStack::PopAndDestroy(userAgent);
       
   266             }
       
   267             break;
       
   268 
       
   269             case KHTTPUserAgentDefinedHeader:
       
   270             {
       
   271                 HBufC* headerValue = HBufC::NewLC(KMaxLength);
       
   272                 TPtr ptr(headerValue->Des());
       
   273                 ret = repository->Get(KHTTPUserAgentHeaderValue, ptr);
       
   274                 header = S60CommonUtils::NativeToJavaString(*aJni, headerValue->Des());
       
   275                 CleanupStack::PopAndDestroy(headerValue);
       
   276             }
       
   277             break;
       
   278 
       
   279             case KHTTPUserAgentS60Header:
       
   280             default:
       
   281                 header = NULL;
       
   282                 break;
       
   283             }
       
   284         }
       
   285 
       
   286         CleanupStack::PopAndDestroy(repository);
       
   287     }
       
   288 
       
   289     return header;
       
   290 }