2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 /***************************************************************************
19 * Description: JNI callbacks implementation for the JNI in process adapter*
20 * Author: Gal Shachor <shachor@il.ibm.com> *
21 * Version: $Revision: 466585 $ *
22 ***************************************************************************/
25 #include "jk_service.h"
30 * Class: org_apache_tomcat_modules_server_JNIConnectionHandler
31 * Method: getNumberOfHeaders
34 JNIEXPORT jint JNICALL
35 Java_org_apache_tomcat_modules_server_JNIConnectionHandler_getNumberOfHeaders
36 (JNIEnv * env, jobject o, jlong s, jlong l)
38 /* [V] Convert indirectly from jlong -> int -> pointer to shut up gcc */
39 /* I hope it's okay on other compilers and/or machines... */
40 jk_ws_service_t *ps = (jk_ws_service_t *)(int)s;
41 jk_logger_t *pl = (jk_logger_t *)(int)l;
43 jk_log(pl, JK_LOG_DEBUG,
44 "Into JNIConnectionHandler::getNumberOfHeaders\n");
47 jk_log(pl, JK_LOG_ERROR,
48 "In JNIConnectionHandler::getNumberOfHeaders, NULL ws service object\n");
49 /* [V] JNIConnectionHandler doesn't handle this */
53 jk_log(pl, JK_LOG_DEBUG,
54 "Done JNIConnectionHandler::getNumberOfHeaders, found %d headers\n",
56 return (jint) ps->num_headers;
60 * Class: org_apache_tomcat_modules_server_JNIConnectionHandler
62 * Signature: (JJ[BII)I
64 JNIEXPORT jint JNICALL
65 Java_org_apache_tomcat_modules_server_JNIConnectionHandler_read
66 (JNIEnv * env, jobject o, jlong s, jlong l, jbyteArray buf, jint from,
69 jk_ws_service_t *ps = (jk_ws_service_t *)(int)s;
70 jk_logger_t *pl = (jk_logger_t *)(int)l;
74 unsigned nfrom = (unsigned)from;
75 unsigned ncnt = (unsigned)cnt;
78 jk_log(pl, JK_LOG_DEBUG, "Into JNIConnectionHandler::read\n");
81 jk_log(pl, JK_LOG_ERROR,
82 "In JNIConnectionHandler::read, NULL ws service object\n");
86 nbuf = (*env)->GetByteArrayElements(env, buf, &iscommit);
89 jk_log(pl, JK_LOG_ERROR,
90 "In JNIConnectionHandler::read, GetByteArrayElements error\n");
94 if (!ps->read(ps, nbuf + nfrom, ncnt, &acc)) {
95 jk_log(pl, JK_LOG_ERROR,
96 "In JNIConnectionHandler::read, failed to read from web server\n");
102 (*env)->ReleaseByteArrayElements(env, buf, nbuf, 0);
104 jk_log(pl, JK_LOG_DEBUG, "Done JNIConnectionHandler::read\n");
109 * Class: org_apache_tomcat_modules_server_JNIConnectionHandler
110 * Method: readEnvironment
111 * Signature: (JJ[Ljava/lang/String;)I
113 JNIEXPORT jint JNICALL
114 Java_org_apache_tomcat_modules_server_JNIConnectionHandler_readEnvironment
115 (JNIEnv * env, jobject o, jlong s, jlong l, jobjectArray envbuf)
117 jk_ws_service_t *ps = (jk_ws_service_t *)(int)s;
118 jk_logger_t *pl = (jk_logger_t *)(int)l;
121 jk_log(pl, JK_LOG_DEBUG,
122 "Into JNIConnectionHandler::readEnvironment. Environment follows --->\n");
125 jk_log(pl, JK_LOG_ERROR,
126 "In JNIConnectionHandler::readEnvironment, NULL ws service object\n");
130 sprintf(port, "%d", ps->server_port);
133 (*env)->SetObjectArrayElement(env,
136 (*env)->NewStringUTF(env, ps->method));
137 jk_log(pl, JK_LOG_DEBUG, "---> method: %s\n", ps->method);
140 (*env)->SetObjectArrayElement(env,
143 (*env)->NewStringUTF(env, ps->req_uri));
144 jk_log(pl, JK_LOG_DEBUG, "---> req_uri: %s\n", ps->req_uri);
146 if (ps->query_string) {
147 (*env)->SetObjectArrayElement(env,
150 (*env)->NewStringUTF(env,
152 jk_log(pl, JK_LOG_DEBUG, "---> query_string: %s\n", ps->query_string);
154 if (ps->remote_addr) {
155 (*env)->SetObjectArrayElement(env,
158 (*env)->NewStringUTF(env,
160 jk_log(pl, JK_LOG_DEBUG, "---> remote_addr: %s\n", ps->remote_addr);
162 if (ps->remote_host) {
163 (*env)->SetObjectArrayElement(env,
166 (*env)->NewStringUTF(env,
168 jk_log(pl, JK_LOG_DEBUG, "---> remote_host: %s\n", ps->remote_host);
170 if (ps->server_name) {
171 (*env)->SetObjectArrayElement(env,
174 (*env)->NewStringUTF(env,
176 jk_log(pl, JK_LOG_DEBUG, "---> server_name: %s\n", ps->server_name);
179 (*env)->SetObjectArrayElement(env,
180 envbuf, 6, (*env)->NewStringUTF(env, port));
181 jk_log(pl, JK_LOG_DEBUG, "---> server_port: %s\n", port);
184 (*env)->SetObjectArrayElement(env,
187 (*env)->NewStringUTF(env,
189 jk_log(pl, JK_LOG_DEBUG, "---> auth_type: %s\n", ps->auth_type);
191 if (ps->remote_user) {
192 (*env)->SetObjectArrayElement(env,
195 (*env)->NewStringUTF(env,
197 jk_log(pl, JK_LOG_DEBUG, "---> remote_user: %s\n", ps->remote_user);
200 (*env)->SetObjectArrayElement(env,
202 9, (*env)->NewStringUTF(env, "https"));
205 (*env)->SetObjectArrayElement(env,
207 9, (*env)->NewStringUTF(env, "http"));
209 jk_log(pl, JK_LOG_DEBUG, "---> is_ssl: %s\n", ps->is_ssl ? "yes" : "no");
212 (*env)->SetObjectArrayElement(env,
215 (*env)->NewStringUTF(env,
217 jk_log(pl, JK_LOG_DEBUG, "---> protocol: %s\n", ps->protocol);
219 if (ps->server_software) {
220 (*env)->SetObjectArrayElement(env,
223 (*env)->NewStringUTF(env,
226 jk_log(pl, JK_LOG_DEBUG, "---> server_software: %s\n",
227 ps->server_software);
231 (*env)->SetObjectArrayElement(env,
234 (*env)->NewStringUTF(env,
236 jk_log(pl, JK_LOG_DEBUG, "---> ssl_cert: %s\n", ps->ssl_cert);
239 if (ps->ssl_cipher) {
240 (*env)->SetObjectArrayElement(env,
243 (*env)->NewStringUTF(env,
246 jk_log(pl, JK_LOG_DEBUG, "---> ssl_cipher: %s\n", ps->ssl_cipher);
249 if (ps->ssl_session) {
250 (*env)->SetObjectArrayElement(env,
253 (*env)->NewStringUTF(env,
256 jk_log(pl, JK_LOG_DEBUG, "---> ssl_session: %s\n",
261 jk_log(pl, JK_LOG_DEBUG, "Done JNIConnectionHandler::readEnvironment\n");
268 * Class: org_apache_tomcat_modules_server_JNIConnectionHandler
269 * Method: readHeaders
270 * Signature: (JJ[Ljava/lang/String;[Ljava/lang/String;)I
272 JNIEXPORT jint JNICALL
273 Java_org_apache_tomcat_modules_server_JNIConnectionHandler_readHeaders
274 (JNIEnv * env, jobject o, jlong s, jlong l, jobjectArray hnames,
275 jobjectArray hvalues)
277 jk_ws_service_t *ps = (jk_ws_service_t *)(int)s;
278 jk_logger_t *pl = (jk_logger_t *)(int)l;
281 jk_log(pl, JK_LOG_DEBUG, "Into JNIConnectionHandler::readHeaders\n");
284 jk_log(pl, JK_LOG_ERROR,
285 "In JNIConnectionHandler::readHeaders, NULL ws service object\n");
289 jk_log(pl, JK_LOG_DEBUG,
290 "In JNIConnectionHandler::readHeaders, %d headers follow --->\n",
293 for (i = 0; i < ps->num_headers; i++) {
294 (*env)->SetObjectArrayElement(env,
297 (*env)->NewStringUTF(env,
300 (*env)->SetObjectArrayElement(env, hvalues, i,
301 (*env)->NewStringUTF(env,
305 jk_log(pl, JK_LOG_DEBUG, "---> %s = %s\n", ps->headers_names[i],
306 ps->headers_values[i]);
308 jk_log(pl, JK_LOG_DEBUG, "Done JNIConnectionHandler::readHeaders\n");
314 * Class: org_apache_tomcat_modules_server_JNIConnectionHandler
315 * Method: startReasponse
316 * Signature: (JJILjava/lang/String;[Ljava/lang/String;[Ljava/lang/String;I)I
318 JNIEXPORT jint JNICALL
319 Java_org_apache_tomcat_modules_server_JNIConnectionHandler_startReasponse
320 (JNIEnv * env, jobject o, jlong s, jlong l,
321 jint sc, jstring msg, jobjectArray hnames, jobjectArray hvalues,
324 jk_ws_service_t *ps = (jk_ws_service_t *)(int)s;
325 jk_logger_t *pl = (jk_logger_t *)(int)l;
326 const char *nmsg = NULL;
327 char **nhnames = NULL;
328 char **nhvalues = NULL;
329 jstring *shnames = NULL;
330 jstring *shvalues = NULL;
334 jk_log(pl, JK_LOG_DEBUG, "Into JNIConnectionHandler::startReasponse\n");
337 jk_log(pl, JK_LOG_ERROR,
338 "In JNIConnectionHandler::startReasponse, NULL ws service object\n");
342 jk_log(pl, JK_LOG_DEBUG,
343 "In JNIConnectionHandler::startReasponse, %d headers follow --->\n",
349 nhnames = (char **)jk_pool_alloc(ps->pool, hcnt * sizeof(char *));
350 nhvalues = (char **)jk_pool_alloc(ps->pool, hcnt * sizeof(char *));
351 shnames = (jstring *) jk_pool_alloc(ps->pool, hcnt * sizeof(jstring));
353 (jstring *) jk_pool_alloc(ps->pool, hcnt * sizeof(jstring));
355 if (nhvalues && nhnames && shnames && shnames) {
356 for (; i < hcnt; i++) {
359 shvalues[i] = shnames[i] = NULL;
360 nhnames[i] = nhvalues[i] = NULL;
362 shnames[i] = (*env)->GetObjectArrayElement(env, hnames, i);
363 shvalues[i] = (*env)->GetObjectArrayElement(env, hvalues, i);
365 if (!shvalues[i] || !shnames[i]) {
366 jk_log(pl, JK_LOG_ERROR,
367 "In JNIConnectionHandler::startReasponse, GetObjectArrayElement error\n");
372 (char *)(*env)->GetStringUTFChars(env, shnames[i],
375 (char *)(*env)->GetStringUTFChars(env, shvalues[i],
378 if (!nhvalues[i] || !nhnames[i]) {
379 jk_log(pl, JK_LOG_ERROR,
380 "In JNIConnectionHandler::startReasponse, GetStringUTFChars error\n");
384 jk_log(pl, JK_LOG_DEBUG, "---> %s=%s\n", nhnames[i],
389 jk_log(pl, JK_LOG_DEBUG,
390 "In JNIConnectionHandler::startReasponse. ----- End headers.\n",
395 jk_log(pl, JK_LOG_ERROR,
396 "In JNIConnectionHandler::startReasponse, memory allocation error\n");
402 nmsg = (*env)->GetStringUTFChars(env, msg, &iscommit);
410 start_response(ps, sc, nmsg, (const char **)nhnames,
411 (const char **)nhvalues, hcnt)) {
413 jk_log(pl, JK_LOG_ERROR,
414 "In JNIConnectionHandler::startReasponse, servers startReasponse failed\n");
419 (*env)->ReleaseStringUTFChars(env, msg, nmsg);
429 for (j = 0; j < i; j++) {
431 (*env)->ReleaseStringUTFChars(env, shvalues[j], nhvalues[j]);
439 for (j = 0; j < i; j++) {
441 (*env)->ReleaseStringUTFChars(env, shnames[j], nhnames[j]);
445 jk_log(pl, JK_LOG_DEBUG, "Done JNIConnectionHandler::startReasponse.\n");
451 * Class: org_apache_tomcat_modules_server_JNIConnectionHandler
453 * Signature: (JJ[BII)I
455 JNIEXPORT jint JNICALL
456 Java_org_apache_tomcat_modules_server_JNIConnectionHandler_write
457 (JNIEnv * env, jobject o, jlong s, jlong l, jbyteArray buf, jint from,
460 jk_ws_service_t *ps = (jk_ws_service_t *)(int)s;
461 jk_logger_t *pl = (jk_logger_t *)(int)l;
465 unsigned nfrom = (unsigned)from;
466 unsigned ncnt = (unsigned)cnt;
468 jk_log(pl, JK_LOG_DEBUG, "In JNIConnectionHandler::write\n");
471 jk_log(pl, JK_LOG_ERROR,
472 "In JNIConnectionHandler::write, NULL ws service object\n");
476 nbuf = (*env)->GetByteArrayElements(env, buf, &iscommit);
479 jk_log(pl, JK_LOG_ERROR,
480 "In JNIConnectionHandler::write, GetByteArrayElements error\n");
484 if (!ps->write(ps, nbuf + nfrom, ncnt)) {
485 jk_log(pl, JK_LOG_ERROR,
486 "In JNIConnectionHandler::write, failed to write to the web server\n");
492 (*env)->ReleaseByteArrayElements(env, buf, nbuf, JNI_ABORT);