]> matita.cs.unibo.it Git - helm.git/blob - helm/uwobo/src/it/unibo/cs/helm/uwobo/Servlet.java
1) New syntax: methods add, reload and remove can now process multiple
[helm.git] / helm / uwobo / src / it / unibo / cs / helm / uwobo / Servlet.java
1 package it.unibo.cs.helm.uwobo;
2
3 import java.io.*;
4 import java.util.*;
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7 import javax.xml.transform.*;
8 import org.xml.sax.*;
9
10 // WARNING: next comment is obsolete!!!!!
11 /*
12 *
13 * usage:
14 *   http://hostname/helm/servlet/uwobo/help
15 *   http://hostname/helm/servlet/uwobo/add?bind=key,stylesheet[&bind=key,stylesheet]*
16 *   http://hostname/helm/servlet/uwobo/remove[?keys=key_1,...,key_n]
17 *   http://hostname/helm/servlet/uwobo/list
18 *   http://hostname/helm/servlet/uwobo/reload[?keys=key_1,...,key_n]
19 *   http://hostname/helm/servlet/uwobo/apply?xmluri=xmldata&keys=key_1,...,key_n[&param.name=value]*[&param.key.name=value]*[&prop.name=[value]]*
20 *
21 * example:
22 *   http://aristotele/helm/servlet/uwobo/add?bind=foo1,file:///D:/Archivio/Progetti/helm/resources/xsl/foo1.xsl
23 *   http://aristotele/helm/servlet/uwobo/add?bind=foo2,file:///D:/Archivio/Progetti/helm/resources/xsl/foo2.xsl
24 *   http://aristotele/helm/servlet/uwobo/apply?xmluri=file:///D:/Archivio/Progetti/helm/resources/xsl/foo.xml&keys=foo1,foo2
25 *
26 * installation notes (Tomcat):
27 *       replace parser.jar and jaxp.jar from /lib with xerces.jar and add xalan.jar
28 *
29 *       add in conf/server.xml
30 *               <Context path="/helm" 
31 *                       docBase="webapps/helm" 
32 *                       crossContext="false"
33 *                       debug="0" 
34 *                       reloadable="true" > 
35 *               </Context>
36 *
37 *       add in uriworkermap.properties
38 *               /helm/*=ajp12
39 *
40 *
41 * @author The HELM team
42 */
43 public class Servlet extends HttpServlet {
44    
45    public static final String[] usage = {
46       "http://<i>hostname</i>/helm/servlet/uwobo/help",
47       "http://<i>hostname</i>/helm/servlet/uwobo/add?bind=<i>key</i>,<i>stylesheet</i>[&bind=<i>key</i>,<i>stylesheet</i>]*",
48       "http://<i>hostname</i>/helm/servlet/uwobo/remove[?keys=<i>key_1,...,key_n</i>]",
49       "http://<i>hostname</i>/helm/servlet/uwobo/list",
50       "http://<i>hostname</i>/helm/servlet/uwobo/reload[?keys=<i>key_1,...,key_n</i>]",
51       "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>]*[&param.<i>key</i>.<i>name</i>=<i>value</i>]*[&prop.<i>name</i>=[<i>value</i>]]*"
52    };
53    public static final String help;
54            
55    static {
56       StringBuffer sb = new StringBuffer();
57       sb.append("<ul>");
58       for (int i=0; i<usage.length; i++)
59          sb.append("<li>").append(usage[i]).append("</li>");
60       sb.append("</ul>");
61       help = sb.toString();
62    }
63
64    private Server server;
65
66    public void init(ServletConfig config)
67    throws ServletException
68    {
69       super.init(config);
70
71       System.out.println("UWOBO init");
72       server = new Server();
73    }
74
75    private static String[] split(final String s, final String delim)
76    {
77       String[] res = {null}; 
78       if (s == null) return res;
79       StringTokenizer st = new StringTokenizer(s, delim);
80       res = new String[st.countTokens()];
81       for (int i = 0; i < res.length; i++) res[i] = st.nextToken();
82       return res;
83    }
84         
85    private static String[] split2(final String s, final String delim)
86    {
87       String[] res = new String[2];
88       StringTokenizer st = new StringTokenizer(s);
89       res[0] = st.nextToken(delim); res[1] = st.nextToken("").substring(1);
90       return res;
91    }
92
93    private void html_open(HttpServletResponse resp, ServletOutputStream out)
94    throws IOException
95    {  
96       resp.setContentType("text/html");
97       out.println("<html><body bgcolor=\"#ffffff\"><h1>" + server.PACKAGE + " servlet</h1>");
98    }
99    
100    private void html_close(ServletOutputStream out)
101    throws IOException
102    {
103       out.println("<p>done</p></body></html>");
104       out.close();
105    }
106
107    private String msg_out(String message)
108    {
109       return message+"<br />";
110    }
111    
112    private String exc_out(String message, Exception e)
113    {
114       if (message == null)
115       {
116          message = e.getClass().getName();
117          message = message.substring(message.lastIndexOf('.')+1);
118       }
119       String local = e.getLocalizedMessage();
120       local = local.substring(local.lastIndexOf(':')+1);
121       return "<b>"+message+": "+local+"</b><br />";
122    }
123    
124    public void doGet(HttpServletRequest request, HttpServletResponse response)
125    throws ServletException, IOException
126    {
127       ServletOutputStream out = response.getOutputStream();
128       String log = "";
129       
130       response.setHeader("Cache-Control", "no-cache");
131       response.setHeader("Pragma", "no-cache");
132       response.setHeader("Expires", "0");
133       System.out.println("UWOBO "+request.getPathInfo());
134                 
135       try { 
136          final String cmd = request.getPathInfo();
137          if (cmd == null) { 
138             sendError(response, out, "unknown command", help); return;
139          } 
140          if (cmd.equals("/add")) {
141             final String[] xslkey = request.getParameterValues("bind");
142             if (xslkey == null) {
143                sendError(response, out, "bad parameters", usage[1]); return;
144             } 
145             html_open(response, out);
146             for (int i = 0; i < xslkey.length; i++) {
147                final String data[] = split2(xslkey[i], ",");
148                out.println(msg_out("adding stylesheet "+data[0]+" ("+data[1]+")"));
149                try {
150                   log = server.add(data[1], data[0]);
151                   if(!log.equals(""))
152                      out.println(msg_out(log));
153                 } catch (TransformerConfigurationException tce) {
154                   out.println(exc_out("stylesheet error", tce));
155                } catch (Exception e) {
156                   out.println(exc_out(null, e));
157                }
158             }
159             html_close(out); return;
160          }
161          if (cmd.equals("/apply")) {
162             final String infile = request.getParameter("xmluri");
163             final String keys = request.getParameter("keys");
164
165             if (infile == null || keys == null) {
166                sendError(response, out, "bad parameters", usage[6]); return;
167             } 
168
169             final String[] keyName = split(keys, ",");
170             final Key[] keySeq = new Key[keyName.length];
171             for (int i = 0; i < keySeq.length; i++) {
172                keySeq[i] = new Key();
173                keySeq[i].name = keyName[i];
174                keySeq[i].params = new HashMap();
175             }
176
177             final Properties props = new Properties();
178             final Enumeration e = request.getParameterNames();
179             while (e.hasMoreElements()) {
180                String param = (String) e.nextElement();
181                if (param.startsWith("param.")) {
182                   final String name = param.substring(6);
183                   final String value = request.getParameter(param);
184                   final String[] keyParam = split(name, ".");
185                   if (keyParam.length == 1) {
186                      // this is a global parameter
187                      Server.log("global parameter: " + keyParam[0] + " = " + value);
188                      for (int i = 0; i < keySeq.length; i++)
189                         // we add the global parameter only if there is no
190                         // local parameter with the same name
191                         if (!keySeq[i].params.containsKey(keyParam[0]))
192                            keySeq[i].params.put(keyParam[0], value);
193                   } else
194                   if (keyParam.length == 2) {
195                      // this is a local parameter
196                      Server.log("local parameter: " + keyParam[0] + "." + keyParam[1] + " = " + value);
197                      for (int i = 0; i < keySeq.length; i++) {
198                         if (keySeq[i].name.equals(keyParam[0]))
199                            keySeq[i].params.put(keyParam[1], value);
200                      }
201                   } else { 
202                      sendError(response, out, "bad parameters", usage[6]); return;
203                   }
204                } else
205                if (param.startsWith("prop.")) {
206                   final String name = param.substring(5);
207                   final String value = request.getParameter(param);
208                   Server.log("property: " + name + " = " + value);
209                   props.setProperty(name, value);
210                }
211             }
212                                         
213             String contentType = props.getProperty(OutputKeys.MEDIA_TYPE);
214             if (contentType == null && keySeq.length > 0) 
215                contentType = server.getContentType(keySeq[keySeq.length - 1].name);
216             else if (contentType == null)
217                contentType = "text/xml";
218             response.setContentType(contentType);
219             Server.log("content type: " + contentType);
220
221             try {
222                out = response.getOutputStream(); 
223                server.apply(infile, out, keySeq, props);
224                out.close();
225             } catch (TransformerConfigurationException tce) {
226                sendError(response, out, exc_out("stylesheet error", tce), "");
227             } catch (Exception ee) {
228                sendError(response, out, exc_out(null, ee), "");
229             }
230             return;
231          }
232          if (cmd.equals("/remove")) {
233             final String key = request.getParameter("keys");
234             final String [] data = split(key, ",");
235             html_open(response, out);
236             for (int i = 0; i < data.length; i++) { 
237                if (data[i] == null) {
238                   out.println(msg_out("removing all keys"));
239                   ArrayList res;
240                   res = server.removeAll();
241                   Iterator j = res.iterator();
242                   while (j.hasNext())
243                      out.println(msg_out((String)j.next()));
244                } else {
245                   out.println(msg_out("removing key "+data[i]));
246                   out.println(msg_out(server.remove(data[i])));
247                }
248             }
249             html_close(out); return;
250          }
251          if (cmd.equals("/list")) {
252             html_open(response, out);
253             out.println("<p>stylesheet list:</p><ul>");
254             
255             Iterator i = server.list().iterator();
256             while (i.hasNext())
257                out.println("<li>"+i.next()+"</li>");
258             out.println("</ul>"); html_close(out); return;
259          }
260          if (cmd.equals("/reload")) {
261             final String key = request.getParameter("keys");
262             final String [] data = split(key, ",");
263             html_open(response, out);
264             for (int i = 0; i < data.length; i++) {
265                try {
266                   if (data[i] == null) {
267                      out.println(msg_out("reloading all keys"));
268                      ArrayList res;
269                      res = server.reloadAll();
270                      Iterator j = res.iterator();
271                      while(j.hasNext())
272                         out.println(msg_out((String)j.next()));
273                   } else {
274                      out.println(msg_out("reloading key "+data[i]));
275                      out.println(msg_out(server.reload(data[i])));
276                   }
277                } catch (TransformerConfigurationException tce) {
278                   out.println(exc_out("stylesheet error", tce));
279                } catch (Exception e) {
280                   out.println(exc_out(null, e));
281                }
282             }
283             html_close(out); return;
284          }
285          if (cmd.equals("/help")) {
286             html_open(response, out);
287             out.println("<h2>Version "+server.VERSION+"</h1>");
288             out.println("<b>compiled "+server.DATE+" at "+server.TIME.substring(0,2)+":"+server.TIME.substring(2)+"</b>");
289             if(Server.SERIALIZATION_DIR.equals("")) {
290                out.println("<p><b>Stylesheet serialization is off.</b>");
291                out.println("(To turn it on, choose a non-empty value for the " +
292                   "SERIALIZATION_DIR property.)</p>");
293             } else
294                out.println("<p><b>Serialized stylesheed are stored in &quot;" +
295                   Server.SERIALIZATION_DIR + "&quot;.</b></p>");
296             out.println("<p>usage:</p>"+help+"</body></html>");
297             html_close(out); return;
298          }
299          sendError(response, out, "unknown command", help); return;
300       } catch (Exception e) {
301          sendError(response, out, exc_out(null, e),"");
302          return;
303       }
304    }
305
306    private void sendError(HttpServletResponse resp, ServletOutputStream out, 
307                           String msg, String usage)
308    throws IOException 
309    {
310       html_open(resp, out);
311       out.println("<p>"+msg+"</p>usage: "+usage);
312       html_close(out);
313    }
314         
315    public String getServletInfo()
316    {
317       return "The UWOBO servlet";
318    }
319 }