001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.language.markup.xsp;
018:
019: import org.apache.avalon.framework.component.ComponentException;
020: import org.apache.avalon.framework.component.ComponentManager;
021: import org.apache.cocoon.ProcessingException;
022: import org.apache.cocoon.components.xscript.XScriptManager;
023: import org.apache.cocoon.components.xscript.XScriptObject;
024: import org.apache.cocoon.components.xscript.XScriptObjectInlineXML;
025: import org.apache.commons.httpclient.Header;
026: import org.apache.commons.httpclient.HttpConnection;
027: import org.apache.commons.httpclient.HttpState;
028: import org.apache.commons.httpclient.methods.PostMethod;
029: import org.apache.excalibur.source.SourceUtil;
030: import org.xml.sax.InputSource;
031:
032: import java.io.InputStreamReader;
033: import java.io.Reader;
034: import java.net.MalformedURLException;
035: import java.net.URL;
036:
037: /**
038: * Helper for the SOAP logicsheet.
039: *
040: * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
041: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
042: * @version CVS $Id: SOAPHelper.java 454485 2006-10-09 20:11:47Z joerg $
043: * @since July 16, 2001
044: */
045: public class SOAPHelper {
046: XScriptManager xscriptManager;
047: URL url;
048: String action = "";
049: XScriptObject xscriptObject;
050: String authorization = "";
051:
052: public SOAPHelper(ComponentManager manager, String urlContext,
053: String url, String action, String authorization,
054: XScriptObject xscriptObject) throws MalformedURLException,
055: ComponentException {
056: this .xscriptManager = (XScriptManager) manager
057: .lookup(XScriptManager.ROLE);
058: URL context = new URL(urlContext);
059: this .url = new URL(context, url);
060: this .action = action;
061: this .authorization = authorization;
062: this .xscriptObject = xscriptObject;
063: }
064:
065: public XScriptObject invoke() throws ProcessingException {
066: HttpConnection conn = null;
067:
068: try {
069: if (this .action == null || this .action.length() == 0) {
070: this .action = "\"\"";
071: }
072:
073: String host = this .url.getHost();
074: int port = this .url.getPort();
075:
076: if (System.getProperty("http.proxyHost") != null) {
077: String proxyHost = System.getProperty("http.proxyHost");
078: int proxyPort = Integer.parseInt(System
079: .getProperty("http.proxyPort"));
080: conn = new HttpConnection(proxyHost, proxyPort, host,
081: port);
082: } else {
083: conn = new HttpConnection(host, port);
084: }
085:
086: PostMethod method = new PostMethod(this .url.getFile());
087: String request;
088:
089: try {
090: // Write the SOAP request body
091: if (this .xscriptObject instanceof XScriptObjectInlineXML) {
092: // Skip overhead
093: request = ((XScriptObjectInlineXML) this .xscriptObject)
094: .getContent();
095: } else {
096: StringBuffer bodyBuffer = new StringBuffer();
097: InputSource saxSource = this .xscriptObject
098: .getInputSource();
099:
100: Reader r = null;
101: // Byte stream or character stream?
102: if (saxSource.getByteStream() != null) {
103: r = new InputStreamReader(saxSource
104: .getByteStream());
105: } else {
106: r = saxSource.getCharacterStream();
107: }
108:
109: try {
110: char[] buffer = new char[1024];
111: int len;
112: while ((len = r.read(buffer)) > 0) {
113: bodyBuffer.append(buffer, 0, len);
114: }
115: } finally {
116: if (r != null) {
117: r.close();
118: }
119: }
120:
121: request = bodyBuffer.toString();
122: }
123:
124: } catch (Exception ex) {
125: throw new ProcessingException(
126: "Error assembling request", ex);
127: }
128:
129: method.setRequestHeader(new Header("Content-type",
130: "text/xml; charset=\"utf-8\""));
131: method.setRequestHeader(new Header("SOAPAction",
132: this .action));
133: method.setRequestBody(request);
134:
135: if (this .authorization != null
136: && !this .authorization.equals("")) {
137: method
138: .setRequestHeader(new Header(
139: "Authorization",
140: "Basic "
141: + SourceUtil
142: .encodeBASE64(this .authorization)));
143: }
144:
145: method.execute(new HttpState(), conn);
146:
147: String ret = method.getResponseBodyAsString();
148: return new XScriptObjectInlineXML(this .xscriptManager, ret);
149: } catch (ProcessingException ex) {
150: throw ex;
151: } catch (Exception ex) {
152: throw new ProcessingException(
153: "Error invoking remote service: " + ex, ex);
154: } finally {
155: try {
156: if (conn != null) {
157: conn.close();
158: }
159: } catch (Exception ex) {
160: }
161: }
162: }
163: }
|