01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.geronimo.console.servlet;
17:
18: import java.io.IOException;
19:
20: import javax.servlet.RequestDispatcher;
21: import javax.servlet.ServletConfig;
22: import javax.servlet.ServletContext;
23: import javax.servlet.ServletException;
24: import javax.servlet.UnavailableException;
25: import javax.servlet.http.HttpServlet;
26: import javax.servlet.http.HttpServletRequest;
27: import javax.servlet.http.HttpServletResponse;
28:
29: /**
30: * Servlet that forwards GET and POST requests to a servlet
31: * in an alternate context. The servlet path and alternate
32: * context are passed in as configuration parameters (e.g.
33: * via <config-param> elements in the web.xml).
34: */
35: public class ContextForwardServlet extends HttpServlet {
36:
37: // name of the configuration parameter containing the context path
38: public static final String CONTEXT_PATH = "context-path";
39: // name of the configuration parameter containing the servlet path
40: public static final String SERVLET_PATH = "servlet-path";
41:
42: private ServletContext forwardContext;
43: private String servletPath;
44:
45: public void init(ServletConfig config) throws ServletException {
46: super .init(config);
47: String contextPath = config.getInitParameter(CONTEXT_PATH);
48: servletPath = config.getInitParameter(SERVLET_PATH);
49: if (contextPath == null || servletPath == null) {
50: throw new UnavailableException(
51: "context-path and servlet-path "
52: + "must be provided as configuration parameters");
53: }
54: forwardContext = getServletContext().getContext(contextPath);
55: }
56:
57: public void doGet(HttpServletRequest req, HttpServletResponse resp)
58: throws ServletException, IOException {
59: doPost(req, resp);
60: }
61:
62: public void doPost(HttpServletRequest req, HttpServletResponse resp)
63: throws ServletException, IOException {
64: String dispatchURI = servletPath
65: + (req.getPathInfo() == null ? "" : req.getPathInfo());
66: String queryString = req.getQueryString();
67: if (queryString != null) {
68: dispatchURI += "?" + queryString;
69: }
70: RequestDispatcher dispatcher = forwardContext
71: .getRequestDispatcher(dispatchURI);
72: dispatcher.forward(req, resp);
73: }
74: }
|