001/*
002 * Copyright 2002-2020 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.accept;
018
019import java.util.Map;
020
021import javax.servlet.ServletContext;
022
023import org.springframework.core.io.Resource;
024import org.springframework.http.MediaType;
025import org.springframework.lang.Nullable;
026import org.springframework.util.Assert;
027import org.springframework.util.StringUtils;
028import org.springframework.web.HttpMediaTypeNotAcceptableException;
029import org.springframework.web.context.request.NativeWebRequest;
030
031/**
032 * Extends {@code PathExtensionContentNegotiationStrategy} that also uses
033 * {@link ServletContext#getMimeType(String)} to resolve file extensions.
034 *
035 * @author Rossen Stoyanchev
036 * @since 3.2
037 * @deprecated as of 5.2.4. See class-level note in
038 * {@link ContentNegotiationManagerFactoryBean} on the deprecation of path
039 * extension config options.
040 */
041@Deprecated
042public class ServletPathExtensionContentNegotiationStrategy extends PathExtensionContentNegotiationStrategy {
043
044        private final ServletContext servletContext;
045
046
047        /**
048         * Create an instance without any mappings to start with. Mappings may be
049         * added later when extensions are resolved through
050         * {@link ServletContext#getMimeType(String)} or via
051         * {@link org.springframework.http.MediaTypeFactory}.
052         */
053        public ServletPathExtensionContentNegotiationStrategy(ServletContext context) {
054                this(context, null);
055        }
056
057        /**
058         * Create an instance with the given extension-to-MediaType lookup.
059         */
060        public ServletPathExtensionContentNegotiationStrategy(
061                        ServletContext servletContext, @Nullable Map<String, MediaType> mediaTypes) {
062
063                super(mediaTypes);
064                Assert.notNull(servletContext, "ServletContext is required");
065                this.servletContext = servletContext;
066        }
067
068
069        /**
070         * Resolve file extension via {@link ServletContext#getMimeType(String)}
071         * and also delegate to base class for a potential
072         * {@link org.springframework.http.MediaTypeFactory} lookup.
073         */
074        @Override
075        @Nullable
076        protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension)
077                        throws HttpMediaTypeNotAcceptableException {
078
079                MediaType mediaType = null;
080                String mimeType = this.servletContext.getMimeType("file." + extension);
081                if (StringUtils.hasText(mimeType)) {
082                        mediaType = MediaType.parseMediaType(mimeType);
083                }
084                if (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
085                        MediaType superMediaType = super.handleNoMatch(webRequest, extension);
086                        if (superMediaType != null) {
087                                mediaType = superMediaType;
088                        }
089                }
090                return mediaType;
091        }
092
093        /**
094         * Extends the base class
095         * {@link PathExtensionContentNegotiationStrategy#getMediaTypeForResource}
096         * with the ability to also look up through the ServletContext.
097         * @param resource the resource to look up
098         * @return the MediaType for the extension, or {@code null} if none found
099         * @since 4.3
100         */
101        @Override
102        public MediaType getMediaTypeForResource(Resource resource) {
103                MediaType mediaType = null;
104                String mimeType = this.servletContext.getMimeType(resource.getFilename());
105                if (StringUtils.hasText(mimeType)) {
106                        mediaType = MediaType.parseMediaType(mimeType);
107                }
108                if (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
109                        MediaType superMediaType = super.getMediaTypeForResource(resource);
110                        if (superMediaType != null) {
111                                mediaType = superMediaType;
112                        }
113                }
114                return mediaType;
115        }
116
117}