org.springframework.web.context.support.ServletContextResource Java Examples
The following examples show how to use
org.springframework.web.context.support.ServletContextResource.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ViewConfiguration.java From java-platform with Apache License 2.0 | 6 votes |
@Bean public CompositeResourceLoader viewResourceLoader() { CompositeResourceLoader compositeResourceLoader = new CompositeResourceLoader(); compositeResourceLoader.addResourceLoader(new StartsWithMatcher(VIEW_PREFIX_CLASSPATH).withoutPrefix(), new ClasspathResourceLoader("/views")); try { compositeResourceLoader.addResourceLoader(new StartsWithMatcher(VIEW_PREFIX_TEMPLATE), new WebAppResourceLoader(new ServletContextResource(servletContext, templateLocation).getFile().getAbsolutePath())); compositeResourceLoader.addResourceLoader(new StartsWithMatcher("/WEB-INF").withPrefix(), new WebAppResourceLoader(servletContext.getRealPath("."))); } catch (IOException e) { e.printStackTrace(); } return compositeResourceLoader; }
Example #2
Source File: ResourceServlet.java From lams with GNU General Public License v2.0 | 6 votes |
/** * 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; } }
Example #3
Source File: ResourceServlet.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * 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) { logger.warn("Couldn't retrieve last-modified timestamp of [" + resource + "] - using ResourceServlet startup time"); return -1; } }
Example #4
Source File: SpringSoyViewBaseConfig.java From spring-soy-view with Apache License 2.0 | 5 votes |
@Bean public DefaultTemplateFilesResolver soyTemplateFilesResolver() throws Exception { final DefaultTemplateFilesResolver defaultTemplateFilesResolver = new DefaultTemplateFilesResolver(); defaultTemplateFilesResolver.setHotReloadMode(hotReloadMode); defaultTemplateFilesResolver.setRecursive(recursive); defaultTemplateFilesResolver.setFilesExtension(fileExtension); defaultTemplateFilesResolver.setTemplatesLocation(new ServletContextResource(servletContext, templatesPath)); return defaultTemplateFilesResolver; }
Example #5
Source File: PathResourceResolverTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void checkServletContextResource() throws Exception { Resource classpathLocation = new ClassPathResource("test/", PathResourceResolver.class); MockServletContext context = new MockServletContext(); ServletContextResource servletContextLocation = new ServletContextResource(context, "/webjars/"); ServletContextResource resource = new ServletContextResource(context, "/webjars/webjar-foo/1.0/foo.js"); assertFalse(this.resolver.checkResource(resource, classpathLocation)); assertTrue(this.resolver.checkResource(resource, servletContextLocation)); }
Example #6
Source File: PathResourceResolver.java From java-technology-stack with MIT License | 5 votes |
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)); }
Example #7
Source File: ImageController.java From tutorials with MIT License | 5 votes |
@RequestMapping(value = "/image-resource", method = RequestMethod.GET) @ResponseBody public ResponseEntity<Resource> getImageAsResource() { final HttpHeaders headers = new HttpHeaders(); Resource resource = new ServletContextResource(servletContext, "/WEB-INF/images/image-example.jpg"); return new ResponseEntity<>(resource, headers, HttpStatus.OK); }
Example #8
Source File: JspResourceViewResolver.java From onetwo with Apache License 2.0 | 5 votes |
protected boolean tryToCheckJspResource(HttpServletRequest request, String viewName){ ServletContext sc = request.getServletContext(); String jsp = getPrefix() + viewName + getSuffix(); ServletContextResource scr = new ServletContextResource(sc, jsp); if(scr.exists()){ return true; } String path = sc.getRealPath(jsp); if(StringUtils.isBlank(path)){ return false; } File jspFile = new File(path); return jspFile.exists(); }
Example #9
Source File: PathResourceResolverTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void checkServletContextResource() throws Exception { Resource classpathLocation = new ClassPathResource("test/", PathResourceResolver.class); MockServletContext context = new MockServletContext(); ServletContextResource servletContextLocation = new ServletContextResource(context, "/webjars/"); ServletContextResource resource = new ServletContextResource(context, "/webjars/webjar-foo/1.0/foo.js"); assertFalse(this.resolver.checkResource(resource, classpathLocation)); assertTrue(this.resolver.checkResource(resource, servletContextLocation)); }
Example #10
Source File: PathResourceResolver.java From spring-analysis-note with MIT License | 5 votes |
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)); }
Example #11
Source File: PathResourceResolverTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void checkServletContextResource() throws Exception { Resource classpathLocation = new ClassPathResource("test/", PathResourceResolver.class); MockServletContext context = new MockServletContext(); ServletContextResource servletContextLocation = new ServletContextResource(context, "/webjars/"); ServletContextResource resource = new ServletContextResource(context, "/webjars/webjar-foo/1.0/foo.js"); assertFalse(this.resolver.checkResource(resource, classpathLocation)); assertTrue(this.resolver.checkResource(resource, servletContextLocation)); }
Example #12
Source File: PathResourceResolver.java From spring4-understanding with Apache License 2.0 | 4 votes |
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 + "/"); if (!resourcePath.startsWith(locationPath)) { return false; } if (resourcePath.contains("%")) { // Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars... if (URLDecoder.decode(resourcePath, "UTF-8").contains("../")) { if (logger.isTraceEnabled()) { logger.trace("Resolved resource path contains \"../\" after decoding: " + resourcePath); } return false; } } return true; }
Example #13
Source File: IconController.java From es with Apache License 2.0 | 4 votes |
/** * 如果量大 建议 在页面设置按钮 然后点击生成 * * @param request * @return */ @RequestMapping(value = "/genCssFile") @ResponseBody public String genIconCssFile(HttpServletRequest request) { this.permissionList.assertHasEditPermission(); String uploadFileTemplate = ".%1$s{background:url(%2$s/%3$s);width:%4$spx;height:%5$spx;display:inline-block;vertical-align: middle;%6$s}"; String cssSpriteTemplate = ".%1$s{background:url(%2$s/%3$s) no-repeat -%4$spx -%5$spx;width:%6$spx;height:%7$spx;display:inline-block;vertical-align: middle;%8$s}"; ServletContext sc = request.getServletContext(); String ctx = sc.getContextPath(); List<String> cssList = Lists.newArrayList(); Searchable searchable = Searchable.newSearchable() .addSearchParam("type_in", new IconType[]{IconType.upload_file, IconType.css_sprite}); List<Icon> iconList = baseService.findAllWithNoPageNoSort(searchable); for (Icon icon : iconList) { if (icon.getType() == IconType.upload_file) { cssList.add(String.format( uploadFileTemplate, icon.getIdentity(), ctx, icon.getImgSrc(), icon.getWidth(), icon.getHeight(), icon.getStyle())); continue; } if (icon.getType() == IconType.css_sprite) { cssList.add(String.format( cssSpriteTemplate, icon.getIdentity(), ctx, icon.getSpriteSrc(), icon.getLeft(), icon.getTop(), icon.getWidth(), icon.getHeight(), icon.getStyle())); continue; } } try { ServletContextResource resource = new ServletContextResource(sc, iconClassFile); FileUtils.writeLines(resource.getFile(), cssList); } catch (Exception e) { LogUtils.logError("gen icon error", e); return "生成失败:" + e.getMessage(); } return "生成成功"; }
Example #14
Source File: TemplateHttpRequestHandler.java From java-platform with Apache License 2.0 | 4 votes |
@Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Supported methods and required session checkRequest(request); // Check whether a matching resource exists Resource resource = getResource(request); if (resource == null) { logger.trace("No matching resource found - returning 404"); response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // Check the resource's media type MediaType mediaType = getMediaType(resource); if (mediaType != null) { if (logger.isTraceEnabled()) { logger.trace("Determined media type '" + mediaType + "' for " + resource); } if (Objects.equal(mediaType, MediaType.TEXT_HTML)) { WebRender render = new WebRender(beetlConfig.getGroupTemplate()); if (resource instanceof ServletContextResource) { render.render(((ServletContextResource) resource).getPath(), request, response); } return; } } else { if (logger.isTraceEnabled()) { logger.trace("No media type found for " + resource + " - not sending a content-type header"); } } // Header phase if (new ServletWebRequest(request, response).checkNotModified(resource.lastModified())) { logger.trace("Resource not modified - returning 304"); return; } // Apply cache settings, if any prepareResponse(response); // Content phase if (METHOD_HEAD.equals(request.getMethod())) { setHeaders(response, resource, mediaType); logger.trace("HEAD request - skipping content"); return; } if (request.getHeader(HttpHeaders.RANGE) == null) { setHeaders(response, resource, mediaType); writeContent(response, resource); } else { writePartialContent(request, response, resource, mediaType); } }
Example #15
Source File: ResourceBundleViewResolverTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
public void setLocation(Resource location) { if (!(location instanceof ServletContextResource)) { throw new IllegalArgumentException("Expecting ServletContextResource, not " + location.getClass().getName()); } }
Example #16
Source File: ViewResolverTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
public void setLocation(Resource location) { if (!(location instanceof ServletContextResource)) { throw new IllegalArgumentException("Expecting ClassPathResource, not " + location.getClass().getName()); } }
Example #17
Source File: PathResourceResolver.java From lams with GNU General Public License v2.0 | 4 votes |
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 + "/"); if (!resourcePath.startsWith(locationPath)) { return false; } if (resourcePath.contains("%")) { // Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars... if (URLDecoder.decode(resourcePath, "UTF-8").contains("../")) { if (logger.isTraceEnabled()) { logger.trace("Resolved resource path contains \"../\" after decoding: " + resourcePath); } return false; } } return true; }
Example #18
Source File: ResourceBundleViewResolverTests.java From java-technology-stack with MIT License | 4 votes |
public void setLocation(Resource location) { if (!(location instanceof ServletContextResource)) { throw new IllegalArgumentException("Expecting ServletContextResource, not " + location.getClass().getName()); } }
Example #19
Source File: ViewResolverTests.java From java-technology-stack with MIT License | 4 votes |
public void setLocation(Resource location) { if (!(location instanceof ServletContextResource)) { throw new IllegalArgumentException("Expecting ClassPathResource, not " + location.getClass().getName()); } }
Example #20
Source File: ResourceBundleViewResolverTests.java From spring-analysis-note with MIT License | 4 votes |
public void setLocation(Resource location) { if (!(location instanceof ServletContextResource)) { throw new IllegalArgumentException("Expecting ServletContextResource, not " + location.getClass().getName()); } }
Example #21
Source File: ViewResolverTests.java From spring-analysis-note with MIT License | 4 votes |
public void setLocation(Resource location) { if (!(location instanceof ServletContextResource)) { throw new IllegalArgumentException("Expecting ClassPathResource, not " + location.getClass().getName()); } }