1 16 package org.apache.cocoon.util; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.Enumeration ; 21 22 import javax.servlet.http.HttpServletRequest ; 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 37 public class RequestForwardingHttpMethod extends EntityEnclosingMethod { 38 39 40 HttpServletRequest originalRequest; 41 42 43 HttpURL destination; 44 45 public RequestForwardingHttpMethod(HttpServletRequest req, HttpURL destination) 46 throws IOException { 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 62 public String getName() { 63 return originalRequest.getMethod(); 64 } 65 66 70 private void cloneHeaders() { 71 Enumeration e = originalRequest.getHeaderNames(); 72 while (e.hasMoreElements()) { 73 String header = (String ) e.nextElement(); 74 String headerValue = originalRequest.getHeader(header); 75 this.addRequestHeader(header, headerValue); 76 } 77 } 78 79 83 private void cloneCookies() { 84 ArrayList newCookiesList = new ArrayList (); 85 javax.servlet.http.Cookie [] cookies = originalRequest.getCookies(); 86 if (cookies != null) { 87 for (int i = 0; i < cookies.length; i++) { 88 String domain = cookies[i].getDomain(); 89 String name = cookies[i].getName(); 90 String path = cookies[i].getPath(); 91 String 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 |