| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.connector.http.transport; |
| 34 | | | |
| 35 | | | import java.io.ByteArrayOutputStream; |
| 36 | | | import java.io.IOException; |
| 37 | | | import java.io.InputStream; |
| 38 | | | import java.io.OutputStream; |
| 39 | | | import java.io.UnsupportedEncodingException; |
| 40 | | | import java.net.Socket; |
| 41 | | | import java.util.HashMap; |
| 42 | | | import java.util.Iterator; |
| 43 | | | import java.util.Map; |
| 44 | | | import java.util.logging.Logger; |
| 45 | | | import org.jcoderz.commons.util.Constants; |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | (1) | public class ClientHandler |
| 53 | | | extends Thread |
| 54 | | | { |
| 55 | | | |
| 56 | 75 | | private static final String CLASSNAME |
| 57 | | | = ClientHandler.class.getName(); |
| 58 | | | |
| 59 | 100 | | private static final Logger logger |
| 60 | | | = Logger.getLogger(CLASSNAME); |
| 61 | | | private static final int MAX_REQUESTS = 1000; |
| 62 | | | private static final int SLEEP_TIME_BETWEEN_REQUESTS = 50; |
| 63 | | | private static final int BUFFER_SIZE = 4096; |
| 64 | | | private static final String NEW_LINE = "\n"; |
| 65 | | | private static final String UTF8 = "UTF-8"; |
| 66 | | | private final Socket mIncoming; |
| 67 | | | private final int mCounter; |
| 68 | 100 | | private InputStream mInStream = null; |
| 69 | 100 | | private OutputStream mOutStream = null; |
| 70 | 100 | | private boolean mHaveToStop = false; |
| 71 | | | |
| 72 | | | |
| 73 | | | private String mRequestLine; |
| 74 | 100 | | private final Map mRequestParameter = new HashMap(); |
| 75 | | | private String mRequestBody; |
| 76 | | | private String mRequestContentType; |
| 77 | 100 | | private boolean mConnectionHaveToBeClosed = false; |
| 78 | 100 | | private boolean mDoNotRespond = false; |
| 79 | 100 | | private boolean mDoImmediateClose = false; |
| 80 | 100 | | private boolean mUseEmptyResponse = false; |
| 81 | | | |
| 82 | | | |
| 83 | | | private String mResponseLine; |
| 84 | | | private String mResponseBody; |
| 85 | 100 | | private final Map mResponseParameter = new HashMap(); |
| 86 | | | |
| 87 | | | |
| 88 | | | |
| 89 | | | |
| 90 | | | @param |
| 91 | | | |
| 92 | | | @param |
| 93 | | | |
| 94 | | | |
| 95 | | | public ClientHandler (Socket incoming, int counter) |
| 96 | 100 | | { |
| 97 | 100 | | mIncoming = incoming; |
| 98 | 100 | | mCounter = counter; |
| 99 | | | |
| 100 | 100 | | logger.info("+++ Creating Thread " |
| 101 | | | + mCounter + "x" |
| 102 | | | + " for Socket(" + incoming.getPort() + ")\n"); |
| 103 | 100 | | } |
| 104 | | | |
| 105 | | | |
| 106 | | | |
| 107 | | | |
| 108 | | | public void run () |
| 109 | | | { |
| 110 | | | try |
| 111 | | | { |
| 112 | 100 | | mInStream = mIncoming.getInputStream(); |
| 113 | 100 | | mOutStream = mIncoming.getOutputStream(); |
| 114 | 100 | | for (int i = 1; i < MAX_REQUESTS; i++) |
| 115 | | | { |
| 116 | 100 | | if (!gotIncomingRequest()) |
| 117 | | | { |
| 118 | 0 | | break; |
| 119 | | | } |
| 120 | | | |
| 121 | 100 | | logger.info( |
| 122 | | | "+++ ITERATION " + i + "x Thread " + mCounter + "\n"); |
| 123 | | | try |
| 124 | | | { |
| 125 | | | |
| 126 | 100 | | readRequest(); |
| 127 | | | |
| 128 | | | |
| 129 | 100 | | if (mDoImmediateClose) |
| 130 | | | { |
| 131 | 100 | | break; |
| 132 | | | } |
| 133 | | | |
| 134 | | | |
| 135 | 100 | | if (!mDoNotRespond) |
| 136 | | | { |
| 137 | 100 | | writeResponse(); |
| 138 | | | } |
| 139 | | | } |
| 140 | 0 | | catch (Exception ie) |
| 141 | | | { |
| 142 | 0 | | logger.warning( |
| 143 | | | "Exception (" + ie.getMessage() + ") while read/write"); |
| 144 | 0 | (2) | ie.printStackTrace(); |
| 145 | 0 | | break; |
| 146 | 100 | | } |
| 147 | | | |
| 148 | 100 | | if (mConnectionHaveToBeClosed) |
| 149 | | | { |
| 150 | 100 | | break; |
| 151 | | | } |
| 152 | | | } |
| 153 | 100 | | mInStream.close(); |
| 154 | 100 | | mOutStream.close(); |
| 155 | 100 | | mIncoming.close(); |
| 156 | | | } |
| 157 | 0 | | catch (Exception ex) |
| 158 | | | { |
| 159 | 0 | (3) | ex.printStackTrace(); |
| 160 | 100 | | } |
| 161 | 100 | | logger.info("+++ Terminating Thread " + mCounter + "x\n"); |
| 162 | 100 | | } |
| 163 | | | |
| 164 | | | private boolean gotIncomingRequest () |
| 165 | | | { |
| 166 | 100 | | int bytesAvailable = 0; |
| 167 | 100 | | boolean handleRequest = true; |
| 168 | | | for (;;) |
| 169 | | | { |
| 170 | | | try |
| 171 | | | { |
| 172 | 100 | | bytesAvailable = mInStream.available(); |
| 173 | | | } |
| 174 | 0 | | catch (IOException ioe) |
| 175 | | | { |
| 176 | 0 | | logger.warning( |
| 177 | | | "IOException (" + ioe.getMessage() |
| 178 | | | + ") on inputstream"); |
| 179 | 0 | | handleRequest = false; |
| 180 | 0 | | break; |
| 181 | 100 | | } |
| 182 | | | |
| 183 | | | |
| 184 | | | try |
| 185 | | | { |
| 186 | 100 | | Thread.sleep(SLEEP_TIME_BETWEEN_REQUESTS); |
| 187 | | | } |
| 188 | 0 | | catch (InterruptedException ie) |
| 189 | | | { |
| 190 | | | |
| 191 | 100 | | } |
| 192 | | | |
| 193 | 100 | | if (bytesAvailable > 0) |
| 194 | | | { |
| 195 | 100 | | break; |
| 196 | | | } |
| 197 | 100 | | if (mHaveToStop) |
| 198 | | | { |
| 199 | | | |
| 200 | 0 | | handleRequest = false; |
| 201 | 0 | | break; |
| 202 | | | } |
| 203 | | | |
| 204 | | | } |
| 205 | 100 | | return handleRequest; |
| 206 | | | } |
| 207 | | | |
| 208 | | | |
| 209 | | | |
| 210 | | | |
| 211 | | | @throws |
| 212 | | | |
| 213 | | | |
| 214 | | | public void readRequest () |
| 215 | | | throws Exception |
| 216 | | | { |
| 217 | | | |
| 218 | | | try |
| 219 | | | { |
| 220 | | | |
| 221 | 100 | | readRequestLine(); |
| 222 | | | |
| 223 | | | |
| 224 | | | |
| 225 | 100 | | readHeaderAttributes(); |
| 226 | | | |
| 227 | | | |
| 228 | | | |
| 229 | 100 | | readRequestBody(); |
| 230 | | | } |
| 231 | 0 | | catch (Exception ex) |
| 232 | | | { |
| 233 | 0 | | throw ex; |
| 234 | 100 | | } |
| 235 | | | |
| 236 | | | |
| 237 | 100 | (4) | final StringBuffer buffer = new StringBuffer(); |
| 238 | 100 | | buffer.append( |
| 239 | | | "\n- REQUEST ------------------------------------------\n"); |
| 240 | 100 | | buffer.append(requestToString()); |
| 241 | 100 | | buffer.append( |
| 242 | | | "\n-----------------------------------------------------\n"); |
| 243 | 100 | | logger.info(buffer.toString()); |
| 244 | 100 | | } |
| 245 | | | |
| 246 | | | |
| 247 | | | |
| 248 | | | |
| 249 | | | @throws |
| 250 | | | |
| 251 | | | |
| 252 | | | protected void readRequestLine () |
| 253 | | | throws Exception |
| 254 | | | { |
| 255 | 100 | | String errorText = null; |
| 256 | | | |
| 257 | 100 | | String path = null; |
| 258 | 100 | | String httpVersion = null; |
| 259 | | | |
| 260 | | | try |
| 261 | | | { |
| 262 | 100 | | mRequestLine = readLine(); |
| 263 | 100 | | while (mRequestLine != null && !mRequestLine.startsWith("POST")) |
| 264 | | | { |
| 265 | 0 | | mRequestLine = readLine(); |
| 266 | | | } |
| 267 | | | } |
| 268 | 0 | | catch (IOException ioe) |
| 269 | | | { |
| 270 | 0 | | errorText = "IOException with message: " + ioe.getMessage(); |
| 271 | 0 | (5) | throw new Exception(errorText); |
| 272 | 100 | | } |
| 273 | | | |
| 274 | 100 | | if (mRequestLine == null) |
| 275 | | | { |
| 276 | | | |
| 277 | | | |
| 278 | 0 | | errorText = "Error in parsing the request line : unable to find line" |
| 279 | | | + " starting with \"POST\""; |
| 280 | 0 | | throw new Exception(errorText); |
| 281 | | | } |
| 282 | | | |
| 283 | | | |
| 284 | 100 | (6)(7) | final int pathIndex = mRequestLine.indexOf("/"); |
| 285 | 100 | | int afterPathIndex = 0; |
| 286 | 100 | | if (pathIndex > 0) |
| 287 | | | { |
| 288 | 100 | (8)(9) | afterPathIndex = mRequestLine.indexOf(" ", pathIndex); |
| 289 | 100 | | if (afterPathIndex > pathIndex) |
| 290 | | | { |
| 291 | 100 | | path = mRequestLine.substring(pathIndex, afterPathIndex - 1); |
| 292 | | | } |
| 293 | | | } |
| 294 | 100 | | if (path == null) |
| 295 | | | { |
| 296 | 0 | | errorText = "Error in parsing the request line : unable to find path"; |
| 297 | 0 | | throw new Exception(errorText); |
| 298 | | | } |
| 299 | | | |
| 300 | | | |
| 301 | 100 | | httpVersion = mRequestLine.substring(afterPathIndex).trim(); |
| 302 | 100 | (10) | if (httpVersion == null) |
| 303 | | | { |
| 304 | 0 | | errorText |
| 305 | | | = "Error in parsing the request line : unable to find HTTP version"; |
| 306 | 0 | | throw new Exception(errorText); |
| 307 | | | } |
| 308 | | | |
| 309 | 100 | | } |
| 310 | | | |
| 311 | | | |
| 312 | | | |
| 313 | | | @throws |
| 314 | | | |
| 315 | | | |
| 316 | | | private void readHeaderAttributes () |
| 317 | | | throws Exception |
| 318 | | | { |
| 319 | 100 | | String errorText = null; |
| 320 | 100 | | String headerLine = null; |
| 321 | | | for (;;) |
| 322 | | | { |
| 323 | | | try |
| 324 | | | { |
| 325 | 100 | | headerLine = readLine(); |
| 326 | | | } |
| 327 | 0 | | catch (IOException ioe) |
| 328 | | | { |
| 329 | 0 | | errorText = "IOException with message: " + ioe.getMessage(); |
| 330 | 0 | (11) | throw new Exception(errorText); |
| 331 | 100 | | } |
| 332 | | | |
| 333 | | | |
| 334 | 100 | | if ((headerLine == null) || (headerLine.length() < 1)) |
| 335 | | | { |
| 336 | 100 | | break; |
| 337 | | | } |
| 338 | | | |
| 339 | 100 | | final int colon = parseHeaderLine(headerLine); |
| 340 | 100 | | final String name = headerLine.substring(0, colon).trim(); |
| 341 | 100 | | final String value = headerLine.substring(colon + 1).trim(); |
| 342 | | | |
| 343 | 100 | | addRequestHeaderParameter(name, value); |
| 344 | 100 | | } |
| 345 | | | |
| 346 | 100 | | assertRequestParameterGiven(); |
| 347 | | | |
| 348 | | | |
| 349 | 100 | | mRequestContentType |
| 350 | | | = (String) mRequestParameter.get("content-type"); |
| 351 | | | |
| 352 | 100 | | checkSpecialParameter(); |
| 353 | 100 | | setEchoParameterToResponse(); |
| 354 | 100 | | } |
| 355 | | | |
| 356 | | | private void checkSpecialParameter () |
| 357 | | | { |
| 358 | | | |
| 359 | 100 | | final String connectionValue |
| 360 | | | = (String) mRequestParameter.get("connection"); |
| 361 | | | |
| 362 | 100 | (12) | if (connectionValue != null |
| 363 | | | && connectionValue.toLowerCase( |
| 364 | | | Constants.SYSTEM_LOCALE).equals("close")) |
| 365 | | | { |
| 366 | 100 | | mConnectionHaveToBeClosed = true; |
| 367 | | | } |
| 368 | | | |
| 369 | | | |
| 370 | 100 | | final String doNotRespondValue |
| 371 | | | = (String) mRequestParameter.get("donotrespond"); |
| 372 | | | |
| 373 | 100 | (13) | if (doNotRespondValue != null |
| 374 | | | && doNotRespondValue.toLowerCase( |
| 375 | | | Constants.SYSTEM_LOCALE).equals("true")) |
| 376 | | | { |
| 377 | 100 | | mDoNotRespond = true; |
| 378 | | | } |
| 379 | | | |
| 380 | | | |
| 381 | 100 | | final String doImmediateCloseValue |
| 382 | | | = (String) mRequestParameter.get("doimmediateclose"); |
| 383 | | | |
| 384 | 100 | (14) | if (doImmediateCloseValue != null |
| 385 | | | && doImmediateCloseValue.toLowerCase( |
| 386 | | | Constants.SYSTEM_LOCALE).equals("true")) |
| 387 | | | { |
| 388 | 100 | | mDoImmediateClose = true; |
| 389 | | | } |
| 390 | | | |
| 391 | | | |
| 392 | 100 | | final String useEmptyResponse |
| 393 | | | = (String) mRequestParameter.get("useemptyresponse"); |
| 394 | | | |
| 395 | 100 | (15) | if (useEmptyResponse != null |
| 396 | | | && useEmptyResponse.toLowerCase( |
| 397 | | | Constants.SYSTEM_LOCALE).equals("true")) |
| 398 | | | { |
| 399 | 100 | | mUseEmptyResponse = true; |
| 400 | | | } |
| 401 | 100 | | } |
| 402 | | | |
| 403 | | | |
| 404 | | | |
| 405 | | | |
| 406 | | | |
| 407 | | | private void setEchoParameterToResponse () |
| 408 | | | { |
| 409 | 100 | | for (final Iterator it = mRequestParameter.keySet().iterator(); |
| 410 | 100 | | it.hasNext();) |
| 411 | | | { |
| 412 | 100 | | final String key = (String) it.next(); |
| 413 | 100 | | final String value = (String) mRequestParameter.get(key); |
| 414 | | | |
| 415 | 100 | | final String prefix = "echo_"; |
| 416 | 100 | | final int prefixLength = prefix.length(); |
| 417 | 100 | | if (key.startsWith(prefix)) |
| 418 | | | { |
| 419 | 100 | | final String responseKey = key.substring(prefixLength); |
| 420 | 100 | | mResponseParameter.put(responseKey, value); |
| 421 | | | } |
| 422 | 100 | | } |
| 423 | 100 | | } |
| 424 | | | |
| 425 | | | private int parseHeaderLine (String headerLine) |
| 426 | | | throws Exception |
| 427 | | | { |
| 428 | | | |
| 429 | 100 | (16)(17) | final int colon = headerLine.indexOf(":"); |
| 430 | 100 | | if (colon < 0) |
| 431 | | | { |
| 432 | 0 | | final String errorText |
| 433 | | | = "Unable to parse header - no colon found: " + headerLine; |
| 434 | 0 | | throw new Exception(errorText); |
| 435 | | | } |
| 436 | 100 | | return colon; |
| 437 | | | } |
| 438 | | | |
| 439 | | | private void addRequestHeaderParameter (String name, String value) |
| 440 | | | { |
| 441 | 100 | | if (name != null && value != null) |
| 442 | | | { |
| 443 | 100 | | mRequestParameter.put(name.toLowerCase( |
| 444 | | | Constants.SYSTEM_LOCALE), value); |
| 445 | 100 | | logger.info("Header Parameter: " + name + "=" + value); |
| 446 | | | } |
| 447 | 100 | | } |
| 448 | | | |
| 449 | | | private void assertRequestParameterGiven () |
| 450 | | | throws Exception |
| 451 | | | { |
| 452 | 100 | | if (mRequestParameter.isEmpty()) |
| 453 | | | { |
| 454 | 0 | | final String errorText = "no header parameter found"; |
| 455 | 0 | | throw new Exception(errorText); |
| 456 | | | } |
| 457 | 100 | | } |
| 458 | | | |
| 459 | | | |
| 460 | | | |
| 461 | | | @throws |
| 462 | | | |
| 463 | | | |
| 464 | | | private void readRequestBody () |
| 465 | | | throws Exception |
| 466 | | | { |
| 467 | 100 | | String errorText = null; |
| 468 | 100 | | byte[] responseBody = null; |
| 469 | | | |
| 470 | | | |
| 471 | 100 | | final String stringValue |
| 472 | | | = (String) mRequestParameter.get("content-length"); |
| 473 | 100 | | if (stringValue == null) |
| 474 | | | { |
| 475 | 0 | | errorText = "no Content-Length attribute found"; |
| 476 | 0 | | logger.warning("Throwing Exception: " + errorText); |
| 477 | 0 | | throw new Exception(errorText); |
| 478 | | | } |
| 479 | 100 | | final int expectedLength = Integer.parseInt(stringValue); |
| 480 | | | |
| 481 | | | |
| 482 | 100 | | final String encoding = getEncoding(); |
| 483 | 100 | | if (encoding == null) |
| 484 | | | { |
| 485 | 0 | | errorText = "no Content-Type attribute found"; |
| 486 | 0 | | logger.warning("Throwing Exception: " + errorText); |
| 487 | 0 | | throw new Exception(errorText); |
| 488 | | | } |
| 489 | | | |
| 490 | | | try |
| 491 | | | { |
| 492 | 100 | | responseBody = read(expectedLength); |
| 493 | | | } |
| 494 | 0 | | catch (IOException ioe) |
| 495 | | | { |
| 496 | 0 | | errorText = "IOException with message: " + ioe.getMessage(); |
| 497 | 0 | (18) | throw new Exception(errorText); |
| 498 | 100 | | } |
| 499 | | | |
| 500 | | | try |
| 501 | | | { |
| 502 | 100 | | mRequestBody |
| 503 | | | = new String(responseBody, 0, responseBody.length, encoding); |
| 504 | | | } |
| 505 | 0 | | catch (UnsupportedEncodingException ee) |
| 506 | | | { |
| 507 | 0 | | errorText = "error while reading body: " |
| 508 | | | + " encoding (" + encoding + ") is not supported"; |
| 509 | 0 | | logger.warning("Throwing Exception: " + errorText); |
| 510 | 0 | (19) | throw new Exception(errorText); |
| 511 | 100 | | } |
| 512 | 100 | | } |
| 513 | | | |
| 514 | | | |
| 515 | | | |
| 516 | | | @return |
| 517 | | | |
| 518 | | | @throws |
| 519 | | | |
| 520 | | | |
| 521 | | | public String readLine () |
| 522 | | | throws IOException |
| 523 | | | { |
| 524 | 100 | | String result = null; |
| 525 | 100 | | StringBuffer buffer = new StringBuffer(); |
| 526 | | | for (;;) |
| 527 | | | { |
| 528 | 100 | | final int ch = mInStream.read(); |
| 529 | 100 | | if (ch < 0) |
| 530 | | | { |
| 531 | 0 | | if (buffer.length() == 0) |
| 532 | | | { |
| 533 | 0 | | buffer = null; |
| 534 | 0 | | break; |
| 535 | | | } |
| 536 | | | else |
| 537 | | | { |
| 538 | | | break; |
| 539 | | | } |
| 540 | | | } |
| 541 | 100 | | else if (ch == '\r') |
| 542 | | | { |
| 543 | 100 | | continue; |
| 544 | | | } |
| 545 | 100 | | else if (ch == '\n') |
| 546 | | | { |
| 547 | 100 | | break; |
| 548 | | | } |
| 549 | 100 | | buffer.append((char) ch); |
| 550 | 100 | | } |
| 551 | 100 | | if (buffer != null) |
| 552 | | | { |
| 553 | 100 | | result = buffer.toString(); |
| 554 | | | } |
| 555 | 100 | | return result; |
| 556 | | | } |
| 557 | | | |
| 558 | | | |
| 559 | | | |
| 560 | | | |
| 561 | | | @param |
| 562 | | | |
| 563 | | | @return |
| 564 | | | |
| 565 | | | @throws |
| 566 | | | |
| 567 | | | |
| 568 | | | public byte[] read (int expectedLength) |
| 569 | | | throws IOException |
| 570 | | | { |
| 571 | 100 | | final byte[] buffer = new byte[BUFFER_SIZE]; |
| 572 | 100 | | final ByteArrayOutputStream tempOut = new ByteArrayOutputStream(); |
| 573 | | | |
| 574 | 100 | | int bytesRead = 0; |
| 575 | 100 | | int foundLength = 0; |
| 576 | | | |
| 577 | | | |
| 578 | | | |
| 579 | | | |
| 580 | 100 | | while (expectedLength == -1 || foundLength < expectedLength) |
| 581 | | | { |
| 582 | 100 | | bytesRead = mInStream.read(buffer); |
| 583 | | | |
| 584 | | | |
| 585 | 100 | | if (bytesRead == -1) |
| 586 | | | { |
| 587 | 0 | | break; |
| 588 | | | } |
| 589 | 100 | | tempOut.write(buffer, 0, bytesRead); |
| 590 | 100 | | foundLength += bytesRead; |
| 591 | 100 | | if (expectedLength > -1) |
| 592 | | | { |
| 593 | | | |
| 594 | 100 | | if (foundLength == expectedLength) |
| 595 | | | { |
| 596 | 100 | | break; |
| 597 | | | } |
| 598 | 0 | | else if (foundLength > expectedLength) |
| 599 | | | { |
| 600 | 0 | (20) | final StringBuffer strbuf = new StringBuffer(); |
| 601 | 0 | (21) | strbuf.append("++ WARNING WHILE READING RESPONSE BODY ++\n"); |
| 602 | 0 | (22)(23) | strbuf.append("++ expected length (" + expectedLength |
| 603 | | | + ") exceeded ++\n"); |
| 604 | 0 | (24) | strbuf.append("++ found " + foundLength + " bytes ++"); |
| 605 | 0 | | logger.warning(strbuf.toString()); |
| 606 | 0 | | break; |
| 607 | | | } |
| 608 | | | } |
| 609 | | | } |
| 610 | | | |
| 611 | | | try |
| 612 | | | { |
| 613 | 100 | | tempOut.close(); |
| 614 | | | } |
| 615 | 0 | | catch (IOException ioe) |
| 616 | | | { |
| 617 | 0 | | final String errorText |
| 618 | | | = "unable to close buffer with data read from socket" |
| 619 | | | + " as response body"; |
| 620 | 0 | (25)(26) | throw new IOException(errorText); |
| 621 | 100 | | } |
| 622 | 100 | | return tempOut.toByteArray(); |
| 623 | | | } |
| 624 | | | |
| 625 | | | |
| 626 | | | |
| 627 | | | @return |
| 628 | | | |
| 629 | | | |
| 630 | | | public String getEncoding () |
| 631 | | | { |
| 632 | 100 | | String result = null; |
| 633 | 100 | | final String contentType = (String) mRequestParameter.get("content-type"); |
| 634 | | | |
| 635 | 100 | | if (contentType != null) |
| 636 | | | { |
| 637 | 100 | (27) | if (contentType.toUpperCase( |
| 638 | | | Constants.SYSTEM_LOCALE).equals("TEXT/PLAIN")) |
| 639 | | | { |
| 640 | 0 | | result = UTF8; |
| 641 | | | } |
| 642 | | | else |
| 643 | | | { |
| 644 | 100 | | result = getEncodingFromCharsetValue (contentType); |
| 645 | | | } |
| 646 | | | } |
| 647 | 100 | | return result; |
| 648 | | | } |
| 649 | | | |
| 650 | | | |
| 651 | | | |
| 652 | | | |
| 653 | | | @param |
| 654 | | | |
| 655 | | | @return |
| 656 | | | |
| 657 | | | |
| 658 | | | |
| 659 | | | private String getEncodingFromCharsetValue (String contentType) |
| 660 | | | { |
| 661 | 100 | | String result = UTF8; |
| 662 | 100 | | final String charsetName = "CHARSET="; |
| 663 | 100 | | final int charsetIndex |
| 664 | | | = contentType.toUpperCase(Constants.SYSTEM_LOCALE). |
| 665 | | | indexOf(charsetName); |
| 666 | 100 | | if (charsetIndex > 0) |
| 667 | | | { |
| 668 | 100 | (28) | final int quote1 = contentType. |
| 669 | | | indexOf("\"", charsetIndex + charsetName.length()); |
| 670 | 100 | (29)(30) | final int quote2 = contentType. |
| 671 | | | indexOf("\"", quote1 + 1); |
| 672 | 100 | | if (quote1 > 0) |
| 673 | | | { |
| 674 | 0 | | result = contentType. |
| 675 | | | substring(quote1 + 1, quote2). |
| 676 | | | toUpperCase(Constants.SYSTEM_LOCALE); |
| 677 | | | } |
| 678 | | | else |
| 679 | | | { |
| 680 | 100 | | result = contentType. |
| 681 | | | substring(charsetIndex + charsetName.length()). |
| 682 | | | toUpperCase(Constants.SYSTEM_LOCALE); |
| 683 | | | } |
| 684 | 100 | | } |
| 685 | | | else |
| 686 | | | { |
| 687 | 0 | | final String messageText = "no charset defined in Content-Type (" |
| 688 | | | + contentType + ")"; |
| 689 | 0 | | logger.info(messageText); |
| 690 | | | } |
| 691 | 100 | | return result; |
| 692 | | | } |
| 693 | | | |
| 694 | | | |
| 695 | | | |
| 696 | | | @throws |
| 697 | | | |
| 698 | | | |
| 699 | | | public void writeResponse () |
| 700 | | | throws Exception |
| 701 | | | { |
| 702 | 100 | | StringBuffer buffer = new StringBuffer(); |
| 703 | | | |
| 704 | | | |
| 705 | | | |
| 706 | 100 | | mResponseLine = "HTTP/1.0 200 OK"; |
| 707 | | | |
| 708 | | | |
| 709 | 100 | | if (!mUseEmptyResponse) |
| 710 | | | { |
| 711 | 100 | | buffer.append("Echo:"); |
| 712 | 100 | | buffer.append(mRequestBody); |
| 713 | | | } |
| 714 | 100 | | mResponseBody = buffer.toString(); |
| 715 | | | |
| 716 | | | |
| 717 | 100 | | final byte[] messageAsByte = mResponseBody.getBytes(UTF8); |
| 718 | | | |
| 719 | | | |
| 720 | | | |
| 721 | 100 | | if (mConnectionHaveToBeClosed) |
| 722 | | | { |
| 723 | 100 | | mResponseParameter.put("Connection", "Close"); |
| 724 | | | } |
| 725 | | | else |
| 726 | | | { |
| 727 | 100 | | mResponseParameter.put("Connection", "Keep-Alive"); |
| 728 | | | } |
| 729 | | | |
| 730 | 100 | | final int bodyLength = messageAsByte.length; |
| 731 | 100 | | mResponseParameter.put( |
| 732 | | | "Content-Length", Integer.toString(bodyLength)); |
| 733 | 100 | | mResponseParameter.put("Content-Type", mRequestContentType); |
| 734 | | | |
| 735 | | | |
| 736 | 100 | | buffer = new StringBuffer(); |
| 737 | 100 | | buffer.append(mResponseLine); |
| 738 | 100 | | buffer.append(NEW_LINE); |
| 739 | 100 | | buffer.append(getResponseParameterAsString()); |
| 740 | 100 | | buffer.append(NEW_LINE); |
| 741 | 100 | | buffer.append(mResponseBody); |
| 742 | | | |
| 743 | | | |
| 744 | 100 | (31) | final StringBuffer strbuf = new StringBuffer(); |
| 745 | 100 | | strbuf.append("\n-- RESPONSE --\n"); |
| 746 | 100 | | strbuf.append(buffer.toString()); |
| 747 | 100 | | strbuf.append("\n--------------\n"); |
| 748 | 100 | | logger.info(strbuf.toString()); |
| 749 | | | |
| 750 | | | |
| 751 | 100 | | final byte[] messageAsBytes = buffer.toString().getBytes(UTF8); |
| 752 | | | |
| 753 | 100 | | mOutStream.write(messageAsBytes); |
| 754 | 100 | | } |
| 755 | | | |
| 756 | | | |
| 757 | | | |
| 758 | | | @return |
| 759 | | | |
| 760 | | | |
| 761 | | | private String getResponseParameterAsString () |
| 762 | | | { |
| 763 | 100 | | final StringBuffer buffer = new StringBuffer(); |
| 764 | 100 | | final Iterator keyIterator = mResponseParameter.keySet().iterator(); |
| 765 | 100 | | while (keyIterator.hasNext()) |
| 766 | | | { |
| 767 | 100 | | final String key = (String) keyIterator.next(); |
| 768 | 100 | | final String value = (String) mResponseParameter.get(key); |
| 769 | 100 | | buffer.append(key); |
| 770 | 100 | | buffer.append(": "); |
| 771 | 100 | | buffer.append(value); |
| 772 | 100 | | buffer.append(NEW_LINE); |
| 773 | 100 | | } |
| 774 | 100 | | return buffer.toString(); |
| 775 | | | } |
| 776 | | | |
| 777 | | | |
| 778 | | | |
| 779 | | | @return |
| 780 | | | |
| 781 | | | |
| 782 | | | private String requestToString () |
| 783 | | | { |
| 784 | 100 | | final StringBuffer buffer = new StringBuffer(); |
| 785 | 100 | | buffer.append(mRequestLine); |
| 786 | 100 | | buffer.append(NEW_LINE); |
| 787 | 100 | | final Iterator keyIterator = mRequestParameter.keySet().iterator(); |
| 788 | 100 | | while (keyIterator.hasNext()) |
| 789 | | | { |
| 790 | 100 | | final String key = (String) keyIterator.next(); |
| 791 | 100 | | final String value = (String) mRequestParameter.get(key); |
| 792 | 100 | | buffer.append(key); |
| 793 | 100 | | buffer.append(": "); |
| 794 | 100 | | buffer.append(value); |
| 795 | 100 | | buffer.append(NEW_LINE); |
| 796 | 100 | | } |
| 797 | 100 | | buffer.append(NEW_LINE); |
| 798 | 100 | | buffer.append(mRequestBody); |
| 799 | 100 | | return buffer.toString(); |
| 800 | | | } |
| 801 | | | |
| 802 | | | |
| 803 | | | |
| 804 | | | |
| 805 | | | public void haveToStop () |
| 806 | | | { |
| 807 | 0 | | mHaveToStop = true; |
| 808 | 0 | | } |
| 809 | | | |
| 810 | | | } |