001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.context.support;
018
019import java.io.IOException;
020
021import javax.servlet.ServletException;
022import javax.servlet.http.HttpServlet;
023import javax.servlet.http.HttpServletRequest;
024import javax.servlet.http.HttpServletResponse;
025
026import org.springframework.context.i18n.LocaleContextHolder;
027import org.springframework.lang.Nullable;
028import org.springframework.util.Assert;
029import org.springframework.util.StringUtils;
030import org.springframework.web.HttpRequestHandler;
031import org.springframework.web.HttpRequestMethodNotSupportedException;
032import org.springframework.web.context.WebApplicationContext;
033
034/**
035 * Simple HttpServlet that delegates to an {@link HttpRequestHandler} bean defined
036 * in Spring's root web application context. The target bean name must match the
037 * HttpRequestHandlerServlet servlet-name as defined in {@code web.xml}.
038 *
039 * <p>This can for example be used to expose a single Spring remote exporter,
040 * such as {@link org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter}
041 * or {@link org.springframework.remoting.caucho.HessianServiceExporter},
042 * per HttpRequestHandlerServlet definition. This is a minimal alternative
043 * to defining remote exporters as beans in a DispatcherServlet context
044 * (with advanced mapping and interception facilities being available there).
045 *
046 * @author Juergen Hoeller
047 * @since 2.0
048 * @see org.springframework.web.HttpRequestHandler
049 * @see org.springframework.web.servlet.DispatcherServlet
050 */
051@SuppressWarnings("serial")
052public class HttpRequestHandlerServlet extends HttpServlet {
053
054        @Nullable
055        private HttpRequestHandler target;
056
057
058        @Override
059        public void init() throws ServletException {
060                WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
061                this.target = wac.getBean(getServletName(), HttpRequestHandler.class);
062        }
063
064
065        @Override
066        protected void service(HttpServletRequest request, HttpServletResponse response)
067                        throws ServletException, IOException {
068
069                Assert.state(this.target != null, "No HttpRequestHandler available");
070
071                LocaleContextHolder.setLocale(request.getLocale());
072                try {
073                        this.target.handleRequest(request, response);
074                }
075                catch (HttpRequestMethodNotSupportedException ex) {
076                        String[] supportedMethods = ex.getSupportedMethods();
077                        if (supportedMethods != null) {
078                                response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
079                        }
080                        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());
081                }
082                finally {
083                        LocaleContextHolder.resetLocaleContext();
084                }
085        }
086
087}