001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.console.servlet;
019    
020    import java.io.IOException;
021    
022    import javax.servlet.RequestDispatcher;
023    import javax.servlet.ServletConfig;
024    import javax.servlet.ServletContext;
025    import javax.servlet.ServletException;
026    import javax.servlet.UnavailableException;
027    import javax.servlet.http.HttpServlet;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    /**
032     * Servlet that forwards GET and POST requests to a servlet
033     * in an alternate context. The servlet path and alternate
034     * context are passed in as configuration parameters (e.g.
035     * via <config-param> elements in the web.xml).
036     */
037    public class ContextForwardServlet extends HttpServlet {
038    
039        // name of the configuration parameter containing the context path
040        public static final String CONTEXT_PATH = "context-path";
041        // name of the configuration parameter containing the servlet path
042        public static final String SERVLET_PATH = "servlet-path";
043    
044        private ServletContext forwardContext;
045        private String servletPath;
046    
047        public void init(ServletConfig config) throws ServletException {
048            super.init(config);
049            String contextPath = config.getInitParameter(CONTEXT_PATH);
050            servletPath = config.getInitParameter(SERVLET_PATH);
051            if (contextPath == null || servletPath == null) {
052                throw new UnavailableException("context-path and servlet-path " +
053                        "must be provided as configuration parameters");
054            }
055            forwardContext = getServletContext().getContext(contextPath);
056        }
057    
058        public void doGet(HttpServletRequest req, HttpServletResponse resp)
059                throws ServletException, IOException {
060            doPost(req, resp);
061        }
062    
063        public void doPost(HttpServletRequest req, HttpServletResponse resp)
064                throws ServletException, IOException {
065            String dispatchURI = servletPath + (req.getPathInfo() == null ? "" : req.getPathInfo());
066            String queryString = req.getQueryString();
067            if (queryString != null) {
068                dispatchURI += "?" + queryString;
069            }
070            RequestDispatcher dispatcher = forwardContext.getRequestDispatcher(dispatchURI);
071            dispatcher.forward(req, resp);
072        }
073    }