1 16 package org.apache.cocoon.generation; 17 18 import org.apache.avalon.framework.parameters.Parameters; 19 import org.apache.avalon.framework.service.ServiceException; 20 import org.apache.cocoon.ProcessingException; 21 import org.apache.cocoon.components.source.SourceUtil; 22 import org.apache.cocoon.environment.ObjectModelHelper; 23 import org.apache.cocoon.environment.Request; 24 import org.apache.cocoon.environment.Session; 25 import org.apache.cocoon.environment.SourceResolver; 26 import org.apache.commons.httpclient.HostConfiguration; 27 import org.apache.commons.httpclient.HttpClient; 28 import org.apache.commons.httpclient.HttpMethod; 29 import org.apache.commons.httpclient.NameValuePair; 30 import org.apache.commons.httpclient.URI; 31 import org.apache.commons.httpclient.URIException; 32 import org.apache.commons.httpclient.methods.GetMethod; 33 import org.apache.commons.httpclient.methods.PostMethod; 34 import org.apache.commons.lang.StringUtils; 35 import org.apache.excalibur.source.Source; 36 import org.apache.excalibur.source.SourceException; 37 import org.apache.excalibur.xml.sax.SAXParser; 38 import org.apache.regexp.RE; 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.SAXException ; 41 42 import java.io.ByteArrayInputStream ; 43 import java.io.IOException ; 44 import java.util.ArrayList ; 45 import java.util.Enumeration ; 46 import java.util.Map ; 47 import java.util.StringTokenizer ; 48 49 76 public class WebServiceProxyGenerator extends ServiceableGenerator { 77 78 private static final String HTTP_CLIENT = "HTTP_CLIENT"; 79 private static final String METHOD_GET = "GET"; 80 private static final String METHOD_POST = "POST"; 81 82 private HttpClient httpClient = null; 83 private String configuredHttpMethod = null; 84 85 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) throws ProcessingException, SAXException , IOException { 86 super.setup(resolver, objectModel, src, par); 87 88 try { 89 Source inputSource = resolver.resolveURI(super.source); 90 this.source = inputSource.getURI(); 91 } catch (SourceException se) { 92 throw SourceUtil.handle("Unable to resolve " + super.source, se); 93 } 94 95 this.configuredHttpMethod = par.getParameter("wsproxy-method", METHOD_GET); 96 this.httpClient = this.getHttpClient(); 97 } 98 99 102 public void generate() throws IOException , SAXException , ProcessingException { 103 SAXParser parser = null; 104 try { 105 if (this.getLogger().isDebugEnabled()) { 106 this.getLogger().debug("processing Web Service request: " + this.source); 107 } 108 109 byte[] response = this.fetch(); 111 if (this.getLogger().isDebugEnabled()) { 112 this.getLogger().debug("response: " + new String (response)); 113 } 114 115 120 ByteArrayInputStream responseStream = new ByteArrayInputStream (response); 121 InputSource inputSource = new InputSource (responseStream); 122 parser = (SAXParser)this.manager.lookup(SAXParser.ROLE); 123 parser.parse(inputSource, super.xmlConsumer); 124 125 } catch (ServiceException ex) { 126 throw new ProcessingException("WebServiceProxyGenerator.generate() error", ex); 127 } finally { 128 this.manager.release(parser); 129 } 130 131 } 133 137 public void recycle() { 138 this.httpClient = null; 139 this.configuredHttpMethod = null; 140 super.recycle(); 141 } 142 143 154 public byte[] fetch() throws ProcessingException { 155 HttpMethod method = null; 156 157 if (this.configuredHttpMethod.equalsIgnoreCase(METHOD_POST)) { 159 method = new PostMethod(this.source); 160 } else { 161 method = new GetMethod(this.source); 162 } 163 164 if (this.getLogger().isDebugEnabled()) { 165 this.getLogger().debug("request HTTP method: " + method.getName()); 166 } 167 168 method.setFollowRedirects(true); 170 171 Request request = ObjectModelHelper.getRequest(objectModel); 173 174 ArrayList paramList = new ArrayList (); 175 Enumeration enumeration = request.getParameterNames(); 176 while (enumeration.hasMoreElements()) { 177 String pname = (String )enumeration.nextElement(); 178 String [] paramsForName = request.getParameterValues(pname); 179 for (int i = 0; i < paramsForName.length; i++) { 180 NameValuePair pair = new NameValuePair(pname, paramsForName[i]); 181 paramList.add(pair); 182 } 183 } 184 185 if (paramList.size() > 0) { 186 NameValuePair[] allSubmitParams = new NameValuePair[paramList.size()]; 187 paramList.toArray(allSubmitParams); 188 189 String urlQryString = method.getQueryString(); 190 191 method.setQueryString(allSubmitParams); 193 String submitQryString = method.getQueryString(); 194 195 197 if (null == urlQryString) { 199 method.setQueryString(submitQryString); 200 } else { 201 method.setQueryString(urlQryString + "&" + submitQryString); 202 } 203 204 } 206 byte[] response = null; 207 try { 208 int httpStatus = httpClient.executeMethod(method); 209 if (httpStatus < 400) { 210 if (this.getLogger().isDebugEnabled()) { 211 this.getLogger().debug("Return code when accessing the remote Url: " + httpStatus); 212 } 213 } else { 214 throw new ProcessingException("The remote returned error " + httpStatus + " when attempting to access remote URL:" + method.getURI()); 215 } 216 } catch (URIException e) { 217 throw new ProcessingException("There is a problem with the URI: " + this.source, e); 218 } catch (IOException e) { 219 try { 220 throw new ProcessingException("Exception when attempting to access the remote URL: " + method.getURI(), e); 221 } catch (URIException ue) { 222 throw new ProcessingException("There is a problem with the URI: " + this.source, ue); 223 } 224 } finally { 225 229 response = method.getResponseBody(); 230 method.releaseConnection(); 231 } 232 233 return response; 234 } 236 239 protected HttpClient getHttpClient() throws ProcessingException { 240 URI uri = null; 241 String host = null; 242 Request request = ObjectModelHelper.getRequest(objectModel); 243 Session session = request.getSession(true); 244 HttpClient httpClient = null; 245 if (session != null) { 246 httpClient = (HttpClient)session.getAttribute(HTTP_CLIENT); 247 } 248 if (httpClient == null) { 249 httpClient = new HttpClient(); 250 HostConfiguration config = httpClient.getHostConfiguration(); 251 if (config == null) { 252 config = new HostConfiguration(); 253 } 254 255 256 261 try { 262 uri = new URI(this.source); 263 host = uri.getHost(); 264 config.setHost(uri); 265 } catch (URIException ex) { 266 throw new ProcessingException("URI format error: " + ex, ex); 267 } 268 269 boolean proxiableHost = true; 272 String nonProxyHosts = System.getProperty("http.nonProxyHosts"); 273 if (nonProxyHosts != null) 274 { 275 StringTokenizer tok = new StringTokenizer (nonProxyHosts, "|"); 276 277 while (tok.hasMoreTokens()) { 278 String nonProxiableHost = tok.nextToken().trim(); 279 280 nonProxiableHost = StringUtils.replace(nonProxiableHost, ".", "\\."); 283 nonProxiableHost = StringUtils.replace(nonProxiableHost, "*", ".*"); 284 285 RE re = null; 290 try { 291 re = new RE("^" + nonProxiableHost + "$"); 292 } 293 catch (Exception ex) { 294 throw new ProcessingException("Regex syntax error: " + ex, ex); 295 } 296 297 if (re.match(host)) 298 { 299 proxiableHost = false; 300 break; 301 } 302 } 303 } 304 305 if (proxiableHost && System.getProperty("http.proxyHost") != null) { 306 String proxyHost = System.getProperty("http.proxyHost"); 307 int proxyPort = Integer.parseInt(System.getProperty("http.proxyPort")); 308 config.setProxy(proxyHost, proxyPort); 309 } 310 311 httpClient.setHostConfiguration(config); 312 313 session.setAttribute(HTTP_CLIENT, httpClient); 314 } 315 return httpClient; 316 } 317 318 319 } | Popular Tags |