KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > markup > xsp > SOAPHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
30
31 import java.io.InputStreamReader JavaDoc;
32 import java.io.Reader JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.net.URL JavaDoc;
35
36 /**
37  * Helper for the SOAP logicsheet.
38  *
39  * @author <a HREF="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
40  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
41  * @version CVS $Id: SOAPHelper.java 30932 2004-07-29 17:35:38Z vgritsenko $
42  * @since July 16, 2001
43  */

44 public class SOAPHelper {
45     XScriptManager xscriptManager;
46     URL JavaDoc url;
47     String JavaDoc action = "";
48     XScriptObject xscriptObject;
49     String JavaDoc authorization = "";
50
51     public SOAPHelper(ComponentManager manager, String JavaDoc urlContext, String JavaDoc url,
52                       String JavaDoc action, String JavaDoc authorization, XScriptObject xscriptObject)
53             throws MalformedURLException JavaDoc, ComponentException
54     {
55         this.xscriptManager = (XScriptManager) manager.lookup(XScriptManager.ROLE);
56         URL JavaDoc context = new URL JavaDoc(urlContext);
57         this.url = new URL JavaDoc(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 JavaDoc host = url.getHost();
73             int port = url.getPort();
74
75             if (System.getProperty("http.proxyHost") != null) {
76                 String JavaDoc 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 JavaDoc request;
85
86             try {
87                 // Write the SOAP request body
88
if (xscriptObject instanceof XScriptObjectInlineXML) {
89                     // Skip overhead
90
request = ((XScriptObjectInlineXML) xscriptObject).getContent();
91                 } else {
92                     StringBuffer JavaDoc bodyBuffer = new StringBuffer JavaDoc();
93                     InputSource JavaDoc saxSource = xscriptObject.getInputSource();
94
95                     Reader JavaDoc r = null;
96                     // Byte stream or character stream?
97
if (saxSource.getByteStream() != null) {
98                         r = new InputStreamReader JavaDoc(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 JavaDoc 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 JavaDoc ret = method.getResponseBodyAsString();
133             int startOfXML = ret.indexOf("<?xml");
134             if (startOfXML == -1) { // No xml?!
135
throw new ProcessingException("Invalid response - no xml");
136             }
137
138             return new XScriptObjectInlineXML(
139                     xscriptManager,
140                     ret.substring(startOfXML));
141         } catch (Exception JavaDoc 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 JavaDoc ex) {
149             }
150         }
151     }
152 }
153
Popular Tags