1 32 package groovy.servlet; 33 34 import groovy.lang.Binding; 35 import groovy.lang.MetaClass; 36 import groovy.lang.Closure; 37 import groovy.util.GroovyScriptEngine; 38 import groovy.util.ResourceConnector; 39 import groovy.util.ResourceException; 40 import groovy.util.ScriptException; 41 42 import java.io.IOException ; 43 import java.net.URL ; 44 import java.net.URLConnection ; 45 import java.util.Collections ; 46 import java.util.Enumeration ; 47 import java.util.HashMap ; 48 import java.util.Map ; 49 50 import javax.servlet.ServletConfig ; 51 import javax.servlet.ServletContext ; 52 import javax.servlet.ServletException ; 53 import javax.servlet.ServletRequest ; 54 import javax.servlet.ServletResponse ; 55 import javax.servlet.http.HttpServlet ; 56 import javax.servlet.http.HttpServletRequest ; 57 import javax.servlet.http.HttpServletResponse ; 58 59 import org.codehaus.groovy.runtime.GroovyCategorySupport; 60 61 108 public class GroovyServlet extends HttpServlet implements ResourceConnector { 109 110 112 115 public static final String GROOVY_EXTENSION = ".groovy"; 116 117 120 private ServletContext sc; 121 122 125 private static ClassLoader parent; 126 127 130 private static GroovyScriptEngine gse; 131 132 134 137 public ServletContext getServletContext() { 138 return sc; 139 } 140 141 144 public void init(ServletConfig config) { 145 146 MetaClass.setUseReflection(true); 148 149 sc = config.getServletContext(); 151 sc.log("Groovy servlet initialized"); 152 153 parent = Thread.currentThread().getContextClassLoader(); 156 if (parent == null) 157 parent = GroovyServlet.class.getClassLoader(); 158 159 gse = new GroovyScriptEngine(this); 161 } 162 163 166 public URLConnection getResourceConnection(String name) throws ResourceException { 167 try { 168 URL url = sc.getResource("/" + name); 169 if (url == null) { 170 url = sc.getResource("/WEB-INF/groovy/" + name); 171 if (url == null) { 172 throw new ResourceException("Resource " + name + " not found"); 173 } 174 } 175 return url.openConnection(); 176 } catch (IOException ioe) { 177 throw new ResourceException("Problem reading resource " + name); 178 } 179 } 180 181 184 public void service(ServletRequest request, ServletResponse response) 185 throws ServletException , IOException { 186 187 final HttpServletRequest httpRequest = (HttpServletRequest ) request; 189 final HttpServletResponse httpResponse = (HttpServletResponse ) response; 190 191 final String scriptFilename = getGroovyScriptPath(httpRequest); 193 194 response.setContentType("text/html"); 196 197 final Binding binding = new ServletBinding((HttpServletRequest ) request, response, sc); 199 200 try { 202 Closure closure = new Closure(gse) { 203 public Object call() { 204 try { 205 return ((GroovyScriptEngine)getDelegate()).run(scriptFilename, binding); 206 } catch (ResourceException e) { 207 throw new RuntimeException (e); 208 } catch (ScriptException e) { 209 throw new RuntimeException (e); 210 } 211 } 212 }; 213 GroovyCategorySupport.use(ServletCategory.class, closure); 214 } catch (RuntimeException re) { 215 216 StringBuffer error = new StringBuffer ("GroovyServlet Error: "); 217 error.append(" script: '"); 218 error.append(scriptFilename); 219 error.append("': "); 220 221 Throwable e = re.getCause(); 222 if (e instanceof ResourceException) { 223 error.append(" Script not found, sending 404."); 224 sc.log(error.toString()); 225 System.out.println(error.toString()); 226 httpResponse.sendError(HttpServletResponse.SC_NOT_FOUND); 227 } else { 228 229 if (re.getMessage() != null) 231 error.append(re.getMessage()); 232 233 if (e != null) { 234 sc.log("An error occurred processing the request", e); 235 } else { 236 sc.log("An error occurred processing the request", re); 237 } 238 sc.log(error.toString()); 239 System.out.println(error.toString()); 240 241 httpResponse.sendError( 242 HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 243 } 244 } 245 } 246 247 249 253 private String getGroovyScriptPath(HttpServletRequest request) { 254 255 int contextLength = request.getContextPath().length(); 257 String scriptFilename = request.getRequestURI() 258 .substring(contextLength).substring(1); 259 260 if (scriptFilename.endsWith(GROOVY_EXTENSION)) 263 return scriptFilename; 264 265 int lastDot = scriptFilename.lastIndexOf("."); 267 scriptFilename = scriptFilename.substring(0, lastDot) 268 + GROOVY_EXTENSION; 269 return scriptFilename; 270 271 } 272 } 273 | Popular Tags |