View Javadoc

1   /*
2    * SoapAccessBean.java
3    *
4    * Created on August 6, 2001, 3:59 PM
5    */
6   package org.apache.jetspeed.webservices.util;
7   // import soap packages
8   import org.apache.soap.SOAPException;
9   import org.apache.soap.Constants;
10  import org.apache.soap.rpc.Call;
11  import org.apache.soap.rpc.Parameter;
12  import org.apache.soap.rpc.Response;
13  // import utility classes
14  import java.util.Vector;
15  
16  // Jetspeed classes
17  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
18  import org.apache.jetspeed.services.logging.JetspeedLogger;
19  
20  /*** SoapAccessBean provides a Soap WebServices Client command bean pattern.
21    *
22    * This bean can be used to interact with webservices via soap. The WSDL
23    * file for the target web service is required since it contains the
24    * parameters required to interact with the service.
25    *
26    * @author Scott A. Roehrig
27    * @version 1.0
28    */
29  public class SoapAccessBean implements java.io.Serializable 
30  {
31      /***
32       * Static initialization of the logger for this class
33       */    
34      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(SoapAccessBean.class.getName());
35      
36      // declare member variables
37      private transient Call request;
38      private Vector parameters; 
39          
40      // declare methods
41      /*** Default constructor required to support Serialization. */
42      public SoapAccessBean() {
43          request = new Call();
44      }
45  
46      /*** Creates new SoapAccessBean with parameters specified.
47      *
48      * @param targetURI type: java.lang.String - 
49      * desc: targetURI for the services
50      *
51      * @param method type: java.lang.String - 
52      * desc: service method
53      */
54      public SoapAccessBean(String targetURI, String method) {
55          // create call object
56          request = new Call();
57          // set targetObjectURI, method, and encoding style
58          request.setTargetObjectURI(targetURI);
59          request.setMethodName(method);
60          request.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
61          // create Vector to store service parameters
62          parameters = new java.util.Vector();
63              
64      }
65      
66      /*** Setter method for parameters property.
67      *
68      * The parameters property is used to set
69      * the various parameters required to interact
70      * with the service.
71      *
72      * @param parameters
73      * type: java.util.Vector - 
74      * desc: stores service parameters
75      */
76      public void setParameters(Vector parameters) {
77          this.parameters = parameters;
78          
79      }
80      
81      /*** Parameters property getter method.
82      *
83      * @return java.util.Vector - 
84      * desc: stores service parameters
85      */
86      public java.util.Vector getParameters() {
87          return parameters;
88          
89      }
90      
91      /*** Adds the needed parameter to the request.
92      *
93      * The parameters required by the service are defined within
94      * its WSDL descriptor file
95      *
96      * @param paramName
97      * type: java.lang.String - 
98      * desc: the parameter name
99      *
100     * @param paramClass 
101     * type: java.lang.Class - 
102     * desc: the class of the parameter
103     *
104     * @param paramValue 
105     * type:java.lang.Object - 
106     * desc: the parameter
107     *
108     * @param encoding 
109     * type:java.lang.String - 
110     * desc: the parameter encoding
111     */
112     public void addParameter(String paramName, Class  paramClass, Object paramValue, String encoding) {
113         parameters.addElement(new Parameter(paramName, paramClass, paramValue, encoding));
114         
115     }
116     
117     /*** Processes client service requests.
118     *
119     * @param url
120     * type: java.lang.String - 
121     * desc: the service endpoint url
122     *       defined in the WSDL file
123     *
124     * @throws SOAPException 
125     * type: org.apache.soap.SOAPException - 
126     * desc: thrown if an exception occurs
127     *       contains details of the exception
128     *
129     * @return java.lang.Object - 
130     * desc: the results of the service request
131     *       must be cast by caller
132     */
133     public Object processRequest(String url) throws SOAPException {
134         // create soap response
135         Response response = null;
136         Object result = null;
137         try {
138             request.setParams(parameters);
139             response = request.invoke(new java.net.URL(url), "");
140             // verify result
141             if (response.generatedFault() == true ) {
142                     logger.warn(response.getFault().getFaultString());
143             }
144             else {
145             /* get result as object
146               * caller must cast result to proper type
147               */
148                 result = response.getReturnValue().getValue();
149                 // if logging enabled, output response
150                 if (logger.isDebugEnabled()) {
151                     logger.debug(response.getReturnValue().toString());
152                 }
153             }
154         }
155         catch (java.net.MalformedURLException exception) {
156             // if logging enabled output exception
157             if (logger.isDebugEnabled()) {
158                 logger.error(exception.getMessage(), exception);
159             }
160         }
161         return result;
162     }
163     
164 }