Axis2/C Basics

Source: Internet
Author: User

Axis2/C is a Web Service engine implemented in C language.

Based on the Axis2 architecture, Axis2/C supports SOAP1.1 and SOAP1.2 protocols and RESTful Web Services. Axis2/C-based Web services can be exposed to both SOAP and RESTful services.

I recently studied Axis2/C. This article details how to use Axis2/C to publish a Web Service and access this Web Service through SOAP.

1. Download and install Axis2/C
1. You can download the latest Axis2/C Release package: Axis2/C from the official website of Axis2/C.

2. Decompress axis2c-bin-1.6.0-win32.zip, add AXIS2_HOME to the system environment variable, and point to the Axis2/C decompress directory. This ensures that the dependencies of Axis2/C can be found during subsequent compilation;

3. Find the Path item in the system environment variable, edit the item, add C: VS8Common7IDE to its beginning, and separate it with semicolons, this ensures that the compiled client program can find the dependencies smoothly;

4. Add % AXIS2_HOME % lib at the end of Path to ensure that the compiler can find the dependencies of Axis2/C;

2. Build and Release Services
5. Create a CPP file named hello_svc.c and add the following code:
1 # include "stdafx. h"
2
3 # include <axis2_svc_skeleton.h>
4 # include <axutil_log_default.h>
5 # include <axutil_error_default.h>
6 # include <axutil_array_list.h>
7 # include <axiom_text.h>
8 # include <axiom_node.h>
9 # include <axiom_element.h>
10 # include <stdio. h>
11
12 axiom_node_t * axis2_hello_greet (const axutil_env_t * env, axiom_node_t * node );
13 int AXIS2_CALL hello_free (axis2_svc_skeleton_t * svc_skeleton, const axutil_env_t * env );
14 axiom_node_t * AXIS2_CALL hello_invoke (optional * svc_skeleton, const axutil_env_t * env, axiom_node_t * node, axis2_msg_ctx_t * msg_ctx );
15 int AXIS2_CALL hello_init (axis2_svc_skeleton_t * svc_skeleton, const axutil_env_t * env );
16 axiom_node_t * AXIS2_CALL hello_on_fault (axis2_svc_skeleton_t * svc_skeli, const axutil_env_t * env, axiom_node_t * node );
17 axiom_node_t * build_greeting_response (const axutil_env_t * env, axis2_char_t * greeting );
18
19 axiom_node_t * axis2_hello_greet (const axutil_env_t * env, axiom_node_t * node)
20 {
21 axiom_node_t * client_greeting_node = NULL;
22 axiom_node_t * return_node = NULL;
23 AXIS2_ENV_CHECK (env, NULL );
24 if (node)
25 {
26 client_greeting_node = axiom_node_get_first_child (node, env );
27 if (client_greeting_node & axiom_node_get_node_type (client_greeting_node, env) = AXIOM_TEXT)
28 {
29 axiom_text_t * greeting = (axiom_text_t *) axiom_node_get_data_element (client_greeting_node, env );
30 if (greeting & axiom_text_get_value (greeting, env ))
31 {
32 const axis2_char_t * greeting_str = axiom_text_get_value (greeting, env );
33 printf ("Client greeted saying" % s "", greeting_str );
34 return_node = build_greeting_response (env, "Hello Client! ");
35}
36}
37}
38 else
39 {
40 AXIS2_ERROR_SET (env-> error, AXIS2_ERROR_SVC_SKEL_INVALID_XML_FORMAT_IN_REQUEST, AXIS2_FAILURE );
41 printf ("ERROR: invalid XML in request ");
42 return_node = build_greeting_response (env, "Client! Who are you? ");
43}
44 return return_node;
45}
46
47
48
49 axiom_node_t * build_greeting_response (const axutil_env_t * env, axis2_char_t * greeting)
50 {
51 axiom_node_t * greeting_om_node = NULL;
52 axiom_element_t * greeting_om_ele = NULL;
53 greeting_om_ele = axiom_element_create (env, NULL, "greetResponse", NULL, & greeting_om_node );
54 axiom_element_set_text (greeting_om_ele, env, greeting, greeting_om_node );
55 return greeting_om_node;
56}
57
58 static const axis2_svc_skeleton_ops_t hello_svc_skeleton_ops_var = {
59 hello_init,
60 hello_invoke,
61 hello_on_fault,
62 hello_free
63 };
64
65 axis2_svc_skeleton_t * axis2_hello_create (const axutil_env_t * env)
66 {
67 axis2_svc_skeleton_t * svc_skeleton = NULL;
68 svc_skeleton = (axis2_svc_skeleton_t *) AXIS2_MALLOC (env-> allocator, sizeof (axis2_svc_skeleton_t ));
69 svc_skeleton-> ops = & hello_svc_skeleton_ops_var;
70 svc_skeleton-> func_array = NULL;
71 return svc_skeleton;
72}
73
74 int AXIS2_CALL hello_init (axis2_svc_skeleton_t * svc_skeleton, const axutil_env_t * env)
75 {
76 svc_skeleton-> func_array = axutil_array_list_create (env, 0 );
77 axutil_array_list_add (svc_skeleton-> func_array, env, "helloString ");
78 return AXIS2_SUCCESS;
79}
80
81
82
83 axiom_node_t * AXIS2_CALL hello_invoke (optional * svc_skeleton, const axutil_env_t * env, axiom_node_t * node, axis2_msg_ctx_t * msg_ctx)
84 {
85 return axis2_hello_greet (env, node );
86}
87
88 axiom_node_t * AXIS2_CALL hello_on_fault (axis2_svc_skeleton_t * svc_skeli, const axutil_env_t * env, axiom_node_t * node)
89 {
90 axiom_node_t * error_node = NULL;
91 axiom_node_t * text_node = NULL;
92 axiom_element_t * error_ele = NULL;
93 error_ele = axiom_element_create (env, node, "EchoServiceError", NULL,
94 & error_node );
95 axiom_element_set_text (error_ele, env, "Echo service failed ",
96 text_node );
97 return error_node;
98}
99
100
101
102 int AXIS2_CALL hello_free (axis2_svc_skeleton_t * svc_skeleton,
103 const axutil_env_t * env)
104 {
105 if (svc_skeleton-> func_array)
106 {
107 axutil_array_list_free (svc_skeleton-> func_array, env );
108 svc_skeleton-> func_array = NULL;
109}
110
111 if (svc_skeleton)
112 {
113 AXIS2_FREE (env-> allocator, svc_skeleton );
114 svc_skeleton = NULL;
115}
116 return AXIS2_SUCCESS;
117}
118
119 AXIS2_EXPORT int axis2_get_instance (axis2_svc_skeleton_t ** inst, const axutil_env_t * env)
120 {
121 * inst = axis2_hello_create (env );
122 if (! (* Inst ))
123 {
124 return AXIS2_FAILURE;
125}
126 return AXIS2_SUCCESS;
127}
128
129
130
131 AXIS2_EXPORT int axis2_remove_instance (axis2_svc_skeleton_t * inst, const axutil_env_t * env)
132 {
133 axis2_status_t status = AXIS2_FAILURE;
134 if (inst)
135 {
136 status = AXIS2_SVC_SKELETON_FREE (inst, env );
137}
138 return status;
139}
140
6. Compile the above Code and run the following command on the command line:
$ C: VS8VCincl.exe/D "WIN32"/D "_ WINDOWS"/D "_ MBCS"/D "AXIS2_DECLARE_EXPORT"/D "AXIS2_SVR_MULTI_THREADED"/w/nologo/I % AXIS2_HOME % include/I c: \ VS8 \ VC \ include/c hello_svc.c
7. link the generated intermediate object and generate the DLL. Run the following command:
$ C: VS8VCinlink.exe/nologo/LIBPATH: % AXIS2_HOME % lib/LIBPATH: C: \ VS8 \ VC \ lib axutil. lib axiom. lib axis2_parser.lib axis2_engine.lib/DLL/OUT: hello. dll *. obj
8. Create the directory hello under % AXIS2_HOME % services, copy the hello. dll generated in Step 7 to it, create the service description file services. xml, and insert the following content:
1 <? Xml version = "1.0" encoding = "UTF-8"?>
2 <service name = "hello">
3 <parameter name = "ServiceClass" locked = "xsd: false"> hello </parameter>
4 <description>
5 Quick start guide hello service sample.
6 </description>
7 <operation name = "greet">
8 <parameter name = "wsamapping"> http://ws.apache.org/axis2/c/samples/hello </parameter>
9 </operation>
10 </service>
9. Start the Axis2 server and run the following command:
$ Axis2_http_server
Iii. Access Services
10. Create the project Axis2Test in VS and insert the code in Axis2Test. cpp:
1 # include "stdafx. h"
2
3 # include <stdio. h>
4 # include <axiom. h>
5 # include <axis2_util.h>
6 # include <axiom_soap.h>
7 # include <axis2_client.h>
8
9 axiom_node_t * build_om_request (const axutil_env_t * env );
10 const axis2_char_t * process_om_response (const axutil_env_t * env, axiom_node_t * node );
11
12 int _ tmain (int argc, char ** argv)
13 {
14 const axutil_env_t * env = NULL;
15 const axis2_char_t * address = NULL;
16 axis2_endpoint_ref_t * endpoint_ref = NULL;
17 axis2_options_t * options = NULL;
18 const axis2_char_t * client_home = NULL;
19 axis2_svc_client_t * svc_client = NULL;
20 axiom_node_t * payload = NULL;
21 axiom_node_t * ret_node = NULL;
22
23 env = axutil_env_create_all ("hello_client.log", AXIS2_LOG_LEVEL_TRACE );
24 options = axis2_options_create (env );
25 address = "http: // localhost: 9090/axis2/services/hello ";
26
27 if (argc> 1)
28 address = argv [1];
29
30 if (axutil_strcmp (address, "-h") = 0)
31 {
32 printf ("Usage: % s [endpoint_url]", argv [0]);
33 printf ("use-h for help ");
34 return 0;
35}
36
37 printf ("Using endpoint: % s", address );
38 endpoint_ref = axis2_endpoint_ref_create (env, address );
39 axis2_options_set_to (options, env, endpoint_ref );
40 axis2_options_set_action (options, env, "http://ws.apache.org/axis2/c/samples/hello ");
41
42 client_home = "E: \ mengli \ axis2c-bin-1.6.0-win32"; // AXIS2_GETENV ("AXIS2C_HOME ");
43 if (! Client_home &&! Strcmp (client_home ,""))
44 client_home = "../..";
45
46 svc_client = axis2_svc_client_create (env, client_home );
47 if (! Svc_client)
48 {
49 printf ("Error creating service client ");
50 AXIS2_LOG_ERROR (env-> log, AXIS2_LOG_SI, "Stub invoke FAILED: Error code :"
51 "% d: % s", env-> error-> error_number,
52 AXIS2_ERROR_GET_MESSAGE (env-> error ));
53 return-1;
54}
55
56 axis2_svc_client_set_options (svc_client, env, options );
57 payload = build_om_request (env );
58 ret_node = axis2_svc_client_send_receive (svc_client, env, payload );
59
60 if (ret_node)
61 {
62 const axis2_char_t * greeting = process_om_response (env, ret_node );
63 if (greeting)
64 printf ("Received greeting:" % s "from service", greeting );
65
66 axiom_node_free_tree (ret_node, env );
67 ret_node = NULL;
68}
69 else
70 {
71 AXIS2_LOG_ERROR (env-> log, AXIS2_LOG_SI, "Stub invoke FAILED: Error code :"
72 "% d: % s", env-> error-> error_number,
73 AXIS2_ERROR_GET_MESSAGE (env-> error ));
74 printf ("hello client invoke FAILED! ");
75}
76
77 if (svc_client)
78 {
79 axis2_svc_client_free (svc_client, env );
80 svc_client = NULL;
81}
82
83 if (env)
84 {
85 axutil_env_free (axutil_env_t *) env );
86 env = NULL;
87}
88
89 return 0;
90
91}
92
93 axiom_node_t *
94 build_om_request (const axutil_env_t * env)
95 {
96 axiom_node_t * greet_om_node = NULL;
97 axiom_element_t * greet_om_ele = NULL;
98
99 greet_om_ele = axiom_element_create (env, NULL, "greet", NULL, & greet_om_node );
100 axiom_element_set_text (greet_om_ele, env, "Hello Server! ", Greet_om_node );
101
102 return greet_om_node;
103}
104
105
106
107 const axis2_char_t *
108 process_om_response (const axutil_env_t * env,
109 axiom_node_t * node)
110 {
111 axiom_node_t * service_greeting_node = NULL;
112 axiom_node_t * return_node = NULL;
113
114 if (node)
115 {
116 service_greeting_node = axiom_node_get_first_child (node, env );
117
118 if (service_greeting_node &&
119 axiom_node_get_node_type (service_greeting_node, env) = AXIOM_TEXT)
120 {
121 axiom_text_t * greeting = (axiom_text_t *) axiom_node_get_data_element (service_greeting_node, env );
122 if (greeting & axiom_text_get_value (greeting, env ))
123 {
124 return axiom_text_get_value (greeting, env );
125}
126}
127}
128
129 return NULL;
130}
11. Compile and execute the program and send the string Hello Service to the remote Service !, And get the response Hello Client!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.