📄 jnlpfilehandler.java
字号:
/* * @(#)JnlpFileHandler.java 1.10 03/10/31 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package jnlp.sample.servlet;import java.util.*;import java.util.regex.*;import java.net.*;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import javax.xml.parsers.*;import org.xml.sax.*;import javax.xml.transform.*;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.w3c.dom.*;/* The JNLP file handler implements a class that keeps * track of JNLP files and their specializations */public class JnlpFileHandler { private static final String JNLP_MIME_TYPE = "application/x-java-jnlp-file"; private static final String HEADER_LASTMOD = "Last-Modified"; private ServletContext _servletContext; private Logger _log = null; private HashMap _jnlpFiles = null; /** Initialize JnlpFileHandler for the specific ServletContext */ public JnlpFileHandler(ServletContext servletContext, Logger log) { _servletContext = servletContext; _log = log; _jnlpFiles = new HashMap(); } private static class JnlpFileEntry { // Response DownloadResponse _response; // Keeps track of cache is out of date private long _lastModified; // Constructor JnlpFileEntry(DownloadResponse response, long lastmodfied) { _response = response; _lastModified = lastmodfied; } public DownloadResponse getResponse() { return _response; } long getLastModified() { return _lastModified; } } /* Main method to lookup an entry */ public synchronized DownloadResponse getJnlpFile(JnlpResource jnlpres, DownloadRequest dreq) throws IOException { String path = jnlpres.getPath(); URL resource = jnlpres.getResource(); long lastModified = jnlpres.getLastModified(); _log.addDebug("lastModified: " + lastModified + " " + new Date(lastModified)); if (lastModified == 0) { _log.addWarning("servlet.log.warning.nolastmodified", path); } // fix for 4474854: use the request URL as key to look up jnlp file // in hash map String reqUrl = HttpUtils.getRequestURL(dreq.getHttpRequest()).toString(); // Check if entry already exist in HashMap JnlpFileEntry jnlpFile = (JnlpFileEntry)_jnlpFiles.get(reqUrl); if (jnlpFile != null && jnlpFile.getLastModified() == lastModified) { // Entry found in cache, so return it return jnlpFile.getResponse(); } // Read information from WAR file long timeStamp = lastModified; String mimeType = _servletContext.getMimeType(path); if (mimeType == null) mimeType = JNLP_MIME_TYPE; StringBuffer jnlpFileTemplate = new StringBuffer(); URLConnection conn = resource.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line = br.readLine(); if (line != null && line.startsWith("TS:")) { timeStamp = parseTimeStamp(line.substring(3)); _log.addDebug("Timestamp: " + timeStamp + " " + new Date(timeStamp)); if (timeStamp == 0) { _log.addWarning("servlet.log.warning.notimestamp", path); timeStamp = lastModified; } line = br.readLine(); } while(line != null) { jnlpFileTemplate.append(line); line = br.readLine(); } String jnlpFileContent = specializeJnlpTemplate(dreq.getHttpRequest(), path, jnlpFileTemplate.toString()); // Convert to bytes as a UTF-8 encoding byte[] byteContent = jnlpFileContent.getBytes("UTF-8"); // Create entry DownloadResponse resp = DownloadResponse.getFileDownloadResponse(byteContent, mimeType, timeStamp, jnlpres.getReturnVersionId()); jnlpFile = new JnlpFileEntry(resp, lastModified); _jnlpFiles.put(reqUrl, jnlpFile); return resp; } /* Main method to lookup an entry (NEW for JavaWebStart 1.5+) */ public synchronized DownloadResponse getJnlpFileEx(JnlpResource jnlpres, DownloadRequest dreq) throws IOException { String path = jnlpres.getPath(); URL resource = jnlpres.getResource(); long lastModified = jnlpres.getLastModified(); _log.addDebug("lastModified: " + lastModified + " " + new Date(lastModified)); if (lastModified == 0) { _log.addWarning("servlet.log.warning.nolastmodified", path); } // fix for 4474854: use the request URL as key to look up jnlp file // in hash map String reqUrl = HttpUtils.getRequestURL(dreq.getHttpRequest()).toString(); // SQE: To support query string, we changed the hash key from Request URL to (Request URL + query string) if (dreq.getQuery() != null) reqUrl += dreq.getQuery(); // Check if entry already exist in HashMap JnlpFileEntry jnlpFile = (JnlpFileEntry)_jnlpFiles.get(reqUrl); if (jnlpFile != null && jnlpFile.getLastModified() == lastModified) { // Entry found in cache, so return it return jnlpFile.getResponse(); } // Read information from WAR file long timeStamp = lastModified; String mimeType = _servletContext.getMimeType(path); if (mimeType == null) mimeType = JNLP_MIME_TYPE; StringBuffer jnlpFileTemplate = new StringBuffer(); URLConnection conn = resource.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line = br.readLine(); if (line != null && line.startsWith("TS:")) { timeStamp = parseTimeStamp(line.substring(3)); _log.addDebug("Timestamp: " + timeStamp + " " + new Date(timeStamp)); if (timeStamp == 0) { _log.addWarning("servlet.log.warning.notimestamp", path); timeStamp = lastModified; } line = br.readLine(); } while(line != null) { jnlpFileTemplate.append(line); line = br.readLine(); } String jnlpFileContent = specializeJnlpTemplate(dreq.getHttpRequest(), path, jnlpFileTemplate.toString()); /* SQE: We need to add query string back to href in jnlp file. We also need to handle JRE requirement for * the test. We reconstruct the xml DOM object, modify the value, then regenerate the jnlpFileContent. */ String query = dreq.getQuery(); String testJRE = dreq.getTestJRE(); _log.addDebug("Double check query string: " + query); // For backward compatibility: Always check if the href value exists. // Bug 4939273: We will retain the jnlp template structure and will NOT add href value. Above old // approach to always check href value caused some test case not run. if (query != null) { byte [] cb = jnlpFileContent.getBytes("UTF-8"); ByteArrayInputStream bis = new ByteArrayInputStream(cb); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(bis); if (document != null && document.getNodeType() == Node.DOCUMENT_NODE) { boolean modified = false; Element root = document.getDocumentElement(); /** * This part is for VM Args Test. If the query string has any parameter * with VM-Args= then this block will add /replace the java-vm-args attribute * with the value given in the VM-Args parameter. The more than one VM-Args * should be seperated using "+" symbol i.e. the query string should comply * with the standard format of the query string. * * Added By Elancheran 09/12/03 */ String vmArgs = null; if( query != null && query.indexOf("VM-Args") != -1) { String tempStr = query.substring(query.indexOf("VM-Args=") + 8); if(tempStr.indexOf('&') != -1) vmArgs = tempStr.substring(0, tempStr.indexOf('&')); vmArgs = tempStr; vmArgs = vmArgs.replace('+', ' '); _log.addDebug("VM-Args given in the query string: " + vmArgs); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -