1 16 package org.apache.cocoon.generation; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.util.Map ; 21 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 25 import org.apache.avalon.framework.parameters.Parameters; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.cocoon.ProcessingException; 29 import org.apache.cocoon.xml.XMLUtils; 30 import org.apache.cocoon.environment.SourceResolver; 31 import org.apache.cocoon.environment.http.HttpEnvironment; 32 import org.apache.cocoon.util.RequestForwardingHttpMethod; 33 import org.apache.commons.httpclient.Header; 34 import org.apache.commons.httpclient.HttpConnection; 35 import org.apache.commons.httpclient.HttpState; 36 import org.apache.commons.httpclient.HttpURL; 37 import org.apache.commons.httpclient.UsernamePasswordCredentials; 38 import org.apache.excalibur.xml.sax.SAXParser; 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.SAXException ; 41 42 53 public class GenericProxyGenerator extends ServiceableGenerator { 54 55 56 HttpURL destination; 57 58 HttpServletRequest request; 59 60 HttpServletResponse response; 61 62 String path; 63 SAXParser parser; 64 65 70 public void service(ServiceManager manager) throws ServiceException { 71 super.service(manager); 72 this.parser = (SAXParser)manager.lookup(SAXParser.ROLE); 73 } 74 75 78 public void dispose() { 79 if ( this.manager != null ) { 80 this.manager.release( this.parser ); 81 this.parser = null; 82 } 83 super.dispose(); 84 } 85 86 95 public void setup( 96 SourceResolver resolver, 97 Map objectModel, 98 String src, 99 Parameters par) 100 throws ProcessingException, SAXException , IOException { 101 String url = par.getParameter("url", null); 102 request = (HttpServletRequest )objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT); 103 response = (HttpServletResponse )objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT); 104 105 if (url == null) { 106 throw new ProcessingException("Missing the \"url\" parameter"); 107 } 108 path = par.getParameter("path", null); 109 if (path == null) 110 path = request.getRequestURI(); 111 destination = new HttpURL(url); 112 113 } 114 115 116 125 public void generate() 126 throws IOException , SAXException , ProcessingException { 127 RequestForwardingHttpMethod method = 128 new RequestForwardingHttpMethod(request, destination); 129 130 HttpConnection conn = new HttpConnection(destination.getHost(), destination.getPort()); 132 HttpState state = new HttpState(); 133 state.setCredentials(null, destination.getHost(), 134 new UsernamePasswordCredentials(destination.getUser(), destination.getPassword())); 135 method.setPath(path); 136 137 method.execute(state, conn); 139 140 response.setStatus(method.getStatusCode()); 142 143 Header[] methodHeaders = method.getResponseHeaders(); 145 for (int i = 0; i < methodHeaders.length; i++) { 146 if (methodHeaders[i].getName().equals("DAV")) { 148 response.addHeader(methodHeaders[i].getName(), methodHeaders[i].getValue()); 149 } else if (methodHeaders[i].getName().equals("Content-Length")) { 150 } else { 153 response.setHeader(methodHeaders[i].getName(), methodHeaders[i].getValue()); 154 } 155 } 156 157 response.setHeader("Connection", "close"); 159 160 if (method.getResponseHeader("Content-Type").getValue().startsWith("text/xml")) { 162 InputStream stream = method.getResponseBodyAsStream(); 163 parser.parse(new InputSource (stream), this.contentHandler, this.lexicalHandler); 164 } else { 165 this.contentHandler.startDocument(); 167 this.contentHandler.startElement("", "no-xml-content", "no-xml-content", XMLUtils.EMPTY_ATTRIBUTES); 168 this.contentHandler.endElement("", "no-xml-content", "no-xml-content"); 169 this.contentHandler.endDocument(); 170 } 171 172 conn.close(); 174 } 175 176 } 177 | Popular Tags |