strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes); strategy.setIgnoreUnknownExtensions(this.ignoreUnknownPathExtensions); if (this.useRegisteredExtensionsOnly != null) { strategy.setUseRegisteredExtensionsOnly(this.useRegisteredExtensionsOnly);
/** * Determine the media type for the given request and the resource matched * to it. This implementation tries to determine the MediaType based on the * file extension of the Resource via * {@link ServletPathExtensionContentNegotiationStrategy#getMediaTypeForResource}. * @param request the current request * @param resource the resource to check * @return the corresponding media type, or {@code null} if none found */ @Nullable protected MediaType getMediaType(HttpServletRequest request, Resource resource) { return (this.contentNegotiationStrategy != null ? this.contentNegotiationStrategy.getMediaTypeForResource(resource) : null); }
/** * Initialize the content negotiation strategy depending on the {@code ContentNegotiationManager} * setup and the availability of a {@code ServletContext}. * @see ServletPathExtensionContentNegotiationStrategy * @see PathExtensionContentNegotiationStrategy */ protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy() { Map<String, MediaType> mediaTypes = null; if (getContentNegotiationManager() != null) { PathExtensionContentNegotiationStrategy strategy = getContentNegotiationManager().getStrategy(PathExtensionContentNegotiationStrategy.class); if (strategy != null) { mediaTypes = new HashMap<>(strategy.getMediaTypes()); } } return (getServletContext() != null ? new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) : new PathExtensionContentNegotiationStrategy(mediaTypes)); }
/** * Create an instance with the given map of file extensions and media types. */ public PathExtensionContentNegotiationStrategy(@Nullable Map<String, MediaType> mediaTypes) { super(mediaTypes); setUseRegisteredExtensionsOnly(false); setIgnoreUnknownExtensions(true); this.urlPathHelper.setUrlDecode(false); }
@Test(expected = HttpMediaTypeNotAcceptableException.class) public void resolveMediaTypesDoNotIgnoreUnknownExtension() throws Exception { this.servletRequest.setRequestURI("test.foobar"); PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(); strategy.setIgnoreUnknownExtensions(false); strategy.resolveMediaTypes(this.webRequest); }
private static PathExtensionContentNegotiationStrategy initPathStrategy(ContentNegotiationManager manager) { Class<PathExtensionContentNegotiationStrategy> clazz = PathExtensionContentNegotiationStrategy.class; PathExtensionContentNegotiationStrategy strategy = manager.getStrategy(clazz); return (strategy != null ? strategy : new PathExtensionContentNegotiationStrategy()); }
@Test public void resolveMediaTypesFromMapping() throws Exception { this.servletRequest.setRequestURI("test.html"); PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(); List<MediaType> mediaTypes = strategy.resolveMediaTypes(this.webRequest); assertEquals(Arrays.asList(new MediaType("text", "html")), mediaTypes); Map<String, MediaType> mapping = Collections.singletonMap("HTML", MediaType.APPLICATION_XHTML_XML); strategy = new PathExtensionContentNegotiationStrategy(mapping); mediaTypes = strategy.resolveMediaTypes(this.webRequest); assertEquals(Arrays.asList(new MediaType("application", "xhtml+xml")), mediaTypes); }
/** * Indicate whether to use the Java Activation Framework as a fallback option * to map from file extensions to media types. * @deprecated as of 5.0, in favor of {@link #setUseRegisteredExtensionsOnly(boolean)}. */ @Deprecated public void setUseJaf(boolean useJaf) { setUseRegisteredExtensionsOnly(!useJaf); }
/** * Resolve file extension via {@link ServletContext#getMimeType(String)} * and also delegate to base class for a potential * {@link org.springframework.http.MediaTypeFactory} lookup. */ @Override @Nullable protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension) throws HttpMediaTypeNotAcceptableException { MediaType mediaType = null; String mimeType = this.servletContext.getMimeType("file." + extension); if (StringUtils.hasText(mimeType)) { mediaType = MediaType.parseMediaType(mimeType); } if (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) { MediaType superMediaType = super.handleNoMatch(webRequest, extension); if (superMediaType != null) { mediaType = superMediaType; } } return mediaType; }
/** * A public method exposing the knowledge of the path extension strategy to * resolve file extensions to a {@link MediaType} in this case for a given * {@link Resource}. The method first looks up any explicitly registered * file extensions first and then falls back on {@link MediaTypeFactory} if available. * @param resource the resource to look up * @return the MediaType for the extension, or {@code null} if none found * @since 4.3 */ @Nullable public MediaType getMediaTypeForResource(Resource resource) { Assert.notNull(resource, "Resource must not be null"); MediaType mediaType = null; String filename = resource.getFilename(); String extension = StringUtils.getFilenameExtension(filename); if (extension != null) { mediaType = lookupMediaType(extension); } if (mediaType == null) { mediaType = MediaTypeFactory.getMediaType(filename).orElse(null); } return mediaType; }
private boolean safeMediaTypesForExtension(NativeWebRequest request, String extension) { List<MediaType> mediaTypes = null; try { mediaTypes = this.pathStrategy.resolveMediaTypeKey(request, extension); } catch (HttpMediaTypeNotAcceptableException ex) { // Ignore } if (CollectionUtils.isEmpty(mediaTypes)) { return false; } for (MediaType mediaType : mediaTypes) { if (!safeMediaType(mediaType)) { return false; } } return true; }
private static PathExtensionContentNegotiationStrategy initPathStrategy(ContentNegotiationManager manager) { Class<PathExtensionContentNegotiationStrategy> clazz = PathExtensionContentNegotiationStrategy.class; PathExtensionContentNegotiationStrategy strategy = manager.getStrategy(clazz); return (strategy != null ? strategy : new PathExtensionContentNegotiationStrategy()); }
@Test public void resolveMediaTypesIgnoreUnknownExtension() throws Exception { this.servletRequest.setRequestURI("test.foobar"); PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(); List<MediaType> mediaTypes = strategy.resolveMediaTypes(this.webRequest); assertEquals(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, mediaTypes); }
/** * Create an instance with the given map of file extensions and media types. */ public PathExtensionContentNegotiationStrategy(@Nullable Map<String, MediaType> mediaTypes) { super(mediaTypes); setUseRegisteredExtensionsOnly(false); setIgnoreUnknownExtensions(true); this.urlPathHelper.setUrlDecode(false); }
/** * Indicate whether to use the Java Activation Framework as a fallback option * to map from file extensions to media types. * @deprecated as of 5.0, in favor of {@link #setUseRegisteredExtensionsOnly(boolean)}. */ @Deprecated public void setUseJaf(boolean useJaf) { setUseRegisteredExtensionsOnly(!useJaf); }
/** * Resolve file extension via {@link ServletContext#getMimeType(String)} * and also delegate to base class for a potential * {@link org.springframework.http.MediaTypeFactory} lookup. */ @Override @Nullable protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension) throws HttpMediaTypeNotAcceptableException { MediaType mediaType = null; String mimeType = this.servletContext.getMimeType("file." + extension); if (StringUtils.hasText(mimeType)) { mediaType = MediaType.parseMediaType(mimeType); } if (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) { MediaType superMediaType = super.handleNoMatch(webRequest, extension); if (superMediaType != null) { mediaType = superMediaType; } } return mediaType; }
/** * A public method exposing the knowledge of the path extension strategy to * resolve file extensions to a {@link MediaType} in this case for a given * {@link Resource}. The method first looks up any explicitly registered * file extensions first and then falls back on {@link MediaTypeFactory} if available. * @param resource the resource to look up * @return the MediaType for the extension, or {@code null} if none found * @since 4.3 */ @Nullable public MediaType getMediaTypeForResource(Resource resource) { Assert.notNull(resource, "Resource must not be null"); MediaType mediaType = null; String filename = resource.getFilename(); String extension = StringUtils.getFilenameExtension(filename); if (extension != null) { mediaType = lookupMediaType(extension); } if (mediaType == null) { mediaType = MediaTypeFactory.getMediaType(filename).orElse(null); } return mediaType; }
private boolean safeMediaTypesForExtension(NativeWebRequest request, String extension) { List<MediaType> mediaTypes = null; try { mediaTypes = this.pathStrategy.resolveMediaTypeKey(request, extension); } catch (HttpMediaTypeNotAcceptableException ex) { // Ignore } if (CollectionUtils.isEmpty(mediaTypes)) { return false; } for (MediaType mediaType : mediaTypes) { if (!safeMediaType(mediaType)) { return false; } } return true; }
strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes); strategy.setIgnoreUnknownExtensions(this.ignoreUnknownPathExtensions); if (this.useRegisteredExtensionsOnly != null) { strategy.setUseRegisteredExtensionsOnly(this.useRegisteredExtensionsOnly);
@Test public void useRegisteredSuffixPatternMatchInitialization() { Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON); PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions); ContentNegotiationManager manager = new ContentNegotiationManager(strategy); final Set<String> extensions = new HashSet<>(); RequestMappingHandlerMapping hm = new RequestMappingHandlerMapping() { @Override protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { extensions.addAll(getFileExtensions()); return super.getMappingForMethod(method, handlerType); } }; wac.registerSingleton("testController", ComposedAnnotationController.class); wac.refresh(); hm.setContentNegotiationManager(manager); hm.setUseRegisteredSuffixPatternMatch(true); hm.setApplicationContext(wac); hm.afterPropertiesSet(); assertEquals(Collections.singleton("json"), extensions); }