]> matita.cs.unibo.it Git - helm.git/blob - helm/uwobo/src/it/unibo/cs/helm/uwobo/Servlet.java
Commit of Ferruccio changes:
[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 /*
11 *
12 * usage:
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=[&param.=]*
20 *
21 * example:
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
25 *
26 * installation notes (Tomcat):
27 *       replace parser.jar and jaxp.jar from /lib with xerces.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 * bugs:
41 *       directory base stylesheet inclusi
42 *
43 * @author The HELM team
44 */
45 public class Servlet extends HttpServlet {
46    
47    public static final String[] usage = {
48       "http://<i>hostname</i>/helm/servlet/uwobo/help",
49       "http://<i>hostname</i>/helm/servlet/uwobo/add?key=<i>key</i>,<i>stylesheet</i>[&key=<i>key</i>,<i>stylesheet</i>]*",
50       "http://<i>hostname</i>/helm/servlet/uwobo/remove[?keys=<i>key_1,...,key_n</i>]",
51       "http://<i>hostname</i>/helm/servlet/uwobo/list",
52       "http://<i>hostname</i>/helm/servlet/uwobo/reload[?keys=<i>key_1,...,key_n</i>]",
53       "http://<i>hostname</i>/helm/servlet/uwobo/update[?keys=<i>key_1,...,key_n</i>]",
54       "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>]]*"
55    };
56    public static final String help;
57            
58    static {
59       StringBuffer sb = new StringBuffer();
60       sb.append("<ul>");
61       for (int i=0; i<usage.length; i++)
62          sb.append("<li>").append(usage[i]).append("</li>");
63       sb.append("</ul>");
64       help = sb.toString();
65    }
66
67    private Server server;
68
69    public void init(ServletConfig config)
70    throws ServletException
71    {
72       super.init(config);
73
74       System.out.println("UWOBO init");
75       server = new Server();
76    }
77
78    private static String[] split(final String s, final String delim)
79    {
80       String[] res = {null}; 
81       if (s == null) return res;
82       StringTokenizer st = new StringTokenizer(s, delim);
83       res = new String[st.countTokens()];
84       for (int i = 0; i < res.length; i++) res[i] = st.nextToken();
85       return res;
86    }
87         
88    private static String[] split2(final String s, final String delim)
89    {
90       String[] res = new String[2];
91       StringTokenizer st = new StringTokenizer(s);
92       res[0] = st.nextToken(delim); res[1] = st.nextToken("").substring(1);
93       return res;
94    }
95
96    private void html_open(HttpServletResponse resp, ServletOutputStream out)
97    throws IOException
98    {  
99       resp.setContentType("text/html");
100       out.println("<html><body bgcolor=\"#ffffff\"><h1>Uwobo servlet</h1>");
101    }
102    
103    private void html_close(ServletOutputStream out)
104    throws IOException
105    {
106       out.println("<p>done</p></body></html>");
107       out.close();
108    }
109
110    private String msg_out(String message)
111    {
112       return message+"<br>";
113    }
114    
115    private String exc_out(String message, Exception e)
116    {
117       if (message == null)
118       {
119          message = e.getClass().getName();
120          message = message.substring(message.lastIndexOf('.')+1);
121       }
122       String local = e.getLocalizedMessage();
123       local = local.substring(local.lastIndexOf(':')+1);
124       return "<b>"+message+": "+local+"</b><br>";
125    }
126    
127    public void doGet(HttpServletRequest request, HttpServletResponse response)
128    throws ServletException, IOException
129    {
130       ServletOutputStream out = response.getOutputStream();
131       
132       response.setHeader("Cache-Control", "no-cache");
133       response.setHeader("Pragma", "no-cache");
134       response.setHeader("Expires", "0");
135       System.out.println("UWOBO "+request.getPathInfo());
136                 
137       try { 
138          final String cmd = request.getPathInfo();
139          if (cmd == null) { 
140             sendError(response, out, "unknown command", help); return;
141          } 
142          if (cmd.equals("/add")) {
143             final String[] xslkey = request.getParameterValues("key");
144             if (xslkey == null) {
145                sendError(response, out, "bad parameters", usage[1]); return;
146             } 
147             html_open(response, out);
148             for (int i = 0; i < xslkey.length; i++) {
149                final String data[] = split2(xslkey[i], ",");
150                out.println(msg_out("adding key "+data[0]+" ("+data[1]+")"));
151                try {
152                   server.add(data[1], data[0]);
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                   server.removeAll();
240                } else {
241                   out.println(msg_out("removing key "+data[i]));
242                   server.remove(data[i]);
243                }
244             }
245             html_close(out); return;
246          }
247          if (cmd.equals("/list")) {
248             html_open(response, out);
249             out.println("<p>stylesheet list:</p><ul>");
250             
251             Iterator i = server.list().iterator();
252             while (i.hasNext())
253                out.println("<li>"+i.next()+"</li>");
254             out.println("</ul>"); html_close(out); return;
255          }
256          if (cmd.equals("/reload")) {
257             final String key = request.getParameter("keys");
258             final String [] data = split(key, ",");
259             html_open(response, out);
260             for (int i = 0; i < data.length; i++) {
261                try {
262                   if (data[i] == null) {
263                      out.println(msg_out("reloading all keys"));
264                      server.reloadAll();
265                   } else {
266                      out.println(msg_out("reloading key "+data[i]));
267                      server.reload(data[i]);
268                   }
269                } catch (TransformerConfigurationException tce) {
270                   out.println(exc_out("stylesheet error", tce));
271                } catch (Exception e) {
272                   out.println(exc_out(null, e));
273                }
274             }
275             html_close(out); return;
276          }
277          if (cmd.equals("/update")) {
278             final String key = request.getParameter("keys");
279             final String [] data = split(key, ",");
280             html_open(response, out);
281             for (int i = 0; i < data.length; i++) {
282                try {
283                   if (data[i] == null) {
284                      out.println(msg_out("updating all keys")); 
285                      server.updateAll();
286                   } else {
287                      out.println(msg_out("updating key "+data[i]));
288                      server.update(data[i]);
289                   }
290                } catch (TransformerConfigurationException tce) {
291                   out.println(exc_out("stylesheet error", tce));
292                } catch (Exception e) {
293                   out.println(exc_out(null, e));
294                }
295             }
296             html_close(out); return;
297          }
298          if (cmd.equals("/help")) {
299             html_open(response, out);
300             out.println("<h2>"+server.PACKAGE+" servlet - version "+server.VERSION+"</h1>");
301             out.println("<b>compiled "+server.DATE+" at "+server.TIME.substring(0,2)+":"+server.TIME.substring(2)+"</b>");
302             out.println("<p>usage:</p>"+help+"</body></html>");
303             html_close(out); return;
304          }
305          sendError(response, out, "unknown command", help); return;
306       } catch (Exception e) {
307          sendError(response, out, exc_out(null, e),""); // FG: non dovrebbe servire mai
308          return;
309       }
310    }
311
312    private void sendError(HttpServletResponse resp, ServletOutputStream out, 
313                           String msg, String usage)
314    throws IOException 
315    {
316       html_open(resp, out);
317       out.println("<p>"+msg+"</p>usage: "+usage);
318       html_close(out);
319    }
320         
321    public String getServletInfo()
322    {
323       return "The UWOBO servlet";
324    }
325 }