1 45 package org.openejb.webadmin.httpd; 46 47 import java.io.ByteArrayInputStream ; 48 import java.io.ByteArrayOutputStream ; 49 import java.io.DataInput ; 50 import java.io.DataInputStream ; 51 import java.io.File ; 52 import java.io.FileOutputStream ; 53 import java.io.IOException ; 54 import java.io.InputStream ; 55 import java.net.URL ; 56 import java.net.URLDecoder ; 57 import java.rmi.RemoteException ; 58 import java.util.HashMap ; 59 import java.util.Hashtable ; 60 import java.util.Iterator ; 61 import java.util.Map ; 62 import java.util.StringTokenizer ; 63 64 import javax.naming.InitialContext ; 65 import javax.naming.NamingException ; 66 67 import org.apache.commons.fileupload.MultipartStream; 68 import org.openejb.webadmin.HttpRequest; 69 import org.openejb.webadmin.HttpSession; 70 import org.openejb.core.stateful.StatefulEjbObjectHandler; 71 import org.openejb.util.FileUtils; 72 73 78 public class HttpRequestImpl implements HttpRequest { 79 public static final String FORM_URL_ENCODED = "application/x-www-form-urlencoded"; 80 public static final String MULITPART_FORM_DATA = "multipart/form-data"; 81 public static final String FILENAME = "filename"; 82 public static final String NAME = "name"; 83 84 85 private String line; 86 87 private int method; 88 89 private URL uri; 90 91 private HashMap headers; 92 93 private HashMap formParams = new HashMap (); 94 95 private HashMap queryParams = new HashMap (); 96 97 private byte[] body; 98 private String [][] formParamsArray; 99 100 101 private String methodString; 102 private String pathString; 103 104 105 108 public String getMethodString() { 109 return methodString; 110 } 111 112 115 public String getPathString() { 116 return pathString; 117 } 118 119 123 public String getHeader(String name) { 124 return (String ) headers.get(name); 125 } 126 127 131 public String getFormParameter(String name) { 132 return (String ) formParams.get(name); 133 } 134 135 148 public String [][] getFormParameters() { 149 Iterator keys = formParams.keySet().iterator(); 150 String [][] returnValue = new String [formParams.size()][2]; 151 152 String temp; 153 int i = 0; 154 while (keys.hasNext()) { 155 temp = (String ) keys.next(); 156 returnValue[i][0] = temp; 157 returnValue[i++][1] = (String ) formParams.get(temp); 158 } 159 160 return returnValue; 161 } 162 163 167 public String getQueryParameter(String name) { 168 return (String ) queryParams.get(name); 169 } 170 171 184 public int getMethod() { 185 return method; 186 } 187 188 191 public URL getURI() { 192 return uri; 193 } 194 195 196 197 198 202 protected void readMessage(InputStream input) throws IOException { 203 DataInput in = new DataInputStream (input); 204 205 readRequestLine(in); 206 readHeaders(in); 207 readBody(in); 208 } 209 210 private String requestLine; 211 212 protected String getRequestLine(){ 213 return requestLine; 214 } 215 219 private void readRequestLine(DataInput in) throws IOException { 220 221 try { 222 line = in.readLine(); 223 requestLine = line; 224 } catch (Exception e) { 226 throw new IOException ( 227 "Could not read the HTTP Request Line :" 228 + e.getClass().getName() 229 + " : " 230 + e.getMessage()); 231 } 232 233 StringTokenizer lineParts = new StringTokenizer (line, " "); 234 235 parseMethod(lineParts); 236 237 parseURI(lineParts); 238 } 239 240 244 private void parseMethod(StringTokenizer lineParts) throws IOException { 245 String token = null; 246 try { 247 token = lineParts.nextToken(); 248 } catch (Exception e) { 249 throw new IOException ( 250 "Could not parse the HTTP Request Method :" 251 + e.getClass().getName() 252 + " : " 253 + e.getMessage()); 254 } 255 256 if (token.equalsIgnoreCase("GET")) { 257 method = GET; 258 } else if (token.equalsIgnoreCase("POST")) { 259 method = POST; 260 } else { 261 method = UNSUPPORTED; 262 throw new IOException ("Unsupported HTTP Request Method :" + token); 263 } 264 } 265 266 270 private void parseURI(StringTokenizer lineParts) throws IOException { 271 String token = null; 272 try { 273 token = lineParts.nextToken(); 274 } catch (Exception e) { 275 throw new IOException ( 276 "Could not parse the HTTP Request Method :" 277 + e.getClass().getName() 278 + " : " 279 + e.getMessage()); 280 } 281 282 try { 283 uri = new URL ("http", "localhost", token); 284 } catch (java.net.MalformedURLException e) { 285 throw new IOException ("Malformed URL :" + token + " Exception: " + e.getMessage()); 286 } 287 288 parseQueryParams(uri.getQuery()); 289 } 290 291 295 private void parseQueryParams(String query) throws IOException { 296 if (query == null) 297 return; 298 StringTokenizer parameters = new StringTokenizer (query, "&"); 299 300 while (parameters.hasMoreTokens()) { 301 StringTokenizer param = new StringTokenizer (parameters.nextToken(), "="); 302 303 304 if (!param.hasMoreTokens()) 305 continue; 306 String name = URLDecoder.decode(param.nextToken()); 307 if (name == null) 308 continue; 309 310 311 if (!param.hasMoreTokens()) 312 continue; 313 String value = URLDecoder.decode(param.nextToken()); 314 if (value == null) 315 continue; 316 317 queryParams.put(name, value); 319 } 320 } 321 322 326 private void readHeaders(DataInput in) throws IOException { 327 headers = new HashMap (); 329 while (true) { 330 String hf = null; 332 333 try { 334 hf = in.readLine(); 335 } catch (Exception e) { 337 throw new IOException ( 338 "Could not read the HTTP Request Header Field :" 339 + e.getClass().getName() 340 + " : " 341 + e.getMessage()); 342 } 343 344 if (hf == null || hf.equals("")) { 345 break; 346 } 347 348 349 int colonIndex = hf.indexOf((int) ':'); 350 String name = hf.substring(0, colonIndex); 351 if (name == null) 352 break; 353 354 355 String value = hf.substring(colonIndex + 1, hf.length()); 356 if (value == null) 357 break; 358 value = value.trim(); 359 headers.put(name, value); 360 } 361 362 } 371 372 376 private void readBody(DataInput in) throws IOException { 377 readRequestBody(in); 378 String type = getHeader(HttpRequest.HEADER_CONTENT_TYPE); 382 if (FORM_URL_ENCODED.equals(type)) { 383 parseFormParams(); 384 } else if (type != null && type.startsWith(MULITPART_FORM_DATA)) { 385 parseMultiPartFormParams(); 386 } 387 } 388 389 393 private void readRequestBody(DataInput in) throws IOException { 394 String len = getHeader(HttpRequest.HEADER_CONTENT_LENGTH); 396 398 int length = -1; 399 if (len != null) { 400 try { 401 length = Integer.parseInt(len); 402 } catch (Exception e) { 403 } 405 } 406 407 if (length < 1) { 408 this.body = new byte[0]; 409 } else if (length > 0) { 410 this.body = new byte[length]; 411 412 try { 413 in.readFully(body); 414 } catch (Exception e) { 415 throw new IOException ( 416 "Could not read the HTTP Request Body :" 417 + e.getClass().getName() 418 + " : " 419 + e.getMessage()); 420 } 421 } 422 } 423 424 427 private void parseFormParams() throws IOException { 428 String rawParams = new String (body); 429 StringTokenizer parameters = new StringTokenizer (rawParams, "&"); 431 String name = null; 432 String value = null; 433 434 while (parameters.hasMoreTokens()) { 435 StringTokenizer param = new StringTokenizer (parameters.nextToken(), "="); 436 437 438 name = URLDecoder.decode(param.nextToken()); 439 if (name == null) 440 break; 441 442 443 if (param.hasMoreTokens()) { 444 value = URLDecoder.decode(param.nextToken()); 445 } else { 446 value = ""; } 448 449 if (value == null) 450 value = ""; 451 452 formParams.put(name, value); 453 } 455 } 456 457 462 private void parseMultiPartFormParams() throws IOException { 463 464 ByteArrayOutputStream output; 465 StringBuffer multiPartBuffer; 466 int j; 467 Map headerMap; 468 boolean isFile; 469 String fileName = null; 470 byte[] outputArray; 471 FileOutputStream fos; 472 473 String contentType = getHeader(HttpRequest.HEADER_CONTENT_TYPE); 474 int boundaryIndex = contentType.indexOf("boundary="); 475 if (boundaryIndex < 0) { 476 throw new IOException ("the request was rejected because no multipart boundary was found"); 477 } 478 byte[] boundary = contentType.substring(boundaryIndex + 9).getBytes(); 479 480 ByteArrayInputStream input = new ByteArrayInputStream (body); 481 MultipartStream multi = new MultipartStream(input, boundary); 482 483 boolean nextPart = multi.skipPreamble(); 484 while (nextPart) { 485 try { 486 output = new ByteArrayOutputStream (); 487 multi.readBodyData(output); 488 outputArray = output.toByteArray(); 489 multiPartBuffer = new StringBuffer (50); 490 isFile = false; 491 File jarFileInTempDir; 492 j = 0; 493 494 for (int i = 0; i < outputArray.length; i++) { 495 if (outputArray[i] == 13 && outputArray[i + 1] == 10) { 497 headerMap = parseMultiPartHeader(multiPartBuffer); 499 if (headerMap.get(NAME) != null) { 500 fileName = (String ) headerMap.get(NAME); 501 } 502 503 if (fileName != null && headerMap.get(FILENAME) != null) { 505 this.formParams.put(fileName, headerMap.get(FILENAME)); 506 isFile = true; 507 } 508 509 if (outputArray[i + 2] == 13 && outputArray[i + 3] == 10) { 510 i+=4; 512 j = i; 513 break; 514 } else { 515 i++; 516 } 517 518 multiPartBuffer = new StringBuffer (50); 519 } else { 520 multiPartBuffer.append((char) outputArray[i]); 521 } 522 } 523 524 if (isFile) { 526 jarFileInTempDir = new File ((String ) this.formParams.get(fileName)); 528 if (!jarFileInTempDir.exists()) { 529 jarFileInTempDir.createNewFile(); 530 } 531 532 fos = new FileOutputStream (jarFileInTempDir); 534 fos.write(outputArray, j, outputArray.length-j); 535 fos.close(); 536 } else { multiPartBuffer = new StringBuffer (outputArray.length-j); 538 for (int i = j; i < outputArray.length; i++) { 539 multiPartBuffer.append((char)outputArray[i]); 540 } 541 542 this.formParams.put( 543 fileName, 544 multiPartBuffer.toString()); 545 } 546 547 nextPart = multi.readBoundary(); 548 } catch (MultipartStream.MalformedStreamException mse) { 549 throw new IOException (mse.getMessage()); 550 } 551 } 552 } 553 554 561 private Map parseMultiPartHeader(StringBuffer headerBuffer) throws IOException { 562 Map headerMap = new HashMap (); 563 int colonIndex = headerBuffer.toString().indexOf(":"); 564 String headerName = headerBuffer.substring(0, colonIndex); 565 StringTokenizer headerValueToken = 566 new StringTokenizer (headerBuffer.substring(colonIndex + 1, headerBuffer.length()), ";"); 567 568 String currentToken; 569 while (headerValueToken.hasMoreTokens()) { 571 currentToken = headerValueToken.nextToken(); 572 if (currentToken.indexOf("=") > -1) { 573 headerMap.put( 574 currentToken.substring(0, currentToken.indexOf("=")).trim(), 575 currentToken 576 .substring(currentToken.indexOf("=") + 2, currentToken.length() - 1) 577 .trim()); 578 } else { 579 headerMap.put(headerName, currentToken.trim()); 580 } 581 } 582 583 String fileName = (String ) headerMap.get(FILENAME); 586 if (fileName != null) { 587 StringBuffer temp; 588 if (fileName.indexOf("\\") > -1) { 589 temp = new StringBuffer (fileName).reverse(); 590 fileName = temp.delete(temp.toString().indexOf("\\"), temp.length()).reverse().toString(); 591 } 592 593 temp = new StringBuffer (); 594 temp.append(FileUtils.createTempDirectory().getAbsolutePath()); 595 temp.append(System.getProperty("file.separator")); 596 temp.append(fileName); 597 headerMap.put(FILENAME, temp.toString()); 598 } 599 600 return headerMap; 601 } 602 603 private HashMap cookies; 604 605 protected HashMap getCookies(){ 606 if (cookies != null) return cookies; 607 608 cookies = new HashMap (); 609 610 String cookieHeader = getHeader(HEADER_COOKIE); 611 if (cookieHeader == null ) return cookies; 612 613 StringTokenizer tokens = new StringTokenizer (cookieHeader, ";"); 614 while (tokens.hasMoreTokens()){ 615 StringTokenizer token = new StringTokenizer (tokens.nextToken(),"="); 616 String name = token.nextToken(); 617 String value = token.nextToken(); 618 cookies.put(name, value); 619 } 620 return cookies; 621 } 622 623 protected static final String EJBSESSIONID = "EJBSESSIONID"; 624 625 protected String getCookie(String name){ 626 return (String ) getCookies().get(name); 627 } 628 629 public HttpSession getSession() { 630 return getSession(true); 631 } 632 633 private WebSession session; 634 635 public HttpSession getSession(boolean create) { 636 if (session != null) return session; 637 638 String id = getCookie(EJBSESSIONID); 639 640 if (id != null) { 641 session = (WebSession)sessions.get(id); 642 } 643 644 if (session == null && create){ 645 session = createSession(); 646 sessions.put(session.getId(), session); 647 } 648 return session; 649 } 650 651 private static final Hashtable sessions = new Hashtable (); 652 653 private WebSession createSession(){ 654 WebSessionHome home = null; 656 657 try { 658 home = (WebSessionHome)new InitialContext ().lookup("java:openejb/ejb/httpd/session"); 659 } catch (NamingException e) { 660 throw new IllegalStateException ("The WebSessionBean has not been deployed. "+ 662 " This is required for the HTTPd service to provide HttpSession support. "+ 663 e.getClass().getName()+": "+e.getMessage()); 664 } 665 666 667 WebSession session = null; 668 try { 669 session = home.create(); 670 } catch (RemoteException e) { 671 e.printStackTrace(); 673 } 674 Object obj = org.openejb.util.proxy.ProxyManager.getInvocationHandler(session); 676 StatefulEjbObjectHandler handler = (StatefulEjbObjectHandler) obj; 677 handler.setIntraVmCopyMode(false); 678 return session; 679 } 680 } | Popular Tags |