1 16 package org.apache.cocoon.generation; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import org.apache.avalon.framework.configuration.Configurable; 25 import org.apache.avalon.framework.configuration.Configuration; 26 import org.apache.avalon.framework.configuration.ConfigurationException; 27 import org.apache.avalon.framework.parameters.Parameters; 28 import org.apache.avalon.framework.service.ServiceException; 29 30 import org.apache.cocoon.ProcessingException; 31 import org.apache.cocoon.ResourceNotFoundException; 32 import org.apache.cocoon.environment.SourceResolver; 33 34 import org.apache.commons.httpclient.HttpConnection; 35 import org.apache.commons.httpclient.HttpMethodBase; 36 import org.apache.commons.httpclient.HttpState; 37 import org.apache.commons.httpclient.HttpURL; 38 import org.apache.commons.httpclient.HostConfiguration; 39 import org.apache.commons.httpclient.NameValuePair; 40 import org.apache.commons.httpclient.URIException; 41 import org.apache.commons.httpclient.methods.GetMethod; 42 import org.apache.commons.httpclient.methods.PostMethod; 43 44 import org.apache.excalibur.xml.sax.SAXParser; 45 46 import org.xml.sax.InputSource ; 47 import org.xml.sax.SAXException ; 48 import org.xml.sax.helpers.AttributesImpl ; 49 50 60 public class HttpProxyGenerator extends ServiceableGenerator implements Configurable { 61 62 63 private HttpMethodBase method = null; 64 65 private HttpURL url = null; 66 67 private ArrayList reqParams = null; 68 69 private ArrayList qryParams = null; 70 71 private boolean debug = false; 72 73 76 public HttpProxyGenerator() { 77 super(); 78 } 79 80 87 public void configure(Configuration configuration) 88 throws ConfigurationException { 89 90 91 String method = configuration.getChild("method").getValue("GET"); 92 if ("GET".equalsIgnoreCase(method)) { 93 this.method = new GetMethod(); 94 } else if ("POST".equalsIgnoreCase(method)) { 95 this.method = new PostMethod(); 96 99 ((PostMethod) this.method).setRequestBody(""); 100 } else { 101 throw new ConfigurationException("Invalid method \"" + method + "\" specified" 102 + " at " + configuration.getChild("method").getLocation()); 103 } 104 105 106 String url = configuration.getChild("url").getValue(null); 107 try { 108 if (url != null) this.url = new HttpURL(url); 109 } catch (URIException e) { 110 throw new ConfigurationException("Cannot process URL \"" + url + "\" specified" 111 + " at " + configuration.getChild("url").getLocation()); 112 } 113 114 115 this.reqParams = this.getParams(configuration.getChildren("param")); 116 this.qryParams = this.getParams(configuration.getChildren("query")); 117 } 118 119 132 public void setup(SourceResolver sourceResolver, Map objectModel, 133 String source, Parameters parameters) 134 throws ProcessingException, SAXException , IOException { 135 136 super.setup(sourceResolver, objectModel, source, parameters); 137 138 144 ArrayList req = new ArrayList (); 145 ArrayList qry = req; 146 if (this.method instanceof PostMethod) qry = new ArrayList (); 147 req.addAll(this.reqParams); 148 qry.addAll(this.qryParams); 149 150 154 String names[] = parameters.getNames(); 155 for (int x = 0; x < names.length; x++) { 156 String name = names[x]; 157 String value = parameters.getParameter(name, null); 158 if (value == null) continue; 159 160 if (name.startsWith("query:")) { 161 name = name.substring("query:".length()); 162 qry.add(new NameValuePair(name, value)); 163 } else if (name.startsWith("param:")) { 164 name = name.substring("param:".length()); 165 req.add(new NameValuePair(name, value)); 166 } else if (name.startsWith("query-override:")) { 167 name = name.substring("query-override:".length()); 168 qry = overrideParams(qry, name, value); 169 } else if (name.startsWith("param-override:")) { 170 name = name.substring("param-override:".length()); 171 req = overrideParams(req, name, value); 172 } 173 } 174 175 176 HttpURL src = (super.source == null ? null : new HttpURL(super.source)); 177 if (this.url != null) src = (src == null ? this.url : new HttpURL(this.url, src)); 178 if (src == null) throw new ProcessingException("No URL specified"); 179 if (src.isRelativeURI()) { 180 throw new ProcessingException("Invalid URL \"" + src.toString() + "\""); 181 } 182 183 184 HostConfiguration hc = new HostConfiguration(); 185 hc.setHost(src); 186 this.method.setHostConfiguration(hc); 187 this.method.setPath(src.getPath()); 188 this.method.setQueryString(src.getQuery()); 189 190 191 if (qry.size() > 0) { 192 String qs = this.method.getQueryString(); 193 NameValuePair nvpa[] = new NameValuePair[qry.size()]; 194 this.method.setQueryString((NameValuePair []) qry.toArray(nvpa)); 195 if (qs != null) { 196 this.method.setQueryString(qs + "&" + this.method.getQueryString()); 197 } 198 } 199 200 201 if ((this.method instanceof PostMethod) && (req.size() > 0)) { 202 PostMethod post = (PostMethod) this.method; 203 NameValuePair nvpa[] = new NameValuePair[req.size()]; 204 post.setRequestBody((NameValuePair []) req.toArray(nvpa)); 205 } 206 207 208 this.debug = parameters.getParameterAsBoolean("debug", false); 209 } 210 211 219 public void recycle() { 220 221 this.method.recycle(); 222 225 if (this.method instanceof PostMethod) ((PostMethod) this.method).setRequestBody(""); 226 227 228 super.recycle(); 229 } 230 231 239 public void generate() 240 throws ResourceNotFoundException, ProcessingException, SAXException , IOException { 241 242 if (this.debug) { 243 this.generateDebugOutput(); 244 return; 245 } 246 247 248 HttpConnection connection = new HttpConnection(this.method.getHostConfiguration()); 249 HttpState state = new HttpState(); 250 this.method.setFollowRedirects(true); 251 int status = this.method.execute(state, connection); 252 if (status == 404) { 253 throw new ResourceNotFoundException("Unable to access \"" + this.method.getURI() 254 + "\" (HTTP 404 Error)"); 255 } else if ((status < 200) || (status > 299)) { 256 throw new IOException ("Unable to access HTTP resource at \"" 257 + this.method.getURI().toString() + "\" (status=" + status + ")"); 258 } 259 InputStream response = this.method.getResponseBodyAsStream(); 260 261 262 SAXParser parser = null; 263 try { 264 InputSource inputSource = new InputSource (response); 265 parser = (SAXParser) this.manager.lookup(SAXParser.ROLE); 266 parser.parse(inputSource, super.xmlConsumer); 267 } catch (ServiceException ex) { 268 throw new ProcessingException("Unable to get parser", ex); 269 } finally { 270 this.manager.release(parser); 271 this.method.releaseConnection(); 272 connection.close(); 273 } 274 } 275 276 282 private void generateDebugOutput() 283 throws SAXException , IOException { 284 super.xmlConsumer.startDocument(); 285 286 AttributesImpl attributes = new AttributesImpl (); 287 attributes.addAttribute("", "method", "method", "CDATA", this.method.getName()); 288 attributes.addAttribute("", "url", "url", "CDATA", this.method.getURI().toString()); 289 attributes.addAttribute("", "protocol", "protocol", "CDATA", 290 (this.method.isHttp11() ? "HTTP/1.1" : "HTTP/1.0")); 291 super.xmlConsumer.startElement("", "request", "request", attributes); 292 293 if (this.method instanceof PostMethod) { 294 String body = ((PostMethod) this.method).getRequestBodyAsString(); 295 296 attributes.clear(); 297 attributes.addAttribute("", "name", "name", "CDATA", "Content-Type"); 298 attributes.addAttribute("", "value", "value", "CDATA", "application/x-www-form-urlencoded"); 299 super.xmlConsumer.startElement("", "header", "header", attributes); 300 super.xmlConsumer.endElement("", "header", "header"); 301 302 attributes.clear(); 303 attributes.addAttribute("", "name", "name", "CDATA", "Content-Length"); 304 attributes.addAttribute("", "value", "value", "CDATA", Integer.toString(body.length())); 305 super.xmlConsumer.startElement("", "header", "header", attributes); 306 super.xmlConsumer.endElement("", "header", "header"); 307 308 attributes.clear(); 309 super.xmlConsumer.startElement("", "body", "body", attributes); 310 super.xmlConsumer.characters(body.toCharArray(), 0, body.length()); 311 super.xmlConsumer.endElement("", "body", "body"); 312 } 313 314 super.xmlConsumer.endElement("", "request", "request"); 315 316 super.xmlConsumer.endDocument(); 317 return; 318 } 319 320 328 private ArrayList getParams(Configuration configurations[]) 329 throws ConfigurationException { 330 ArrayList list = new ArrayList (); 331 332 if (configurations.length < 1) return (list); 333 334 for (int x = 0; x < configurations.length; x++) { 335 Configuration configuration = configurations[x]; 336 String name = configuration.getAttribute("name", null); 337 if (name == null) { 338 throw new ConfigurationException("No name specified for parameter at " 339 + configuration.getLocation()); 340 } 341 342 String value = configuration.getAttribute("value", null); 343 if (value != null) list.add(new NameValuePair(name, value)); 344 345 Configuration subconfigurations[] = configuration.getChildren("value"); 346 for (int y = 0; y < subconfigurations.length; y++) { 347 value = subconfigurations[y].getValue(null); 348 if (value != null) list.add(new NameValuePair(name, value)); 349 } 350 } 351 352 return (list); 353 } 354 355 364 private ArrayList overrideParams(ArrayList list, String name, String value) { 365 Iterator iterator = list.iterator(); 366 while (iterator.hasNext()) { 367 NameValuePair param = (NameValuePair) iterator.next(); 368 if (param.getName().equals(name)) { 369 iterator.remove(); 370 break; 371 } 372 } 373 list.add(new NameValuePair(name, value)); 374 return (list); 375 } 376 } 377 378 | Popular Tags |