Skip to content

Commit

Permalink
Consistent configurer access in WebMvcConfigurationSupport
Browse files Browse the repository at this point in the history
Issue: SPR-16017
  • Loading branch information
jhoeller committed Sep 27, 2017
1 parent cc70fdc commit 40ba95f
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* {@code MediaTypeFileExtensionResolver} instances.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 3.2
*/
public class ContentNegotiationManager implements ContentNegotiationStrategy, MediaTypeFileExtensionResolver {
Expand All @@ -65,6 +66,7 @@ public ContentNegotiationManager(ContentNegotiationStrategy... strategies) {
* A collection-based alternative to
* {@link #ContentNegotiationManager(ContentNegotiationStrategy...)}.
* @param strategies the strategies to use
* @since 3.2.2
*/
public ContentNegotiationManager(Collection<ContentNegotiationStrategy> strategies) {
Assert.notEmpty(strategies, "At least one ContentNegotiationStrategy is expected");
Expand Down Expand Up @@ -95,7 +97,7 @@ public List<ContentNegotiationStrategy> getStrategies() {
/**
* Find a {@code ContentNegotiationStrategy} of the given type.
* @param strategyType the strategy type
* @return the first matching strategy or {@code null}.
* @return the first matching strategy, or {@code null} if none
* @since 4.3
*/
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -85,6 +85,7 @@
* extension to a MediaType. You may {@link #setUseJaf suppress} the use of JAF.
*
* @author Rossen Stoyanchev
* @author Brian Clozel
* @since 3.2
*/
public class ContentNegotiationManagerFactoryBean
Expand Down Expand Up @@ -255,8 +256,7 @@ public void afterPropertiesSet() {
if (this.favorPathExtension) {
PathExtensionContentNegotiationStrategy strategy;
if (this.servletContext != null && !isUseJafTurnedOff()) {
strategy = new ServletPathExtensionContentNegotiationStrategy(
this.servletContext, this.mediaTypes);
strategy = new ServletPathExtensionContentNegotiationStrategy(this.servletContext, this.mediaTypes);
}
else {
strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes);
Expand All @@ -269,8 +269,7 @@ public void afterPropertiesSet() {
}

if (this.favorParameter) {
ParameterContentNegotiationStrategy strategy =
new ParameterContentNegotiationStrategy(this.mediaTypes);
ParameterContentNegotiationStrategy strategy = new ParameterContentNegotiationStrategy(this.mediaTypes);
strategy.setParameterName(this.parameterName);
strategies.add(strategy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.web.servlet.config.annotation;

import java.util.ArrayList;
Expand All @@ -22,7 +23,6 @@

import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.util.Assert;
import org.springframework.web.context.request.async.CallableProcessingInterceptor;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.DeferredResultProcessingInterceptor;
Expand Down Expand Up @@ -82,7 +82,6 @@ public AsyncSupportConfigurer setDefaultTimeout(long timeout) {
* @param interceptors the interceptors to register
*/
public AsyncSupportConfigurer registerCallableInterceptors(CallableProcessingInterceptor... interceptors) {
Assert.notNull(interceptors, "Interceptors are required");
this.callableInterceptors.addAll(Arrays.asList(interceptors));
return this;
}
Expand All @@ -93,7 +92,6 @@ public AsyncSupportConfigurer registerCallableInterceptors(CallableProcessingInt
* @param interceptors the interceptors to register
*/
public AsyncSupportConfigurer registerDeferredResultInterceptors(DeferredResultProcessingInterceptor... interceptors) {
Assert.notNull(interceptors, "Interceptors are required");
this.deferredResultInterceptors.addAll(Arrays.asList(interceptors));
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.web.servlet.config.annotation;

import java.util.HashMap;
Expand All @@ -32,9 +33,6 @@
* Creates a {@code ContentNegotiationManager} and configures it with
* one or more {@link ContentNegotiationStrategy} instances.
*
* <p>As of 5.0 you can set the exact strategies to use via
* {@link #strategies(List)}.
*
* <p>As an alternative you can also rely on the set of defaults described below
* which can be turned on or off or customized through the methods of this
* builder:
Expand Down Expand Up @@ -86,12 +84,14 @@
* of JAF.
*
* @author Rossen Stoyanchev
* @author Brian Clozel
* @author Juergen Hoeller
* @since 3.2
* @see ContentNegotiationManagerFactoryBean
*/
public class ContentNegotiationConfigurer {

private final ContentNegotiationManagerFactoryBean factory =
new ContentNegotiationManagerFactoryBean();
private final ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();

private final Map<String, MediaType> mediaTypes = new HashMap<String, MediaType>();

Expand Down Expand Up @@ -226,18 +226,32 @@ public ContentNegotiationConfigurer defaultContentType(MediaType defaultContentT
* Set a custom {@link ContentNegotiationStrategy} to use to determine
* the content type to use when no content type is requested.
* <p>By default this is not set.
* @see #defaultContentType
* @since 4.1.2
* @see #defaultContentType
*/
public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) {
this.factory.setDefaultContentTypeStrategy(defaultStrategy);
return this;
}

protected ContentNegotiationManager getContentNegotiationManager() throws Exception {

/**
* Build a {@link ContentNegotiationManager} based on this configurer's settings.
* @since 4.3.12
* @see ContentNegotiationManagerFactoryBean#getObject()
*/
protected ContentNegotiationManager buildContentNegotiationManager() {
this.factory.addMediaTypes(this.mediaTypes);
this.factory.afterPropertiesSet();
return this.factory.getObject();
}

/**
* @deprecated as of 4.3.12, in favor of {@link #buildContentNegotiationManager()}
*/
@Deprecated
protected ContentNegotiationManager getContentNegotiationManager() throws Exception {
return buildContentNegotiationManager();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

package org.springframework.web.servlet.config.annotation;

import java.util.HashMap;
import java.util.Map;
import java.util.Collections;
import javax.servlet.ServletContext;

import org.springframework.util.Assert;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
Expand Down Expand Up @@ -82,23 +80,30 @@ public void enable(String defaultServletName) {
this.handler.setServletContext(this.servletContext);
}


/**
* Return a handler mapping instance ordered at {@link Integer#MAX_VALUE} containing the
* {@link DefaultServletHttpRequestHandler} instance mapped to {@code "/**"};
* or {@code null} if default servlet handling was not been enabled.
* @since 4.3.12
*/
protected AbstractHandlerMapping getHandlerMapping() {
protected SimpleUrlHandlerMapping buildHandlerMapping() {
if (this.handler == null) {
return null;
}

Map<String, HttpRequestHandler> urlMap = new HashMap<String, HttpRequestHandler>();
urlMap.put("/**", this.handler);

SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
handlerMapping.setUrlMap(Collections.singletonMap("/**", this.handler));
handlerMapping.setOrder(Integer.MAX_VALUE);
handlerMapping.setUrlMap(urlMap);
return handlerMapping;
}

/**
* @deprecated as of 4.3.12, in favor of {@link #buildHandlerMapping()}
*/
@Deprecated
protected AbstractHandlerMapping getHandlerMapping() {
return buildHandlerMapping();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -78,9 +78,7 @@ public PathMatchConfigurer setUseTrailingSlashMatch(Boolean trailingSlashMatch)
* <p>By default this is set to "false".
* @see WebMvcConfigurer#configureContentNegotiation
*/
public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(
Boolean registeredSuffixPatternMatch) {

public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(Boolean registeredSuffixPatternMatch) {
this.registeredSuffixPatternMatch = registeredSuffixPatternMatch;
return this;
}
Expand All @@ -106,6 +104,7 @@ public PathMatchConfigurer setPathMatcher(PathMatcher pathMatcher) {
return this;
}


public Boolean isUseSuffixPatternMatch() {
return this.suffixPatternMatch;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,14 +36,27 @@
*/
public class ViewControllerRegistry {

private ApplicationContext applicationContext;

private final List<ViewControllerRegistration> registrations = new ArrayList<ViewControllerRegistration>(4);

private final List<RedirectViewControllerRegistration> redirectRegistrations =
new ArrayList<RedirectViewControllerRegistration>(10);

private int order = 1;

private ApplicationContext applicationContext;

/**
* Class constructor with {@link ApplicationContext}.
* @since 4.3.12
*/
public ViewControllerRegistry(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}

@Deprecated
public ViewControllerRegistry() {
}


/**
Expand Down Expand Up @@ -96,30 +109,42 @@ public void setOrder(int order) {
this.order = order;
}

protected void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}


/**
* Return the {@code HandlerMapping} that contains the registered view
* controller mappings, or {@code null} for no registrations.
* @since 4.3.12
*/
protected AbstractHandlerMapping getHandlerMapping() {
protected SimpleUrlHandlerMapping buildHandlerMapping() {
if (this.registrations.isEmpty() && this.redirectRegistrations.isEmpty()) {
return null;
}

Map<String, Object> urlMap = new LinkedHashMap<String, Object>();
for (ViewControllerRegistration registration : this.registrations) {
urlMap.put(registration.getUrlPath(), registration.getViewController());
}
for (RedirectViewControllerRegistration registration : this.redirectRegistrations) {
urlMap.put(registration.getUrlPath(), registration.getViewController());
}

SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
handlerMapping.setOrder(this.order);
handlerMapping.setUrlMap(urlMap);
handlerMapping.setOrder(this.order);
return handlerMapping;
}

/**
* @deprecated as of 4.3.12, in favor of {@link #buildHandlerMapping()}
*/
@Deprecated
protected AbstractHandlerMapping getHandlerMapping() {
return buildHandlerMapping();
}

@Deprecated
protected void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}

}
Loading

0 comments on commit 40ba95f

Please sign in to comment.