1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18
19 package io.undertow.servlet.handlers;
20
21 import java.io.IOException;
22
23 import javax.servlet.Servlet;
24 import javax.servlet.ServletException;
25 import javax.servlet.ServletRequest;
26 import javax.servlet.ServletResponse;
27 import javax.servlet.UnavailableException;
28
29 import io.undertow.server.HttpServerExchange;
30 import io.undertow.server.HttpHandler;
31 import io.undertow.servlet.UndertowServletLogger;
32 import io.undertow.servlet.api.InstanceHandle;
33 import io.undertow.servlet.core.ManagedServlet;
34 import io.undertow.util.StatusCodes;
35
36 /**
37  * The handler that is responsible for invoking the servlet
38  * <p>
39  * TODO: do we want to move lifecycle considerations out of this handler?
40  *
41  * @author Stuart Douglas
42  */

43 public class ServletHandler implements HttpHandler {
44
45     private final ManagedServlet managedServlet;
46
47
48     public ServletHandler(final ManagedServlet managedServlet) {
49         this.managedServlet = managedServlet;
50     }
51
52     @Override
53     public void handleRequest(final HttpServerExchange exchange) throws IOException, ServletException {
54         if (managedServlet.isPermanentlyUnavailable()) {
55             UndertowServletLogger.REQUEST_LOGGER.debugf("Returning 404 for servlet %s due to permanent unavailability", managedServlet.getServletInfo().getName());
56             exchange.setStatusCode(StatusCodes.NOT_FOUND);
57             return;
58         }
59
60         if (managedServlet.isTemporarilyUnavailable()) {
61             UndertowServletLogger.REQUEST_LOGGER.debugf("Returning 503 for servlet %s due to temporary unavailability", managedServlet.getServletInfo().getName());
62             exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
63             return;
64         }
65         final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
66         if(!managedServlet.getServletInfo().isAsyncSupported()) {
67             servletRequestContext.setAsyncSupported(false);
68         }
69         ServletRequest request = servletRequestContext.getServletRequest();
70         ServletResponse response = servletRequestContext.getServletResponse();
71         InstanceHandle<? extends Servlet> servlet = null;
72         try {
73             servlet = managedServlet.getServlet();
74             servlet.getInstance().service(request, response);
75
76             //according to the spec we have to call AsyncContext.complete() at this point
77             //straight after the service method
78             //not super sure about this, surely it would make more sense to do this when the request has returned to the container, however the spec is quite clear wording wise
79             //todo: should we actually enable this? Apparently other containers do not do it
80             //if(!request.isAsyncStarted()) {
81             //    AsyncContextImpl existingAsyncContext = servletRequestContext.getOriginalRequest().getAsyncContextInternal();
82             //    if (existingAsyncContext != null) {
83             //        existingAsyncContext.complete();
84             //    }
85             //}
86         } catch (UnavailableException e) {
87             managedServlet.handleUnavailableException(e);
88             if (e.isPermanent()) {
89                 exchange.setStatusCode(StatusCodes.NOT_FOUND);
90             } else {
91                 exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
92             }
93         } finally {
94             if(servlet != null) {
95                 servlet.release();
96             }
97         }
98     }
99
100     public ManagedServlet getManagedServlet() {
101         return managedServlet;
102     }
103 }
104