1 16 package org.apache.cocoon.portal.reading; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.net.HttpURLConnection ; 21 import java.net.URL ; 22 import java.util.Enumeration ; 23 import java.util.Map ; 24 25 import org.apache.avalon.framework.parameters.Parameters; 26 import org.apache.cocoon.ProcessingException; 27 import org.apache.cocoon.environment.ObjectModelHelper; 28 import org.apache.cocoon.environment.Request; 29 import org.apache.cocoon.environment.Response; 30 import org.apache.cocoon.environment.SourceResolver; 31 import org.apache.cocoon.portal.coplet.CopletInstanceData; 32 import org.apache.cocoon.portal.transformation.ProxyTransformer; 33 import org.apache.cocoon.reading.ServiceableReader; 34 import org.apache.cocoon.util.NetUtils; 35 import org.xml.sax.SAXException ; 36 37 50 public class ProxyReader extends ServiceableReader { 51 52 55 protected CopletInstanceData copletInstanceData; 56 57 60 protected Response response; 61 62 65 protected Request request; 66 67 68 protected String prefix; 69 70 73 public void setup(SourceResolver resolver, 74 Map objectModel, 75 String src, 76 Parameters par) 77 throws ProcessingException, SAXException , IOException { 78 super.setup(resolver, objectModel, src, par); 79 80 request = ObjectModelHelper.getRequest(objectModel); 81 response = ObjectModelHelper.getResponse(objectModel); 82 83 String copletID = request.getParameter(ProxyTransformer.COPLETID); 84 String portalName = request.getParameter(ProxyTransformer.PORTALNAME); 85 86 copletInstanceData = 87 ProxyTransformer.getInstanceData( 88 this.manager, 89 copletID, 90 portalName); 91 this.prefix = par.getParameter("prefix", ProxyTransformer.PROXY_PREFIX); 92 } 93 94 97 public void recycle() { 98 this.response = null; 99 this.request = null; 100 this.copletInstanceData = null; 101 super.recycle(); 102 } 103 104 109 public void generate() throws IOException { 110 String link = request.getRequestURI(); 111 link = link.substring(link.indexOf(this.prefix) + this.prefix.length()); 112 113 String documentBase = 114 (String ) copletInstanceData.getAttribute( 115 ProxyTransformer.DOCUMENT_BASE); 116 String remoteURI = null; 117 118 remoteURI = ProxyTransformer.resolveURI(link, documentBase); 119 120 HttpURLConnection connection = connect(request, remoteURI); 121 copyHeaderFields(connection, response); 122 sendData(connection.getInputStream()); 123 } 124 125 130 protected void sendData(InputStream in) throws IOException { 131 int length = -1; 132 byte[] buf = new byte[4096]; 133 134 while ((length = in.read(buf)) > -1) { 135 out.write(buf, 0, length); 136 } 137 out.flush(); 138 in.close(); 139 } 140 141 148 protected HttpURLConnection connect(Request request, String uri) 149 throws IOException { 150 String cookie = 151 (String ) copletInstanceData.getAttribute(ProxyTransformer.COOKIE); 152 153 Enumeration enumeration = request.getParameterNames(); 154 155 boolean firstattribute = true; 156 StringBuffer query = new StringBuffer (); 157 158 while (enumeration.hasMoreElements()) { 159 String paramName = (String ) enumeration.nextElement(); 160 161 if (!paramName.startsWith("cocoon-portal-")) { 162 163 String [] paramValues = request.getParameterValues(paramName); 164 165 for (int i = 0; i < paramValues.length; i++) { 166 if (firstattribute) { 167 query.append('?'); 168 firstattribute = false; 169 } 170 else { 171 query.append('&'); 172 } 173 174 query.append(NetUtils.encode(paramName, "utf-8")); 175 query.append('='); 176 query.append(NetUtils.encode(paramValues[i], "utf-8")); 177 178 } 179 } 180 } 181 182 uri = uri + query.toString(); 183 184 URL url = new URL (uri); 185 HttpURLConnection connection = (HttpURLConnection ) url.openConnection(); 186 connection.setInstanceFollowRedirects(true); 187 188 if (cookie != null) { 189 connection.setRequestProperty(ProxyTransformer.COOKIE, cookie); 190 } 191 192 connection.connect(); 193 194 copletInstanceData.setAttribute( 195 ProxyTransformer.COOKIE, 196 connection.getHeaderField(ProxyTransformer.COOKIE)); 197 198 return connection; 199 } 200 201 206 private void copyHeaderFields( 207 HttpURLConnection connection, 208 Response response) { 209 String [] fieldNames = 210 new String [] { 211 "Content-Range", 212 "Accept-Ranges", 213 "Content-Length", 214 "Last-Modified", 215 "Content-Type", 216 "Expires" }; 217 for (int i = 0; i < fieldNames.length; i++) { 218 String value = connection.getHeaderField(fieldNames[i]); 219 if (value != null) { 220 response.setHeader(fieldNames[i], value); 221 } 222 223 } 224 } 225 } 226 | Popular Tags |