]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/uwobo/src/it/unibo/cs/helm/uwobo/Servlet.java
This commit was manufactured by cvs2svn to create branch 'start'.
[helm.git] / helm / uwobo / src / it / unibo / cs / helm / uwobo / Servlet.java
diff --git a/helm/uwobo/src/it/unibo/cs/helm/uwobo/Servlet.java b/helm/uwobo/src/it/unibo/cs/helm/uwobo/Servlet.java
new file mode 100644 (file)
index 0000000..0e28877
--- /dev/null
@@ -0,0 +1,247 @@
+package it.unibo.cs.helm.uwobo;
+
+import java.io.*;
+import java.util.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+import javax.xml.transform.*;
+import org.xml.sax.*;
+
+/*
+*
+* 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=[&param.=]*
+*
+* 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
+*
+* installation notes (Tomcat):
+*      replace parser.jar and jaxp.jar from /lib with xerces.jar
+*
+*      add in conf/server.xml
+*              <Context path="/helm" 
+*                      docBase="webapps/helm" 
+*                      crossContext="false"
+*                      debug="0" 
+*                      reloadable="true" > 
+*              </Context>
+*
+*      add in uriworkermap.properties
+*              /helm/*=ajp12
+*
+* bugs:
+*      directory base stylesheet inclusi
+*
+* @author Riccardo Solmi
+*/
+public class Servlet extends HttpServlet {
+       public static final String[] usage = {
+               "http://<i>hostname</i>/helm/servlet/uwobo/help",
+               "http://<i>hostname</i>/helm/servlet/uwobo/add?xsluri=<i>stylesheet</i>&key=<i>name</i>",
+               "http://<i>hostname</i>/helm/servlet/uwobo/remove[?key=<i>name</i>]",
+               "http://<i>hostname</i>/helm/servlet/uwobo/list",
+               "http://<i>hostname</i>/helm/servlet/uwobo/reload[?key=<i>name</i>]",
+               "http://<i>hostname</i>/helm/servlet/uwobo/update[?key=<i>name</i>]",
+               "http://<i>hostname</i>/helm/servlet/uwobo/apply?xmluri=<i>xmldata</i>&keys=<i>key_1,...,key_n</i>[&param.<i>name</i>=<i>value</i>]*"
+       };
+       public static final String help;
+       
+       static {
+               StringBuffer sb = new StringBuffer();
+               sb.append("<ul>");
+               for (int i=0; i<usage.length; i++)
+                       sb.append("<li>").append(usage[i]).append("</li>");
+               sb.append("</ul>");
+               help = sb.toString();
+       }
+
+       private Server server;
+
+       public void init(ServletConfig config) throws ServletException
+       {
+               super.init(config);
+
+               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;
+       }
+       
+       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");
+
+                               if (infile == null || keys == null) {
+                                       sendError(response, HttpServletResponse.SC_BAD_REQUEST, "bad parameters", usage[6]);
+                                       return;
+                               }
+
+                               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();
+                               }
+
+                               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);
+
+                               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();
+
+                               response.setContentType("text/html");
+                               out = response.getOutputStream();
+                               out.println("<html><body><h1>Uwobo servlet</h1><p>stylesheet list:</p><ul>");
+                               while (i.hasNext())
+                                       out.println("<li>"+i.next()+"</li>");
+                               out.println("</ul></body></html>");
+                               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("<html><body><h1>"+server.PACKAGE+" servlet - version "+server.VERSION+"</h1>");
+                               out.println("<b>compiled "+server.DATE+" at "+server.TIME.substring(0,2)+":"+server.TIME.substring(2)+"</b>");
+                               out.println("<p>usage:</p>"+help+"</body></html>");
+                               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;
+               }
+
+               response.setContentType("text/html");
+               response.setHeader("Cache-Control", "no-cache");
+               response.setHeader("Pragma", "no-cache");
+               response.setHeader("Expires", "0");
+               out = response.getOutputStream();
+               out.println("<html><body><h1>Uwobo servlet</h1><p>done</p></body></html>");
+               out.close();
+       }
+
+       private void sendError(HttpServletResponse response, int code, String msg, Exception e) throws IOException {
+               String err;
+               if (e != null)
+                       err = "<p>"+e.getMessage()+"</P>";
+               else
+                       err = "";
+               response.setContentType("text/html");
+               response.sendError(code, "<html><body><h1>Uwobo servlet</h1><p>"+msg+"</p>"+err+"</body></html>");
+       }
+       
+       private void sendError(HttpServletResponse response, int code, String msg, String usage) throws IOException {
+               response.setContentType("text/html");
+               response.sendError(code, "<html><body><h1>Uwobo servlet</h1><p>"+msg+"</p>usage: "+usage+"</body></html>");
+       }
+       
+       public String getServletInfo() {
+               return "The UWOBO servlet";
+       }
+}