|
| 1 | +/* |
| 2 | + * Copyright 2002-2012 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.web.accept; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Properties; |
| 24 | + |
| 25 | +import javax.servlet.ServletContext; |
| 26 | + |
| 27 | +import org.springframework.beans.factory.FactoryBean; |
| 28 | +import org.springframework.beans.factory.InitializingBean; |
| 29 | +import org.springframework.http.MediaType; |
| 30 | +import org.springframework.util.CollectionUtils; |
| 31 | + |
| 32 | +/** |
| 33 | + * A factory providing convenient access to a {@code ContentNegotiationManager} |
| 34 | + * configured with one or more {@link ContentNegotiationStrategy} instances. |
| 35 | + * |
| 36 | + * <p>By default strategies for checking the extension of the request path and |
| 37 | + * the {@code Accept} header are registered. The path extension check will perform |
| 38 | + * lookups through the {@link ServletContext} and the Java Activation Framework |
| 39 | + * (if present) unless {@linkplain #setMediaTypes(Map) media types} are configured. |
| 40 | + * |
| 41 | + * @author Rossen Stoyanchev |
| 42 | + * @since 3.2 |
| 43 | + */ |
| 44 | +public class ContentNegotiationManagerFactoryBean implements FactoryBean<ContentNegotiationManager>, InitializingBean { |
| 45 | + |
| 46 | + private boolean favorPathExtension = true; |
| 47 | + |
| 48 | + private boolean favorParameter = false; |
| 49 | + |
| 50 | + private boolean ignoreAcceptHeader = false; |
| 51 | + |
| 52 | + private Map<String, MediaType> mediaTypes = new HashMap<String, MediaType>(); |
| 53 | + |
| 54 | + private Boolean useJaf; |
| 55 | + |
| 56 | + private String parameterName; |
| 57 | + |
| 58 | + private MediaType defaultContentType; |
| 59 | + |
| 60 | + private ContentNegotiationManager contentNegotiationManager; |
| 61 | + |
| 62 | + /** |
| 63 | + * Indicate whether the extension of the request path should be used to determine |
| 64 | + * the requested media type with the <em>highest priority</em>. |
| 65 | + * <p>By default this value is set to {@code true} in which case a request |
| 66 | + * for {@code /hotels.pdf} will be interpreted as a request for |
| 67 | + * {@code "application/pdf"} regardless of the {@code Accept} header. |
| 68 | + */ |
| 69 | + public void setFavorPathExtension(boolean favorPathExtension) { |
| 70 | + this.favorPathExtension = favorPathExtension; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Add mappings from file extensions to media types. |
| 75 | + * <p>If this property is not set, the Java Action Framework, if available, may |
| 76 | + * still be used in conjunction with {@link #setFavorPathExtension(boolean)}. |
| 77 | + */ |
| 78 | + public void setMediaTypes(Properties mediaTypes) { |
| 79 | + if (!CollectionUtils.isEmpty(mediaTypes)) { |
| 80 | + for (Map.Entry<Object, Object> entry : mediaTypes.entrySet()) { |
| 81 | + String extension = ((String) entry.getKey()).toLowerCase(Locale.ENGLISH); |
| 82 | + this.mediaTypes.put(extension, MediaType.valueOf((String) entry.getValue())); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Indicate whether to use the Java Activation Framework as a fallback option |
| 89 | + * to map from file extensions to media types. This is used only when |
| 90 | + * {@link #setFavorPathExtension(boolean)} is set to {@code true}. |
| 91 | + * <p>The default value is {@code true}. |
| 92 | + * @see #parameterName |
| 93 | + * @see #setMediaTypes(Map) |
| 94 | + */ |
| 95 | + public void setUseJaf(boolean useJaf) { |
| 96 | + this.useJaf = useJaf; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Indicate whether a request parameter should be used to determine the |
| 101 | + * requested media type with the <em>2nd highest priority</em>, i.e. |
| 102 | + * after path extensions but before the {@code Accept} header. |
| 103 | + * <p>The default value is {@code false}. If set to to {@code true}, a request |
| 104 | + * for {@code /hotels?format=pdf} will be interpreted as a request for |
| 105 | + * {@code "application/pdf"} regardless of the {@code Accept} header. |
| 106 | + * <p>To use this option effectively you must also configure the MediaType |
| 107 | + * type mappings via {@link #setMediaTypes(Map)}. |
| 108 | + * @see #setParameterName(String) |
| 109 | + */ |
| 110 | + public void setFavorParameter(boolean favorParameter) { |
| 111 | + this.favorParameter = favorParameter; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Set the parameter name that can be used to determine the requested media type |
| 116 | + * if the {@link #setFavorParameter} property is {@code true}. |
| 117 | + * <p>The default parameter name is {@code "format"}. |
| 118 | + */ |
| 119 | + public void setParameterName(String parameterName) { |
| 120 | + this.parameterName = parameterName; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Indicate whether the HTTP {@code Accept} header should be ignored altogether. |
| 125 | + * If set the {@code Accept} header is checked at the |
| 126 | + * <em>3rd highest priority</em>, i.e. after the request path extension and |
| 127 | + * possibly a request parameter if configured. |
| 128 | + * <p>By default this value is set to {@code false}. |
| 129 | + */ |
| 130 | + public void setIgnoreAcceptHeader(boolean ignoreAcceptHeader) { |
| 131 | + this.ignoreAcceptHeader = ignoreAcceptHeader; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Set the default content type. |
| 136 | + * <p>This content type will be used when neither the request path extension, |
| 137 | + * nor a request parameter, nor the {@code Accept} header could help determine |
| 138 | + * the requested content type. |
| 139 | + */ |
| 140 | + public void setDefaultContentType(MediaType defaultContentType) { |
| 141 | + this.defaultContentType = defaultContentType; |
| 142 | + } |
| 143 | + |
| 144 | + public void afterPropertiesSet() throws Exception { |
| 145 | + List<ContentNegotiationStrategy> strategies = new ArrayList<ContentNegotiationStrategy>(); |
| 146 | + |
| 147 | + if (this.favorPathExtension) { |
| 148 | + PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes); |
| 149 | + if (this.useJaf != null) { |
| 150 | + strategy.setUseJaf(this.useJaf); |
| 151 | + } |
| 152 | + strategies.add(strategy); |
| 153 | + } |
| 154 | + |
| 155 | + if (this.favorParameter) { |
| 156 | + ParameterContentNegotiationStrategy strategy = new ParameterContentNegotiationStrategy(this.mediaTypes); |
| 157 | + strategy.setParameterName(this.parameterName); |
| 158 | + strategies.add(strategy); |
| 159 | + } |
| 160 | + |
| 161 | + if (!this.ignoreAcceptHeader) { |
| 162 | + strategies.add(new HeaderContentNegotiationStrategy()); |
| 163 | + } |
| 164 | + |
| 165 | + if (this.defaultContentType != null) { |
| 166 | + strategies.add(new FixedContentNegotiationStrategy(this.defaultContentType)); |
| 167 | + } |
| 168 | + |
| 169 | + ContentNegotiationStrategy[] array = strategies.toArray(new ContentNegotiationStrategy[strategies.size()]); |
| 170 | + this.contentNegotiationManager = new ContentNegotiationManager(array); |
| 171 | + } |
| 172 | + |
| 173 | + public Class<?> getObjectType() { |
| 174 | + return ContentNegotiationManager.class; |
| 175 | + } |
| 176 | + |
| 177 | + public boolean isSingleton() { |
| 178 | + return true; |
| 179 | + } |
| 180 | + |
| 181 | + public ContentNegotiationManager getObject() throws Exception { |
| 182 | + return this.contentNegotiationManager; |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments