1 12 package org.eclipse.equinox.http.servlet.internal; 13 14 import java.io.*; 15 import java.net.URL ; 16 import java.net.URLConnection ; 17 import java.security.*; 18 import javax.servlet.ServletContext ; 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 import org.osgi.service.http.HttpContext; 22 23 public class ResourceRegistration extends Registration { 24 private static final String LAST_MODIFIED = "Last-Modified"; private static final String IF_MODIFIED_SINCE = "If-Modified-Since"; private static final String IF_NONE_MATCH = "If-None-Match"; private static final String ETAG = "ETag"; 29 private String internalName; 30 HttpContext httpContext; 31 ServletContext servletContext; 32 private AccessControlContext acc; 33 34 public ResourceRegistration(String internalName, HttpContext context, ServletContext servletContext, AccessControlContext acc) { 35 this.internalName = internalName; 36 if (internalName.equals("/")) { this.internalName = ""; } 39 this.httpContext = context; 40 this.servletContext = servletContext; 41 this.acc = acc; 42 } 43 44 public boolean handleRequest(HttpServletRequest req, final HttpServletResponse resp, String alias) throws IOException { 45 if (httpContext.handleSecurity(req, resp)) { 46 47 String method = req.getMethod(); 48 if (method.equals("GET") || method.equals("POST") || method.equals("HEAD")) { 50 String pathInfo = HttpServletRequestAdaptor.getDispatchPathInfo(req); 51 int aliasLength = alias.equals("/") ? 0 : alias.length(); String resourcePath = internalName + pathInfo.substring(aliasLength); 53 URL testURL = httpContext.getResource(resourcePath); 54 if (testURL == null || resourcePath.endsWith("/")) { return false; 56 } 57 return writeResource(req, resp, resourcePath); 58 } 59 resp.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); 60 } 61 return true; 62 } 63 64 private boolean writeResource(final HttpServletRequest req, final HttpServletResponse resp, final String resourcePath) throws IOException { 65 Boolean result = Boolean.TRUE; 66 try { 67 result = (Boolean ) AccessController.doPrivileged(new PrivilegedExceptionAction() { 68 69 public Object run() throws Exception { 70 URL url = httpContext.getResource(resourcePath); 71 if (url == null) 72 return Boolean.FALSE; 73 74 URLConnection connection = url.openConnection(); 75 long lastModified = connection.getLastModified(); 76 int contentLength = connection.getContentLength(); 77 78 if (contentLength <= 0) 80 return Boolean.FALSE; 81 82 String etag = null; 83 if (lastModified != -1 && contentLength != -1) 84 etag = "W/\"" + contentLength + "-" + lastModified + "\""; 86 String ifNoneMatch = req.getHeader(IF_NONE_MATCH); 89 if (ifNoneMatch != null && etag != null && ifNoneMatch.indexOf(etag) != -1) { 90 resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 91 return Boolean.TRUE; 92 } else { 93 long ifModifiedSince = req.getDateHeader(IF_MODIFIED_SINCE); 94 if (ifModifiedSince > -1 && lastModified > 0 && lastModified <= (ifModifiedSince + 999)) { 97 resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 98 return Boolean.TRUE; 99 } 100 } 101 102 if (contentLength != -1) 104 resp.setContentLength(contentLength); 105 106 String contentType = httpContext.getMimeType(resourcePath); 107 if (contentType == null) 108 contentType = servletContext.getMimeType(resourcePath); 109 110 if (contentType != null) 111 resp.setContentType(contentType); 112 113 if (lastModified > 0) 114 resp.setDateHeader(LAST_MODIFIED, lastModified); 115 116 if (etag != null) 117 resp.setHeader(ETAG, etag); 118 119 try { 120 OutputStream os = resp.getOutputStream(); 121 int writtenContentLength = writeResourceToOutputStream(connection, os); 122 if (contentLength == -1 || contentLength != writtenContentLength) 123 resp.setContentLength(writtenContentLength); 124 } catch (IllegalStateException e) { Writer writer = resp.getWriter(); 126 writeResourceToWriter(connection, writer); 127 } 131 return Boolean.TRUE; 132 } 133 }, acc); 134 } catch (PrivilegedActionException e) { 135 throw (IOException) e.getException(); 136 } 137 return result.booleanValue(); 138 } 139 140 int writeResourceToOutputStream(URLConnection connection, OutputStream os) throws IOException { 141 InputStream is = connection.getInputStream(); 142 try { 143 byte[] buffer = new byte[8192]; 144 int bytesRead = is.read(buffer); 145 int writtenContentLength = 0; 146 while (bytesRead != -1) { 147 os.write(buffer, 0, bytesRead); 148 writtenContentLength += bytesRead; 149 bytesRead = is.read(buffer); 150 } 151 return writtenContentLength; 152 } finally { 153 if (is != null) 154 is.close(); 155 } 156 } 157 158 void writeResourceToWriter(URLConnection connection, Writer writer) throws IOException { 159 InputStream is = connection.getInputStream(); 160 try { 161 Reader reader = new InputStreamReader(connection.getInputStream()); 162 try { 163 char[] buffer = new char[8192]; 164 int charsRead = reader.read(buffer); 165 while (charsRead != -1) { 166 writer.write(buffer, 0, charsRead); 167 charsRead = reader.read(buffer); 168 } 169 } finally { 170 if (reader != null) { 171 reader.close(); is = null; 173 } 174 } 175 } finally { 176 if (is != null) 177 is.close(); 178 } 179 } 180 } 181 | Popular Tags |