1 16 package org.apache.axis2.transport.http; 17 18 import org.apache.axis2.context.ConfigurationContext; 19 import org.apache.axis2.description.OperationDescription; 20 import org.apache.axis2.description.ServiceDescription; 21 import org.apache.axis2.engine.AxisFault; 22 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.util.*; 26 27 30 public class HTTPTransportReceiver { 31 34 private static final int END = 1; 35 36 39 private static final int END_OF_LINE = 2; 40 41 44 private static final int BEFORE_SEPERATOR = 3; 45 46 49 private static final int AFTER_SEPERATOR = 4; 50 51 54 private int lastRead = -1; 55 56 59 private byte[] buf = new byte[1024]; 60 61 64 int index = 0; 65 66 69 int length = 0; 70 71 74 private boolean done = false; 75 76 133 134 157 public HashMap parseTheHeaders(InputStream in, boolean serverSide) 158 throws AxisFault { 159 HashMap map = new HashMap(); 160 try { 161 StringBuffer str = new StringBuffer (); 162 int state = BEFORE_SEPERATOR; 163 String key = null; 164 String value = null; 165 int start = 0; 166 length = readLine(in, buf); 167 if (serverSide) { 168 if ((buf[0] == 'P') 169 && (buf[1] == 'O') 170 && (buf[2] == 'S') 171 && (buf[3] == 'T')) { 172 map.put( 173 HTTPConstants.HTTP_REQ_TYPE, 174 HTTPConstants.HEADER_POST); 175 index = 5; 176 177 } else if ( 178 (buf[0] == 'G') && (buf[1] == 'E') && (buf[2] == 'T')) { 179 map.put( 180 HTTPConstants.HTTP_REQ_TYPE, 181 HTTPConstants.HEADER_GET); 182 index = 4; 183 184 } else { 185 throw new AxisFault("Unsupported HTTP request type: Only GET and POST is supported"); 186 } 187 188 value = readFirstLineArg(' '); 189 map.put(HTTPConstants.REQUEST_URI, value); 190 value = readFirstLineArg('\n'); 191 map.put(HTTPConstants.PROTOCOL_VERSION, value); 192 } else { 193 index = 0; 194 value = readFirstLineArg(' '); 195 if (value != null && value.indexOf("HTTP") >= 0) { 196 map.put(HTTPConstants.PROTOCOL_VERSION, value); 197 value = readFirstLineArg(' '); 198 map.put(HTTPConstants.RESPONSE_CODE, value); 199 } else { 200 map.put(HTTPConstants.RESPONSE_CODE, value); 201 } 202 203 value = readFirstLineArg('\n'); 204 map.put(HTTPConstants.RESPONSE_WORD, value); 205 } 206 state = BEFORE_SEPERATOR; 207 while (!done) { 208 length = readLine(in, buf); 209 if (length <= 0) { 210 throw new AxisFault("Premature end of steam"); 211 } 212 for (int i = 0; i < length; i++) { 213 switch (state) { 214 case BEFORE_SEPERATOR : 215 if (buf[i] == ':') { 216 key = str.toString(); 217 str = new StringBuffer (); 218 state = AFTER_SEPERATOR; 219 if (buf[i + 1] == ' ') { 220 i++; } 222 } else { 223 str.append((char) buf[i]); 224 } 225 break; 226 case AFTER_SEPERATOR : 227 if (buf[i] == '\n') { 228 value = str.toString(); 229 map.put(key, value); 230 str = new StringBuffer (); 231 i = length; 232 } else { 233 str.append((char) buf[i]); 234 } 235 break; 236 237 default : 249 throw new AxisFault( 250 "Error Occured Unknown state " + state); 251 } 252 } 253 state = BEFORE_SEPERATOR; 254 } 255 } catch (IOException e) { 256 throw new AxisFault(e.getMessage(), e); 257 } 258 return map; 259 } 260 261 365 372 private String readFirstLineArg(char terminal) throws AxisFault { 373 StringBuffer str = new StringBuffer (); 374 try { 375 while ((buf[index] != terminal) && (index < length)) { 376 str.append((char) buf[index]); 377 index++; 378 } 379 index++; 380 return str.toString(); 381 } catch (Exception e) { 382 throw new AxisFault(e.getMessage(), e); 383 } 384 } 385 386 396 protected int readLine(InputStream is, byte[] b) 397 throws java.io.IOException { 398 int count = 0, c; 399 400 if (lastRead == -1) { 402 c = is.read(); 403 } else { 404 c = lastRead; 405 } 406 int off = 0; 407 while (c != -1) { 408 if ((c != '\n') && (c != '\r')) { 409 b[off++] = (byte) c; 410 count++; 411 c = is.read(); 412 } else { 413 if ('\n' == c) { 414 c = is.read(); 415 if (c == '\r') { 416 c = is.read(); 417 } 418 419 if ((c != ' ') && (c != '\t')) { 421 if (c == '\n') { 422 done = true; 423 } 424 lastRead = c; 425 b[off++] = '\n'; 426 count++; 427 break; 428 } 429 } else { 430 c = is.read(); 431 } 432 } 433 } 434 if (c == -1) { 435 throw new AxisFault("Every line should ends with the \\n, unexpected End of stream"); 436 } else { 437 return (count > 0) ? count : -1; 438 } 439 } 440 441 442 443 449 public static String getServicesHTML(ConfigurationContext configurationContext) { 450 String temp = ""; 451 Map services = 452 configurationContext.getAxisConfiguration().getServices(); 453 Hashtable erroneousServices = 454 configurationContext.getAxisConfiguration().getFaulytServices(); 455 boolean status = false; 456 457 if (services != null && !services.isEmpty()) { 458 status = true; 459 Collection serviceCollection = services.values(); 460 temp += "<h2>" + "Deployed services" + "</h2>"; 461 for (Iterator it = serviceCollection.iterator(); it.hasNext();) { 462 Map operations; 463 Collection operationsList; 464 ServiceDescription axisService = (ServiceDescription) it.next(); 465 operations = axisService.getOperations(); 466 operationsList = operations.values(); 467 468 temp += "<h3>" + axisService.getName().getLocalPart() + "</h3>"; 469 if (operationsList.size() > 0) { 470 temp += "Available operations <ul>"; 471 for (Iterator iterator1 = operationsList.iterator(); 472 iterator1.hasNext(); 473 ) { 474 OperationDescription axisOperation = 475 (OperationDescription) iterator1.next(); 476 temp += "<li>" 477 + axisOperation.getName().getLocalPart() 478 + "</li>"; 479 } 480 temp += "</ul>"; 481 } else { 482 temp += "No operations speficied for this service"; 483 } 484 } 485 } 486 487 if (erroneousServices != null && !erroneousServices.isEmpty()) { 488 489 temp += "<hr><h2><font color=\"blue\">Faulty Services</font></h2>"; 490 status = true; 491 Enumeration faultyservices = erroneousServices.keys(); 492 while (faultyservices.hasMoreElements()) { 493 String faultyserviceName = 494 (String ) faultyservices.nextElement(); 495 temp += "<h3><font color=\"blue\">" 496 + faultyserviceName 497 + "</font></h3>"; 498 } 499 } 500 501 if (!status) { 502 temp = "<h2>There are no services deployed</h2>"; 503 } 504 505 temp = 506 "<html><head><title>Axis2: Services</title></head>" 507 + "<body>" 508 + temp 509 + "</body></html>"; 510 511 return temp; 512 } 513 514 public static Map getGetRequestParameters(String requestURI) { 515 Map map = new HashMap(); 516 517 char[] chars = requestURI.toCharArray(); 518 final int NOT_BEGUN = 1500; 519 final int INSIDE_NAME = 1501; 520 final int INSIDE_VALUE = 1502; 521 522 int state = NOT_BEGUN; 523 StringBuffer name = new StringBuffer (); 524 StringBuffer value = new StringBuffer (); 525 526 for (int index = 0; index < chars.length; index++) { 527 if (state == NOT_BEGUN) { 528 if (chars[index] == '?') { 529 state = INSIDE_NAME; 530 } 531 } else if (state == INSIDE_NAME) { 532 if (chars[index] == '=') { 533 state = INSIDE_VALUE; 534 } else { 535 name.append(chars[index]); 536 } 537 } else if (state == INSIDE_VALUE) { 538 if (chars[index] == ',') { 539 state = INSIDE_NAME; 540 map.put(name.toString(), value.toString()); 541 name.delete(0, name.length()); 542 value.delete(0, value.length()); 543 } else { 544 value.append(chars[index]); 545 } 546 } 547 } 548 if (name.length() + value.length() > 0) { 549 map.put(name.toString(), value.toString()); 550 } 551 return map; 552 } 553 554 } 555 | Popular Tags |