KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > RequestForwardingHttpMethod


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.util;
17
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Enumeration JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23
24 import org.apache.commons.httpclient.Cookie;
25 import org.apache.commons.httpclient.Header;
26 import org.apache.commons.httpclient.HttpURL;
27 import org.apache.commons.httpclient.cookie.CookieSpecBase;
28 import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
29
30 /**
31  * This is a generic and externally configurable method, to forward any Request
32  * to a server.
33  *
34  * @author <a HREF="mailto:gianugo@apache.org">Gianugo Rabellino</a>
35  * @version $Id: RequestForwardingHttpMethod.java 124700 2005-01-09 02:17:17Z antonio $
36  */

37 public class RequestForwardingHttpMethod extends EntityEnclosingMethod {
38     
39     /** The request to be forwarded */
40     HttpServletRequest JavaDoc originalRequest;
41     
42     /** The HTTPUrl to forward this request to */
43     HttpURL destination;
44     
45     public RequestForwardingHttpMethod(HttpServletRequest JavaDoc req, HttpURL destination)
46         throws IOException JavaDoc {
47             this.originalRequest = req;
48             this.destination = destination;
49             this.setFollowRedirects(true);
50             this.setPath(req.getRequestURI());
51             cloneHeaders();
52             cloneCookies();
53             setRequestBody(originalRequest.getInputStream());
54     }
55     
56
57     /**
58      * Dinamically get the method.
59      *
60      * @see org.apache.commons.httpclient.HttpMethod#getName()
61      */

62     public String JavaDoc getName() {
63         return originalRequest.getMethod();
64     }
65
66     /**
67      * Clone the original request headers.
68      *
69      */

70     private void cloneHeaders() {
71         Enumeration JavaDoc e = originalRequest.getHeaderNames();
72         while (e.hasMoreElements()) {
73             String JavaDoc header = (String JavaDoc) e.nextElement();
74             String JavaDoc headerValue = originalRequest.getHeader(header);
75             this.addRequestHeader(header, headerValue);
76         }
77     }
78     
79     /**
80      * Clone cookies, if any.
81      *
82      */

83     private void cloneCookies() {
84         ArrayList JavaDoc newCookiesList = new ArrayList JavaDoc();
85         javax.servlet.http.Cookie JavaDoc[] cookies = originalRequest.getCookies();
86         if (cookies != null) {
87             for (int i = 0; i < cookies.length; i++) {
88                 String JavaDoc domain = cookies[i].getDomain();
89                 String JavaDoc name = cookies[i].getName();
90                 String JavaDoc path = cookies[i].getPath();
91                 String JavaDoc value = cookies[i].getValue();
92                 Cookie cookie = new Cookie(domain, path, value);
93                 cookie.setName(name);
94                 newCookiesList.add(cookie);
95             }
96
97             CookieSpecBase cookieFormatter = new CookieSpecBase();
98             Header cookieHeader =
99                 cookieFormatter.formatCookieHeader((Cookie[])newCookiesList.toArray(new Cookie[newCookiesList.size()]));
100             this.addRequestHeader(cookieHeader);
101         }
102
103     }
104     
105 }
106
Popular Tags