/** * '$RCSfile: EMLParserServlet.java,v $' * Copyright: 1997-2002 Regents of the University of California, * University of New Mexico, and * Arizona State University * Sponsors: National Center for Ecological Analysis and Synthesis and * Partnership for Interdisciplinary Studies of Coastal Oceans, * University of California Santa Barbara * Long-Term Ecological Research Network Office, * University of New Mexico * Center for Environmental Studies, Arizona State University * Other funding: National Science Foundation (see README for details) * The David and Lucile Packard Foundation * For Details: http://knb.ecoinformatics.org/ * * '$Author: berkley $' * '$Date: 2002-10-29 18:00:20 $' * '$Revision: 1.8 $' * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package org.ecoinformatics.eml; import java.io.*; import java.lang.*; import java.util.*; import java.lang.reflect.*; import java.net.*; import java.util.zip.*; import com.oreilly.servlet.multipart.FilePart; import com.oreilly.servlet.multipart.MultipartParser; import com.oreilly.servlet.multipart.ParamPart; import com.oreilly.servlet.multipart.Part; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpUtils; import javax.servlet.ServletOutputStream; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; /** * Servlet interface for the EMLParser */ public class EMLParserServlet extends HttpServlet { private ServletConfig servletconfig = null; private ServletContext context = null; private HttpServletRequest request; private static HttpServletResponse response; private static PrintWriter out = null; private Hashtable params = new Hashtable(); private static final String namespaces = "@namespaces@"; /** * Initialize the servlet */ public void init(ServletConfig servletconfig) throws ServletException { try { super.init(servletconfig); this.servletconfig = servletconfig; this.context = servletconfig.getServletContext(); System.out.println("Starting EMLParserServlet"); } catch (ServletException ex) { throw ex; } } /** * Destroy the servlet */ public void destroy() { System.out.println("Destroying EMLParserServlet"); } /** Handle "GET" method requests from HTTP clients */ public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Process the data and send back the response handleGetOrPost(request, response); } /** Handle "POST" method requests from HTTP clients */ public void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Process the data and send back the response handleGetOrPost(request, response); } /** * Control servlet response depending on the action parameter specified */ private void handleGetOrPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.request = request; this.response = response; StringBuffer html = new StringBuffer(); out = response.getWriter(); String ctype = request.getContentType(); InputStream fileToParse = null; File tempfile = null; html.append(""); html.append(""); html.append(""); html.append("EML ID and References Parser"); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append(""); html.append("
"); html.append(""); html.append("
"); html.append("EML ID and References Parser
KNB Home Data People Informatics Biocomplexity Education Software
"); html.append("
"); HttpSession sess = request.getSession(true); String sess_id = ""; try { //get the cookie for the session. sess_id = (String)sess.getId(); } catch(IllegalStateException ise) { System.out.println("error in handleGetOrPost: this shouldn't " + "happen: the session should be valid: " + ise.getMessage()); } File tempdir = new File("@tempdir@"); if(!tempdir.exists()) { tempdir.mkdirs(); } tempfile = new File("@tempdir@/.tmpfile." + sess_id); if (ctype != null && ctype.startsWith("multipart/form-data")) { //deal with multipart encoding of the package zip file try { fileToParse = handleGetFile(request, response); int c = fileToParse.read(); FileOutputStream fos = new FileOutputStream(tempfile); while(c != -1) { fos.write(c); c = fileToParse.read(); } fos.flush(); fos.close(); } catch(Exception e) { out.println("

Error handling multipart data: " + e.getMessage() + "

"); System.out.println("Error handling multipart data: " + e.getMessage()); e.printStackTrace(); } } else { Enumeration paramlist = request.getParameterNames(); while (paramlist.hasMoreElements()) { String name = (String)paramlist.nextElement(); Object value = request.getParameterValues(name); params.put(name,value); } } String action = ((String[])params.get("action"))[0]; if(action.equals("parse")) { //parse action html.append(parse(tempfile)); } else if(action.equals("textparse")) { String doctext = ((String[])params.get("doctext"))[0]; if(doctext == null || doctext.trim().equals("")) { html.append("

Error. Submitted document is null.

"); } else { StringReader sr = new StringReader(doctext); FileWriter fw = new FileWriter(tempfile); int c = sr.read(); while(c != -1) { fw.write(c); c = sr.read(); } fw.flush(); fw.close(); html.append(parse(tempfile)); } } else { html.append("

Error. Action '").append(action); html.append("' not registered

"); } tempfile.delete(); html.append("
Back to the previous page."); html.append(""); response.setContentType("text/html"); out.println(html.toString()); out.flush(); } private String parse(File tempfile) { StringBuffer html = new StringBuffer(); try { if(tempfile != null) { EMLParser parser = new EMLParser(tempfile, new File("@servletconfigfile@")); html.append("

Document is EML valid.

There "); html.append("were no EML errors found in your document.

"); } else { html.append("

Error: The file sent to the parser was null.

"); } } catch(Exception e) { html.append("

EML Errors Found

The following errors were found:"); html.append("

").append(e.getMessage()).append("

"); } try { SAXValidate validator = new SAXValidate(true); validator.runTest(new FileReader(tempfile), "DEFAULT", namespaces); html.append("

Document is XML-schema valid.

"); html.append("

There were no XML errors found in your document.

"); } catch(SAXException se) { html.append("

XML-Schema Errors Found

The following errors were "); html.append("found:

").append(se.getMessage()).append("

"); } catch(IOException ioe) { html.append("

IOException: Error reading file

"); html.append("

").append(ioe.getMessage()).append("

"); } catch(ClassNotFoundException cnfe) { html.append("

Parser class not found

"); html.append("

").append(cnfe.getMessage()).append("

"); } return html.toString(); } /** * This method deals with getting the zip file from the client, unzipping * it, then running the process on it. */ private InputStream handleGetFile(HttpServletRequest request, HttpServletResponse response) throws Exception { Hashtable fileList = new Hashtable(); try { MultipartParser mp = new MultipartParser(request, 1024 * 1024 * 8); Part part; while ((part = mp.readNextPart()) != null) { String name = part.getName(); if (part.isParam()) { // it's a parameter part ParamPart paramPart = (ParamPart) part; String value = paramPart.getStringValue(); String[] s = {value}; params.put(name, s); } else if (part.isFile()) { // it's a file part FilePart filePart = (FilePart) part; fileList.put(name, filePart); // Stop once the first file part is found, otherwise going onto the // next part prevents access to the file contents. So...for upload // to work, the datafile must be the last part break; } } } catch (Exception ioe) { throw ioe; } //now that we have the file, do some checking and get the files we need //out of the zip file. FilePart fp = (FilePart)fileList.get("filename"); return fp.getInputStream(); } }