001/*
002 * Copyright 2002-2016 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 org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023
024import org.springframework.http.MediaType;
025import org.springframework.util.Assert;
026import org.springframework.web.HttpMediaTypeNotAcceptableException;
027import org.springframework.web.context.request.NativeWebRequest;
028
029/**
030 * A {@code ContentNegotiationStrategy} that resolves a query parameter to a key
031 * to be used to look up a media type. The default parameter name is {@code format}.
032 *
033 * @author Rossen Stoyanchev
034 * @since 3.2
035 */
036public class ParameterContentNegotiationStrategy extends AbstractMappingContentNegotiationStrategy {
037
038        private static final Log logger = LogFactory.getLog(ParameterContentNegotiationStrategy.class);
039
040        private String parameterName = "format";
041
042
043        /**
044         * Create an instance with the given map of file extensions and media types.
045         */
046        public ParameterContentNegotiationStrategy(Map<String, MediaType> mediaTypes) {
047                super(mediaTypes);
048        }
049
050
051        /**
052         * Set the name of the parameter to use to determine requested media types.
053         * <p>By default this is set to {@code "format"}.
054         */
055        public void setParameterName(String parameterName) {
056                Assert.notNull(parameterName, "'parameterName' is required");
057                this.parameterName = parameterName;
058        }
059
060        public String getParameterName() {
061                return this.parameterName;
062        }
063
064
065        @Override
066        protected String getMediaTypeKey(NativeWebRequest request) {
067                return request.getParameter(getParameterName());
068        }
069
070        @Override
071        protected void handleMatch(String mediaTypeKey, MediaType mediaType) {
072                if (logger.isDebugEnabled()) {
073                        logger.debug("Requested media type: '" + mediaType + "' based on '" +
074                                        getParameterName() + "'='" + mediaTypeKey + "'");
075                }
076        }
077
078        @Override
079        protected MediaType handleNoMatch(NativeWebRequest request, String key)
080                        throws HttpMediaTypeNotAcceptableException {
081
082                throw new HttpMediaTypeNotAcceptableException(getAllMediaTypes());
083        }
084
085}