1 36 37 package jnlp.sample.servlet; 38 39 import java.io.*; 40 import java.util.*; 41 import java.net.*; 42 import javax.servlet.*; 43 import javax.servlet.http.*; 44 45 63 public class JnlpDownloadServlet extends HttpServlet { 64 65 private static ResourceBundle _resourceBundle = null; 67 68 private static final String PARAM_JNLP_EXTENSION = "jnlp-extension"; 70 private static final String PARAM_JAR_EXTENSION = "jar-extension"; 71 72 private Logger _log = null; 74 75 private JnlpFileHandler _jnlpFileHandler = null; 76 private JarDiffHandler _jarDiffHandler = null; 77 private ResourceCatalog _resourceCatalog = null; 78 79 80 public void init(ServletConfig config) throws ServletException { 81 super.init(config); 82 83 84 _log = new Logger(config, getResourceBundle()); 86 _log.addDebug("Initializing"); 87 88 JnlpResource.setDefaultExtensions( 90 config.getInitParameter(PARAM_JNLP_EXTENSION), 91 config.getInitParameter(PARAM_JAR_EXTENSION)); 92 93 _jnlpFileHandler = new JnlpFileHandler(config.getServletContext(), _log); 94 _jarDiffHandler = new JarDiffHandler(config.getServletContext(), _log); 95 _resourceCatalog = new ResourceCatalog(config.getServletContext(), _log); 96 } 97 98 public static synchronized ResourceBundle getResourceBundle() { 99 if (_resourceBundle == null) { 100 _resourceBundle = ResourceBundle.getBundle("jnlp/sample/servlet/resources/strings"); 101 } 102 return _resourceBundle; 103 } 104 105 106 public void doHead(HttpServletRequest request, HttpServletResponse response) 107 throws ServletException, IOException { 108 handleRequest(request, response, true); 109 } 110 111 112 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 113 handleRequest(request, response, false); 114 } 115 116 private void handleRequest(HttpServletRequest request, 117 HttpServletResponse response, boolean isHead) throws IOException { 118 String requestStr = request.getRequestURI(); 119 if (request.getQueryString() != null) requestStr += "?" + request.getQueryString().trim(); 120 121 DownloadRequest dreq = new DownloadRequest(getServletContext(), request); 123 if (_log.isInformationalLevel()) { 124 _log.addInformational("servlet.log.info.request", requestStr); 125 _log.addInformational("servlet.log.info.useragent", request.getHeader("User-Agent")); 126 } 127 if (_log.isDebugLevel()) { 128 _log.addDebug(dreq.toString()); 129 } 130 131 long ifModifiedSince = request.getDateHeader("If-Modified-Since"); 132 133 try { 135 validateRequest(dreq); 137 138 JnlpResource jnlpres = locateResource(dreq); 140 _log.addDebug("JnlpResource: " + jnlpres); 141 142 143 if (_log.isInformationalLevel()) { 144 _log.addInformational("servlet.log.info.goodrequest", jnlpres.getPath()); 145 } 146 147 DownloadResponse dres = null; 148 149 if (isHead) { 150 151 int cl = 152 jnlpres.getResource().openConnection().getContentLength(); 153 154 dres = DownloadResponse.getHeadRequestResponse( 156 jnlpres.getMimeType(), jnlpres.getVersionId(), 157 jnlpres.getLastModified(), cl); 158 159 } else if (ifModifiedSince != -1 && 160 (ifModifiedSince / 1000) >= 161 (jnlpres.getLastModified() / 1000)) { 162 168 _log.addDebug("return 304 Not modified"); 170 dres = DownloadResponse.getNotModifiedResponse(); 171 172 } else { 173 174 dres = constructResponse(jnlpres, dreq); 176 } 177 178 dres.sendRespond(response); 179 180 } catch(ErrorResponseException ere) { 181 if (_log.isInformationalLevel()) { 182 _log.addInformational("servlet.log.info.badrequest", requestStr); 183 } 184 if (_log.isDebugLevel()) { 185 _log.addDebug("Response: "+ ere.toString()); 186 } 187 ere.getDownloadResponse().sendRespond(response); 189 } catch(Throwable e) { 190 _log.addFatal("servlet.log.fatal.internalerror", e); 191 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 192 } 193 } 194 195 198 private void validateRequest(DownloadRequest dreq) throws ErrorResponseException { 199 String path = dreq.getPath(); 200 if (path.endsWith(ResourceCatalog.VERSION_XML_FILENAME) || 201 path.indexOf("__") != -1 ) { 202 throw new ErrorResponseException(DownloadResponse.getNoContentResponse()); 203 } 204 } 205 206 209 private JnlpResource locateResource(DownloadRequest dreq) throws IOException, ErrorResponseException { 210 if (dreq.getVersion() == null) { 211 return handleBasicDownload(dreq); 212 } else { 213 return handleVersionRequest(dreq); 214 } 215 } 216 217 private JnlpResource handleBasicDownload(DownloadRequest dreq) throws ErrorResponseException, IOException { 218 _log.addDebug("Basic Protocol lookup"); 219 if (dreq.getPath() == null || dreq.getPath().endsWith("/")) { 221 throw new ErrorResponseException(DownloadResponse.getNoContentResponse()); 222 } 223 JnlpResource jnlpres = new JnlpResource(getServletContext(), dreq.getPath()); 225 if (!jnlpres.exists()) { 226 throw new ErrorResponseException(DownloadResponse.getNoContentResponse()); 227 } 228 return jnlpres; 229 } 230 231 private JnlpResource handleVersionRequest(DownloadRequest dreq) throws IOException, ErrorResponseException { 232 _log.addDebug("Version-based/Extension based lookup"); 233 return _resourceCatalog.lookupResource(dreq); 234 } 235 236 239 private DownloadResponse constructResponse(JnlpResource jnlpres, DownloadRequest dreq) throws IOException { 240 String path = jnlpres.getPath(); 241 if (jnlpres.isJnlpFile()) { 242 boolean supportQuery = JarDiffHandler.isJavawsVersion(dreq, "1.5+"); 244 _log.addDebug("SupportQuery in Href: " + supportQuery); 245 246 if (supportQuery) { 248 return _jnlpFileHandler.getJnlpFileEx(jnlpres, dreq); 249 } else { 250 return _jnlpFileHandler.getJnlpFile(jnlpres, dreq); 251 } 252 } 253 254 if (dreq.getCurrentVersionId() != null && jnlpres.isJarFile()) { 256 DownloadResponse response = _jarDiffHandler.getJarDiffEntry(_resourceCatalog, dreq, jnlpres); 257 if (response != null) { 258 _log.addInformational("servlet.log.info.jardiff.response"); 259 return response; 260 } 261 } 262 263 JnlpResource jr = new JnlpResource(getServletContext(), 265 jnlpres.getName(), 266 jnlpres.getVersionId(), 267 jnlpres.getOSList(), 268 jnlpres.getArchList(), 269 jnlpres.getLocaleList(), 270 jnlpres.getPath(), 271 jnlpres.getReturnVersionId(), 272 dreq.getEncoding()); 273 274 _log.addDebug("Real resource returned: " + jr); 275 276 return DownloadResponse.getFileDownloadResponse(jr.getResource(), 278 jr.getMimeType(), 279 jr.getLastModified(), 280 jr.getReturnVersionId()); 281 } 282 } 283 284 285 | Popular Tags |