/** * This implementation supports file paths beneath the root of the web application. * @see ServletContextResource */ @Override protected Resource getResourceByPath(String path) { return new ServletContextResource(this.servletContext, path); }
/** * Overridden version which checks for ServletContextResource * and uses {@code ServletContext.getResourcePaths} to find * matching resources below the web application root directory. * In case of other resources, delegates to the superclass version. * @see #doRetrieveMatchingServletContextResources * @see ServletContextResource * @see javax.servlet.ServletContext#getResourcePaths */ @Override protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException { if (rootDirResource instanceof ServletContextResource) { ServletContextResource scResource = (ServletContextResource) rootDirResource; ServletContext sc = scResource.getServletContext(); String fullPattern = scResource.getPath() + subPattern; Set<Resource> result = new LinkedHashSet<>(8); doRetrieveMatchingServletContextResources(sc, fullPattern, scResource.getPath(), result); return result; } else { return super.doFindPathMatchingFileResources(rootDirResource, subPattern); } }
ServletContextResource resource = new ServletContextResource(getServletContext(), "/" + path); if (resource.exists())
public InputStream loadInputStream() throws IOException { InputStream in = null; ServletContextResource resource = new ServletContextResource(servletContext, path); { if (resource.exists()) { in = resource.getInputStream(); } } return in; } }
/** * Return the file timestamp for the given resource. * @param resourceUrl the URL of the resource * @return the file timestamp in milliseconds, or -1 if not determinable */ protected long getFileTimestamp(String resourceUrl) { ServletContextResource resource = new ServletContextResource(getServletContext(), resourceUrl); try { long lastModifiedTime = resource.lastModified(); if (logger.isDebugEnabled()) { logger.debug("Last-modified timestamp of " + resource + " is " + lastModifiedTime); } return lastModifiedTime; } catch (IOException ex) { if (logger.isWarnEnabled()) { logger.warn("Couldn't retrieve last-modified timestamp of " + resource + " - using ResourceServlet startup time"); } return -1; } }
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException { if (resource.getClass() != location.getClass()) { return false; } String resourcePath; String locationPath; if (resource instanceof UrlResource) { resourcePath = resource.getURL().toExternalForm(); locationPath = StringUtils.cleanPath(location.getURL().toString()); } else if (resource instanceof ClassPathResource) { resourcePath = ((ClassPathResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath()); } else if (resource instanceof ServletContextResource) { resourcePath = ((ServletContextResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ServletContextResource) location).getPath()); } else { resourcePath = resource.getURL().getPath(); locationPath = StringUtils.cleanPath(location.getURL().getPath()); } if (locationPath.equals(resourcePath)) { return true; } locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/"); return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath)); }
/** * This implementation delegates to {@code ServletContext.getResourceAsStream}, * but throws a FileNotFoundException if no resource found. * @see javax.servlet.ServletContext#getResourceAsStream(String) */ @Override public InputStream getInputStream() throws IOException { InputStream is = this.servletContext.getResourceAsStream(this.path); if (is == null) { throw new FileNotFoundException("Could not open " + getDescription()); } return is; }
/** * Returns the context path. * * @param rootFile * @return context path */ private String getContextPath(Resource rootFile) { String contextPath = null; if (rootFile instanceof ServletContextResource) { ServletContextResource servletResource = (ServletContextResource) rootFile; contextPath = servletResource.getServletContext().getContextPath(); if ("/".equals(contextPath)) { contextPath = "/root"; } } else if (resources instanceof IScope) { contextPath = ((IScope) resources).getContextPath(); if (contextPath == null) { contextPath = "/root"; } } log.debug("Persistence context path: {}", contextPath); return contextPath; }
ServletContextResource resource = new ServletContextResource(getServletContext(), "/" + path); if (resource.exists())
public InputStream loadInputStream() throws IOException { InputStream in = null; ServletContextResource resource = new ServletContextResource(servletContext, path); { if (resource.exists()) { in = resource.getInputStream(); } } return in; } }
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException { if (resource.getClass() != location.getClass()) { return false; } String resourcePath; String locationPath; if (resource instanceof UrlResource) { resourcePath = resource.getURL().toExternalForm(); locationPath = StringUtils.cleanPath(location.getURL().toString()); } else if (resource instanceof ClassPathResource) { resourcePath = ((ClassPathResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath()); } else if (resource instanceof ServletContextResource) { resourcePath = ((ServletContextResource) resource).getPath(); locationPath = StringUtils.cleanPath(((ServletContextResource) location).getPath()); } else { resourcePath = resource.getURL().getPath(); locationPath = StringUtils.cleanPath(location.getURL().getPath()); } if (locationPath.equals(resourcePath)) { return true; } locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/"); return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath)); }
/** * This implementation delegates to {@code ServletContext.getResource}, * but throws a FileNotFoundException if no resource found. * @see javax.servlet.ServletContext#getResource(String) */ @Override public URL getURL() throws IOException { URL url = this.servletContext.getResource(this.path); if (url == null) { throw new FileNotFoundException( getDescription() + " cannot be resolved to URL because it does not exist"); } return url; }
/** * This implementation supports file paths beneath the root of the ServletContext. * @see ServletContextResource */ @Override protected Resource getResourceByPath(String path) { Assert.state(this.servletContext != null, "No ServletContext available"); return new ServletContextResource(this.servletContext, path); }
ServletContextResource resource = new ServletContextResource(getServletContext(), "/" + path); if (resource.exists())
/** * Overridden version which checks for ServletContextResource * and uses {@code ServletContext.getResourcePaths} to find * matching resources below the web application root directory. * In case of other resources, delegates to the superclass version. * @see #doRetrieveMatchingServletContextResources * @see ServletContextResource * @see javax.servlet.ServletContext#getResourcePaths */ @Override protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException { if (rootDirResource instanceof ServletContextResource) { ServletContextResource scResource = (ServletContextResource) rootDirResource; ServletContext sc = scResource.getServletContext(); String fullPattern = scResource.getPath() + subPattern; Set<Resource> result = new LinkedHashSet<>(8); doRetrieveMatchingServletContextResources(sc, fullPattern, scResource.getPath(), result); return result; } else { return super.doFindPathMatchingFileResources(rootDirResource, subPattern); } }
@Test public void testServletContextResourcePatternResolverWithAbsolutePaths() throws IOException { final Set<String> paths = new HashSet<>(); paths.add("C:/webroot/WEB-INF/context1.xml"); paths.add("C:/webroot/WEB-INF/context2.xml"); paths.add("C:/webroot/someOtherDirThatDoesntContainPath"); MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") { @Override public Set<String> getResourcePaths(String path) { if ("/WEB-INF/".equals(path)) { return paths; } return null; } }; ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc); Resource[] found = rpr.getResources("/WEB-INF/*.xml"); Set<String> foundPaths = new HashSet<>(); for (Resource resource : found) { foundPaths.add(((ServletContextResource) resource).getPath()); } assertEquals(2, foundPaths.size()); assertTrue(foundPaths.contains("/WEB-INF/context1.xml")); assertTrue(foundPaths.contains("/WEB-INF/context2.xml")); }
/** * This implementation delegates to {@code ServletContext.getResourceAsStream}, * but throws a FileNotFoundException if no resource found. * @see javax.servlet.ServletContext#getResourceAsStream(String) */ @Override public InputStream getInputStream() throws IOException { InputStream is = this.servletContext.getResourceAsStream(this.path); if (is == null) { throw new FileNotFoundException("Could not open " + getDescription()); } return is; }
/** * This implementation supports file paths beneath the root of the ServletContext. * @see ServletContextResource */ @Override protected Resource getResourceByPath(String path) { Assert.state(this.servletContext != null, "No ServletContext available"); return new ServletContextResource(this.servletContext, path); }
ServletContextResource resource = new ServletContextResource(this.servletContext, p); if (resource.exists())