How to Consume a Simple Java Web Service From a C++(Legacy) & Java Application
Intro: How to Consume a Simple Java Web Service From a C++(Legacy) & Java Application
You need to install the following softwares(Search Google):
Apache Tomcat(on WIndows7 in my case)
Apache Axis(on WIndows7 in my case)
GSoap(on Redhat running on Virtual Box in my case)
Video:
First , write a functionality which you want to be consumed by a JAVA/C++ application as a Web Service & save it as *.jws in your axis installation director(\Tomcat 7.0\webapps\axis). After this run the http:///axis/EncryptDecrypt.jws?wsdl in your browser to test the wsdl.
EncryptDecrypt.jws
public class EncryptDecrypt {
public String encDec(String _inpDat){
String _outDat=new String();
int j=1;
if(_inpDat.charAt(0)!='#')
{
_outDat+="#";
j=0;
}
for(int i=j;i<_inpDat.length();i++)
{
_outDat+=_inpDat.charAt(0)=='#'?(char)((int)_inpDat.charAt(i)-1):(char)((int)_inpDat.charAt(i)+1);
}
return _outDat;
}}
Secondly, we design the client application in JAVA/C++
JavaWebClient.java
import org.apache.axis.client.Call;
public class JavaWebClient {
public String main(String args) throws Exception {
String url="http:///axis/EncryptDecrypt.jws";
Call data= new Call(url);
String val= (String)data.invoke("","encDec",new Object [] {args}); //encDec is the function name from EncryptDecrypt.jws
return val;
}
}
For C++ client you need to perform the following steps on the console:
wsdl2h -o EncryptDecrypt.h http://:8080/axis/EncryptDecrypt.jws?wsdl( this will generate a header file from wsdl file)
soapcpp2 -i -C -I/import EncryptDecrypt.h (this will generate the cpp files needed to call the webservice)
Apache Tomcat(on WIndows7 in my case)
Apache Axis(on WIndows7 in my case)
GSoap(on Redhat running on Virtual Box in my case)
Video:
First , write a functionality which you want to be consumed by a JAVA/C++ application as a Web Service & save it as *.jws in your axis installation director(\Tomcat 7.0\webapps\axis). After this run the http:///axis/EncryptDecrypt.jws?wsdl in your browser to test the wsdl.
EncryptDecrypt.jws
public class EncryptDecrypt {
public String encDec(String _inpDat){
String _outDat=new String();
int j=1;
if(_inpDat.charAt(0)!='#')
{
_outDat+="#";
j=0;
}
for(int i=j;i<_inpDat.length();i++)
{
_outDat+=_inpDat.charAt(0)=='#'?(char)((int)_inpDat.charAt(i)-1):(char)((int)_inpDat.charAt(i)+1);
}
return _outDat;
}}
Secondly, we design the client application in JAVA/C++
JavaWebClient.java
import org.apache.axis.client.Call;
public class JavaWebClient {
public String main(String args) throws Exception {
String url="http:///axis/EncryptDecrypt.jws";
Call data= new Call(url);
String val= (String)data.invoke("","encDec",new Object [] {args}); //encDec is the function name from EncryptDecrypt.jws
return val;
}
}
For C++ client you need to perform the following steps on the console:
wsdl2h -o EncryptDecrypt.h http://:8080/axis/EncryptDecrypt.jws?wsdl( this will generate a header file from wsdl file)
soapcpp2 -i -C -I/import EncryptDecrypt.h (this will generate the cpp files needed to call the webservice)
STEP 1: Create & Execute C++ App
Now, we will create Xmotif application.
Cclient.c
#include
#include
#include "soapEncryptDecryptSoapBindingProxy.h" // obtain the generated stub
#include "EncryptDecryptSoapBinding.nsmap"
#include
#include
void callWeb();
Widget text,text1;
main (int argc, char *argv[])
{
XtAppContext app;
Widget toplevel, button;
void my_callback(Widget, XtPointer, XtPointer);
XmString btn_text;
Arg args[2];
XtSetLanguageProc (NULL, NULL, NULL);
toplevel = XtVaOpenApplication (&app, "Web Client", NULL, 0, &argc, argv, NULL,
sessionShellWidgetClass, NULL);
Widget form=XtVaCreateManagedWidget("",xmFormWidgetClass,
toplevel,XmNrightAttachment,XmATTACH_FORM,XmNy,10,XmNwidth,447,XmNheight,587,XmNbackground,0xa1b6d6,NULL);
Widget firstLabel=XtVaCreateManagedWidget("",xmLabelWidgetClass,form,
XmNlabelString,XmStringCreateLocalized("Encrypt/Decrypt Web Service Client by Saurabh Saxena"),XmNbackground,0xc1ffff,XmNtopAttachment
,XmATTACH_FORM,XmNleftAttachment,XmATTACH_FORM,XmNrightAttachment,XmATTACH_FORM,NULL);
Widget Label=XtVaCreateManagedWidget("",xmLabelWidgetClass,form,XmNlabelString,XmStringCreateLocalized("Enter Text"),XmNbackground,0xc1ffcc,
XmNtopAttachment,XmATTACH_WIDGET,XmNtopWidget,firstLabel,XmNleftAttachment,XmATTACH_FORM,XmNwidth,75,XmNheight,29,NULL);
text=XtVaCreateManagedWidget("",xmTextWidgetClass,form,XmNleftAttachment,XmATTACH_WIDGET,XmNleftWidget,Label,XmNtopAttachment,
XmATTACH_WIDGET,XmNtopWidget,firstLabel,XmNwidth,75,XmNheight,29,XmNbackground,0xffffff,XmNeditable,TRUE,XmNcursorPositionVisible,
FALSE,XmNhighlightThickness,0,XmNshadowThickness,1,NULL);
button = XtVaCreateManagedWidget("",xmPushButtonWidgetClass,form,XmNtopAttachment,XmATTACH_WIDGET,XmNtopWidget,firstLabel,
XmNleftAttachment,XmATTACH_WIDGET,XmNleftWidget,text,XmNlabelString,XmStringCreateSimple("Click"),XmNheight,20,XmNwidth,40,NULL);
Widget Label2=XtVaCreateManagedWidget("",xmLabelWidgetClass,form,XmNlabelString,XmStringCreateLocalized("Output Text"),XmNbackground,
0xc1ffcc,XmNtopAttachment,XmATTACH_WIDGET,XmNtopWidget,Label,XmNleftAttachment,XmATTACH_FORM,XmNwidth,75,XmNheight,29,NULL);
text1=XtVaCreateManagedWidget("",xmTextWidgetClass,form,XmNleftAttachment,XmATTACH_WIDGET,XmNleftWidget,Label2,XmNtopAttachment,
XmATTACH_WIDGET,XmNtopWidget,text,XmNwidth,75,XmNheight,29,XmNbackground,0xffffff,XmNeditable,FALSE,XmNcursorPositionVisible,
FALSE,XmNhighlightThickness,0,XmNshadowThickness,1,NULL); XtAddCallback (button, XmNactivateCallback, my_callback, NULL); XtManageChild (button); XtRealizeWidget (toplevel);
XtAppMainLoop (app);}void my_callback (Widget w, XtPointer client_data, XtPointer call_data)
{ XmPushButtonCallbackStruct *cbs =(XmPushButtonCallbackStruct *) call_data; callWeb();
}void callWeb()
{ EncryptDecryptSoapBindingProxy service; std::string result;
if (service.encDec(XmTextGetString(text),result) == SOAP_OK)
{
std::cout << "Result" << result << std::endl;
char *cstr = new char[result.length() + 1];
strcpy(cstr, result.c_str());
XmTextSetString(text1,cstr);
delete [] cstr;
}
else
service.soap_stream_fault(std::cerr);
}
Compile the above code using the following command:
g++ -o Cclient Cclient.c soapC.cpp soapEncryptDecryptSoapBindingProxy.cpp -lgsoap++ -lXm -lXt -lX11
Run the Executable to access the functionality provided by Web Service.
Cclient.c
#include
#include
#include "soapEncryptDecryptSoapBindingProxy.h" // obtain the generated stub
#include "EncryptDecryptSoapBinding.nsmap"
#include
#include
void callWeb();
Widget text,text1;
main (int argc, char *argv[])
{
XtAppContext app;
Widget toplevel, button;
void my_callback(Widget, XtPointer, XtPointer);
XmString btn_text;
Arg args[2];
XtSetLanguageProc (NULL, NULL, NULL);
toplevel = XtVaOpenApplication (&app, "Web Client", NULL, 0, &argc, argv, NULL,
sessionShellWidgetClass, NULL);
Widget form=XtVaCreateManagedWidget("",xmFormWidgetClass,
toplevel,XmNrightAttachment,XmATTACH_FORM,XmNy,10,XmNwidth,447,XmNheight,587,XmNbackground,0xa1b6d6,NULL);
Widget firstLabel=XtVaCreateManagedWidget("",xmLabelWidgetClass,form,
XmNlabelString,XmStringCreateLocalized("Encrypt/Decrypt Web Service Client by Saurabh Saxena"),XmNbackground,0xc1ffff,XmNtopAttachment
,XmATTACH_FORM,XmNleftAttachment,XmATTACH_FORM,XmNrightAttachment,XmATTACH_FORM,NULL);
Widget Label=XtVaCreateManagedWidget("",xmLabelWidgetClass,form,XmNlabelString,XmStringCreateLocalized("Enter Text"),XmNbackground,0xc1ffcc,
XmNtopAttachment,XmATTACH_WIDGET,XmNtopWidget,firstLabel,XmNleftAttachment,XmATTACH_FORM,XmNwidth,75,XmNheight,29,NULL);
text=XtVaCreateManagedWidget("",xmTextWidgetClass,form,XmNleftAttachment,XmATTACH_WIDGET,XmNleftWidget,Label,XmNtopAttachment,
XmATTACH_WIDGET,XmNtopWidget,firstLabel,XmNwidth,75,XmNheight,29,XmNbackground,0xffffff,XmNeditable,TRUE,XmNcursorPositionVisible,
FALSE,XmNhighlightThickness,0,XmNshadowThickness,1,NULL);
button = XtVaCreateManagedWidget("",xmPushButtonWidgetClass,form,XmNtopAttachment,XmATTACH_WIDGET,XmNtopWidget,firstLabel,
XmNleftAttachment,XmATTACH_WIDGET,XmNleftWidget,text,XmNlabelString,XmStringCreateSimple("Click"),XmNheight,20,XmNwidth,40,NULL);
Widget Label2=XtVaCreateManagedWidget("",xmLabelWidgetClass,form,XmNlabelString,XmStringCreateLocalized("Output Text"),XmNbackground,
0xc1ffcc,XmNtopAttachment,XmATTACH_WIDGET,XmNtopWidget,Label,XmNleftAttachment,XmATTACH_FORM,XmNwidth,75,XmNheight,29,NULL);
text1=XtVaCreateManagedWidget("",xmTextWidgetClass,form,XmNleftAttachment,XmATTACH_WIDGET,XmNleftWidget,Label2,XmNtopAttachment,
XmATTACH_WIDGET,XmNtopWidget,text,XmNwidth,75,XmNheight,29,XmNbackground,0xffffff,XmNeditable,FALSE,XmNcursorPositionVisible,
FALSE,XmNhighlightThickness,0,XmNshadowThickness,1,NULL); XtAddCallback (button, XmNactivateCallback, my_callback, NULL); XtManageChild (button); XtRealizeWidget (toplevel);
XtAppMainLoop (app);}void my_callback (Widget w, XtPointer client_data, XtPointer call_data)
{ XmPushButtonCallbackStruct *cbs =(XmPushButtonCallbackStruct *) call_data; callWeb();
}void callWeb()
{ EncryptDecryptSoapBindingProxy service; std::string result;
if (service.encDec(XmTextGetString(text),result) == SOAP_OK)
{
std::cout << "Result" << result << std::endl;
char *cstr = new char[result.length() + 1];
strcpy(cstr, result.c_str());
XmTextSetString(text1,cstr);
delete [] cstr;
}
else
service.soap_stream_fault(std::cerr);
}
Compile the above code using the following command:
g++ -o Cclient Cclient.c soapC.cpp soapEncryptDecryptSoapBindingProxy.cpp -lgsoap++ -lXm -lXt -lX11
Run the Executable to access the functionality provided by Web Service.