KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > webservices > WebServiceHelper


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.deliver.util.webservices;
25
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.xml.namespace.QName JavaDoc;
31 import javax.xml.rpc.ParameterMode JavaDoc;
32
33 import org.apache.axis.client.Call;
34 import org.apache.axis.client.Service;
35 import org.apache.axis.encoding.XMLType;
36 import org.infoglue.cms.exception.SystemException;
37
38 /**
39  * This class helps in requesting information from an webservice.
40  * @author Mattias Bogeblad
41  */

42
43 public class WebServiceHelper
44 {
45     private String JavaDoc serviceUrl = "";
46     
47     private boolean isSuccessfull;
48     private String JavaDoc message;
49     
50     /**
51      * The constructor for this class.
52      */

53     
54     public WebServiceHelper()
55     {
56     }
57     
58     /**
59      * A method to set the serviceUrl which is the endpoint of this call.
60      */

61     
62     public String JavaDoc getServiceUrl()
63     {
64         return serviceUrl;
65     }
66
67     /**
68      * A method to get the serviceUrl which is the endpoint of this call.
69      */

70     
71     public void setServiceUrl(String JavaDoc serviceUrl)
72     {
73         this.serviceUrl = serviceUrl;
74     }
75
76     /**
77      * This is the method that lets you call the endpoint and get a single string-value back.
78      */

79
80     public String JavaDoc getString(String JavaDoc method)
81     {
82         String JavaDoc response = "";
83         
84         try
85         {
86             Service service = new Service();
87             Call call = (Call)service.createCall();
88
89             String JavaDoc endpoint = this.serviceUrl;
90
91             call.setTargetEndpointAddress(endpoint); //Set the target service host and service location,
92
call.setOperationName(new QName JavaDoc("http://soapinterop.org/", method)); //This is the target services method to invoke.
93
call.setEncodingStyle( "http://schemas.xmlsoap.org/soap/encoding/" );
94
95             QName JavaDoc qnameAttachment = new QName JavaDoc("urn:EchoAttachmentsService", "DataHandler");
96
97             call.setReturnType(qnameAttachment);
98
99             response = (String JavaDoc)call.invoke(new Object JavaDoc[0]); //Add the attachment.
100
}
101         catch (Exception JavaDoc e)
102         {
103             e.printStackTrace();
104         }
105         return response;
106     }
107         
108     /**
109      * This is the method that lets you call the endpoint and get a list of values back.
110      */

111
112     public Collection JavaDoc getCollection(String JavaDoc method)
113     {
114         Collection JavaDoc response = null;
115         
116         try
117         {
118             Service service = new Service();
119             Call call = (Call)service.createCall();
120
121             String JavaDoc endpoint = this.serviceUrl;
122
123             call.setTargetEndpointAddress(endpoint); //Set the target service host and service location,
124
call.setOperationName(new QName JavaDoc("http://soapinterop.org/", method)); //This is the target services method to invoke.
125
call.setEncodingStyle( "http://schemas.xmlsoap.org/soap/encoding/" );
126
127             QName JavaDoc qnameAttachment = new QName JavaDoc("urn:EchoAttachmentsService", "DataHandler");
128
129             call.setReturnType(qnameAttachment);
130
131             response = (Collection JavaDoc)call.invoke(new Object JavaDoc[0]); //Add the attachment.
132
}
133         catch (Exception JavaDoc e)
134         {
135             e.printStackTrace();
136         }
137         return response;
138     }
139
140     /**
141      * This is the method that lets you call the endpoint and get a map of values back.
142      */

143
144     public Map JavaDoc getMap(String JavaDoc method)
145     {
146         Map JavaDoc response = null;
147         
148         try
149         {
150             Service service = new Service();
151             Call call = (Call)service.createCall();
152
153             String JavaDoc endpoint = this.serviceUrl;
154
155             call.setTargetEndpointAddress(endpoint); //Set the target service host and service location,
156
call.setOperationName(new QName JavaDoc("http://soapinterop.org/", method)); //This is the target services method to invoke.
157
call.setEncodingStyle( "http://schemas.xmlsoap.org/soap/encoding/" );
158
159             QName JavaDoc qnameAttachment = new QName JavaDoc("urn:EchoAttachmentsService", "DataHandler");
160
161             call.setReturnType(qnameAttachment);
162
163             response = (Map JavaDoc)call.invoke(new Object JavaDoc[0]); //Add the attachment.
164
}
165         catch (Exception JavaDoc e)
166         {
167             response.put("errorStatus", new Integer JavaDoc(1));
168             response.put("errorMessage", e.getMessage());
169             e.printStackTrace();
170         }
171         return response;
172     }
173     
174     
175     /**
176      * This is the method that lets you call the endpoint and get a map of values back.
177      */

178
179     public Map JavaDoc getMap(String JavaDoc method, Map JavaDoc argument) throws SystemException
180     {
181         Map JavaDoc response = new HashMap JavaDoc();
182         
183         try
184         {
185             Service service = new Service();
186             Call call = (Call)service.createCall();
187
188             String JavaDoc endpoint = this.serviceUrl;
189
190             call.setTargetEndpointAddress(endpoint); //Set the target service host and service location,
191
call.setOperationName(new QName JavaDoc("http://soapinterop.org/", method)); //This is the target services method to invoke.
192
call.setEncodingStyle( "http://schemas.xmlsoap.org/soap/encoding/" );
193
194             QName JavaDoc qnameAttachment = new QName JavaDoc("urn:EchoAttachmentsService", "DataHandler");
195
196             call.addParameter("data", XMLType.XSD_STRING, ParameterMode.IN);
197
198             call.setReturnType(qnameAttachment);
199             Object JavaDoc[] args = {argument};
200             response = (Map JavaDoc)call.invoke(args); //Add the attachment.
201
}
202         catch (Exception JavaDoc e)
203         {
204             response.put("errorStatus", new Integer JavaDoc(1));
205             response.put("errorMessage", e.getMessage());
206             e.printStackTrace();
207         }
208         
209         return response;
210     }
211     
212     
213     /**
214      * A helper method that lets the template get hold of a Map-object to populate.
215      */

216     
217     public Map JavaDoc getMap()
218     {
219         return new HashMap JavaDoc();
220     }
221     
222     
223     /**
224      * This method returns true if the request to the webservice returned successfully.
225      */

226
227     public boolean getIsSuccessfull()
228     {
229         return this.isSuccessfull;
230     }
231
232     /**
233      * This method sets if the request to the webservice returned successfully.
234      */

235
236     public void setIsSuccessfull(boolean isSuccessfull)
237     {
238         this.isSuccessfull = isSuccessfull;
239     }
240
241     /**
242      * This method returns any message coming from the webservice.
243      */

244
245     public String JavaDoc getMessage()
246     {
247         return this.message;
248     }
249
250     /**
251      * This method sets a message from the webservice.
252      */

253     
254     public void setMessage(String JavaDoc message)
255     {
256         this.message = message;
257     }
258 }
Popular Tags