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 String servletPath;
045 private String contextPath;
046
047 public void init(ServletConfig config) throws ServletException {
048 super.init(config);
049 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 }
056
057 public void doGet(HttpServletRequest req, HttpServletResponse resp)
058 throws ServletException, IOException {
059 doPost(req, resp);
060 }
061
062 public void doPost(HttpServletRequest req, HttpServletResponse resp)
063 throws ServletException, IOException {
064 String dispatchURI = servletPath + (req.getPathInfo() == null ? "" : req.getPathInfo());
065 String queryString = req.getQueryString();
066 if (queryString != null) {
067 dispatchURI += "?" + queryString;
068 }
069 ServletContext forwardContext = getServletContext().getContext(contextPath);
070 RequestDispatcher dispatcher = forwardContext.getRequestDispatcher(dispatchURI);
071 dispatcher.forward(req, resp);
072 }
073 }