1 39 package groovy.servlet; 40 41 import groovy.lang.MetaClass; 42 import groovy.util.ResourceConnector; 43 import groovy.util.ResourceException; 44 45 import java.io.File ; 46 import java.io.IOException ; 47 import java.net.URL ; 48 import java.net.URLConnection ; 49 50 import javax.servlet.ServletConfig ; 51 import javax.servlet.ServletContext ; 52 import javax.servlet.ServletException ; 53 import javax.servlet.http.HttpServlet ; 54 import javax.servlet.http.HttpServletRequest ; 55 56 61 public abstract class AbstractHttpServlet extends HttpServlet implements 62 ResourceConnector { 63 64 67 public static final String CONTENT_TYPE_TEXT_HTML = "text/html"; 68 69 72 public static final String INC_PATH_INFO = "javax.servlet.include.path_info"; 73 74 77 79 82 public static final String INC_SERVLET_PATH = "javax.servlet.include.servlet_path"; 83 84 87 private boolean logRequestClassAndLoaderOnce; 88 89 92 protected ServletContext servletContext; 93 94 98 public AbstractHttpServlet() { 99 this.logRequestClassAndLoaderOnce = true; 100 this.servletContext = null; 101 } 102 103 106 public URLConnection getResourceConnection(String name) 107 throws ResourceException { 108 try { 109 URL url = servletContext.getResource("/" + name); 110 if (url == null) { 111 url = servletContext.getResource("/WEB-INF/groovy/" + name); 112 if (url == null) { 113 throw new ResourceException("Resource " + name + " not found"); 114 } 115 } 116 return url.openConnection(); 117 } 118 catch (IOException ioe) { 119 throw new ResourceException("Problem reading resource " + name); 120 } 121 } 122 123 131 protected String getScriptUri(HttpServletRequest request) { 132 if (logRequestClassAndLoaderOnce) { 134 log("Logging request class and its class loader:"); 135 log(" c = request.getClass() :\"" + request.getClass()+ "\""); 136 log(" l = c.getClassLoader() :\"" + request.getClass().getClassLoader()+ "\""); 137 log(" l.getClass() :\"" + request.getClass().getClassLoader().getClass()+ "\""); 138 logRequestClassAndLoaderOnce = false; 139 } 140 141 150 String uri = null; 151 String info = null; 152 153 uri = (String ) request.getAttribute(INC_SERVLET_PATH); 158 if (uri != null) { 159 info = (String ) request.getAttribute(INC_PATH_INFO); 165 if (info != null) { 166 uri += info; 167 } 168 return uri; 169 } 170 171 uri = request.getServletPath(); 177 info = request.getPathInfo(); 178 if (info != null) { 179 uri += info; 180 } 181 182 return uri; 183 } 184 185 194 protected File getScriptUriAsFile(HttpServletRequest request) { 195 String uri = getScriptUri(request); 196 String real = servletContext.getRealPath(uri); 197 File file = new File (real).getAbsoluteFile(); 198 199 209 216 return file; 217 } 218 219 232 public void init(ServletConfig config) throws ServletException { 233 super.init(config); 234 this.servletContext = config.getServletContext(); 235 236 MetaClass.setUseReflection(true); 238 String value = config.getInitParameter("logRequestClassAndLoaderOnce"); 239 if (value != null) { 240 this.logRequestClassAndLoaderOnce = Boolean.valueOf(value).booleanValue(); 241 } 242 243 } 244 245 } 246 | Popular Tags |