001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.ws.scout.transport;
017:
018: import java.io.ByteArrayInputStream;
019: import java.net.URI;
020: import java.util.Vector;
021:
022: import org.apache.axis.AxisFault;
023: import org.apache.axis.Message;
024: import org.apache.axis.client.Call;
025: import org.apache.axis.client.Service;
026: import org.apache.axis.message.SOAPBodyElement;
027: import org.apache.axis.utils.XMLUtils;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.ws.scout.registry.RegistryException;
031: import org.w3c.dom.Element;
032:
033: /**
034: * Message transport class.
035: *
036: * <p><i>Borrowed from jUDDI project.</i></p>
037: *
038: * @author Steve Viens (sviens@apache.org)
039: */
040: public class AxisTransport implements Transport {
041: // private reference to the jUDDI logger
042: private static Log log = LogFactory.getLog(AxisTransport.class);
043:
044: public Element send(Element request, URI endpointURL)
045: throws RegistryException {
046: Service service = null;
047: Call call = null;
048: Element response = null;
049:
050: if (log.isDebugEnabled()) {
051: log.debug("\nRequest message:\n"
052: + XMLUtils.ElementToString(request));
053: log.debug(endpointURL.toString());
054: }
055:
056: try {
057: service = new Service();
058: call = (Call) service.createCall();
059: call.setTargetEndpointAddress(endpointURL.toURL());
060:
061: String requestString = XMLUtils.ElementToString(request);
062: SOAPBodyElement body = new SOAPBodyElement(
063: new ByteArrayInputStream(requestString
064: .getBytes("UTF-8")));
065: Object[] soapBodies = new Object[] { body };
066:
067: Vector result = (Vector) call.invoke(soapBodies);
068: response = ((SOAPBodyElement) result.elementAt(0))
069: .getAsDOM();
070: } catch (AxisFault fault) {
071:
072: fault.printStackTrace();
073:
074: try {
075: Message msg = call.getResponseMessage();
076: response = msg.getSOAPEnvelope().getFirstBody()
077: .getAsDOM();
078: } catch (Exception ex) {
079: throw new RegistryException(ex);
080: }
081: } catch (Exception ex) {
082: throw new RegistryException(ex);
083: }
084:
085: if (log.isDebugEnabled()) {
086: log.debug("\nResponse message:\n"
087: + XMLUtils.ElementToString(response));
088: }
089:
090: return response;
091: }
092:
093: public String send(String request, URI endpointURL)
094: throws RegistryException {
095: Service service = null;
096: Call call = null;
097: String response = null;
098:
099: log.debug("\nRequest message:\n" + request);
100:
101: try {
102:
103: service = new Service();
104: call = (Call) service.createCall();
105: call.setTargetEndpointAddress(endpointURL.toURL());
106:
107: SOAPBodyElement body = new SOAPBodyElement(
108: new ByteArrayInputStream(request.getBytes("UTF-8")));
109: Object[] soapBodies = new Object[] { body };
110:
111: Vector result = (Vector) call.invoke(soapBodies);
112: response = ((SOAPBodyElement) result.elementAt(0))
113: .getAsString();
114: } catch (AxisFault fault) {
115:
116: fault.printStackTrace();
117:
118: try {
119: Message msg = call.getResponseMessage();
120: response = msg.getSOAPEnvelope().getFirstBody()
121: .getAsString();
122: } catch (Exception ex) {
123: throw new RegistryException(ex);
124: }
125: } catch (Exception ex) {
126: throw new RegistryException(ex);
127: }
128:
129: log.debug("\nResponse message:\n" + response);
130:
131: return response;
132: }
133: }
|