package org.springframework.http.server.reactive;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.DispatcherType;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpLogging;
import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@SuppressWarnings("serial")
public class ServletHttpHandlerAdapter implements Servlet {
private static final Log logger = HttpLogging.forLogName(ServletHttpHandlerAdapter.class);
private static final int DEFAULT_BUFFER_SIZE = 8192;
private static final String WRITE_ERROR_ATTRIBUTE_NAME = ServletHttpHandlerAdapter.class.getName() + ".ERROR";
private final HttpHandler httpHandler;
private int bufferSize = DEFAULT_BUFFER_SIZE;
@Nullable
private String servletPath;
private DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory(false);
public ServletHttpHandlerAdapter(HttpHandler httpHandler) {
Assert.notNull(httpHandler, "HttpHandler must not be null");
this.httpHandler = httpHandler;
}
public void setBufferSize(int bufferSize) {
Assert.isTrue(bufferSize > 0, "Buffer size must be larger than zero");
this.bufferSize = bufferSize;
}
public int getBufferSize() {
return this.bufferSize;
}
@Nullable
public String getServletPath() {
return this.servletPath;
}
public void setDataBufferFactory(DataBufferFactory dataBufferFactory) {
Assert.notNull(dataBufferFactory, "DataBufferFactory must not be null");
this.dataBufferFactory = dataBufferFactory;
}
public DataBufferFactory getDataBufferFactory() {
return this.dataBufferFactory;
}
@Override
public void init(ServletConfig config) {
this.servletPath = getServletPath(config);
}
private String getServletPath(ServletConfig config) {
String name = config.getServletName();
ServletRegistration registration = config.getServletContext().getServletRegistration(name);
if (registration == null) {
throw new IllegalStateException("ServletRegistration not found for Servlet '" + name + "'");
}
Collection<String> mappings = registration.getMappings();
if (mappings.size() == 1) {
String mapping = mappings.iterator().next();
if (mapping.equals("/")) {
return "";
}
if (mapping.endsWith("/*")) {
String path = mapping.substring(0, mapping.length() - 2);
if (!path.isEmpty() && logger.isDebugEnabled()) {
logger.debug("Found servlet mapping prefix '" + path + "' for '" + name + "'");
}
return path;
}
}
throw new IllegalArgumentException("Expected a single Servlet mapping: " +
"either the default Servlet mapping (i.e. '/'), " +
"or a path based mapping (e.g. '/*', '/foo/*'). " +
"Actual mappings: " + mappings + " for Servlet '" + name + "'");
}
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
if (DispatcherType.ASYNC.equals(request.getDispatcherType())) {
Throwable ex = (Throwable) request.getAttribute(WRITE_ERROR_ATTRIBUTE_NAME);
throw new ServletException("Failed to create response content", ex);
}
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(-1);
ServletServerHttpRequest httpRequest;
try {
httpRequest = createRequest(((HttpServletRequest) request), asyncContext);
}
catch (URISyntaxException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to get request URL: " + ex.getMessage());
}
((HttpServletResponse) response).setStatus(400);
asyncContext.complete();
return;
}
ServerHttpResponse httpResponse = createResponse(((HttpServletResponse) response), asyncContext, httpRequest);
if (httpRequest.getMethod() == HttpMethod.HEAD) {
httpResponse = new HttpHeadResponseDecorator(httpResponse);
}
AtomicBoolean isCompleted = new AtomicBoolean();
HandlerResultAsyncListener listener = new HandlerResultAsyncListener(isCompleted, httpRequest);
asyncContext.addListener(listener);
HandlerResultSubscriber subscriber = new HandlerResultSubscriber(asyncContext, isCompleted, httpRequest);
this.httpHandler.handle(httpRequest, httpResponse).subscribe(subscriber);
}
protected ServletServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context)
throws IOException, URISyntaxException {
Assert.notNull(this.servletPath, "Servlet path is not initialized");
return new ServletServerHttpRequest(
request, context, this.servletPath, getDataBufferFactory(), getBufferSize());
}
protected ServletServerHttpResponse createResponse(HttpServletResponse response,
AsyncContext context, ServletServerHttpRequest request) throws IOException {
return new ServletServerHttpResponse(response, context, getDataBufferFactory(), getBufferSize(), request);
}
@Override
public String getServletInfo() {
return "";
}
@Override
@Nullable
public ServletConfig getServletConfig() {
return null;
}
@Override
public void destroy() {
}
private static void runIfAsyncNotComplete(AsyncContext asyncContext, AtomicBoolean isCompleted, Runnable task) {
try {
if (asyncContext.getRequest().isAsyncStarted() && isCompleted.compareAndSet(false, true)) {
task.run();
}
}
catch (IllegalStateException ex) {
}
}
private static class HandlerResultAsyncListener implements AsyncListener {
private final AtomicBoolean isCompleted;
private final String logPrefix;
public HandlerResultAsyncListener(AtomicBoolean isCompleted, ServletServerHttpRequest httpRequest) {
this.isCompleted = isCompleted;
this.logPrefix = httpRequest.getLogPrefix();
}
@Override
public void onTimeout(AsyncEvent event) {
logger.debug(this.logPrefix + "Timeout notification");
AsyncContext context = event.getAsyncContext();
runIfAsyncNotComplete(context, this.isCompleted, context::complete);
}
@Override
public void onError(AsyncEvent event) {
Throwable ex = event.getThrowable();
logger.debug(this.logPrefix + "Error notification: " + (ex != null ? ex : "<no Throwable>"));
AsyncContext context = event.getAsyncContext();
runIfAsyncNotComplete(context, this.isCompleted, context::complete);
}
@Override
public void onStartAsync(AsyncEvent event) {
}
@Override
public void onComplete(AsyncEvent event) {
}
}
private class HandlerResultSubscriber implements Subscriber<Void> {
private final AsyncContext asyncContext;
private final AtomicBoolean isCompleted;
private final String logPrefix;
public HandlerResultSubscriber(
AsyncContext asyncContext, AtomicBoolean isCompleted, ServletServerHttpRequest httpRequest) {
this.asyncContext = asyncContext;
this.isCompleted = isCompleted;
this.logPrefix = httpRequest.getLogPrefix();
}
@Override
public void onSubscribe(Subscription subscription) {
subscription.request(Long.MAX_VALUE);
}
@Override
public void onNext(Void aVoid) {
}
@Override
public void onError(Throwable ex) {
logger.trace(this.logPrefix + "Failed to complete: " + ex.getMessage());
runIfAsyncNotComplete(this.asyncContext, this.isCompleted, () -> {
if (this.asyncContext.getResponse().isCommitted()) {
logger.trace(this.logPrefix + "Dispatch to container, to raise the error on servlet thread");
this.asyncContext.getRequest().setAttribute(WRITE_ERROR_ATTRIBUTE_NAME, ex);
this.asyncContext.dispatch();
}
else {
try {
logger.trace(this.logPrefix + "Setting ServletResponse status to 500 Server Error");
this.asyncContext.getResponse().resetBuffer();
((HttpServletResponse) this.asyncContext.getResponse()).setStatus(500);
}
finally {
this.asyncContext.complete();
}
}
});
}
@Override
public void onComplete() {
logger.trace(this.logPrefix + "Handling completed");
runIfAsyncNotComplete(this.asyncContext, this.isCompleted, this.asyncContext::complete);
}
}
}