Polishing

(cherry picked from commit d2cc97a)
This commit is contained in:
Juergen Hoeller 2017-02-21 22:41:40 +01:00
parent ba0484fcac
commit 34a0857628
7 changed files with 60 additions and 51 deletions

View File

@ -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.
@ -40,7 +40,6 @@ public abstract class AbstractSubscriptionRegistry implements SubscriptionRegist
private static MultiValueMap<String, String> EMPTY_MAP =
CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<String, String>(0));
protected final Log logger = LogFactory.getLog(getClass());
@ -55,19 +54,25 @@ public abstract class AbstractSubscriptionRegistry implements SubscriptionRegist
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
if (sessionId == null) {
logger.error("No sessionId in " + message);
if (logger.isErrorEnabled()) {
logger.error("No sessionId in " + message);
}
return;
}
String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
if (subscriptionId == null) {
logger.error("No subscriptionId in " + message);
if (logger.isErrorEnabled()) {
logger.error("No subscriptionId in " + message);
}
return;
}
String destination = SimpMessageHeaderAccessor.getDestination(headers);
if (destination == null) {
logger.error("No destination in " + message);
if (logger.isErrorEnabled()) {
logger.error("No destination in " + message);
}
return;
}
@ -85,13 +90,17 @@ public abstract class AbstractSubscriptionRegistry implements SubscriptionRegist
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
if (sessionId == null) {
logger.error("No sessionId in " + message);
if (logger.isErrorEnabled()) {
logger.error("No sessionId in " + message);
}
return;
}
String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
if (subscriptionId == null) {
logger.error("No subscriptionId " + message);
if (logger.isErrorEnabled()) {
logger.error("No subscriptionId " + message);
}
return;
}
@ -109,7 +118,9 @@ public abstract class AbstractSubscriptionRegistry implements SubscriptionRegist
String destination = SimpMessageHeaderAccessor.getDestination(headers);
if (destination == null) {
logger.error("No destination in " + message);
if (logger.isErrorEnabled()) {
logger.error("No destination in " + message);
}
return EMPTY_MAP;
}
@ -117,14 +128,13 @@ public abstract class AbstractSubscriptionRegistry implements SubscriptionRegist
}
protected abstract void addSubscriptionInternal(String sessionId, String subscriptionId,
protected abstract void addSubscriptionInternal(
String sessionId, String subscriptionId, String destination, Message<?> message);
protected abstract void removeSubscriptionInternal(
String sessionId, String subscriptionId, Message<?> message);
protected abstract MultiValueMap<String, String> findSubscriptionsInternal(
String destination, Message<?> message);
protected abstract void removeSubscriptionInternal(String sessionId, String subscriptionId, Message<?> message);
@Override
public abstract void unregisterAllSubscriptions(String sessionId);
protected abstract MultiValueMap<String, String> findSubscriptionsInternal(String destination, Message<?> message);
}

View File

@ -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.
@ -136,8 +136,8 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
@Override
protected void addSubscriptionInternal(String sessionId, String subsId, String destination,
Message<?> message) {
protected void addSubscriptionInternal(
String sessionId, String subsId, String destination, Message<?> message) {
Expression expression = null;
MessageHeaders headers = message.getHeaders();
@ -410,7 +410,7 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
new ConcurrentHashMap<String, Set<Subscription>>(4);
public SessionSubscriptionInfo(String sessionId) {
Assert.notNull(sessionId, "sessionId must not be null");
Assert.notNull(sessionId, "'sessionId' must not be null");
this.sessionId = sessionId;
}
@ -480,13 +480,14 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
}
private static class Subscription {
private static final class Subscription {
private final String id;
private final Expression selectorExpression;
public Subscription(String id, Expression selector) {
Assert.notNull(id, "Subscription id must not be null");
this.id = id;
this.selectorExpression = selector;
}
@ -501,19 +502,12 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
return getId().equals(((Subscription) other).getId());
return (this == other || (other instanceof Subscription && this.id.equals(((Subscription) other).id)));
}
@Override
public int hashCode() {
return getId().hashCode();
return this.id.hashCode();
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 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.
@ -16,7 +16,6 @@
package org.springframework.messaging.tcp;
/**
* A contract to determine the frequency of reconnect attempts after connection failure.
*

View File

@ -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.
@ -103,8 +103,6 @@ public class CorsConfiguration {
/**
Set<String> combined = new LinkedHashSet<String>(source);
return new ArrayList<String>(combined);
* Set the origins to allow, e.g. {@code "http://domain1.com"}.
* <p>The special value {@code "*"} allows all domains.
* <p>By default this is not set.
@ -114,7 +112,7 @@ public class CorsConfiguration {
}
/**
* Return the configured origins to allow, possibly {@code null}.
* Return the configured origins to allow, or {@code null} if none.
* @see #addAllowedOrigin(String)
* @see #setAllowedOrigins(List)
*/
@ -157,7 +155,7 @@ public class CorsConfiguration {
}
/**
* Return the allowed HTTP methods, possibly {@code null} in which case
* Return the allowed HTTP methods, or {@code null} in which case
* only {@code "GET"} and {@code "HEAD"} allowed.
* @see #addAllowedMethod(HttpMethod)
* @see #addAllowedMethod(String)
@ -210,7 +208,7 @@ public class CorsConfiguration {
}
/**
* Return the allowed actual request headers, possibly {@code null}.
* Return the allowed actual request headers, or {@code null} if none.
* @see #addAllowedHeader(String)
* @see #setAllowedHeaders(List)
*/
@ -244,7 +242,7 @@ public class CorsConfiguration {
}
/**
* Return the configured response headers to expose, possibly {@code null}.
* Return the configured response headers to expose, or {@code null} if none.
* @see #addExposedHeader(String)
* @see #setExposedHeaders(List)
*/
@ -275,7 +273,7 @@ public class CorsConfiguration {
}
/**
* Return the configured {@code allowCredentials} flag, possibly {@code null}.
* Return the configured {@code allowCredentials} flag, or {@code null} if none.
* @see #setAllowCredentials(Boolean)
*/
public Boolean getAllowCredentials() {
@ -292,7 +290,7 @@ public class CorsConfiguration {
}
/**
* Return the configured {@code maxAge} value, possibly {@code null}.
* Return the configured {@code maxAge} value, or {@code null} if none.
* @see #setMaxAge(Long)
*/
public Long getMaxAge() {
@ -380,7 +378,7 @@ public class CorsConfiguration {
/**
* Check the origin of the request against the configured allowed origins.
* @param requestOrigin the origin to check
* @return the origin to use for the response, possibly {@code null} which
* @return the origin to use for the response, or {@code null} which
* means the request origin is not allowed
*/
public String checkOrigin(String requestOrigin) {

View File

@ -40,11 +40,11 @@ public class CorsRegistration {
private final CorsConfiguration config;
/**
* Create a new {@link CorsRegistration} that allows all origins, headers, and
* credentials for {@code GET}, {@code HEAD}, and {@code POST} requests with
* max age set to 1800 seconds (30 minutes) for the specified path.
*
* @param pathPattern the path that the CORS configuration should apply to;
* exact path mapping URIs (such as {@code "/admin"}) are supported as well
* as Ant-style path patterns (such as {@code "/admin/**"}).
@ -55,13 +55,14 @@ public class CorsRegistration {
this.config = new CorsConfiguration().applyPermitDefaultValues();
}
/**
* Set the origins to allow, e.g. {@code "http://domain1.com"}.
* <p>The special value {@code "*"} allows all domains.
* <p>By default, all origins are allowed.
*/
public CorsRegistration allowedOrigins(String... origins) {
this.config.setAllowedOrigins(new ArrayList<String>(Arrays.asList(origins)));
this.config.setAllowedOrigins(Arrays.asList(origins));
return this;
}
@ -73,7 +74,7 @@ public class CorsRegistration {
* are allowed.
*/
public CorsRegistration allowedMethods(String... methods) {
this.config.setAllowedMethods(new ArrayList<String>(Arrays.asList(methods)));
this.config.setAllowedMethods(Arrays.asList(methods));
return this;
}
@ -87,7 +88,7 @@ public class CorsRegistration {
* <p>By default all headers are allowed.
*/
public CorsRegistration allowedHeaders(String... headers) {
this.config.setAllowedHeaders(new ArrayList<String>(Arrays.asList(headers)));
this.config.setAllowedHeaders(Arrays.asList(headers));
return this;
}
@ -100,7 +101,7 @@ public class CorsRegistration {
* <p>By default this is not set.
*/
public CorsRegistration exposedHeaders(String... headers) {
this.config.setExposedHeaders(new ArrayList<String>(Arrays.asList(headers)));
this.config.setExposedHeaders(Arrays.asList(headers));
return this;
}

View File

@ -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.
@ -37,14 +37,15 @@ public class CorsRegistry {
/**
* Enable cross origin request handling for the specified path pattern.
*
* Enable cross-origin request handling for the specified path pattern.
* <p>Exact path mapping URIs (such as {@code "/admin"}) are supported as
* well as Ant-style path patterns (such as {@code "/admin/**"}).
*
* <p>By default, all origins, all headers, credentials and {@code GET},
* {@code HEAD}, and {@code POST} methods are allowed, and the max age
* is set to 30 minutes.
* @param pathPattern the path pattern to enable CORS handling for
* @return CorsRegistration the corresponding registration object,
* allowing for further fine-tuning
*/
public CorsRegistration addMapping(String pathPattern) {
CorsRegistration registration = new CorsRegistration(pathPattern);
@ -52,6 +53,10 @@ public class CorsRegistry {
return registration;
}
/**
* Return the registered {@link CorsConfiguration} objects,
* keyed by path pattern.
*/
protected Map<String, CorsConfiguration> getCorsConfigurations() {
Map<String, CorsConfiguration> configs = new LinkedHashMap<String, CorsConfiguration>(this.registrations.size());
for (CorsRegistration registration : this.registrations) {

View File

@ -942,6 +942,8 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
}
/**
* Return the registered {@link CorsConfiguration} objects,
* keyed by path pattern.
* @since 4.2
*/
protected final Map<String, CorsConfiguration> getCorsConfigurations() {