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: package org.apache.cocoon.util;
018:
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.Enumeration;
022:
023: import javax.servlet.http.HttpServletRequest;
024:
025: import org.apache.commons.httpclient.Cookie;
026: import org.apache.commons.httpclient.Header;
027: import org.apache.commons.httpclient.HttpURL;
028: import org.apache.commons.httpclient.cookie.CookieSpecBase;
029: import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
030:
031: /**
032: * This is a generic and externally configurable method, to forward any Request
033: * to a server.
034: *
035: * @author <a href="mailto:gianugo@apache.org">Gianugo Rabellino</a>
036: * @version $Id: RequestForwardingHttpMethod.java 433543 2006-08-22 06:22:54Z crossley $
037: */
038: public class RequestForwardingHttpMethod extends EntityEnclosingMethod {
039:
040: /** The request to be forwarded */
041: HttpServletRequest originalRequest;
042:
043: /** The HTTPUrl to forward this request to */
044: HttpURL destination;
045:
046: public RequestForwardingHttpMethod(HttpServletRequest req,
047: HttpURL destination) throws IOException {
048: this .originalRequest = req;
049: this .destination = destination;
050: this .setFollowRedirects(true);
051: this .setPath(req.getRequestURI());
052: cloneHeaders();
053: cloneCookies();
054: setRequestBody(originalRequest.getInputStream());
055: }
056:
057: /**
058: * Dinamically get the method.
059: *
060: * @see org.apache.commons.httpclient.HttpMethod#getName()
061: */
062: public String getName() {
063: return originalRequest.getMethod();
064: }
065:
066: /**
067: * Clone the original request headers.
068: *
069: */
070: private void cloneHeaders() {
071: Enumeration e = originalRequest.getHeaderNames();
072: while (e.hasMoreElements()) {
073: String header = (String) e.nextElement();
074: String headerValue = originalRequest.getHeader(header);
075: this .addRequestHeader(header, headerValue);
076: }
077: }
078:
079: /**
080: * Clone cookies, if any.
081: *
082: */
083: private void cloneCookies() {
084: ArrayList newCookiesList = new ArrayList();
085: javax.servlet.http.Cookie[] cookies = originalRequest
086: .getCookies();
087: if (cookies != null) {
088: for (int i = 0; i < cookies.length; i++) {
089: String domain = cookies[i].getDomain();
090: String name = cookies[i].getName();
091: String path = cookies[i].getPath();
092: String value = cookies[i].getValue();
093: Cookie cookie = new Cookie(domain, path, value);
094: cookie.setName(name);
095: newCookiesList.add(cookie);
096: }
097:
098: CookieSpecBase cookieFormatter = new CookieSpecBase();
099: Header cookieHeader = cookieFormatter
100: .formatCookieHeader((Cookie[]) newCookiesList
101: .toArray(new Cookie[newCookiesList.size()]));
102: this.addRequestHeader(cookieHeader);
103: }
104:
105: }
106:
107: }
|