KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > http > handler > ForwardHandler


1 // ========================================================================
2
// $Id: ForwardHandler.java,v 1.16 2005/08/13 00:01:26 gregwilkins Exp $
3
// Copyright 199-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.http.handler;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.mortbay.log.LogFactory;
23 import org.mortbay.http.HttpException;
24 import org.mortbay.http.HttpMessage;
25 import org.mortbay.http.HttpRequest;
26 import org.mortbay.http.HttpResponse;
27 import org.mortbay.http.PathMap;
28 import org.mortbay.util.URI;
29 import org.mortbay.util.UrlEncoded;
30
31
32 /* ------------------------------------------------------------ */
33 /** Forward Request Handler.
34  * Forwards a request to a new URI. Experimental - use with caution.
35  * @version $Revision: 1.16 $
36  * @author Greg Wilkins (gregw)
37  */

38 public class ForwardHandler extends AbstractHttpHandler
39 {
40     private static Log log = LogFactory.getLog(ForwardHandler.class);
41
42     PathMap _forward = new PathMap();
43     String JavaDoc _root;
44     boolean _handleQueries = false;
45     
46     /* ------------------------------------------------------------ */
47     /** Constructor.
48      */

49     public ForwardHandler()
50     {}
51     
52     /* ------------------------------------------------------------ */
53     /** Constructor.
54      * @param rootForward
55      */

56     public ForwardHandler(String JavaDoc rootForward)
57     {
58         _root=rootForward;
59     }
60     
61     /* ------------------------------------------------------------ */
62     /** Add a forward mapping.
63      * @param pathSpecInContext The path to forward from
64      * @param newPath The path to forward to.
65      */

66     public void addForward(String JavaDoc pathSpecInContext,
67                            String JavaDoc newPath)
68     {
69         _forward.put(pathSpecInContext,newPath);
70     }
71     
72     /* ------------------------------------------------------------ */
73     /** Add a forward mapping for root path.
74      * This allows a forward for exactly / which is the default
75      * path in a pathSpec.
76      * @param newPath The path to forward to.
77      */

78     public void setRootForward(String JavaDoc newPath)
79     {
80         _root=newPath;
81     }
82     
83     /* ------------------------------------------------------------ */
84     /** Set the Handler up to cope with forwards to paths that contain query
85      * elements (e.g. "/blah"->"/foo?a=b").
86      * @param b
87      */

88     public void setHandleQueries(boolean b)
89     {
90         _handleQueries = b;
91     }
92     
93     /* ------------------------------------------------------------ */
94     public void handle(String JavaDoc pathInContext,
95                        String JavaDoc pathParams,
96                        HttpRequest request,
97                        HttpResponse response)
98         throws HttpException, IOException JavaDoc
99     {
100         if(log.isTraceEnabled())log.trace("Look for "+pathInContext+" in "+_forward);
101         
102         String JavaDoc newPath=null;
103         String JavaDoc query=null;
104         if (_root!=null && ("/".equals(pathInContext) || pathInContext.startsWith("/;")))
105             newPath=_root;
106         else
107         {
108             Map.Entry JavaDoc entry = _forward.getMatch(pathInContext);
109             if (entry!=null)
110             {
111                 String JavaDoc match = (String JavaDoc)entry.getValue();
112                 if (_handleQueries)
113                 {
114                     int hook = match.indexOf('?');
115                     if (hook != -1){
116                         query = match.substring(hook+1);
117                         match = match.substring(0, hook);
118                     }
119                 }
120                 String JavaDoc info=PathMap.pathInfo((String JavaDoc)entry.getKey(),pathInContext);
121                 if(log.isDebugEnabled())log.debug("Forward: match:\""+ match+ "\" info:"+info+"\" query:"+query);
122                 newPath=info==null?match:(URI.addPaths(match,info));
123             }
124         }
125         
126         if (newPath!=null)
127         {
128             if(log.isDebugEnabled())log.debug("Forward from "+pathInContext+" to "+newPath);
129             
130             int last=request.setState(HttpMessage.__MSG_EDITABLE);
131             String JavaDoc context=getHttpContext().getContextPath();
132             if (context.length()==1)
133                 request.setPath(newPath);
134             else
135                 request.setPath(URI.addPaths(context,newPath));
136             if (_handleQueries && query != null){
137                 // add forwarded to query string to parameters
138
UrlEncoded.decodeTo(query, request.getParameters());
139             }
140             request.setState(last);
141             getHttpContext().getHttpServer().service(request,response);
142             return;
143         }
144     }
145 }
146
Popular Tags