001/*
002 * Copyright 2002-2012 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;
020import javax.servlet.ServletException;
021import javax.servlet.http.HttpServlet;
022import javax.servlet.http.HttpServletRequest;
023import javax.servlet.http.HttpServletResponse;
024
025import org.springframework.context.i18n.LocaleContextHolder;
026import org.springframework.util.StringUtils;
027import org.springframework.web.HttpRequestHandler;
028import org.springframework.web.HttpRequestMethodNotSupportedException;
029import org.springframework.web.context.WebApplicationContext;
030
031/**
032 * Simple HttpServlet that delegates to an {@link HttpRequestHandler} bean defined
033 * in Spring's root web application context. The target bean name must match the
034 * HttpRequestHandlerServlet servlet-name as defined in {@code web.xml}.
035 *
036 * <p>This can for example be used to expose a single Spring remote exporter,
037 * such as {@link org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter}
038 * or {@link org.springframework.remoting.caucho.HessianServiceExporter},
039 * per HttpRequestHandlerServlet definition. This is a minimal alternative
040 * to defining remote exporters as beans in a DispatcherServlet context
041 * (with advanced mapping and interception facilities being available there).
042 *
043 * @author Juergen Hoeller
044 * @since 2.0
045 * @see org.springframework.web.HttpRequestHandler
046 * @see org.springframework.web.servlet.DispatcherServlet
047 */
048@SuppressWarnings("serial")
049public class HttpRequestHandlerServlet extends HttpServlet {
050
051        private HttpRequestHandler target;
052
053
054        @Override
055        public void init() throws ServletException {
056                WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
057                this.target = wac.getBean(getServletName(), HttpRequestHandler.class);
058        }
059
060
061        @Override
062        protected void service(HttpServletRequest request, HttpServletResponse response)
063                        throws ServletException, IOException {
064
065                LocaleContextHolder.setLocale(request.getLocale());
066                try {
067                        this.target.handleRequest(request, response);
068                }
069                catch (HttpRequestMethodNotSupportedException ex) {
070                        String[] supportedMethods = ex.getSupportedMethods();
071                        if (supportedMethods != null) {
072                                response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
073                        }
074                        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());
075                }
076                finally {
077                        LocaleContextHolder.resetLocaleContext();
078                }
079        }
080
081}