/** * Send JAR or JAR.PACK.GZ file to end user. * * @param filename Name of JAR file * @param response Object for writing to end user. * @throws java.io.IOException */ void sendJar( JnlpJarFiles jnlpFiles, String filename, HttpServletRequest request, HttpServletResponse response) throws IOException { logger.debug("Send jar file " + filename); final File localFile = jnlpFiles.getJarFile(filename); if (localFile == null || !localFile.exists()) { response.sendError( HttpServletResponse.SC_NOT_FOUND, "Jar file '" + filename + "' not found."); return; } logger.debug("Actual file " + localFile.getAbsolutePath()); if (localFile.getName().endsWith(".jar")) { // response.setHeader(CONTENT_ENCODING, JAR_MIME_TYPE); response.setContentType(JAR_MIME_TYPE); } else if (localFile.getName().endsWith(".jar.pack.gz")) { response.setHeader(CONTENT_ENCODING, PACK200_GZIP_ENCODING); response.setContentType(PACK_MIME_TYPE); } // It is very improbable that a 64 bit jar is needed, but // it is better to be ready // response.setContentLength(Integer.parseInt(Long.toString(localFile.length()))); response.setHeader("Content-Length", Long.toString(localFile.length())); response.setDateHeader("Last-Modified", localFile.lastModified()); final FileInputStream fis = new FileInputStream(localFile); final ServletOutputStream os = response.getOutputStream(); try { // Transfer bytes from in to out final byte[] buf = new byte[4096]; int len; while ((len = fis.read(buf)) > 0) { os.write(buf, 0, len); } } catch (final IllegalStateException ex) { logger.debug(ex.getMessage()); } catch (final IOException ex) { logger.debug("Ignore IOException for '" + filename + "'"); } os.flush(); os.close(); fis.close(); }
/** * Write JNLP xml file to browser. * * @param response Object for writing to end user. * @throws java.io.IOException */ void writeJnlpXML( JnlpJarFiles jnlpFiles, HttpServletRequest request, HttpServletResponse response) throws IOException { logger.debug("Writing JNLP file"); // Format URL: "http://host:8080/CONTEXT/webstart/exist.jnlp" final String currentUrl = request.getRequestURL().toString(); // Find BaseUrl http://host:8080/CONTEXT final int webstartPos = currentUrl.indexOf("/webstart"); final String existBaseUrl = currentUrl.substring(0, webstartPos); // Find codeBase for jarfiles http://host:8080/CONTEXT/webstart/ final String codeBase = existBaseUrl + "/webstart/"; // Perfom sanity checks int counter = 0; for (final File jar : jnlpFiles.getAllWebstartJars()) { counter++; // debugging if (jar == null || !jar.exists()) { response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Missing Jar file! (" + counter + ")"); return; } } // Find URL to connect to with client final String startUrl = existBaseUrl .replaceFirst("http:", "xmldb:exist:") .replaceFirst("https:", "xmldb:exist:") .replaceAll("-", "%2D") + "/xmlrpc"; // response.setDateHeader("Last-Modified", mainJar.lastModified()); response.setContentType("application/x-java-jnlp-file"); try { final XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(response.getOutputStream()); writer.writeStartDocument(); writer.writeStartElement("jnlp"); writer.writeAttribute("spec", "1.0+"); writer.writeAttribute("codebase", codeBase); writer.writeAttribute("href", "exist.jnlp"); writer.writeStartElement("information"); writer.writeStartElement("title"); writer.writeCharacters("eXist XML-DB client"); writer.writeEndElement(); writer.writeStartElement("vendor"); writer.writeCharacters("exist-db.org"); writer.writeEndElement(); writer.writeStartElement("homepage"); writer.writeAttribute("href", "http://exist-db.org"); writer.writeEndElement(); writer.writeStartElement("description"); writer.writeCharacters( "Integrated command-line and gui client, " + "entirely based on the XML:DB API and provides commands " + "for most database related tasks, like creating and " + "removing collections, user management, batch-loading " + "XML data or querying."); writer.writeEndElement(); writer.writeStartElement("description"); writer.writeAttribute("kind", "short"); writer.writeCharacters("eXist XML-DB client"); writer.writeEndElement(); writer.writeStartElement("description"); writer.writeAttribute("kind", "tooltip"); writer.writeCharacters("eXist XML-DB client"); writer.writeEndElement(); writer.writeStartElement("icon"); writer.writeAttribute("href", "jnlp_logo.jpg"); writer.writeEndElement(); writer.writeStartElement("icon"); writer.writeAttribute("href", "jnlp_icon_128x128.gif"); writer.writeAttribute("width", "128"); writer.writeAttribute("height", "128"); writer.writeEndElement(); writer.writeStartElement("icon"); writer.writeAttribute("href", "jnlp_icon_64x64.gif"); writer.writeAttribute("width", "64"); writer.writeAttribute("height", "64"); writer.writeEndElement(); writer.writeStartElement("icon"); writer.writeAttribute("href", "jnlp_icon_32x32.gif"); writer.writeAttribute("width", "32"); writer.writeAttribute("height", "32"); writer.writeEndElement(); writer.writeEndElement(); // information writer.writeStartElement("security"); writer.writeEmptyElement("all-permissions"); writer.writeEndElement(); // ---------- writer.writeStartElement("resources"); writer.writeStartElement("property"); writer.writeAttribute("name", "jnlp.packEnabled"); writer.writeAttribute("value", "true"); writer.writeEndElement(); writer.writeStartElement("j2se"); writer.writeAttribute("version", "1.6+"); writer.writeEndElement(); for (final File jar : jnlpFiles.getAllWebstartJars()) { writer.writeStartElement("jar"); writer.writeAttribute("href", jar.getName()); writer.writeAttribute("size", "" + jar.length()); writer.writeEndElement(); } writer.writeEndElement(); // resources writer.writeStartElement("application-desc"); writer.writeAttribute("main-class", "org.exist.client.InteractiveClient"); writer.writeStartElement("argument"); writer.writeCharacters("-ouri=" + startUrl); writer.writeEndElement(); writer.writeStartElement("argument"); writer.writeCharacters("--no-embedded-mode"); writer.writeEndElement(); if (request.isSecure()) { writer.writeStartElement("argument"); writer.writeCharacters("--use-ssl"); writer.writeEndElement(); } writer.writeEndElement(); // application-desc writer.writeEndElement(); // jnlp writer.writeEndDocument(); writer.flush(); writer.close(); } catch (final Throwable ex) { logger.error(ex); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage()); } }