KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > servlet > GenericForwardServlet


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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  */

17 package org.apache.geronimo.console.servlet;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.geronimo.console.gbean.ContextForward;
22 import org.apache.geronimo.gbean.AbstractName;
23 import org.apache.geronimo.gbean.AbstractNameQuery;
24 import org.apache.geronimo.kernel.Kernel;
25 import org.apache.geronimo.kernel.KernelRegistry;
26 import org.apache.geronimo.kernel.lifecycle.LifecycleAdapter;
27 import org.apache.geronimo.kernel.lifecycle.LifecycleListener;
28
29 import javax.servlet.RequestDispatcher JavaDoc;
30 import javax.servlet.ServletConfig JavaDoc;
31 import javax.servlet.ServletContext JavaDoc;
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServlet JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Set JavaDoc;
41
42 /**
43  * Servlet that forwards GET and POST requests to a servlet in an alternate
44  * context. The servlet path and alternate context are defined in GBeans of
45  * type ContextForward, and this one servlet handles the forwarding for all
46  * those different paths.
47  *
48  * NOTE: This does not work for DWR, because it changes the request path info
49  * while forwarding, and DWR requires the exact initial request info in order
50  * to construct URLs in the data that it returns. It should work to forward
51  * to most typical servlets, JSPs, and static content.
52  */

53 public class GenericForwardServlet extends HttpServlet JavaDoc {
54     private final static Log log = LogFactory.getLog(GenericForwardServlet.class);
55     private Map JavaDoc forwards = new HashMap JavaDoc(); // Maps a prefix String to ForwardData
56
private Kernel kernel;
57     private LifecycleListener listener;
58
59     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
60         super.init(config);
61
62         kernel = KernelRegistry.getSingleKernel();
63         AbstractNameQuery query = new AbstractNameQuery(ContextForward.class.getName());
64         Set JavaDoc set = kernel.listGBeans(query);
65         for (Iterator JavaDoc it = set.iterator(); it.hasNext();) {
66             AbstractName name = (AbstractName) it.next();
67             addGBean(name);
68         }
69         kernel.getLifecycleMonitor().addLifecycleListener(listener = new LifecycleAdapter() {
70             public void running(AbstractName abstractName) {
71                 addGBean(abstractName);
72             }
73
74             public void stopping(AbstractName abstractName) {
75                 removeGBean(abstractName);
76             }
77
78             public void stopped(AbstractName abstractName) {
79                 removeGBean(abstractName);
80             }
81
82             public void failed(AbstractName abstractName) {
83                 removeGBean(abstractName);
84             }
85
86             public void unloaded(AbstractName abstractName) {
87                 removeGBean(abstractName);
88             }
89         }, query);
90     }
91
92     public void destroy() {
93         if(listener != null) {
94             kernel.getLifecycleMonitor().removeLifecycleListener(listener);
95             listener = null;
96         }
97     }
98
99     private void addGBean(AbstractName name) {
100         ContextForward forward = (ContextForward) kernel.getProxyManager().createProxy(name, ContextForward.class);
101         forwards.put(forward.getPortalPathPrefix(), new ForwardData(getServletContext().getContext(forward.getPortletContextPath()),
102                                                                     forward.getPortletServletPath(), name));
103     }
104
105     private void removeGBean(AbstractName name) {
106         for (Iterator JavaDoc it = forwards.entrySet().iterator(); it.hasNext();) {
107             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
108             ForwardData data = (ForwardData) entry.getValue();
109             if(data.getGbean().equals(name)) {
110                 it.remove();
111                 break;
112             }
113         }
114     }
115
116     public void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
117             throws ServletException JavaDoc, IOException JavaDoc {
118         doPost(req, resp);
119     }
120
121     public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
122             throws ServletException JavaDoc, IOException JavaDoc {
123         String JavaDoc path = req.getPathInfo();
124         if(path == null) {
125             log.error("Unable to forward request; no path information provided. Path is used to identify where to forward to.");
126             return;
127         }
128         ForwardData forward = null;
129         for (Iterator JavaDoc it = forwards.keySet().iterator(); it.hasNext();) {
130             String JavaDoc prefix = (String JavaDoc) it.next();
131             if(path.startsWith(prefix)) {
132                 forward = (ForwardData) forwards.get(prefix);
133                 path = path.substring(prefix.length());
134             }
135         }
136         if(forward == null) {
137             log.error("Unable to forward URL "+path+"; does not match any known ContextForward definitions.");
138             return;
139         }
140         if(!path.equals("") && !path.startsWith("/")) path = "/"+path;
141         String JavaDoc queryString = req.getQueryString();
142         if (queryString != null) {
143             path += "?" + queryString;
144         }
145         path = forward.getServletPath()+path;
146         RequestDispatcher JavaDoc dispatcher = forward.getForwardContext().getRequestDispatcher(path);
147         dispatcher.forward(req, resp);
148     }
149
150     private static class ForwardData {
151         private ServletContext JavaDoc forwardContext;
152         private String JavaDoc servletPath;
153         private AbstractName gbean;
154
155         public ForwardData(ServletContext JavaDoc forwardContext, String JavaDoc servletPath, AbstractName gbean) {
156             this.forwardContext = forwardContext;
157             this.servletPath = servletPath;
158             this.gbean = gbean;
159         }
160
161         public ServletContext JavaDoc getForwardContext() {
162             return forwardContext;
163         }
164
165         public String JavaDoc getServletPath() {
166             return servletPath;
167         }
168
169         public AbstractName getGbean() {
170             return gbean;
171         }
172     }
173 }
174
Popular Tags