String name; File file = super.getScriptUriAsFile(request); if (file != null) { name = file.getName(); getMillis = System.currentTimeMillis() - getMillis; } else { name = super.getScriptUri(request); URL url = servletContext.getResource(name); getMillis = System.currentTimeMillis();
/** * Initialize the GroovyServlet. * * @throws ServletException * if this method encountered difficulties */ public void init(ServletConfig config) throws ServletException { super.init(config); // Set up the scripting engine gse = createGroovyScriptEngine(); servletContext.log("Groovy servlet initialized on " + gse + "."); }
protected String applyResourceNameMatcher (String uri) { if (resourceNamePattern != null) {// mangle resource name with the compiled pattern. Matcher matcher = resourceNamePattern.matcher(uri); String replaced; if (resourceNameReplaceAll) { replaced = matcher.replaceAll(resourceNameReplacement); } else { replaced = matcher.replaceFirst(resourceNameReplacement); } if (!uri.equals(replaced)) { if (verbose) { log("Replaced resource name \"" + uri + "\" with \"" + replaced + "\"."); } return replaced; } } return uri; }
log("Logging request class and its class loader:"); log(" c = request.getClass() :\"" + request.getClass() + "\""); log(" l = c.getClassLoader() :\"" + request.getClass().getClassLoader() + "\""); log(" l.getClass() :\"" + request.getClass().getClassLoader().getClass() + "\""); uri += info; return applyResourceNameMatcher(uri); return applyResourceNameMatcher(uri);
/** * Parses the http request for the real script or template source file. * * @param request * the http request to analyze * @return a file object using an absolute file path name, or <code>null</code> if the * servlet container cannot translate the virtual path to a real * path for any reason (such as when the content is being made * available from a .war archive). */ protected File getScriptUriAsFile(HttpServletRequest request) { String uri = getScriptUri(request); String real = servletContext.getRealPath(uri); if (real == null) { return null; } return new File(real).getAbsoluteFile(); }
File file = super.getScriptUriAsFile(request); String name = file.getName(); if (!file.exists()) {
/** * Interface method for ResourceContainer. This is used by the GroovyScriptEngine. */ public URLConnection getResourceConnection (String name) throws ResourceException { name = removeNamePrefix(name).replace('\\', '/'); //remove the leading / as we are trying with a leading / now if (name.startsWith("WEB-INF/groovy/")) { name = name.substring(15);//just for uniformity } else if (name.startsWith("/")) { name = name.substring(1); } /* * Try to locate the resource and return an opened connection to it. */ try { URL url = servletContext.getResource('/' + name); if (url == null) { url = servletContext.getResource("/WEB-INF/groovy/" + name); } if (url == null) { throw new ResourceException("Resource \"" + name + "\" not found!"); } return url.openConnection(); } catch (IOException e) { throw new ResourceException("Problems getting resource named \"" + name + "\"!", e); } }
protected String removeNamePrefix(String name) throws ResourceException { if (namePrefix == null) { generateNamePrefixOnce(); } if (name.startsWith(namePrefix)) {//usualy name has text return name.substring(namePrefix.length()); } return name; }
log("Logging request class and its class loader:"); log(" c = request.getClass() :\"" + request.getClass() + "\""); log(" l = c.getClassLoader() :\"" + request.getClass().getClassLoader() + "\""); log(" l.getClass() :\"" + request.getClass().getClassLoader().getClass() + "\""); uri += info; return applyResourceNameMatcher(uri); return applyResourceNameMatcher(uri);
/** * Parses the http request for the real script or template source file. * @param request the http request to analyze * @return a file object using an absolute file path name */ protected File getScriptUriAsFile(HttpServletRequest request) { String uri = getScriptUri(request); String real = servletContext.getRealPath(uri); File file = new File(real).getAbsoluteFile(); return file; }
File file = super.getScriptUriAsFile(request); String name = file.getName(); if (!file.exists()) {
/** * Interface method for ResourceContainer. This is used by the GroovyScriptEngine. */ public URLConnection getResourceConnection (String name) throws ResourceException { name = removeNamePrefix(name).replace('\\', '/'); //remove the leading / as we are trying with a leading / now if (name.startsWith("WEB-INF/groovy/")) { name = name.substring(15);//just for uniformity } else if (name.startsWith("/")) { name = name.substring(1); } /* * Try to locate the resource and return an opened connection to it. */ try { URL url = servletContext.getResource('/' + name); if (url == null) { url = servletContext.getResource("/WEB-INF/groovy/" + name); } if (url == null) { throw new ResourceException("Resource \"" + name + "\" not found!"); } return url.openConnection(); } catch (IOException e) { throw new ResourceException("Problems getting resource named \"" + name + "\"!", e); } }
protected String removeNamePrefix(String name) throws ResourceException { if (namePrefix == null) { generateNamePrefixOnce(); } if (name.startsWith(namePrefix)) {//usualy name has text return name.substring(namePrefix.length()); } return name; }
protected void generateNamePrefixOnce () { URI uri = null; String realPath = servletContext.getRealPath("/"); if (realPath != null) { uri = new File(realPath).toURI();}//prevent NPE if in .war try { URL res = servletContext.getResource("/"); if (res != null) { uri = res.toURI(); } } catch (MalformedURLException ignore) { } catch (URISyntaxException ignore) { } if (uri != null) { try { namePrefix = uri.toURL().toExternalForm(); return; } catch (MalformedURLException e) { log("generateNamePrefixOnce [ERROR] Malformed URL for base path / == '"+ uri +'\'', e); } } namePrefix = ""; }
/** * Initializes the servlet from hints the container passes. * <p> * Delegates to sub-init methods and parses the following parameters: * <ul> * <li> <tt>"generatedBy"</tt> : boolean, appends "Generated by ..." to the * HTML response text generated by this servlet. * </li> * </ul> * * @param config Passed by the servlet container. * @throws ServletException if this method encountered difficulties * @see TemplateServlet#initTemplateEngine(ServletConfig) */ public void init(ServletConfig config) throws ServletException { super.init(config); this.engine = initTemplateEngine(config); if (engine == null) { throw new ServletException("Template engine not instantiated."); } String value = config.getInitParameter("generated.by"); if (value != null) { this.generateBy = Boolean.valueOf(value); } value = config.getInitParameter(GROOVY_SOURCE_ENCODING); if (value != null) { this.fileEncodingParamVal = value; } log("Servlet " + getClass().getName() + " initialized on " + engine.getClass()); }
log("Logging request class and its class loader:"); log(" c = request.getClass() :\"" + request.getClass() + "\""); log(" l = c.getClassLoader() :\"" + request.getClass().getClassLoader() + "\""); log(" l.getClass() :\"" + request.getClass().getClassLoader().getClass() + "\""); uri += info; return applyResourceNameMatcher(uri); return applyResourceNameMatcher(uri);
String name; File file = super.getScriptUriAsFile(request); if (file != null) { name = file.getName(); getMillis = System.currentTimeMillis() - getMillis; } else { name = super.getScriptUri(request); URL url = servletContext.getResource(name); getMillis = System.currentTimeMillis();
/** * Parses the http request for the real script or template source file. * @param request the http request to analyze * @return a file object using an absolute file path name */ protected File getScriptUriAsFile(HttpServletRequest request) { String uri = getScriptUri(request); String real = servletContext.getRealPath(uri); File file = new File(real).getAbsoluteFile(); return file; }
File file = super.getScriptUriAsFile(request); String name = file.getName(); if (!file.exists()) {
/** * Interface method for ResourceContainer. This is used by the GroovyScriptEngine. */ public URLConnection getResourceConnection(String name) throws ResourceException { name = removeNamePrefix(name); name = name.replaceAll("\\\\", "/"); //remove the leading / as we are trying with a leading / now if (name.startsWith("/")) name = name.substring(1); /* * Try to locate the resource and return an opened connection to it. */ try { String tryScriptName = "/" + name; URL url = servletContext.getResource(tryScriptName); if (url == null) { tryScriptName = "/WEB-INF/groovy/" + name; url = servletContext.getResource("/WEB-INF/groovy/" + name); } if (url == null) { throw new ResourceException("Resource \"" + name + "\" not found!"); } return url.openConnection(); } catch (IOException e) { throw new ResourceException("Problems getting resource named \"" + name + "\"!", e); } }