/** * Gets a resource in the given servlet context. */ public static ServletResource getResource(ServletContext sc,String path){ return new SimpleServletResource(sc,path); }
public static String getRequestPathFromUri(String url) { return getRequestPathFromUri(url, ""); }
/** * This implementation delegates to {@code ServletContext.getResourceAsStream}, * but throws a FileNotFoundException if no resource found. * @see javax.servlet.ServletContext#getResourceAsStream(String) */ public InputStream getInputStream() throws IOException { InputStream is = this.servletContext.getResourceAsStream(this.path); if (is == null) { throw new FileNotFoundException("Could not open " + getDescription()); } return is; }
protected boolean initHomeDirectoryFromWebapp() throws Throwable { if(context.isServletEnvironment()){ ServletResource r = Servlets.getResource(context.getServletContext(), "/WEB-INF"); if(r.exists() && r.isFile()){ dir = Resources.createFileResource(r.getFile()); //project // /src/main/webapp/WEB-INF // /target/classes if(dir.createRelative("../../../../target/classes").exists()){ dir = dir.createRelative("../../../../target"); } return true; } } return false; }
private void loadServletContextResource(ServletContext sc, String path, String pathPrefix) { ServletResource resource = new SimpleServletResource(sc, path); if(!resource.isDirectory()) { add(resource, Strings.removeStart(path, pathPrefix)); }else{ loadServletContextResources(sc, path, pathPrefix); } }
/** * This implementation resolves "file:" URLs or alternatively delegates to * {@code ServletContext.getRealPath}, throwing a FileNotFoundException * if not found or not resolvable. * @see javax.servlet.ServletContext#getResource(String) * @see javax.servlet.ServletContext#getRealPath(String) */ @Override public File getFile() { try { URL url = this.servletContext.getResource(this.path); if (url != null && Urls.isFileUrl(url)) { // Proceed with file system resolution... return super.getFile(); } else if(url != null && Urls.isJarUrl(url)){ return new File(url.getFile()); } else { String realPath = Servlets.getRealPath(this.servletContext, this.path); return new File(realPath); } } catch (IOException e) { throw Exceptions.wrap(e); } }
ServletResource r = Servlets.getResource(sc, "/WEB-INF"); if(r.exists() && r.isFile()){ FileResource dir = Resources.createFileResource(r.getFile());
/** * This implementation creates a ServletContextResource, applying the given path * relative to the path of the underlying file of this resource descriptor. * @see org.springframework.util.StringUtils#applyRelativePath(String, String) */ @Override public SimpleServletResource createRelative(String relativePath) { String pathToUse = Paths.applyRelative(this.path, relativePath); return new SimpleServletResource(this.servletContext, pathToUse); }
String redirectUri = config.getClientRedirectUri(); if(redirectUri.startsWith("/")) { redirectPath = Servlets.getRequestPathFromUri(redirectUri); }else{ redirectPath = Servlets.getRequestPathFromUri(redirectUri, app.getContextPath()); if(!Strings.isEmpty(logoutUri)) { if(logoutUri.startsWith("/")){ logoutPath = Servlets.getRequestPathFromUri(logoutUri); }else{ logoutPath = Servlets.getRequestPathFromUri(logoutUri, app.getContextPath());
/** * 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; }
public static ServletResource getResource(ServletContext sc,String path, Locale locale) { String[] paths = Locales.getLocaleFilePaths(locale, path); for (String p : paths) { try { if (null != sc.getResource(p)) { return new SimpleServletResource(sc, p); } } catch (MalformedURLException ex) { } } return null; }
@Override @SuppressWarnings("unchecked") public ServletResource[] scan(String subPattern) { Args.notEmpty(subPattern,"subPattern"); Set<String> subPaths = this.servletContext.getResourcePaths(this.path); List<SimpleServletResource> resources = new ArrayList<>(); PathMatcher matcher = Resources.getPathMatcher(); for(String subPath : subPaths){ if(matcher.match(subPattern, subPath)){ resources.add(new SimpleServletResource(servletContext, subPath)); } } return resources.toArray(new SimpleServletResource[resources.size()]); }