1 16 package org.apache.cocoon.components.language.markup.xsp; 17 18 import org.apache.avalon.framework.component.ComponentException; 19 import org.apache.avalon.framework.component.ComponentManager; 20 import org.apache.cocoon.ProcessingException; 21 import org.apache.cocoon.components.xscript.XScriptManager; 22 import org.apache.cocoon.components.xscript.XScriptObject; 23 import org.apache.cocoon.components.xscript.XScriptObjectInlineXML; 24 import org.apache.commons.httpclient.Header; 25 import org.apache.commons.httpclient.HttpConnection; 26 import org.apache.commons.httpclient.HttpState; 27 import org.apache.commons.httpclient.methods.PostMethod; 28 import org.apache.excalibur.source.SourceUtil; 29 import org.xml.sax.InputSource ; 30 31 import java.io.InputStreamReader ; 32 import java.io.Reader ; 33 import java.net.MalformedURLException ; 34 import java.net.URL ; 35 36 44 public class SOAPHelper { 45 XScriptManager xscriptManager; 46 URL url; 47 String action = ""; 48 XScriptObject xscriptObject; 49 String authorization = ""; 50 51 public SOAPHelper(ComponentManager manager, String urlContext, String url, 52 String action, String authorization, XScriptObject xscriptObject) 53 throws MalformedURLException , ComponentException 54 { 55 this.xscriptManager = (XScriptManager) manager.lookup(XScriptManager.ROLE); 56 URL context = new URL (urlContext); 57 this.url = new URL (context, url); 58 this.action = action; 59 this.authorization = authorization; 60 this.xscriptObject = xscriptObject; 61 } 62 63 public XScriptObject invoke() throws ProcessingException 64 { 65 HttpConnection conn = null; 66 67 try { 68 if (action == null || action.equals("")) { 69 action = "\"\""; 70 } 71 72 String host = url.getHost(); 73 int port = url.getPort(); 74 75 if (System.getProperty("http.proxyHost") != null) { 76 String proxyHost = System.getProperty("http.proxyHost"); 77 int proxyPort = Integer.parseInt(System.getProperty("http.proxyPort")); 78 conn = new HttpConnection(proxyHost, proxyPort, host, port); 79 } else { 80 conn = new HttpConnection(host, port); 81 } 82 83 PostMethod method = new PostMethod(url.getFile()); 84 String request; 85 86 try { 87 if (xscriptObject instanceof XScriptObjectInlineXML) { 89 request = ((XScriptObjectInlineXML) xscriptObject).getContent(); 91 } else { 92 StringBuffer bodyBuffer = new StringBuffer (); 93 InputSource saxSource = xscriptObject.getInputSource(); 94 95 Reader r = null; 96 if (saxSource.getByteStream() != null) { 98 r = new InputStreamReader (saxSource.getByteStream()); 99 } else { 100 r = saxSource.getCharacterStream(); 101 } 102 103 try { 104 char[] buffer = new char[1024]; 105 int len; 106 while ((len = r.read(buffer)) > 0) 107 bodyBuffer.append(buffer, 0, len); 108 } finally { 109 if (r != null) { 110 r.close(); 111 } 112 } 113 114 request = bodyBuffer.toString(); 115 } 116 117 } catch (Exception ex) { 118 throw new ProcessingException("Error assembling request", ex); 119 } 120 121 method.setRequestHeader( 122 new Header("Content-type", "text/xml; charset=\"utf-8\"")); 123 method.setRequestHeader(new Header("SOAPAction", action)); 124 method.setRequestBody(request); 125 126 if (authorization != null && !authorization.equals("")) { 127 method.setRequestHeader(new Header("Authorization","Basic "+SourceUtil.encodeBASE64(authorization))); 128 } 129 130 method.execute(new HttpState(), conn); 131 132 String ret = method.getResponseBodyAsString(); 133 int startOfXML = ret.indexOf("<?xml"); 134 if (startOfXML == -1) { throw new ProcessingException("Invalid response - no xml"); 136 } 137 138 return new XScriptObjectInlineXML( 139 xscriptManager, 140 ret.substring(startOfXML)); 141 } catch (Exception ex) { 142 throw new ProcessingException("Error invoking remote service: " + ex, 143 ex); 144 } finally { 145 try { 146 if (conn != null) 147 conn.close(); 148 } catch (Exception ex) { 149 } 150 } 151 } 152 } 153 | Popular Tags |