1 package it.unibo.cs.helm.uwobo;
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7 import javax.xml.transform.*;
10 // WARNING: next comment is obsolete!!!!!
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[¶m.name=value]*[¶m.key.name=value]*[&prop.name=[value]]*
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
26 * installation notes (Tomcat):
27 * replace parser.jar and jaxp.jar from /lib with xerces.jar and add xalan.jar
29 * add in conf/server.xml
30 * <Context path="/helm"
31 * docBase="webapps/helm"
32 * crossContext="false"
37 * add in uriworkermap.properties
41 * @author The HELM team
43 public class Servlet extends HttpServlet {
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>[¶m.<i>name</i>=<i>value</i>]*[¶m.<i>key</i>.<i>name</i>=<i>value</i>]*[&prop.<i>name</i>=[<i>value</i>]]*"
53 public static final String help;
56 StringBuffer sb = new StringBuffer();
58 for (int i=0; i<usage.length; i++)
59 sb.append("<li>").append(usage[i]).append("</li>");
64 private Server server;
66 public void init(ServletConfig config)
67 throws ServletException
71 System.out.println("UWOBO init");
72 server = new Server();
75 private static String[] split(final String s, final String delim)
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();
85 private static String[] split2(final String s, final String delim)
87 String[] res = new String[2];
88 StringTokenizer st = new StringTokenizer(s);
89 res[0] = st.nextToken(delim); res[1] = st.nextToken("").substring(1);
93 private void html_open(HttpServletResponse resp, ServletOutputStream out)
96 resp.setContentType("text/html");
97 out.println("<html><body bgcolor=\"#ffffff\"><h1>" + server.PACKAGE + " servlet</h1>");
100 private void html_close(ServletOutputStream out)
103 out.println("<p>done</p></body></html>");
107 private String msg_out(String message)
109 return message+"<br />";
112 private String exc_out(String message, Exception e)
116 message = e.getClass().getName();
117 message = message.substring(message.lastIndexOf('.')+1);
119 String local = e.getLocalizedMessage();
120 local = local.substring(local.lastIndexOf(':')+1);
121 return "<b>"+message+": "+local+"</b><br />";
124 public void doGet(HttpServletRequest request, HttpServletResponse response)
125 throws ServletException, IOException
127 ServletOutputStream out = response.getOutputStream();
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());
136 final String cmd = request.getPathInfo();
138 sendError(response, out, "unknown command", help); return;
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;
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]+")"));
150 log = server.add(data[1], data[0]);
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));
159 html_close(out); return;
161 if (cmd.equals("/apply")) {
162 final String infile = request.getParameter("xmluri");
163 final String keys = request.getParameter("keys");
165 if (infile == null || keys == null) {
166 sendError(response, out, "bad parameters", usage[6]); return;
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();
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);
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);
202 sendError(response, out, "bad parameters", usage[6]); return;
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);
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);
222 out = response.getOutputStream();
223 server.apply(infile, out, keySeq, props);
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), "");
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"));
240 res = server.removeAll();
241 Iterator j = res.iterator();
243 out.println(msg_out((String)j.next()));
245 out.println(msg_out("removing key "+data[i]));
246 out.println(msg_out(server.remove(data[i])));
249 html_close(out); return;
251 if (cmd.equals("/list")) {
252 html_open(response, out);
253 out.println("<p>stylesheet list:</p><ul>");
255 Iterator i = server.list().iterator();
257 out.println("<li>"+i.next()+"</li>");
258 out.println("</ul>"); html_close(out); return;
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++) {
266 if (data[i] == null) {
267 out.println(msg_out("reloading all keys"));
269 res = server.reloadAll();
270 Iterator j = res.iterator();
272 out.println(msg_out((String)j.next()));
274 out.println(msg_out("reloading key "+data[i]));
275 out.println(msg_out(server.reload(data[i])));
277 } catch (TransformerConfigurationException tce) {
278 out.println(exc_out("stylesheet error", tce));
279 } catch (Exception e) {
280 out.println(exc_out(null, e));
283 html_close(out); return;
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>");
294 out.println("<p><b>Serialized stylesheed are stored in "" +
295 Server.SERIALIZATION_DIR + "".</b></p>");
296 out.println("<p>usage:</p>"+help+"</body></html>");
297 html_close(out); return;
299 sendError(response, out, "unknown command", help); return;
300 } catch (Exception e) {
301 sendError(response, out, exc_out(null, e),"");
306 private void sendError(HttpServletResponse resp, ServletOutputStream out,
307 String msg, String usage)
310 html_open(resp, out);
311 out.println("<p>"+msg+"</p>usage: "+usage);
315 public String getServletInfo()
317 return "The UWOBO servlet";