1 5 package com.oreilly.servlet; 6 7 import java.io.*; 8 import java.util.*; 9 import javax.servlet.*; 10 import javax.servlet.http.*; 11 12 import java.util.Enumeration ; 13 import java.util.Map ; 14 15 import java.security.Principal ; 16 17 import com.oreilly.servlet.multipart.MultipartParser; 18 import com.oreilly.servlet.multipart.Part; 19 import com.oreilly.servlet.multipart.FilePart; 20 import com.oreilly.servlet.multipart.ParamPart; 21 import com.oreilly.servlet.multipart.FileRenamePolicy; 22 23 72 public class MultipartRequest implements HttpServletRequest { 73 74 private static final int DEFAULT_MAX_POST_SIZE = 1024 * 1024; 76 protected Hashtable parameters = new Hashtable(); protected Hashtable files = new Hashtable(); protected Cookie[] cookies; 79 protected Locale requestlocale; 80 protected String sessionid; 81 protected String encoding; 82 protected String servletpath; 83 protected String requesturi; 84 protected String remoteuser; 85 protected String querystr; 86 protected String ctxpath; 87 protected String trnpath; 88 protected String pathinfo; 89 protected String formmethod; 90 protected String contenttype; 91 protected int contentlength; 92 protected StringBuffer requesturl; 93 protected String savedir; 94 protected boolean bRequestedSessionIdFromURL; 95 protected boolean bRequestedSessionIdFromCookie; 96 protected boolean bRequestedSessionIdValid; 97 98 111 public MultipartRequest(HttpServletRequest request, 112 String saveDirectory) throws IOException { 113 this(request, saveDirectory, DEFAULT_MAX_POST_SIZE); 114 } 115 116 130 public MultipartRequest(HttpServletRequest request, 131 String saveDirectory, 132 int maxPostSize) throws IOException { 133 this(request, saveDirectory, maxPostSize, null, null); 134 } 135 136 150 public MultipartRequest(HttpServletRequest request, 151 String saveDirectory, 152 String encoding) throws IOException { 153 this(request, saveDirectory, DEFAULT_MAX_POST_SIZE, encoding, null); 154 } 155 156 171 public MultipartRequest(HttpServletRequest request, 172 String saveDirectory, 173 int maxPostSize, 174 FileRenamePolicy policy) throws IOException { 175 this(request, saveDirectory, maxPostSize, null, policy); 176 } 177 178 193 public MultipartRequest(HttpServletRequest request, 194 String saveDirectory, 195 int maxPostSize, 196 String encoding) throws IOException { 197 this(request, saveDirectory, maxPostSize, encoding, null); 198 } 199 200 219 public MultipartRequest(HttpServletRequest request, 220 String saveDirectory, 221 int maxPostSize, 222 String encoding, 223 FileRenamePolicy policy) throws IOException { 224 if (request == null) 226 throw new IllegalArgumentException ("request cannot be null"); 227 if (saveDirectory == null) 228 throw new IllegalArgumentException ("saveDirectory cannot be null"); 229 if (maxPostSize <= 0) { 230 throw new IllegalArgumentException ("maxPostSize must be positive"); 231 } 232 233 requestlocale = request.getLocale(); 234 encoding = request.getCharacterEncoding(); 235 sessionid = request.getRequestedSessionId(); 236 requesturl = request.getRequestURL(); 237 requesturi = request.getRequestURI(); 238 remoteuser = request.getRemoteUser(); 239 querystr = request.getQueryString(); 240 ctxpath = request.getContextPath(); 241 trnpath = request.getPathTranslated(); 242 pathinfo = request.getPathInfo(); 243 servletpath = request.getServletPath(); 244 formmethod = request.getMethod(); 245 cookies = request.getCookies(); 246 247 File dir = new File(saveDirectory); 249 250 if (!dir.isDirectory()) 252 throw new IllegalArgumentException ("Not a directory: " + saveDirectory); 253 254 if (!dir.canWrite()) 256 throw new IllegalArgumentException ("Not writable: " + saveDirectory); 257 258 savedir = saveDirectory; 259 260 MultipartParser parser = new MultipartParser(request, maxPostSize, true, true, encoding); 263 264 if (request.getQueryString() != null) { 268 Hashtable queryParameters = 270 HttpUtils.parseQueryString(request.getQueryString()); 271 Enumeration queryParameterNames = queryParameters.keys(); 273 while (queryParameterNames.hasMoreElements()) { 274 Object paramName = queryParameterNames.nextElement(); 275 String [] values = (String [])queryParameters.get(paramName); 276 Vector newValues = new Vector(); 277 for (int i = 0; i < values.length; i++) { 278 newValues.add(values[i]); 279 } 280 parameters.put(paramName, newValues); 281 } 282 } 283 284 Part part; 285 while ((part = parser.readNextPart()) != null) { 286 String name = part.getName(); 287 if (part.isParam()) { 288 ParamPart paramPart = (ParamPart) part; 290 String value = paramPart.getStringValue(); 291 Vector existingValues = (Vector)parameters.get(name); 292 if (existingValues == null) { 293 existingValues = new Vector(); 294 parameters.put(name, existingValues); 295 } 296 existingValues.addElement(value); 297 } 298 else if (part.isFile()) { 299 FilePart filePart = (FilePart) part; 301 String fileName = filePart.getFileName(); 302 if (fileName != null) { 303 filePart.setRenamePolicy(policy); filePart.writeTo(dir); 306 files.put(name, new UploadedFile(dir.toString(), 307 filePart.getFileName(), 308 fileName, 309 filePart.getContentType())); 310 } 311 else { 312 files.put(name, new UploadedFile(null, null, null, null)); 314 } 315 } 316 } 317 bRequestedSessionIdFromURL = request.isRequestedSessionIdFromURL(); 318 bRequestedSessionIdFromCookie = request.isRequestedSessionIdFromCookie(); 319 bRequestedSessionIdValid = request.isRequestedSessionIdValid(); 320 } 321 322 330 public MultipartRequest(ServletRequest request, 331 String saveDirectory) throws IOException { 332 this((HttpServletRequest)request, saveDirectory); 333 } 334 335 343 public MultipartRequest(ServletRequest request, 344 String saveDirectory, 345 int maxPostSize) throws IOException { 346 this((HttpServletRequest)request, saveDirectory, maxPostSize); 347 } 348 349 public Cookie[] getCookies() { 350 return cookies; 351 } 352 353 public Locale getLocale() { 354 return requestlocale; 355 } 356 357 363 public Enumeration getParameterNames() { 364 return parameters.keys(); 365 } 366 367 370 371 public int getFileCount() { 372 int iCount = 0; 373 Enumeration oFiles = files.keys(); 374 Object oFileName; 375 376 while (oFiles.hasMoreElements()) { 377 oFileName = oFiles.nextElement(); 378 if (null!=oFileName) 379 if (!oFileName.equals("")) 380 iCount++; 381 } return iCount; 383 } 385 393 public Enumeration getFileNames() { 394 return files.keys(); 395 } 396 397 408 public String getParameter(String name) { 409 try { 410 Vector values = (Vector)parameters.get(name); 411 if (values == null || values.size() == 0) { 412 return null; 413 } 414 String value = (String )values.elementAt(values.size() - 1); 415 return value; 416 } 417 catch (Exception e) { 418 return null; 419 } 420 } 421 422 432 public String [] getParameterValues(String name) { 433 try { 434 Vector values = (Vector)parameters.get(name); 435 if (values == null || values.size() == 0) { 436 return null; 437 } 438 String [] valuesArray = new String [values.size()]; 439 values.copyInto(valuesArray); 440 return valuesArray; 441 } 442 catch (Exception e) { 443 return null; 444 } 445 } 446 447 456 public String getFilesystemName(String name) { 457 try { 458 UploadedFile file = (UploadedFile)files.get(name); 459 return file.getFilesystemName(); } 461 catch (Exception e) { 462 return null; 463 } 464 } 465 466 474 public String getOriginalFileName(String name) { 475 try { 476 UploadedFile file = (UploadedFile)files.get(name); 477 return file.getOriginalFileName(); } 479 catch (Exception e) { 480 return null; 481 } 482 } 483 484 public String getCharacterEncoding() { 485 return encoding; 486 } 487 488 495 public String getContentType(String name) { 496 try { 497 UploadedFile file = (UploadedFile)files.get(name); 498 return file.getContentType(); } 500 catch (Exception e) { 501 return null; 502 } 503 } 504 505 511 public File getFile(String name) { 512 try { 513 UploadedFile file = (UploadedFile)files.get(name); 514 return file.getFile(); } 516 catch (Exception e) { 517 return null; 518 } 519 } 520 521 526 public File getFile(int number) { 527 UploadedFile file = null; 528 Enumeration fileenum = files.elements(); 529 try { 530 for (int f=0; f<getFileCount(); f++) 531 file = (UploadedFile) fileenum.nextElement(); 532 return file.getFile(); } 534 catch (Exception e) { 535 return null; 536 } 537 } 539 public String getContentType() { 540 return contenttype; 541 } 542 543 public int getContentLength() { 544 return contentlength; 545 } 546 547 556 public String getServletPath() { 557 return servletpath; 558 } 559 560 public String getContextPath() { 561 return ctxpath; 562 } 563 564 public String getPathInfo() { 565 return pathinfo; 566 } 567 568 public String getPathTranslated() { 569 return trnpath; 570 } 571 572 public String getMethod() { 573 return formmethod; 574 } 575 576 public String getRemoteUser() { 577 return remoteuser; 578 } 579 580 public String getRequestURI() { 581 return requesturi; 582 } 583 584 public StringBuffer getRequestURL() { 585 return requesturl; 586 } 587 588 public String getRequestedSessionId() { 589 return sessionid; 590 } 591 592 public String getQueryString() { 593 return querystr; 594 } 595 596 public boolean isRequestedSessionIdFromURL() { 597 return bRequestedSessionIdFromURL; 598 } 599 600 public boolean isRequestedSessionIdFromUrl() { 601 return bRequestedSessionIdFromURL; 602 } 603 604 public boolean isRequestedSessionIdFromCookie() { 605 return bRequestedSessionIdFromCookie; 606 } 607 608 public boolean isRequestedSessionIdValid() { 609 return bRequestedSessionIdValid; 610 } 611 612 public Enumeration getAttributeNames() { 613 throw new RuntimeException ("HttpServletRequest.getAttributeNames() method not implemented for MultipartRequest"); 614 } 615 616 public Object getAttribute(String sAttrName) { 617 throw new RuntimeException ("HttpServletRequest.getAttribute() method not implemented for MultipartRequest"); 618 } 619 620 public void setAttribute(String sAttrName, Object sAttrVal) { 621 throw new RuntimeException ("HttpServletRequest.setAttribute() method not implemented for MultipartRequest"); 622 } 623 624 public void removeAttribute(String sAttrName) { 625 throw new RuntimeException ("HttpServletRequest.removeAttribute() method not implemented for MultipartRequest"); 626 } 627 628 public Enumeration getLocales() { 629 throw new RuntimeException ("HttpServletRequest.getLocales() method not implemented for MultipartRequest"); 630 } 631 632 public boolean isSecure() { 633 throw new RuntimeException ("HttpServletRequest.isSecure() method not implemented for MultipartRequest"); 634 } 635 636 public String getAuthType() { 637 throw new RuntimeException ("HttpServletRequest.getAuthType() method not implemented for MultipartRequest"); 638 } 639 640 public int getLocalPort() { 641 throw new RuntimeException ("HttpServletRequest.getLocalPort() method not implemented for MultipartRequest"); 642 } 643 644 public String getProtocol() { 645 throw new RuntimeException ("HttpServletRequest.getProtocol() method not implemented for MultipartRequest"); 646 } 647 648 public Map getParameterMap() { 649 throw new RuntimeException ("HttpServletRequest.getParameterMap() method not implemented for MultipartRequest"); 650 } 651 652 public String getScheme() { 653 throw new RuntimeException ("HttpServletRequest.getScheme() method not implemented for MultipartRequest"); 654 } 655 656 public String getServerName() { 657 throw new RuntimeException ("HttpServletRequest.getServerName() method not implemented for MultipartRequest"); 658 } 659 660 public int getServerPort() { 661 throw new RuntimeException ("HttpServletRequest.getServerPort() method not implemented for MultipartRequest"); 662 } 663 664 public int getRemotePort() { 665 throw new RuntimeException ("HttpServletRequest.getRemotePort() method not implemented for MultipartRequest"); 666 } 667 668 public String getLocalAddr() { 669 throw new RuntimeException ("HttpServletRequest.getLocalAddr() method not implemented for MultipartRequest"); 670 } 671 672 public String getLocalName() { 673 throw new RuntimeException ("HttpServletRequest.getLocalName() method not implemented for MultipartRequest"); 674 } 675 676 public String getRemoteAddr() { 677 throw new RuntimeException ("HttpServletRequest.getRemoteAddr() method not implemented for MultipartRequest"); 678 } 679 680 public String getRemoteHost() { 681 throw new RuntimeException ("HttpServletRequest.getRemoteHost() method not implemented for MultipartRequest"); 682 } 683 684 public HttpSession getSession() { 685 throw new RuntimeException ("HttpServletRequest.getSession() method not implemented for MultipartRequest"); 686 } 687 688 public HttpSession getSession(boolean b) { 689 throw new RuntimeException ("HttpServletRequest.getSession() method not implemented for MultipartRequest"); 690 } 691 692 public Principal getUserPrincipal() { 693 throw new RuntimeException ("HttpServletRequest.getUserPrincipal() method not implemented for MultipartRequest"); 694 } 695 696 public String getRealPath(String s) { 697 throw new RuntimeException ("HttpServletRequest.getRealPath() method not implemented for MultipartRequest"); 698 } 699 700 public RequestDispatcher getRequestDispatcher(String s) { 701 throw new RuntimeException ("HttpServletRequest.getRequestDispatcher() method not implemented for MultipartRequest"); 702 } 703 704 public boolean isUserInRole(String role) { 705 throw new RuntimeException ("HttpServletRequest.isUserInRole() method not implemented for MultipartRequest"); 706 } 707 708 public String getHeader(String hname) { 709 throw new RuntimeException ("HttpServletRequest.getHeader() method not implemented for MultipartRequest"); 710 } 711 712 public Enumeration getHeaders(String hname) { 713 throw new RuntimeException ("HttpServletRequest.getHeaders() method not implemented for MultipartRequest"); 714 } 715 716 public int getIntHeader(String hname) { 717 throw new RuntimeException ("HttpServletRequest.getIntHeader() method not implemented for MultipartRequest"); 718 } 719 720 public long getDateHeader(String hname) { 721 throw new RuntimeException ("HttpServletRequest.getDateHeader() method not implemented for MultipartRequest"); 722 } 723 724 public Enumeration getHeaderNames() { 725 throw new RuntimeException ("HttpServletRequest.getHeaderNames() method not implemented for MultipartRequest"); 726 } 727 728 public BufferedReader getReader() { 729 throw new RuntimeException ("HttpServletRequest.getReader() method not implemented for MultipartRequest"); 730 } 731 732 public ServletInputStream getInputStream() { 733 throw new RuntimeException ("HttpServletRequest.getInputStream() method not implemented for MultipartRequest"); 734 } 735 736 public void setCharacterEncoding(String sEncoding) { 737 throw new RuntimeException ("HttpServletRequest.setCharacterEncoding() method not implemented for MultipartRequest"); 738 } 739 740 } 741 742 743 class UploadedFile { 746 747 private String dir; 748 private String filename; 749 private String original; 750 private String type; 751 752 UploadedFile(String dir, String filename, String original, String type) { 753 this.dir = dir; 754 this.filename = filename; 755 this.original = original; 756 this.type = type; 757 } 758 759 public String getContentType() { 760 return type; 761 } 762 763 public String getFilesystemName() { 764 return filename; 765 } 766 767 public String getOriginalFileName() { 768 return original; 769 } 770 771 public File getFile() { 772 if (dir == null || filename == null) { 773 return null; 774 } 775 else { 776 return new File(dir + File.separator + filename); 777 } 778 } 779 780 } 781 782 | Popular Tags |