jnlpfilehandler.java

来自「一个小公司要求给写的很简单的任务管理系统。」· Java 代码 · 共 421 行 · 第 1/2 页

JAVA
421
字号
/* * @(#)JnlpFileHandler.java	1.12 05/11/17 *  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * -Redistribution of source code must retain the above copyright notice, this *  list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduce the above copyright notice, *  this list of conditions and the following disclaimer in the documentation *  and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. */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.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?