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 The HELM team
45 public class Servlet extends HttpServlet {
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>[¶m.<i>name</i>=<i>value</i>]*[¶m.<i>key</i>.<i>name</i>=<i>value</i>]*[&prop.<i>name</i>=[<i>value</i>]]*"
56 public static final String help;
59 StringBuffer sb = new StringBuffer();
61 for (int i=0; i<usage.length; i++)
62 sb.append("<li>").append(usage[i]).append("</li>");
67 private Server server;
69 public void init(ServletConfig config)
70 throws ServletException
74 System.out.println("UWOBO init");
75 server = new Server();
78 private static String[] split(final String s, final String delim)
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();
88 private static String[] split2(final String s, final String delim)
90 String[] res = new String[2];
91 StringTokenizer st = new StringTokenizer(s);
92 res[0] = st.nextToken(delim); res[1] = st.nextToken("").substring(1);
96 private void html_open(HttpServletResponse resp, ServletOutputStream out)
99 resp.setContentType("text/html");
100 out.println("<html><body bgcolor=\"#ffffff\"><h1>Uwobo servlet</h1>");
103 private void html_close(ServletOutputStream out)
106 out.println("<p>done</p></body></html>");
110 private String msg_out(String message)
112 return message+"<br>";
115 private String exc_out(String message, Exception e)
119 message = e.getClass().getName();
120 message = message.substring(message.lastIndexOf('.')+1);
122 String local = e.getLocalizedMessage();
123 local = local.substring(local.lastIndexOf(':')+1);
124 return "<b>"+message+": "+local+"</b><br>";
127 public void doGet(HttpServletRequest request, HttpServletResponse response)
128 throws ServletException, IOException
130 ServletOutputStream out = response.getOutputStream();
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());
138 final String cmd = request.getPathInfo();
140 sendError(response, out, "unknown command", help); return;
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;
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]+")"));
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));
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"));
241 out.println(msg_out("removing key "+data[i]));
242 server.remove(data[i]);
245 html_close(out); return;
247 if (cmd.equals("/list")) {
248 html_open(response, out);
249 out.println("<p>stylesheet list:</p><ul>");
251 Iterator i = server.list().iterator();
253 out.println("<li>"+i.next()+"</li>");
254 out.println("</ul>"); html_close(out); return;
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++) {
262 if (data[i] == null) {
263 out.println(msg_out("reloading all keys"));
266 out.println(msg_out("reloading key "+data[i]));
267 server.reload(data[i]);
269 } catch (TransformerConfigurationException tce) {
270 out.println(exc_out("stylesheet error", tce));
271 } catch (Exception e) {
272 out.println(exc_out(null, e));
275 html_close(out); return;
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++) {
283 if (data[i] == null) {
284 out.println(msg_out("updating all keys"));
287 out.println(msg_out("updating key "+data[i]));
288 server.update(data[i]);
290 } catch (TransformerConfigurationException tce) {
291 out.println(exc_out("stylesheet error", tce));
292 } catch (Exception e) {
293 out.println(exc_out(null, e));
296 html_close(out); return;
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;
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
312 private void sendError(HttpServletResponse resp, ServletOutputStream out,
313 String msg, String usage)
316 html_open(resp, out);
317 out.println("<p>"+msg+"</p>usage: "+usage);
321 public String getServletInfo()
323 return "The UWOBO servlet";