This project has retired. For details please refer to its Attic page.
AxisTransport xref
View Javadoc
1   /*
2    * Copyright 2001-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.ws.scout.transport;
17  
18  import java.io.ByteArrayInputStream;
19  import java.net.URI;
20  import java.util.Vector;
21  
22  import org.apache.axis.AxisFault;
23  import org.apache.axis.Message;
24  import org.apache.axis.client.Call;
25  import org.apache.axis.client.Service;
26  import org.apache.axis.message.SOAPBodyElement;
27  import org.apache.axis.utils.XMLUtils;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.w3c.dom.Element;
31  
32  /**
33   * Message transport class.
34   * 
35   * <p><i>Borrowed from jUDDI project.</i></p>
36   * 
37   * @author Steve Viens (sviens@apache.org)
38   */
39  public class AxisTransport implements Transport
40  {
41    // private reference to the jUDDI logger
42    private static Log log = LogFactory.getLog(AxisTransport.class);
43  
44    public Element send(Element request,URI endpointURL)
45      throws TransportException
46    {    
47      Service service = null;
48      Call call = null;
49      Element response = null;
50  
51      if (log.isDebugEnabled()) {
52          log.debug("\nRequest message:\n" + XMLUtils.ElementToString(request));
53          log.debug(endpointURL.toString());
54      }
55  
56      try {
57        service = new Service();
58        call = (Call)service.createCall();
59        call.setTargetEndpointAddress(endpointURL.toURL());
60        
61        String requestString = XMLUtils.ElementToString(request);
62        SOAPBodyElement body = new SOAPBodyElement(new ByteArrayInputStream(requestString.getBytes("UTF-8")));
63        Object[] soapBodies = new Object[] { body };
64  
65        Vector result = (Vector)call.invoke(soapBodies);
66        if (result.size() > 0) {
67      	  response = ((SOAPBodyElement)result.elementAt(0)).getAsDOM();
68        }
69      }
70      catch (AxisFault fault) {
71  
72        try {
73          Message msg = call.getResponseMessage();
74          response = msg.getSOAPEnvelope().getFirstBody().getAsDOM();
75        }
76        catch (Exception ex) {
77          throw new TransportException(ex);
78        }
79      }
80      catch (Exception ex) {
81        throw new TransportException(ex);
82      }
83  
84      if (log.isDebugEnabled()) {
85          log.debug("\nResponse message:\n" + XMLUtils.ElementToString(response));
86      }
87  
88  
89      return response;
90    }
91    
92    public String send(String request,URI endpointURL)
93      throws TransportException
94    {    
95      Service service = null;
96      Call call = null;
97      String response = null;
98  
99      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(new ByteArrayInputStream(request.getBytes("UTF-8")));
108       Object[] soapBodies = new Object[] { body };
109     
110       Vector result = (Vector)call.invoke(soapBodies);
111       if (result.size() > 0 ) {
112     	  response = ((SOAPBodyElement)result.elementAt(0)).getAsString();
113       }
114     }
115     catch (AxisFault fault) {
116 
117       try {
118         Message msg = call.getResponseMessage();
119         response = msg.getSOAPEnvelope().getFirstBody().getAsString();
120       }
121       catch (Exception ex) {
122         throw new TransportException(ex);
123       }
124     }
125     catch (Exception ex) {
126       throw new TransportException(ex);
127     }
128 
129     log.debug("\nResponse message:\n" + response);
130 
131     return response;
132   }
133 }