X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fuwobo%2Fsrc%2Fit%2Funibo%2Fcs%2Fhelm%2Fuwobo%2FServlet.java;h=d259c3f89f68db1d8c6f48875d897a9a21ab947a;hb=caab26f85122e0040555139c48a9111a0b2b9fff;hp=0e28877fd42827e43518c48d8435a7d01a85e059;hpb=02bc1a8c06b2d2c5169c9f1beecf9daa94a85247;p=helm.git diff --git a/helm/uwobo/src/it/unibo/cs/helm/uwobo/Servlet.java b/helm/uwobo/src/it/unibo/cs/helm/uwobo/Servlet.java index 0e28877fd..d259c3f89 100644 --- a/helm/uwobo/src/it/unibo/cs/helm/uwobo/Servlet.java +++ b/helm/uwobo/src/it/unibo/cs/helm/uwobo/Servlet.java @@ -1,3 +1,27 @@ +/* Copyright (C) 2001, HELM Team + * + * This file is part of UWOBO, developed at the Computer Science + * Department, University of Bologna, Italy. + * + * UWOBO 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. + * + * UWOBO 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 UWOBO; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + * + * For details, see the UWOBO World-Wide-Web page, + * http://cs.unibo.it/helm/uwobo + */ + package it.unibo.cs.helm.uwobo; import java.io.*; @@ -7,24 +31,24 @@ import javax.servlet.http.*; import javax.xml.transform.*; import org.xml.sax.*; +// WARNING: next comment is obsolete!!!!! /* * * usage: -* http://aristotele/helm/servlet/uwobo/help -* http://aristotele/helm/servlet/uwobo/add?xsluri=&key= -* http://aristotele/helm/servlet/uwobo/remove[?key=] -* http://aristotele/helm/servlet/uwobo/list -* http://aristotele/helm/servlet/uwobo/reload[?key=] -* http://aristotele/helm/servlet/uwobo/update[?key=] -* http://aristotele/helm/servlet/uwobo/apply?xmluri=&keys=[¶m.=]* +* http://hostname/helm/servlet/uwobo/help +* http://hostname/helm/servlet/uwobo/add?bind=key,stylesheet[&bind=key,stylesheet]* +* http://hostname/helm/servlet/uwobo/remove[?keys=key_1,...,key_n] +* http://hostname/helm/servlet/uwobo/list +* http://hostname/helm/servlet/uwobo/reload[?keys=key_1,...,key_n] +* http://hostname/helm/servlet/uwobo/apply?xmluri=xmldata&keys=key_1,...,key_n[¶m.name=value]*[¶m.key.name=value]*[&prop.name=[value]]* * * example: -* http://aristotele/helm/servlet/uwobo/add?xsluri=file:///D:/Archivio/Progetti/helm/resources/xsl/foo1.xsl&key=foo1 -* http://aristotele/helm/servlet/uwobo/add?xsluri=file:///D:/Archivio/Progetti/helm/resources/xsl/foo2.xsl&key=foo2 -* http://aristotele/helm/servlet/uwobo/apply?xmluri=file:///D:/Archivio/Progetti/helm/resources/xsl/foo.xml&key=foo1&key=foo2 +* http://aristotele/helm/servlet/uwobo/add?bind=foo1,file:///D:/Archivio/Progetti/helm/resources/xsl/foo1.xsl +* http://aristotele/helm/servlet/uwobo/add?bind=foo2,file:///D:/Archivio/Progetti/helm/resources/xsl/foo2.xsl +* http://aristotele/helm/servlet/uwobo/apply?xmluri=file:///D:/Archivio/Progetti/helm/resources/xsl/foo.xml&keys=foo1,foo2 * * installation notes (Tomcat): -* replace parser.jar and jaxp.jar from /lib with xerces.jar +* replace parser.jar and jaxp.jar from /lib with xerces.jar and add xalan.jar * * add in conf/server.xml * hostname/helm/servlet/uwobo/help", - "http://hostname/helm/servlet/uwobo/add?xsluri=stylesheet&key=name", - "http://hostname/helm/servlet/uwobo/remove[?key=name]", - "http://hostname/helm/servlet/uwobo/list", - "http://hostname/helm/servlet/uwobo/reload[?key=name]", - "http://hostname/helm/servlet/uwobo/update[?key=name]", - "http://hostname/helm/servlet/uwobo/apply?xmluri=xmldata&keys=key_1,...,key_n[¶m.name=value]*" - }; - public static final String help; - - static { - StringBuffer sb = new StringBuffer(); - sb.append(""); - help = sb.toString(); - } + + public static final String[] usage = { + "http://hostname/helm/servlet/uwobo/help", + "http://hostname/helm/servlet/uwobo/add?bind=key,stylesheet[&bind=key,stylesheet]*", + "http://hostname/helm/servlet/uwobo/remove[?keys=key_1,...,key_n]", + "http://hostname/helm/servlet/uwobo/list", + "http://hostname/helm/servlet/uwobo/reload[?keys=key_1,...,key_n]", + "http://hostname/helm/servlet/uwobo/apply?xmluri=xmldata&keys=key_1,...,key_n[¶m.name=value]*[¶m.key.name=value]*[&prop.name=[value]]*" + }; + public static final String help; + + static { + StringBuffer sb = new StringBuffer(); + sb.append(""); + help = sb.toString(); + } - private Server server; + private Server server; - public void init(ServletConfig config) throws ServletException - { - super.init(config); + public void init(ServletConfig config) + throws ServletException + { + super.init(config); - System.out.println("UWOBO init"); - server = new Server(); - } + System.out.println("UWOBO init"); + server = new Server(); + } - public static String[] split(final String s, final String delim) - { - StringTokenizer st = new StringTokenizer(s, delim); - String[] res = new String[st.countTokens()]; - for (int i = 0; i < res.length; i++) res[i] = st.nextToken(); - return res; - } + private static String[] split(final String s, final String delim) + { + String[] res = {null}; + if (s == null) return res; + StringTokenizer st = new StringTokenizer(s, delim); + res = new String[st.countTokens()]; + for (int i = 0; i < res.length; i++) res[i] = st.nextToken(); + return res; + } - public void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException - { - System.out.println("UWOBO "+request.getPathInfo()); - ServletOutputStream out; - - try { - final String cmd = request.getPathInfo(); - if (cmd == null) { - sendError(response, HttpServletResponse.SC_NOT_FOUND, "unknown command", help); - return; - } - if (cmd.equals("/add")) { - final String filename = request.getParameter("xsluri"); - if (filename == null) { - sendError(response, HttpServletResponse.SC_BAD_REQUEST, "bad parameters", usage[1]); - return; - } - final String key = request.getParameter("key"); - server.add(filename, key); - } else if (cmd.equals("/apply")) { - final String infile = request.getParameter("xmluri"); - final String keys = request.getParameter("keys"); + private static String[] split2(final String s, final String delim) + { + String[] res = new String[2]; + StringTokenizer st = new StringTokenizer(s); + res[0] = st.nextToken(delim); res[1] = st.nextToken("").substring(1); + return res; + } - if (infile == null || keys == null) { - sendError(response, HttpServletResponse.SC_BAD_REQUEST, "bad parameters", usage[6]); - return; - } + private void html_open(HttpServletResponse resp, ServletOutputStream out) + throws IOException + { + resp.setContentType("text/html"); + out.println("

" + server.PACKAGE + " servlet

"); + } + + private void html_close(ServletOutputStream out) + throws IOException + { + out.println("

done

"); + out.close(); + } - final String[] keyName = split(keys, ","); - final Key[] keySeq = new Key[keyName.length]; - for (int i = 0; i < keySeq.length; i++) { - keySeq[i] = new Key(); - keySeq[i].name = keyName[i]; - keySeq[i].params = new HashMap(); - } + private String msg_out(String message) + { + return message+"
"; + } + + private String exc_out(String message, Exception e) + { + if (message == null) + { + message = e.getClass().getName(); + message = message.substring(message.lastIndexOf('.')+1); + } + String local = e.getLocalizedMessage(); + local = local.substring(local.lastIndexOf(':')+1); + return ""+message+": "+local+"
"; + } + + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException + { + ServletOutputStream out = response.getOutputStream(); + String log = ""; + + response.setHeader("Cache-Control", "no-cache"); + response.setHeader("Pragma", "no-cache"); + response.setHeader("Expires", "0"); + System.out.println("UWOBO "+request.getPathInfo()); + + try { + final String cmd = request.getPathInfo(); + if (cmd == null) { + sendError(response, out, "unknown command", help); return; + } + if (cmd.equals("/add")) { + final String[] xslkey = request.getParameterValues("bind"); + if (xslkey == null) { + sendError(response, out, "bad parameters", usage[1]); return; + } + html_open(response, out); + for (int i = 0; i < xslkey.length; i++) { + final String data[] = split2(xslkey[i], ","); + out.println(msg_out("adding stylesheet "+data[0]+" ("+data[1]+")")); + try { + log = server.add(data[1], data[0]); + if(!log.equals("")) + out.println(msg_out(log)); + } catch (TransformerConfigurationException tce) { + out.println(exc_out("Stylesheet Error", tce)); + } catch (Exception e) { + out.println(exc_out(null, e)); + } + } + html_close(out); return; + } + if (cmd.equals("/apply")) { + final String infile = request.getParameter("xmluri"); + final String keys = request.getParameter("keys"); - final Properties props = new Properties(); - final Enumeration e = request.getParameterNames(); - while (e.hasMoreElements()) { - String param = (String) e.nextElement(); - if (param.startsWith("param.")) { - final String name = param.substring(6); - final String value = request.getParameter(param); - final String[] keyParam = split(name, "."); - if (keyParam.length == 1) { - // this is a global parameter - Server.log("global parameter: " + keyParam[0] + " = " + value); - for (int i = 0; i < keySeq.length; i++) - // we add the global parameter only if there is no - // local parameter with the same name - if (!keySeq[i].params.containsKey(keyParam[0])) - keySeq[i].params.put(keyParam[0], value); - } else if (keyParam.length == 2) { - // this is a local parameter - Server.log("local parameter: " + keyParam[0] + "." + keyParam[1] + " = " + value); - for (int i = 0; i < keySeq.length; i++) { - if (keySeq[i].name.equals(keyParam[0])) - keySeq[i].params.put(keyParam[1], value); - } - } else { - sendError(response, HttpServletResponse.SC_BAD_REQUEST, "bad parameters", usage[6]); - return; - } - } else if (param.startsWith("prop.")) { - final String name = param.substring(5); - final String value = request.getParameter(param); - Server.log("property: " + name + " = " + value); - props.setProperty(name, value); - } - } - - String contentType = props.getProperty(OutputKeys.MEDIA_TYPE); - if (contentType == null && keySeq.length > 0) - contentType = server.getContentType(keySeq[keySeq.length - 1].name); - else if (contentType == null) - contentType = "text/xml"; - response.setContentType(contentType); - Server.log("content type: " + contentType); + if (infile == null || keys == null) { + sendError(response, out, "bad parameters", usage[6]); return; + } - out = response.getOutputStream(); - server.apply(infile, out, keySeq, props); - out.close(); - return; - } else if (cmd.equals("/remove")) { - final String key = request.getParameter("key"); - if (key == null) - server.removeAll(); - else - server.remove(key); - } else if (cmd.equals("/list")) { - Iterator i = server.list().iterator(); + final String[] keyName = split(keys, ","); + final Key[] keySeq = new Key[keyName.length]; + for (int i = 0; i < keySeq.length; i++) { + keySeq[i] = new Key(); + keySeq[i].name = keyName[i]; + keySeq[i].params = new HashMap(); + } - response.setContentType("text/html"); - out = response.getOutputStream(); - out.println("

Uwobo servlet

stylesheet list:

"); - out.close(); - return; - } else if (cmd.equals("/reload")) { - final String key = request.getParameter("key"); - if (key == null) - server.reloadAll(); - else - server.reload(key); - } else if (cmd.equals("/update")) { - final String key = request.getParameter("key"); - if (key == null) - server.updateAll(); - else - server.update(key); - } else if (cmd.equals("/help")) { - response.setContentType("text/html"); - out = response.getOutputStream(); - out.println("

"+server.PACKAGE+" servlet - version "+server.VERSION+"

"); - out.println("compiled "+server.DATE+" at "+server.TIME.substring(0,2)+":"+server.TIME.substring(2)+""); - out.println("

usage:

"+help+""); - out.close(); - return; - } else { - sendError(response, HttpServletResponse.SC_NOT_FOUND, "unknown command", help); - return; - } - } catch (TransformerConfigurationException tce) { - sendError(response, HttpServletResponse.SC_BAD_REQUEST, "stylesheet error", tce); - return; - } catch (SAXException se) { - sendError(response, HttpServletResponse.SC_BAD_REQUEST, "SAX Exception", se); - return; - } catch (Exception e) { - sendError(response, HttpServletResponse.SC_BAD_REQUEST, "exception", e); - return; - } + final Properties props = new Properties(); + final Enumeration e = request.getParameterNames(); + while (e.hasMoreElements()) { + String param = (String) e.nextElement(); + if (param.startsWith("param.")) { + final String name = param.substring(6); + final String value = request.getParameter(param); + final String[] keyParam = split(name, "."); + if (keyParam.length == 1) { + // this is a global parameter + Server.log("global parameter: " + keyParam[0] + " = " + value); + for (int i = 0; i < keySeq.length; i++) + // we add the global parameter only if there is no + // local parameter with the same name + if (!keySeq[i].params.containsKey(keyParam[0])) + keySeq[i].params.put(keyParam[0], value); + } else + if (keyParam.length == 2) { + // this is a local parameter + Server.log("local parameter: " + keyParam[0] + "." + keyParam[1] + " = " + value); + for (int i = 0; i < keySeq.length; i++) { + if (keySeq[i].name.equals(keyParam[0])) + keySeq[i].params.put(keyParam[1], value); + } + } else { + sendError(response, out, "bad parameters", usage[6]); return; + } + } else + if (param.startsWith("prop.")) { + final String name = param.substring(5); + final String value = request.getParameter(param); + Server.log("property: " + name + " = " + value); + props.setProperty(name, value); + } + } + + String contentType = props.getProperty(OutputKeys.MEDIA_TYPE); + if (contentType == null && keySeq.length > 0) + contentType = server.getContentType(keySeq[keySeq.length - 1].name); + else if (contentType == null) + contentType = "text/xml"; + response.setContentType(contentType); + Server.log("content type: " + contentType); - response.setContentType("text/html"); - response.setHeader("Cache-Control", "no-cache"); - response.setHeader("Pragma", "no-cache"); - response.setHeader("Expires", "0"); - out = response.getOutputStream(); - out.println("

Uwobo servlet

done

"); - out.close(); - } + try { + out = response.getOutputStream(); + server.apply(infile, out, keySeq, props); + out.close(); + } catch (TransformerConfigurationException tce) { + sendError(response, out, exc_out("stylesheet error", tce), ""); + } catch (Exception ee) { + sendError(response, out, exc_out(null, ee), ""); + } + return; + } + if (cmd.equals("/remove")) { + final String key = request.getParameter("keys"); + final String [] data = split(key, ","); + html_open(response, out); + for (int i = 0; i < data.length; i++) { + if (data[i] == null) { + out.println(msg_out("removing all keys")); + ArrayList res; + res = server.removeAll(); + Iterator j = res.iterator(); + while (j.hasNext()) + out.println(msg_out((String)j.next())); + } else { + out.println(msg_out("removing key "+data[i])); + out.println(msg_out(server.remove(data[i]))); + } + } + html_close(out); return; + } + if (cmd.equals("/list")) { + html_open(response, out); + out.println("

stylesheet list:

"); html_close(out); return; + } + if (cmd.equals("/reload")) { + final String key = request.getParameter("keys"); + final String [] data = split(key, ","); + html_open(response, out); + for (int i = 0; i < data.length; i++) { + try { + if (data[i] == null) { + out.println(msg_out("reloading all keys")); + ArrayList res; + res = server.reloadAll(); + Iterator j = res.iterator(); + while(j.hasNext()) + out.println(msg_out((String)j.next())); + } else { + out.println(msg_out("reloading key "+data[i])); + out.println(msg_out(server.reload(data[i]))); + } + } catch (TransformerConfigurationException tce) { + out.println(exc_out("Stylesheet Error", tce)); + } catch (Exception e) { + out.println(exc_out(null, e)); + } + } + html_close(out); return; + } + if (cmd.equals("/help")) { + html_open(response, out); + out.println("

Version "+server.VERSION+"

"); + out.println("compiled "+server.DATE+" at "+server.TIME.substring(0,2)+":"+server.TIME.substring(2)+""); + if(Server.SERIALIZATION_DIR.equals("")) { + out.println("

Stylesheet serialization is off."); + out.println("(To turn it on, choose a non-empty value for the " + + "SERIALIZATION_DIR property.)

"); + } else + out.println("

Serialized stylesheed are stored in "" + + Server.SERIALIZATION_DIR + "".

"); + out.println("

usage:

"+help+""); + html_close(out); return; + } + sendError(response, out, "unknown command", help); return; + } catch (Exception e) { + sendError(response, out, exc_out(null, e),""); + return; + } + } - private void sendError(HttpServletResponse response, int code, String msg, Exception e) throws IOException { - String err; - if (e != null) - err = "

"+e.getMessage()+"

"; - else - err = ""; - response.setContentType("text/html"); - response.sendError(code, "

Uwobo servlet

"+msg+"

"+err+""); - } - - private void sendError(HttpServletResponse response, int code, String msg, String usage) throws IOException { - response.setContentType("text/html"); - response.sendError(code, "

Uwobo servlet

"+msg+"

usage: "+usage+""); - } + private void sendError(HttpServletResponse resp, ServletOutputStream out, + String msg, String usage) + throws IOException + { + html_open(resp, out); + out.println("

"+msg+"

usage: "+usage); + html_close(out); + } - public String getServletInfo() { - return "The UWOBO servlet"; - } + public String getServletInfo() + { + return "The UWOBO servlet"; + } }