17 |
17 |
18 |
18 |
19 #include <e32def.h> |
19 #include <e32def.h> |
20 #include <centralrepository.h> |
20 #include <centralrepository.h> |
21 |
21 |
22 #ifdef RD_JAVA_S60_RELEASE_10_1_ONWARDS |
|
23 #include <CUserAgent.h> |
|
24 #else |
|
25 #include <cuseragent.h> |
|
26 #endif |
|
27 |
|
28 #include "com_nokia_mj_impl_http_HttpConnectionNative.h" |
22 #include "com_nokia_mj_impl_http_HttpConnectionNative.h" |
29 #include "nativehttptransaction.h" |
23 #include "nativehttptransaction.h" |
30 #include "nativehttpsession.h" |
24 #include "nativehttpsession.h" |
31 #include "monitor.h" |
25 #include "monitor.h" |
32 #include "logger.h" |
26 #include "logger.h" |
33 #include "s60commonutils.h" |
27 #include "s60commonutils.h" |
|
28 #include "javauseragent.h" |
34 |
29 |
35 using namespace java::util; |
30 using namespace java::util; |
36 |
31 |
37 // Constants for default User-Agent Header |
32 // Constants for default User-Agent Header |
38 const TUid KCRUidJ2MEConfiguration = {0x102823D3}; |
33 const TUid KCRUidJ2MEConfiguration = {0x102823D3}; |
67 { |
62 { |
68 |
63 |
69 NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction); |
64 NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction); |
70 tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer); |
65 tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer); |
71 try |
66 try |
72 { |
67 { |
73 tran->Dispose(); |
68 tran->Dispose(); |
74 } |
69 } |
75 catch(...) |
70 catch (...) |
76 { |
71 { |
77 // function server usage may throw an exception. |
72 // function server usage may throw an exception. |
78 // ignore, called when transcation is closed |
73 // ignore, called when transcation is closed |
79 ELOG(ESOCKET,"Http JNI Error, exception caught!: _closeTransaction"); |
74 ELOG(ESOCKET,"Http JNI Error, exception caught!: _closeTransaction"); |
80 } |
75 } |
81 delete tran; |
76 delete tran; |
82 } |
77 } |
83 |
78 |
84 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1createNativeTransaction( |
79 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1createNativeTransaction( |
85 JNIEnv* aJni, |
80 JNIEnv* aJni, |
86 jobject aPeer, |
81 jobject aPeer, |
87 jint aNativeHttpSession, |
82 jint aNativeHttpSession, |
88 jstring aUri, |
83 jstring aUri, |
89 jstring aRequestMethod) |
84 jstring aRequestMethod) |
90 { |
85 { |
91 LOG(ESOCKET,EInfo,"http jni _createNativeTransaction()"); |
86 LOG(ESOCKET,EInfo,"http jni _createNativeTransaction()"); |
92 NativeHttpSession* session = reinterpret_cast<NativeHttpSession*>(aNativeHttpSession); |
87 NativeHttpSession* session = reinterpret_cast<NativeHttpSession*>(aNativeHttpSession); |
93 //tran->iJniPeer = aJni->NewGlobalRef(aPeer); |
88 //tran->iJniPeer = aJni->NewGlobalRef(aPeer); |
94 try |
89 try |
95 { |
90 { |
96 TRAPD(handle, handle = session->CreateTransactionL(aJni, aPeer , aUri, aRequestMethod);); |
91 TRAPD(handle, handle = session->CreateTransactionL(aJni, aPeer , aUri, aRequestMethod);); |
97 return handle; |
92 return handle; |
98 } |
93 } |
99 catch(...) |
94 catch (...) |
100 { |
95 { |
101 // function server usage may throw an exception. |
96 // function server usage may throw an exception. |
102 ELOG(ESOCKET,"Http JNI Error, exception caught!: _createTransaction"); |
97 ELOG(ESOCKET,"Http JNI Error, exception caught!: _createTransaction"); |
103 return -1; |
98 return -1; |
104 } |
99 } |
105 |
100 |
106 } |
101 } |
107 |
102 |
108 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1submitTransaction( |
103 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1submitTransaction( |
109 JNIEnv* aJni, |
104 JNIEnv* aJni, |
110 jobject aPeer, |
105 jobject aPeer, |
112 jobjectArray aHeaders, |
107 jobjectArray aHeaders, |
113 jbyteArray aPostData, |
108 jbyteArray aPostData, |
114 jint aPostDataLength, |
109 jint aPostDataLength, |
115 jint aResponseTimeout) |
110 jint aResponseTimeout) |
116 { |
111 { |
117 LOG(ESOCKET,EInfo,"http jni _submitTransaction"); |
112 LOG(ESOCKET,EInfo,"http jni _submitTransaction"); |
118 NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction); |
113 NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction); |
119 int respTimeOut = aResponseTimeout; |
114 int respTimeOut = aResponseTimeout; |
120 tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer); |
115 tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer); |
121 try |
116 try |
122 { |
117 { |
123 TRAPD(err,tran->SubmitL(aJni, &aPeer,aHeaders, aPostData, aPostDataLength, respTimeOut)); |
118 TRAPD(err,tran->SubmitL(aJni, &aPeer,aHeaders, aPostData, aPostDataLength, respTimeOut)); |
124 return err; |
119 return err; |
125 } |
120 } |
126 catch(...) |
121 catch (...) |
127 { |
122 { |
128 ELOG(ESOCKET,"Http JNI Error, exception caught!: _submitTransaction"); |
123 ELOG(ESOCKET,"Http JNI Error, exception caught!: _submitTransaction"); |
129 return -1; |
124 return -1; |
130 } |
125 } |
131 } |
126 } |
132 |
127 |
133 JNIEXPORT jobjectArray JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1getResponse( |
128 JNIEXPORT jobjectArray JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1getResponse( |
134 JNIEnv* aJni, |
129 JNIEnv* aJni, |
135 jobject aPeer, |
130 jobject aPeer, |
136 jint aNativeHttpTransaction) |
131 jint aNativeHttpTransaction) |
137 { |
132 { |
138 LOG(ESOCKET,EInfo,"http jni _getResponse"); |
133 LOG(ESOCKET,EInfo,"http jni _getResponse"); |
139 jobjectArray rawHeaders=NULL; |
134 jobjectArray rawHeaders=NULL; |
140 NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction); |
135 NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction); |
141 tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer); |
136 tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer); |
142 try |
137 try |
143 { |
138 { |
144 TRAPD(err, rawHeaders = tran->GetResponseL(aJni)); |
139 TRAPD(err, rawHeaders = tran->GetResponseL(aJni)); |
145 if (err!=KErrNone) |
140 if (err!=KErrNone) |
146 { |
141 { |
147 rawHeaders=NULL; |
142 rawHeaders=NULL; |
148 } |
143 } |
149 } |
144 } |
150 catch(...) |
145 catch (...) |
151 { |
146 { |
152 rawHeaders=NULL; |
147 rawHeaders=NULL; |
153 ELOG(ESOCKET,"Http JNI Error, exception caught!: _getResponse"); |
148 ELOG(ESOCKET,"Http JNI Error, exception caught!: _getResponse"); |
154 |
149 |
155 } |
150 } |
156 return rawHeaders; |
151 return rawHeaders; |
157 } |
152 } |
158 |
153 |
159 |
154 |
160 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1getBytes( |
155 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1getBytes( |
162 jobject aPeer, |
157 jobject aPeer, |
163 jint aNativeHttpTransaction, |
158 jint aNativeHttpTransaction, |
164 jbyteArray aBytes, |
159 jbyteArray aBytes, |
165 jint aLength) |
160 jint aLength) |
166 { |
161 { |
167 LOG(ESOCKET,EInfo,"http jni _getBytes"); |
162 LOG(ESOCKET,EInfo,"http jni _getBytes"); |
168 jbyte* bytes = aEnv->GetByteArrayElements(aBytes, NULL); |
163 jbyte* bytes = aEnv->GetByteArrayElements(aBytes, NULL); |
169 |
164 |
170 if (bytes == NULL) |
165 if (bytes == NULL) |
171 { |
166 { |
172 return -1; |
167 return -1; |
173 } |
168 } |
174 try |
169 try |
175 { |
170 { |
176 NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction); |
171 NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction); |
177 tran->iJniPeer = aEnv->NewWeakGlobalRef(aPeer); |
172 tran->iJniPeer = aEnv->NewWeakGlobalRef(aPeer); |
178 TInt length = tran->ReadBytes(reinterpret_cast<TUint8*>(bytes), aLength); |
173 TInt length = tran->ReadBytes(reinterpret_cast<TUint8*>(bytes), aLength); |
179 |
174 |
180 aEnv->ReleaseByteArrayElements(aBytes, bytes, NULL); |
175 aEnv->ReleaseByteArrayElements(aBytes, bytes, NULL); |
181 return length; |
176 return length; |
182 } |
177 } |
183 catch(...) |
178 catch (...) |
184 { |
179 { |
185 ELOG(ESOCKET,"Http JNI Error, exception caught!: _getBytes"); |
180 ELOG(ESOCKET,"Http JNI Error, exception caught!: _getBytes"); |
186 return -1; |
181 return -1; |
187 } |
182 } |
188 } |
183 } |
189 |
184 |
190 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1available( |
185 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1available( |
191 JNIEnv* aJni, |
186 JNIEnv* aJni, |
192 jobject aPeer, |
187 jobject aPeer, |
194 { |
189 { |
195 NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction); |
190 NativeHttpTransaction* tran = reinterpret_cast<NativeHttpTransaction*>(aNativeHttpTransaction); |
196 tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer); |
191 tran->iJniPeer = aJni->NewWeakGlobalRef(aPeer); |
197 try |
192 try |
198 { |
193 { |
199 return tran->Available(); |
194 return tran->Available(); |
200 } |
195 } |
201 catch(...) |
196 catch (...) |
202 { |
197 { |
203 ELOG(ESOCKET,"Http JNI Error, exception caught!: _available"); |
198 ELOG(ESOCKET,"Http JNI Error, exception caught!: _available"); |
204 return -1; |
199 return -1; |
205 } |
200 } |
206 } |
201 } |
207 |
202 |
208 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1getUserAgentHeaderValue( |
203 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_http_HttpConnectionNative__1getUserAgentHeaderValue( |
209 JNIEnv *aJni, |
204 JNIEnv *aJni, |
218 return header; |
213 return header; |
219 } |
214 } |
220 |
215 |
221 jstring GetUserAgentL(JNIEnv *aJni, jboolean aMidpRuntime) |
216 jstring GetUserAgentL(JNIEnv *aJni, jboolean aMidpRuntime) |
222 { |
217 { |
223 |
218 LOG(ESOCKET,EInfo,"GetUserAgentL() +"); |
224 jstring header = NULL; |
219 jstring header = NULL; |
225 |
220 |
226 if (aMidpRuntime == false) |
221 if (aMidpRuntime == false) |
227 { |
222 { |
228 CUserAgent* userAgent = CUserAgent::NewL(); |
223 HBufC* stringBufPtr = JavaUserAgent::GetUserAgentL(); |
229 CleanupStack::PushL(userAgent); |
224 header = S60CommonUtils::NativeToJavaString(*aJni,stringBufPtr->Des()); |
230 |
225 delete stringBufPtr; |
231 HBufC8* agent8 = userAgent->UserAgentL(); |
|
232 CleanupStack::PushL(agent8); |
|
233 HBufC* agent = HBufC::NewMaxLC(agent8->Length()); |
|
234 agent->Des().Copy(*agent8); |
|
235 header = S60CommonUtils::NativeToJavaString(*aJni, agent->Des()); |
|
236 |
|
237 CleanupStack::PopAndDestroy(agent); |
|
238 CleanupStack::PopAndDestroy(agent8); |
|
239 CleanupStack::PopAndDestroy(userAgent); |
|
240 |
|
241 return header; |
226 return header; |
242 } |
227 } |
243 |
228 |
244 CRepository* repository = NULL; |
229 CRepository* repository = NULL; |
245 |
230 |
255 { |
240 { |
256 switch (headerType) |
241 switch (headerType) |
257 { |
242 { |
258 case KHTTPUserAgentBrowserHeader: |
243 case KHTTPUserAgentBrowserHeader: |
259 { |
244 { |
260 CUserAgent* userAgent = CUserAgent::NewL(); |
245 HBufC* stringBufPtr = (JavaUserAgent::GetUserAgentL()); |
261 CleanupStack::PushL(userAgent); |
246 header = S60CommonUtils::NativeToJavaString(*aJni, stringBufPtr->Des()); |
262 |
247 delete stringBufPtr; |
263 HBufC8* agent8 = userAgent->UserAgentL(); |
|
264 CleanupStack::PushL(agent8); |
|
265 HBufC* agent = HBufC::NewMaxLC(agent8->Length()); |
|
266 agent->Des().Copy(*agent8); |
|
267 header = S60CommonUtils::NativeToJavaString(*aJni, agent->Des()); |
|
268 |
|
269 CleanupStack::PopAndDestroy(agent); |
|
270 CleanupStack::PopAndDestroy(agent8); |
|
271 CleanupStack::PopAndDestroy(userAgent); |
|
272 } |
248 } |
273 break; |
249 break; |
274 |
250 |
275 case KHTTPUserAgentDefinedHeader: |
251 case KHTTPUserAgentDefinedHeader: |
276 { |
252 { |