1 23 package com.sun.enterprise.tools.admingui.servlet; 24 25 import java.io.IOException ; 26 import java.io.BufferedInputStream ; 27 import java.io.InputStream ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import java.util.StringTokenizer ; 31 32 import javax.servlet.Servlet ; 33 import javax.servlet.ServletConfig ; 34 import javax.servlet.ServletException ; 35 import javax.servlet.ServletRequest ; 36 import javax.servlet.ServletResponse ; 37 import javax.servlet.http.HttpServlet ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 41 42 57 public class DownloadServlet extends HttpServlet { 58 59 62 public DownloadServlet() { 63 super(); 64 } 65 66 69 public void init(ServletConfig config) throws ServletException { 70 super.init(config); 71 72 String sources = config.getInitParameter(CONTENT_SOURCES); 74 if ((sources == null) || (sources.trim().length() == 0)) { 75 throw new ServletException ("No ContentSources specified! Ensure " 76 + "at least 1 DownloadServlet.ContentSource is provided as" 77 + " a Servlet init parameter (key: " + CONTENT_SOURCES 78 + ")."); 79 } 80 StringTokenizer tokens = new StringTokenizer (sources, " \t\n\r\f,;:"); 81 while (tokens.hasMoreTokens()) { 82 registerContentSource(tokens.nextToken()); 83 } 84 } 85 86 92 public void registerContentSource(String className) { 93 if ((className == null) || className.trim().equals("")) { 95 return; 96 } 97 98 Class cls = null; 99 try { 100 cls = Class.forName(className); 101 } catch (Exception ex) { 102 throw new RuntimeException (ex); 103 } 104 registerContentSource(cls); 105 } 106 107 112 public void registerContentSource(Class cls) { 113 DownloadServlet.ContentSource source = null; 115 try { 116 source = (DownloadServlet.ContentSource) cls.newInstance(); 117 } catch (Exception ex) { 118 throw new RuntimeException (ex); 119 } 120 _contentSources.put(source.getId(), source); 122 } 123 124 129 public DownloadServlet.ContentSource getContentSource(String id) { 130 return _contentSources.get(id); 131 } 132 133 136 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 137 doPost(request, response); 138 } 139 140 148 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 149 DownloadServlet.Context context = getDownloadContext(request, response); 151 152 ContentSource source = getContentSource(request); 154 155 writeContent(source, context); 157 158 source.cleanUp(context); 160 } 161 162 167 protected DownloadServlet.Context getDownloadContext(HttpServletRequest request, HttpServletResponse response) { 168 DownloadServlet.Context ctx = 169 (DownloadServlet.Context) request.getAttribute(DOWNLOAD_CONTEXT); 170 if (ctx == null) { 171 ctx = new DownloadServlet.Context(); 172 173 ctx.setServlet(this); 174 ctx.setServletConfig(getServletConfig()); 175 ctx.setServletRequest(request); 176 request.setAttribute(DOWNLOAD_CONTEXT, ctx); 177 } 178 179 ctx.setServletResponse(response); 182 183 return ctx; 184 } 185 186 194 protected DownloadServlet.ContentSource getContentSource(ServletRequest request) { 195 String id = request.getParameter(CONTENT_SOURCE_ID); 197 if (id == null) { 198 id = getServletConfig().getInitParameter(CONTENT_SOURCE_ID); 199 if(id == null) { 200 throw new RuntimeException ("You must provide the '" 201 + CONTENT_SOURCE_ID + "' request parameter!"); 202 } 203 } 204 205 DownloadServlet.ContentSource src = getContentSource(id); 207 if (src == null) { 208 throw new RuntimeException ("The ContentSource with id '" + id 209 + "' is not registered!"); 210 } 211 212 return src; 214 } 215 216 220 protected void writeHeader(DownloadServlet.ContentSource source, DownloadServlet.Context context) { 221 ServletResponse resp = context.getServletResponse(); 222 if (!(resp instanceof HttpServletResponse )) { 223 return; 225 } 226 227 long longTime = source.getLastModified(context); 230 if (longTime != -1) { 231 ((HttpServletResponse ) resp). 232 setDateHeader("Last-Modified", longTime); 233 } 234 235 String contentType = (String ) context.getAttribute(CONTENT_TYPE); 237 if (contentType == null) { 238 String ext = (String ) context.getAttribute(EXTENSION); 240 if (ext != null) { 241 contentType = mimeTypes.get(ext); 242 } 243 if (contentType == null) { 244 contentType = DEFAULT_CONTENT_TYPE; 246 } 247 } 248 ((HttpServletResponse ) resp).setHeader("Content-type", contentType); 249 } 250 251 257 protected void writeContent(DownloadServlet.ContentSource source, DownloadServlet.Context context) { 258 InputStream in = source.getInputStream(context); 260 261 ServletResponse resp = context.getServletResponse(); 263 if(in == null) { 264 String jspPage = (String )context.getAttribute("JSP_PAGE_SERVED"); 266 if(jspPage != null && (jspPage.equals("false"))) { 267 try { 268 ((HttpServletResponse )resp).sendError(404, "File Not Found"); 271 } catch (IOException ex) { 272 } 274 } 275 return; 276 } 277 try { 278 javax.servlet.ServletOutputStream out = resp.getOutputStream(); 279 280 InputStream stream = new BufferedInputStream (in); 282 283 writeHeader(source, context); 285 286 byte [] buf = new byte[512]; int read = stream.read(buf, 0, 512); 289 while (read != -1) { 290 out.write(buf, 0, read); 292 293 read = stream.read(buf, 0, 512); 295 } 296 297 stream.close(); 299 } catch (IOException ex) { 300 throw new RuntimeException (ex); 301 } 302 } 303 304 305 309 318 public static interface ContentSource { 319 320 326 public String getId(); 327 328 335 public InputStream getInputStream(DownloadServlet.Context ctx); 336 337 342 public void cleanUp(DownloadServlet.Context ctx); 343 344 349 public long getLastModified(DownloadServlet.Context context); 350 } 351 352 353 360 public static class Context { 361 362 365 public Context() { 366 } 367 368 374 public Object getAttribute(String name) { 375 if (name == null) { 376 return null; 377 } 378 379 Object value = _att.get(name); 381 if (value == null) { 382 value = getServletRequest().getParameter(name); 384 } 385 386 return value; 388 } 389 390 396 public void setAttribute(String name, Object value) { 397 if (name != null) { 398 _att.put(name, value); 399 } 400 } 401 402 408 public void removeAttribute(String name) { 409 _att.remove(name); 410 } 411 412 417 public Servlet getServlet() { 418 return _servlet; 419 } 420 421 425 protected void setServlet(Servlet servlet) { 426 _servlet = servlet; 427 } 428 429 432 public ServletConfig getServletConfig() { 433 return _servletConfig; 434 } 435 436 439 protected void setServletConfig(ServletConfig config) { 440 _servletConfig = config; 441 } 442 443 448 public ServletRequest getServletRequest() { 449 return _request; 450 } 451 452 456 protected void setServletRequest(ServletRequest request) { 457 _request = request; 458 } 459 460 465 public ServletResponse getServletResponse() { 466 return _response; 467 } 468 469 473 protected void setServletResponse(ServletResponse response) { 474 _response = response; 475 } 476 477 478 private Servlet _servlet = null; 479 private ServletConfig _servletConfig = null; 480 private ServletRequest _request = null; 481 private ServletResponse _response = null; 482 private Map <String , Object > _att = new HashMap <String , Object >(); 483 } 484 485 491 protected long getLastModified(HttpServletRequest request) { 492 DownloadServlet.Context context = getDownloadContext(request, null); 494 495 ContentSource source = getContentSource(request); 497 498 return source.getLastModified(context); 500 } 501 502 503 506 private static Map <String , String > mimeTypes = 507 new HashMap <String , String >(120); 508 static { 509 mimeTypes.put("aif", "audio/x-aiff"); 510 mimeTypes.put("aifc", "audio/x-aiff"); 511 mimeTypes.put("aiff", "audio/x-aiff"); 512 mimeTypes.put("asc", "text/plain"); 513 mimeTypes.put("asf", "application/x-ms-asf"); 514 mimeTypes.put("asx", "application/x-ms-asf"); 515 mimeTypes.put("au", "audio/basic"); 516 mimeTypes.put("avi", "video/x-msvideo"); 517 mimeTypes.put("bin", "application/octet-stream"); 518 mimeTypes.put("bmp", "image/bmp"); 519 mimeTypes.put("bwf", "audio/wav"); 520 mimeTypes.put("bz2", "application/x-bzip2"); 521 mimeTypes.put("c", "text/plain"); 522 mimeTypes.put("cc", "text/plain"); 523 mimeTypes.put("cdda", "audio/x-aiff"); 524 mimeTypes.put("class", "application/octet-stream"); 525 mimeTypes.put("com", "application/octet-stream"); 526 mimeTypes.put("cpp", "text/plain"); 527 mimeTypes.put("cpr", "image/cpr"); 528 mimeTypes.put("css", "text/css"); 529 mimeTypes.put("doc", "application/msword"); 530 mimeTypes.put("dot", "application/msword"); 531 mimeTypes.put("dtd", "text/xml"); 532 mimeTypes.put("ear", "application/zip"); 533 mimeTypes.put("exe", "application/octet-stream"); 534 mimeTypes.put("flc", "video/flc"); 535 mimeTypes.put("fm", "application/x-maker"); 536 mimeTypes.put("frame", "application/x-maker"); 537 mimeTypes.put("frm", "application/x-maker"); 538 mimeTypes.put("h", "text/plain"); 539 mimeTypes.put("hh", "text/plain"); 540 mimeTypes.put("hpp", "text/plain"); 541 mimeTypes.put("hqx", "application/mac-binhex40"); 542 mimeTypes.put("htm", "text/html"); 543 mimeTypes.put("html", "text/html"); 544 mimeTypes.put("gif", "image/gif"); 545 mimeTypes.put("gz", "application/x-gunzip"); 546 mimeTypes.put("ico", "image/x-icon"); 547 mimeTypes.put("iso", "application/octet-stream"); 548 mimeTypes.put("jar", "application/zip"); 549 mimeTypes.put("java", "text/plain"); 550 mimeTypes.put("jnlp", "application/x-java-jnlp-file"); 551 mimeTypes.put("jpeg", "image/jpeg"); 552 mimeTypes.put("jpe", "image/jpeg"); 553 mimeTypes.put("jpg", "image/jpeg"); 554 mimeTypes.put("js", "text/x-javascript"); 555 mimeTypes.put("m3u", "audio/x-mpegurl"); 556 mimeTypes.put("maker", "application/x-maker"); 557 mimeTypes.put("mid", "audio/midi"); 558 mimeTypes.put("midi", "audio/midi"); 559 mimeTypes.put("mim", "application/mime"); 560 mimeTypes.put("mime", "application/mime"); 561 mimeTypes.put("mov", "video/quicktime"); 562 mimeTypes.put("mp2", "audio/mpeg"); 563 mimeTypes.put("mp3", "audio/mpeg"); 564 mimeTypes.put("mp4", "video/mpeg4"); 565 mimeTypes.put("mpa", "video/mpeg"); 566 mimeTypes.put("mpe", "video/mpeg"); 567 mimeTypes.put("mpeg", "video/mpeg"); 568 mimeTypes.put("mpg", "video/mpeg"); 569 mimeTypes.put("mpga", "audio/mpeg"); 570 mimeTypes.put("mpm", "video/mpeg"); 571 mimeTypes.put("mpv", "video/mpeg"); 572 mimeTypes.put("pdf", "application/pdf"); 573 mimeTypes.put("pic", "image/x-pict"); 574 mimeTypes.put("pict", "image/x-pict"); 575 mimeTypes.put("pct", "image/x-pict"); 576 mimeTypes.put("pl", "application/x-perl"); 577 mimeTypes.put("png", "image/png"); 578 mimeTypes.put("pnm", "image/x-portable-anymap"); 579 mimeTypes.put("pbm", "image/x-portable-bitmap"); 580 mimeTypes.put("ppm", "image/x-portable-pixmap"); 581 mimeTypes.put("ps", "application/postscript"); 582 mimeTypes.put("ppt", "application/vnd.ms-powerpoint"); 583 mimeTypes.put("qt", "video/quicktime"); 584 mimeTypes.put("ra", "application/vnd.rn-realaudio"); 585 mimeTypes.put("rar", "application/zip"); 586 mimeTypes.put("rf", "application/vnd.rn-realflash"); 587 mimeTypes.put("ra", "audio/vnd.rn-realaudio"); 588 mimeTypes.put("ram", "audio/x-pn-realaudio"); 589 mimeTypes.put("rm", "application/vnd.rn-realmedia"); 590 mimeTypes.put("rmm", "audio/x-pn-realaudio"); 591 mimeTypes.put("rsml", "application/vnd.rn-rsml"); 592 mimeTypes.put("rtf", "text/rtf"); 593 mimeTypes.put("rv", "video/vnd.rn-realvideo"); 594 mimeTypes.put("spl", "application/futuresplash"); 595 mimeTypes.put("snd", "audio/basic"); 596 mimeTypes.put("ssm", "application/smil"); 597 mimeTypes.put("swf", "application/x-shockwave-flash"); 598 mimeTypes.put("tar", "application/x-tar"); 599 mimeTypes.put("tgz", "application/x-gtar"); 600 mimeTypes.put("tif", "image/tiff"); 601 mimeTypes.put("tiff", "image/tiff"); 602 mimeTypes.put("txt", "text/plain"); 603 mimeTypes.put("ulw", "audio/basic"); 604 mimeTypes.put("war", "application/zip"); 605 mimeTypes.put("wav", "audio/x-wav"); 606 mimeTypes.put("wax", "application/x-ms-wax"); 607 mimeTypes.put("wm", "application/x-ms-wm"); 608 mimeTypes.put("wma", "application/x-ms-wma"); 609 mimeTypes.put("wml", "text/wml"); 610 mimeTypes.put("wmw", "application/x-ms-wmw"); 611 mimeTypes.put("wrd", "application/msword"); 612 mimeTypes.put("wvx", "application/x-ms-wvx"); 613 mimeTypes.put("xbm", "image/x-xbitmap"); 614 mimeTypes.put("xpm", "image/image/x-xpixmap"); 615 mimeTypes.put("xml", "text/xml"); 616 mimeTypes.put("xsl", "text/xml"); 617 mimeTypes.put("xls", "application/vnd.ms-excel"); 618 mimeTypes.put("zip", "application/zip"); 619 mimeTypes.put("z", "application/x-compress"); 620 mimeTypes.put("Z", "application/x-compress"); 621 } 622 623 624 private static Map <String , DownloadServlet.ContentSource> _contentSources = 625 new HashMap <String , DownloadServlet.ContentSource>(); 626 627 632 public static final String DOWNLOAD_CONTEXT = "downloadContext"; 633 634 639 public static final String CONTENT_SOURCES = "ContentSources"; 640 641 648 public static final String CONTENT_SOURCE_ID = "contentSourceId"; 649 650 659 public static final String CONTENT_TYPE = "ContentType"; 660 661 664 public static final String DEFAULT_CONTENT_TYPE = 665 "application/octet-stream"; 666 667 674 public static final String EXTENSION = "extension"; 675 } 676 | Popular Tags |