1 package it.unibo.cs.helm.uwobo;
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7 import javax.xml.transform.*;
13 * http://aristotele/helm/servlet/uwobo/help
14 * http://aristotele/helm/servlet/uwobo/add?xsluri=&key=
15 * http://aristotele/helm/servlet/uwobo/remove[?key=]
16 * http://aristotele/helm/servlet/uwobo/list
17 * http://aristotele/helm/servlet/uwobo/reload[?key=]
18 * http://aristotele/helm/servlet/uwobo/update[?key=]
19 * http://aristotele/helm/servlet/uwobo/apply?xmluri=&keys=[¶m.=]*
22 * http://aristotele/helm/servlet/uwobo/add?xsluri=file:///D:/Archivio/Progetti/helm/resources/xsl/foo1.xsl&key=foo1
23 * http://aristotele/helm/servlet/uwobo/add?xsluri=file:///D:/Archivio/Progetti/helm/resources/xsl/foo2.xsl&key=foo2
24 * http://aristotele/helm/servlet/uwobo/apply?xmluri=file:///D:/Archivio/Progetti/helm/resources/xsl/foo.xml&key=foo1&key=foo2
26 * installation notes (Tomcat):
27 * replace parser.jar and jaxp.jar from /lib with xerces.jar
29 * add in conf/server.xml
30 * <Context path="/helm"
31 * docBase="webapps/helm"
32 * crossContext="false"
37 * add in uriworkermap.properties
41 * directory base stylesheet inclusi
43 * @author Riccardo Solmi
45 public class Servlet extends HttpServlet {
46 public static final String[] usage = {
47 "http://<i>hostname</i>/helm/servlet/uwobo/help",
48 "http://<i>hostname</i>/helm/servlet/uwobo/add?xsluri=<i>stylesheet</i>&key=<i>name</i>",
49 "http://<i>hostname</i>/helm/servlet/uwobo/remove[?key=<i>name</i>]",
50 "http://<i>hostname</i>/helm/servlet/uwobo/list",
51 "http://<i>hostname</i>/helm/servlet/uwobo/reload[?key=<i>name</i>]",
52 "http://<i>hostname</i>/helm/servlet/uwobo/update[?key=<i>name</i>]",
53 "http://<i>hostname</i>/helm/servlet/uwobo/apply?xmluri=<i>xmldata</i>&keys=<i>key_1,...,key_n</i>[¶m.<i>name</i>=<i>value</i>]*"
55 public static final String help;
58 StringBuffer sb = new StringBuffer();
60 for (int i=0; i<usage.length; i++)
61 sb.append("<li>").append(usage[i]).append("</li>");
66 private Server server;
68 public void init(ServletConfig config) throws ServletException
72 System.out.println("UWOBO init");
73 server = new Server();
76 public static String[] split(final String s, final String delim)
78 StringTokenizer st = new StringTokenizer(s, delim);
79 String[] res = new String[st.countTokens()];
80 for (int i = 0; i < res.length; i++) res[i] = st.nextToken();
84 public void doGet(HttpServletRequest request, HttpServletResponse response)
85 throws ServletException, IOException
87 System.out.println("UWOBO "+request.getPathInfo());
88 ServletOutputStream out;
91 final String cmd = request.getPathInfo();
93 sendError(response, HttpServletResponse.SC_NOT_FOUND, "unknown command", help);
96 if (cmd.equals("/add")) {
97 final String filename = request.getParameter("xsluri");
98 if (filename == null) {
99 sendError(response, HttpServletResponse.SC_BAD_REQUEST, "bad parameters", usage[1]);
102 final String key = request.getParameter("key");
103 server.add(filename, key);
104 } else if (cmd.equals("/apply")) {
105 final String infile = request.getParameter("xmluri");
106 final String keys = request.getParameter("keys");
108 if (infile == null || keys == null) {
109 sendError(response, HttpServletResponse.SC_BAD_REQUEST, "bad parameters", usage[6]);
113 final String[] keyName = split(keys, ",");
114 final Key[] keySeq = new Key[keyName.length];
115 for (int i = 0; i < keySeq.length; i++) {
116 keySeq[i] = new Key();
117 keySeq[i].name = keyName[i];
118 keySeq[i].params = new HashMap();
121 final Properties props = new Properties();
122 final Enumeration e = request.getParameterNames();
123 while (e.hasMoreElements()) {
124 String param = (String) e.nextElement();
125 if (param.startsWith("param.")) {
126 final String name = param.substring(6);
127 final String value = request.getParameter(param);
128 final String[] keyParam = split(name, ".");
129 if (keyParam.length == 1) {
130 // this is a global parameter
131 Server.log("global parameter: " + keyParam[0] + " = " + value);
132 for (int i = 0; i < keySeq.length; i++)
133 // we add the global parameter only if there is no
134 // local parameter with the same name
135 if (!keySeq[i].params.containsKey(keyParam[0]))
136 keySeq[i].params.put(keyParam[0], value);
137 } else if (keyParam.length == 2) {
138 // this is a local parameter
139 Server.log("local parameter: " + keyParam[0] + "." + keyParam[1] + " = " + value);
140 for (int i = 0; i < keySeq.length; i++) {
141 if (keySeq[i].name.equals(keyParam[0]))
142 keySeq[i].params.put(keyParam[1], value);
145 sendError(response, HttpServletResponse.SC_BAD_REQUEST, "bad parameters", usage[6]);
148 } else if (param.startsWith("prop.")) {
149 final String name = param.substring(5);
150 final String value = request.getParameter(param);
151 Server.log("property: " + name + " = " + value);
152 props.setProperty(name, value);
156 String contentType = props.getProperty(OutputKeys.MEDIA_TYPE);
157 if (contentType == null && keySeq.length > 0)
158 contentType = server.getContentType(keySeq[keySeq.length - 1].name);
159 else if (contentType == null)
160 contentType = "text/xml";
161 response.setContentType(contentType);
162 Server.log("content type: " + contentType);
164 out = response.getOutputStream();
165 server.apply(infile, out, keySeq, props);
168 } else if (cmd.equals("/remove")) {
169 final String key = request.getParameter("key");
174 } else if (cmd.equals("/list")) {
175 Iterator i = server.list().iterator();
177 response.setContentType("text/html");
178 out = response.getOutputStream();
179 out.println("<html><body><h1>Uwobo servlet</h1><p>stylesheet list:</p><ul>");
181 out.println("<li>"+i.next()+"</li>");
182 out.println("</ul></body></html>");
185 } else if (cmd.equals("/reload")) {
186 final String key = request.getParameter("key");
191 } else if (cmd.equals("/update")) {
192 final String key = request.getParameter("key");
197 } else if (cmd.equals("/help")) {
198 response.setContentType("text/html");
199 out = response.getOutputStream();
200 out.println("<html><body><h1>"+server.PACKAGE+" servlet - version "+server.VERSION+"</h1>");
201 out.println("<b>compiled "+server.DATE+" at "+server.TIME.substring(0,2)+":"+server.TIME.substring(2)+"</b>");
202 out.println("<p>usage:</p>"+help+"</body></html>");
206 sendError(response, HttpServletResponse.SC_NOT_FOUND, "unknown command", help);
209 } catch (TransformerConfigurationException tce) {
210 sendError(response, HttpServletResponse.SC_BAD_REQUEST, "stylesheet error", tce);
212 } catch (SAXException se) {
213 sendError(response, HttpServletResponse.SC_BAD_REQUEST, "SAX Exception", se);
215 } catch (Exception e) {
216 sendError(response, HttpServletResponse.SC_BAD_REQUEST, "exception", e);
220 response.setContentType("text/html");
221 response.setHeader("Cache-Control", "no-cache");
222 response.setHeader("Pragma", "no-cache");
223 response.setHeader("Expires", "0");
224 out = response.getOutputStream();
225 out.println("<html><body><h1>Uwobo servlet</h1><p>done</p></body></html>");
229 private void sendError(HttpServletResponse response, int code, String msg, Exception e) throws IOException {
232 err = "<p>"+e.getMessage()+"</P>";
235 response.setContentType("text/html");
236 response.sendError(code, "<html><body><h1>Uwobo servlet</h1><p>"+msg+"</p>"+err+"</body></html>");
239 private void sendError(HttpServletResponse response, int code, String msg, String usage) throws IOException {
240 response.setContentType("text/html");
241 response.sendError(code, "<html><body><h1>Uwobo servlet</h1><p>"+msg+"</p>usage: "+usage+"</body></html>");
244 public String getServletInfo() {
245 return "The UWOBO servlet";