Polishing
This commit is contained in:
parent
80a0cf71f4
commit
e5c1deea63
|
@ -101,6 +101,7 @@ public class MockMultipartFile implements MultipartFile {
|
||||||
this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
|
this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return this.name;
|
return this.name;
|
||||||
|
|
|
@ -21,7 +21,6 @@ import java.util.Enumeration;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
|
|
|
@ -121,11 +121,12 @@ public class ServletHttpHandlerAdapter implements Servlet {
|
||||||
this.servletPath = getServletPath(config);
|
this.servletPath = getServletPath(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private String getServletPath(ServletConfig config) {
|
private String getServletPath(ServletConfig config) {
|
||||||
String name = config.getServletName();
|
String name = config.getServletName();
|
||||||
ServletRegistration registration = config.getServletContext().getServletRegistration(name);
|
ServletRegistration registration = config.getServletContext().getServletRegistration(name);
|
||||||
Assert.notNull(registration, "ServletRegistration not found for Servlet '" + name + "'.");
|
if (registration == null) {
|
||||||
|
throw new IllegalStateException("ServletRegistration not found for Servlet '" + name + "'");
|
||||||
|
}
|
||||||
|
|
||||||
Collection<String> mappings = registration.getMappings();
|
Collection<String> mappings = registration.getMappings();
|
||||||
if (mappings.size() == 1) {
|
if (mappings.size() == 1) {
|
||||||
|
@ -136,16 +137,16 @@ public class ServletHttpHandlerAdapter implements Servlet {
|
||||||
if (mapping.endsWith("/*")) {
|
if (mapping.endsWith("/*")) {
|
||||||
String path = mapping.substring(0, mapping.length() - 2);
|
String path = mapping.substring(0, mapping.length() - 2);
|
||||||
if (!path.isEmpty()) {
|
if (!path.isEmpty()) {
|
||||||
logger.info("Found Servlet mapping '" + path + "' for Servlet '" + name + "'.");
|
logger.info("Found Servlet mapping '" + path + "' for Servlet '" + name + "'");
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IllegalArgumentException("Expected a single Servlet mapping -- " +
|
throw new IllegalArgumentException("Expected a single Servlet mapping: " +
|
||||||
"either the default Servlet mapping (i.e. '/'), " +
|
"either the default Servlet mapping (i.e. '/'), " +
|
||||||
"or a path based mapping (e.g. '/*', '/foo/*'). " +
|
"or a path based mapping (e.g. '/*', '/foo/*'). " +
|
||||||
"Actual mappings: " + mappings + " for Servlet '" + name + "'.");
|
"Actual mappings: " + mappings + " for Servlet '" + name + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -168,18 +169,13 @@ public class ServletHttpHandlerAdapter implements Servlet {
|
||||||
this.httpHandler.handle(httpRequest, httpResponse).subscribe(subscriber);
|
this.httpHandler.handle(httpRequest, httpResponse).subscribe(subscriber);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context)
|
protected ServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context) throws IOException {
|
||||||
throws IOException {
|
Assert.notNull(this.servletPath, "Servlet path is not initialized");
|
||||||
|
|
||||||
Assert.notNull(this.servletPath, "servletPath is not initialized.");
|
|
||||||
|
|
||||||
return new ServletServerHttpRequest(
|
return new ServletServerHttpRequest(
|
||||||
request, context, this.servletPath, getDataBufferFactory(), getBufferSize());
|
request, context, this.servletPath, getDataBufferFactory(), getBufferSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ServerHttpResponse createResponse(HttpServletResponse response, AsyncContext context)
|
protected ServerHttpResponse createResponse(HttpServletResponse response, AsyncContext context) throws IOException {
|
||||||
throws IOException {
|
|
||||||
|
|
||||||
return new ServletServerHttpResponse(response, context, getDataBufferFactory(), getBufferSize());
|
return new ServletServerHttpResponse(response, context, getDataBufferFactory(), getBufferSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,25 +215,25 @@ public class ServletHttpHandlerAdapter implements Servlet {
|
||||||
private final static AsyncListener ERROR_LISTENER = new AsyncListener() {
|
private final static AsyncListener ERROR_LISTENER = new AsyncListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTimeout(AsyncEvent event) throws IOException {
|
public void onTimeout(AsyncEvent event) {
|
||||||
AsyncContext context = event.getAsyncContext();
|
AsyncContext context = event.getAsyncContext();
|
||||||
runIfAsyncNotComplete(context, context::complete);
|
runIfAsyncNotComplete(context, context::complete);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(AsyncEvent event) throws IOException {
|
public void onError(AsyncEvent event) {
|
||||||
AsyncContext context = event.getAsyncContext();
|
AsyncContext context = event.getAsyncContext();
|
||||||
runIfAsyncNotComplete(context, context::complete);
|
runIfAsyncNotComplete(context, context::complete);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStartAsync(AsyncEvent event) throws IOException {
|
public void onStartAsync(AsyncEvent event) {
|
||||||
// No-op
|
// no-op
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onComplete(AsyncEvent event) throws IOException {
|
public void onComplete(AsyncEvent event) {
|
||||||
// No-op
|
// no-op
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -246,7 +242,7 @@ public class ServletHttpHandlerAdapter implements Servlet {
|
||||||
|
|
||||||
private final AsyncContext asyncContext;
|
private final AsyncContext asyncContext;
|
||||||
|
|
||||||
HandlerResultSubscriber(AsyncContext asyncContext) {
|
public HandlerResultSubscriber(AsyncContext asyncContext) {
|
||||||
this.asyncContext = asyncContext;
|
this.asyncContext = asyncContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,7 +253,7 @@ public class ServletHttpHandlerAdapter implements Servlet {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNext(Void aVoid) {
|
public void onNext(Void aVoid) {
|
||||||
// no op
|
// no-op
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -40,7 +40,9 @@ class HeaderValueHolder {
|
||||||
|
|
||||||
public void setValue(Object value) {
|
public void setValue(Object value) {
|
||||||
this.values.clear();
|
this.values.clear();
|
||||||
this.values.add(value);
|
if (value != null) {
|
||||||
|
this.values.add(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addValue(Object value) {
|
public void addValue(Object value) {
|
||||||
|
|
|
@ -21,8 +21,6 @@ import javax.servlet.jsp.PageContext;
|
||||||
|
|
||||||
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
|
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mock implementation of the JSP 2.0 {@link javax.servlet.jsp.el.ExpressionEvaluator}
|
* Mock implementation of the JSP 2.0 {@link javax.servlet.jsp.el.ExpressionEvaluator}
|
||||||
* interface, delegating to the Apache JSTL ExpressionEvaluatorManager.
|
* interface, delegating to the Apache JSTL ExpressionEvaluatorManager.
|
||||||
|
@ -57,9 +55,7 @@ public class MockExpressionEvaluator extends javax.servlet.jsp.el.ExpressionEval
|
||||||
|
|
||||||
return new javax.servlet.jsp.el.Expression() {
|
return new javax.servlet.jsp.el.Expression() {
|
||||||
@Override
|
@Override
|
||||||
public Object evaluate(javax.servlet.jsp.el.VariableResolver variableResolver)
|
public Object evaluate(javax.servlet.jsp.el.VariableResolver variableResolver) throws javax.servlet.jsp.el.ELException {
|
||||||
throws javax.servlet.jsp.el.ELException {
|
|
||||||
|
|
||||||
return doEvaluate(expression, expectedType, functionMapper);
|
return doEvaluate(expression, expectedType, functionMapper);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -67,26 +63,21 @@ public class MockExpressionEvaluator extends javax.servlet.jsp.el.ExpressionEval
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
public Object evaluate(String expression, Class expectedType,
|
public Object evaluate(String expression, Class expectedType, javax.servlet.jsp.el.VariableResolver variableResolver,
|
||||||
javax.servlet.jsp.el.VariableResolver variableResolver,
|
|
||||||
javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException {
|
javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException {
|
||||||
|
|
||||||
Assert.isNull(variableResolver, "Custom VariableResolver not supported");
|
|
||||||
return doEvaluate(expression, expectedType, functionMapper);
|
return doEvaluate(expression, expectedType, functionMapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
protected Object doEvaluate(String expression, Class expectedType,
|
protected Object doEvaluate(String expression, Class expectedType, javax.servlet.jsp.el.FunctionMapper functionMapper)
|
||||||
javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException {
|
throws javax.servlet.jsp.el.ELException {
|
||||||
|
|
||||||
Assert.isNull(functionMapper, "Custom FunctionMapper not supported");
|
|
||||||
try {
|
try {
|
||||||
return ExpressionEvaluatorManager.evaluate(
|
return ExpressionEvaluatorManager.evaluate("JSP EL expression", expression, expectedType, this.pageContext);
|
||||||
"JSP EL expression", expression, expectedType, this.pageContext);
|
|
||||||
}
|
}
|
||||||
catch (JspException ex) {
|
catch (JspException ex) {
|
||||||
throw new javax.servlet.jsp.el.ELException(
|
throw new javax.servlet.jsp.el.ELException("Parsing of JSP EL expression \"" + expression + "\" failed", ex);
|
||||||
"Parsing of JSP EL expression \"" + expression + "\" failed", ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,6 @@ import org.springframework.util.ObjectUtils;
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
*
|
|
||||||
* @since 2.0.3
|
* @since 2.0.3
|
||||||
* @see MockFilterConfig
|
* @see MockFilterConfig
|
||||||
* @see PassThroughFilterChain
|
* @see PassThroughFilterChain
|
||||||
|
@ -70,7 +69,6 @@ public class MockFilterChain implements FilterChain {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a FilterChain with a Servlet.
|
* Create a FilterChain with a Servlet.
|
||||||
*
|
|
||||||
* @param servlet the Servlet to invoke
|
* @param servlet the Servlet to invoke
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
|
@ -80,7 +78,6 @@ public class MockFilterChain implements FilterChain {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a {@code FilterChain} with Filter's and a Servlet.
|
* Create a {@code FilterChain} with Filter's and a Servlet.
|
||||||
*
|
|
||||||
* @param servlet the {@link Servlet} to invoke in this {@link FilterChain}
|
* @param servlet the {@link Servlet} to invoke in this {@link FilterChain}
|
||||||
* @param filters the {@link Filter}'s to invoke in this {@link FilterChain}
|
* @param filters the {@link Filter}'s to invoke in this {@link FilterChain}
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
|
@ -96,6 +93,7 @@ public class MockFilterChain implements FilterChain {
|
||||||
return Arrays.asList(allFilters);
|
return Arrays.asList(allFilters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the request that {@link #doFilter} has been called with.
|
* Return the request that {@link #doFilter} has been called with.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
|
@ -267,7 +267,7 @@ public class MockPageContext extends PageContext {
|
||||||
return this.request.getAttributeNames();
|
return this.request.getAttributeNames();
|
||||||
case SESSION_SCOPE:
|
case SESSION_SCOPE:
|
||||||
HttpSession session = this.request.getSession(false);
|
HttpSession session = this.request.getSession(false);
|
||||||
return (session != null ? session.getAttributeNames() : null);
|
return (session != null ? session.getAttributeNames() : Collections.emptyEnumeration());
|
||||||
case APPLICATION_SCOPE:
|
case APPLICATION_SCOPE:
|
||||||
return this.servletContext.getAttributeNames();
|
return this.servletContext.getAttributeNames();
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -141,6 +141,7 @@ public class MockServletContext implements ServletContext {
|
||||||
|
|
||||||
private final Map<String, MediaType> mimeTypes = new LinkedHashMap<>();
|
private final Map<String, MediaType> mimeTypes = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@code MockServletContext}, using no base path and a
|
* Create a new {@code MockServletContext}, using no base path and a
|
||||||
* {@link DefaultResourceLoader} (i.e. the classpath root as WAR root).
|
* {@link DefaultResourceLoader} (i.e. the classpath root as WAR root).
|
||||||
|
@ -179,7 +180,7 @@ public class MockServletContext implements ServletContext {
|
||||||
*/
|
*/
|
||||||
public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader) {
|
public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader) {
|
||||||
this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader());
|
this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader());
|
||||||
this.resourceBasePath = (resourceBasePath != null ? resourceBasePath : "");
|
this.resourceBasePath = resourceBasePath;
|
||||||
|
|
||||||
// Use JVM temp dir as ServletContext temp dir.
|
// Use JVM temp dir as ServletContext temp dir.
|
||||||
String tempDir = System.getProperty(TEMP_DIR_SYSTEM_PROPERTY);
|
String tempDir = System.getProperty(TEMP_DIR_SYSTEM_PROPERTY);
|
||||||
|
@ -204,7 +205,7 @@ public class MockServletContext implements ServletContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setContextPath(String contextPath) {
|
public void setContextPath(String contextPath) {
|
||||||
this.contextPath = (contextPath != null ? contextPath : "");
|
this.contextPath = contextPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -28,7 +28,7 @@ import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of the {@link javax.servlet.FilterConfig} interface which
|
* Implementation of the {@link javax.servlet.FilterConfig} interface which
|
||||||
* simply passes the call through to a given Filter/FilterChain combo
|
* simply passes the call through to a given Filter/FilterChain combination
|
||||||
* (indicating the next Filter in the chain along with the FilterChain that it is
|
* (indicating the next Filter in the chain along with the FilterChain that it is
|
||||||
* supposed to work on) or to a given Servlet (indicating the end of the chain).
|
* supposed to work on) or to a given Servlet (indicating the end of the chain).
|
||||||
*
|
*
|
||||||
|
@ -79,6 +79,7 @@ public class PassThroughFilterChain implements FilterChain {
|
||||||
this.filter.doFilter(request, response, this.nextFilterChain);
|
this.filter.doFilter(request, response, this.nextFilterChain);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
Assert.state(this.servlet != null, "Neither a Filter not a Servlet set");
|
||||||
this.servlet.service(request, response);
|
this.servlet.service(request, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue