1 16 17 package org.apache.commons.jelly.servlet; 18 19 import java.io.IOException ; 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 import java.io.UnsupportedEncodingException ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 26 import javax.servlet.ServletException ; 27 import javax.servlet.ServletOutputStream ; 28 import javax.servlet.http.HttpServlet ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.apache.commons.jelly.JellyContext; 33 import org.apache.commons.jelly.JellyException; 34 import org.apache.commons.jelly.XMLOutput; 35 36 42 public class JellyServlet extends HttpServlet { 43 46 public static final String REQUEST = "request"; 47 48 51 public static final String RESPONSE = "response"; 52 53 protected void doGet( 54 HttpServletRequest request, 55 HttpServletResponse response) 56 throws ServletException , IOException { 57 58 doRequest(request, response); 59 } 60 61 protected void doPost( 62 HttpServletRequest request, 63 HttpServletResponse response) 64 throws ServletException , IOException { 65 66 doRequest(request, response); 67 } 68 69 76 protected void doRequest(HttpServletRequest req, HttpServletResponse res) 77 throws ServletException , IOException { 78 79 JellyContext context = createContext(req, res); 80 try { 81 URL script = getScript(req); 82 runScript(script, context, req, res); 83 } 84 catch (Exception e) { 85 error(req, res, e); 86 } 87 } 88 89 95 protected JellyContext createContext( 96 HttpServletRequest req, 97 HttpServletResponse res) { 98 99 JellyContext ctx = new JellyServletContext(getServletContext()); 100 ctx.setVariable(REQUEST, req); 101 ctx.setVariable(RESPONSE, res); 102 return ctx; 103 } 104 105 119 protected URL getScript(HttpServletRequest req) 120 throws MalformedURLException { 121 122 String scriptUrl = req.getParameter("script"); 123 if (scriptUrl == null) { 124 scriptUrl = req.getPathInfo(); 125 } 126 if (scriptUrl == null) { 127 scriptUrl = req.getServletPath(); 128 } 129 URL url = getServletContext().getResource(scriptUrl); 130 if (url == null) { 131 throw new IllegalArgumentException ("Invalid script url:" + scriptUrl); 132 } 133 return url; 134 } 135 136 146 protected void runScript( 147 URL script, 148 JellyContext context, 149 HttpServletRequest req, 150 HttpServletResponse res) 151 throws IOException , UnsupportedEncodingException , JellyException { 152 153 ServletOutputStream output = res.getOutputStream(); 154 XMLOutput xmlOutput = XMLOutput.createXMLOutput(output); 155 context.runScript(script, xmlOutput); 156 xmlOutput.flush(); 157 xmlOutput.close(); 158 output.flush(); 159 } 160 161 172 protected void error( 173 HttpServletRequest request, 174 HttpServletResponse response, 175 Exception cause) 176 throws ServletException , IOException { 177 178 StringBuffer html = new StringBuffer (); 179 html.append("<html>"); 180 html.append("<title>Error</title>"); 181 html.append("<body bgcolor=\"#ffffff\">"); 182 html.append("<h2>JellyServlet : Error processing the script</h2>"); 183 html.append("<pre>"); 184 String why = cause.getMessage(); 185 if (why != null && why.trim().length() > 0) { 186 html.append(why); 187 html.append("<br>"); 188 } 189 190 StringWriter sw = new StringWriter (); 191 cause.printStackTrace(new PrintWriter (sw)); 192 193 html.append(sw.toString()); 194 html.append("</pre>"); 195 html.append("</body>"); 196 html.append("</html>"); 197 response.getOutputStream().print(html.toString()); 198 } 199 } 200 | Popular Tags |