1 /* @source ajsoap *************************************************************
2 **
3 ** support for webservices access using Axis2C library
4 **
5 ** @author Copyright (c) 2009 Mahmut Uludag
6 ** @version $Revision: 1.8 $
7 ** @modified $Date: 2011/10/18 14:23:39 $ by $Author: rice $
8 ** @@
9 **
10 ** This library is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU Lesser General Public
12 ** License as published by the Free Software Foundation; either
13 ** version 2.1 of the License, or (at your option) any later version.
14 **
15 ** This library is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ** Lesser General Public License for more details.
19 **
20 ** You should have received a copy of the GNU Lesser General Public
21 ** License along with this library; if not, write to the Free Software
22 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 ** MA  02110-1301,  USA.
24 **
25 ******************************************************************************/
26 
27 #include "ajsoap.h"
28 #include "ajlib.h"
29 #include "ajnam.h"
30 #include "ajutil.h"
31 
32 
33 
34 
35 #ifdef HAVE_AXIS2C
36 
37 #define AXIS_NS "http://xml.apache.org/axis/"
38 
39 static char axis2C_HOME[] = AXIS2C_LOC;
40 
41 
42 
43 
44 /* @func ajSoapAxis2Call ******************************************************
45 **
46 ** Makes an axis2 webservices call and logs/reports its raw results
47 **
48 ** @param [u] client [axis2_svc_client_t*] axis2 client obj
49 ** @param [r] env [const axutil_env_t*] axis2 environment obj
50 ** @param [u] payload [axiom_node_t*] webservices input
51 ** @return [axiom_node_t*] axis2 OM object, webservices output
52 **
53 ** @release 6.4.0
54 ** @@
55 ******************************************************************************/
56 
ajSoapAxis2Call(axis2_svc_client_t * client,const axutil_env_t * env,axiom_node_t * payload)57 axiom_node_t* ajSoapAxis2Call(axis2_svc_client_t *client,
58                               const axutil_env_t *env,
59                               axiom_node_t *payload)
60 {
61     axiom_node_t *ret_node = NULL;
62     axis2_char_t * om_str  = NULL;
63 
64     if (ajDebugOn())
65     {
66 	om_str = axiom_node_to_string(payload, env);
67 
68 	if (om_str)
69 	{
70 	    ajDebug("\nSending OM : %s\n", om_str);
71 	    AXIS2_FREE(env->allocator, om_str);
72 	    om_str = NULL;
73 	}
74     }
75 
76     /* Send request */
77     ret_node = axis2_svc_client_send_receive(client, env, payload);
78 
79     if (ret_node)
80     {
81 	if(ajDebugOn())
82 	{
83 	    om_str = axiom_node_to_string(ret_node, env);
84 
85 	    if (om_str)
86 		ajDebug("\nReceived OM : %s\n", om_str);
87 
88 	    AXIS2_FREE(env->allocator, om_str);
89 	    om_str = NULL;
90 	}
91     }
92     else
93     {
94 	ajDebug("seqAxis2wsCall: webservice call failed: %s\n",
95 	                AXIS2_ERROR_GET_MESSAGE(env->error));
96 	ajErr("webservice call FAILED: %s\n",
97 		AXIS2_ERROR_GET_MESSAGE(env->error));
98     }
99 
100     return ret_node;
101 }
102 
103 
104 
105 
106 /* @func ajSoapAxis2GetClient *************************************************
107 **
108 ** Returns an axis2 client object for web services calls
109 **
110 ** @param [r] env [const axutil_env_t*] axis2 environment
111 ** @param [r] address [const axis2_char_t*] webservices endpoint
112 ** @return [axis2_svc_client_t*] axis2 service client
113 **
114 ** @release 6.4.0
115 ** @@
116 ******************************************************************************/
117 
ajSoapAxis2GetClient(const axutil_env_t * env,const axis2_char_t * address)118 axis2_svc_client_t* ajSoapAxis2GetClient(const axutil_env_t *env,
119                                          const axis2_char_t *address)
120 {
121     AjPStr home = NULL;
122 
123     axis2_endpoint_ref_t* endpoint_ref = NULL;
124     axis2_options_t* options           = NULL;
125     axis2_svc_client_t* svc_client     = NULL;
126 
127     options      = axis2_options_create(env);
128 
129     endpoint_ref = axis2_endpoint_ref_create(env, address);
130 
131     axis2_options_set_soap_version(options, env, AXIOM_SOAP11);
132     axis2_options_set_to(options, env, endpoint_ref);
133 
134     /* Set up axis2c location.
135     ** This is either from:
136     ** EMBOSS_AXIS2C_HOME: Used by mEMBOSS as axis2c files are
137     **               provided by the setup and there could
138     **               be confusion with an existing axis2c
139     **               installation otherwise.
140     ** axis2C_HOME: The location specified during the
141     **              EMBOSS configuration.
142     **
143     ** The client uses this information to pick up the
144     ** libraries, modules and axis2.xml file
145     */
146 
147     if(!ajNamGetValueC("AXIS2C_HOME",&home))
148         home = ajStrNewC(axis2C_HOME);
149 
150     /* Create service client */
151     svc_client = axis2_svc_client_create(env, ajStrGetPtr(home));
152 
153     if (!svc_client)
154     {
155 	ajErr("Error creating webservice client: %s",
156 	      AXIS2_ERROR_GET_MESSAGE(env->error));
157 	return NULL;
158     }
159 
160     axis2_svc_client_set_options(svc_client, env, options);
161     axis2_svc_client_engage_module(svc_client, env, AXIS2_MODULE_ADDRESSING);
162 
163     ajStrDel(&home);
164 
165     return svc_client;
166 }
167 
168 
169 
170 
171 /* @func ajSoapAxis2Error *****************************************************
172 **
173 ** Processes a webservices fault object received after a webservices call,
174 ** and generates an ajax error unless it is wsdbfetch no-entry-found exception.
175 **
176 ** @param [u] fault [axiom_node_t*] fault object
177 **                                  received after a webservices call
178 ** @param [r] env [const axutil_env_t*] axis2 environment obj
179 ** @return [AjBool] returns true if the fault object is recognised
180 **
181 ** @release 6.4.0
182 ** @@
183 ******************************************************************************/
184 
ajSoapAxis2Error(axiom_node_t * fault,const axutil_env_t * env)185 AjBool ajSoapAxis2Error(axiom_node_t *fault, const axutil_env_t *env)
186 {
187     axis2_char_t* faultcode = NULL;
188     axis2_char_t* faultmsg  = NULL;
189     axis2_char_t* exception = NULL;
190     axiom_element_t* parent = NULL;
191     axiom_element_t* child  = NULL;
192     axiom_node_t* node      = NULL;
193     axutil_qname_t* qname   = NULL;
194 
195     parent = axiom_node_get_data_element(fault, env);
196 
197     if (!parent)
198 	return ajFalse;
199 
200     /* fault->faultcode */
201     qname = axutil_qname_create(env, "faultcode", NULL, "");
202     child = axiom_element_get_first_child_with_qname(parent, env, qname,
203                                                      fault, &node);
204     if (child)
205 	faultcode = axiom_element_get_text(child, env, node);
206 
207     axutil_qname_free(qname, env);
208 
209     /* fault->faultstring */
210     qname = axutil_qname_create(env, "faultstring", NULL, "");
211     child = axiom_element_get_first_child_with_qname(parent, env, qname,
212                                                      fault, &node);
213     if (child)
214 	faultmsg = axiom_element_get_text(child, env, node);
215 
216     axutil_qname_free(qname, env);
217 
218     /* fault->detail */
219     qname = axutil_qname_create(env, "detail", NULL, "");
220     parent = axiom_element_get_first_child_with_qname(parent, env, qname,
221                                                       fault, &node);
222     axutil_qname_free(qname, env);
223 
224     if(!parent)
225 	return ajFalse;
226 
227     /* fault->detail->exceptionName */
228     qname = axutil_qname_create(env, "exceptionName", AXIS_NS , "");
229     child = axiom_element_get_first_child_with_qname(parent, env, qname,
230                                                      node, &node);
231     if(child)
232 	exception = axiom_element_get_text(child, env, node);
233 
234 
235     if(!strstr(exception, "DbfNoEntryFoundException"))
236 	ajErr("webservices error: %s, %s %s", faultcode, faultmsg, exception);
237 
238     axutil_qname_free(qname, env);
239 
240     return ajTrue;
241 }
242 
243 
244 
245 
246 /* @func ajSoapAxis2GetEnv ****************************************************
247 **
248 ** Returns an axis2 environment object for webservices calls
249 **
250 ** @return [axutil_env_t*] axis2 environment object
251 **
252 ** @release 6.4.0
253 ** @@
254 ******************************************************************************/
255 
ajSoapAxis2GetEnv(void)256 axutil_env_t* ajSoapAxis2GetEnv(void)
257 {
258     axutil_env_t* env = NULL;
259     AjPStr ax2log     = NULL;
260 
261     ax2log = ajStrNew();
262 
263     /* If debug mode is on, an extra log-file with .ax2log extension
264      * is created in current working directory. Otherwise axis2 log
265      * messages are not recorded */
266 
267     ajFmtPrintS(&ax2log, "%s%S.ax2log", CURRENT_DIR, ajUtilGetProgram());
268 
269     if(ajDebugOn())
270 	env = axutil_env_create_all(ajStrGetPtr(ax2log),
271 	                            AXIS2_LOG_LEVEL_DEBUG);
272     else
273 	env = axutil_env_create_all(_BLACKHOLE,
274 	                            AXIS2_LOG_LEVEL_ERROR);
275 
276     ajStrDel(&ax2log);
277 
278     return env;
279 }
280 
281 
282 #endif
283