Use CXF client api with Java web application - web-services

How to consume Web service suing Apache CXF client API.
I have generated the client Code using eclispe but I didn't found any document specifying how to use that generated code in my web application.
How to configure CXF? I am using tomcat to run my java web appliation.
How to use the generated code?
Do I need to add anyhting in my my web.xml?
I have downloaded CXF binaries from apache CXF website but don't know which libraries are needed. I am affraid i may end up adding all the jars.
I am using Tomcat 7, Java 1.6 and plane jsp/Servlet for my application
I am new to web services.
Thanks in advance

One sample code that may help.
URL wsdlurl=SOAPWebServiceTransport.class.getClassLoader().
getResource("my.wsdl");
// MyService will be one of the service class that you have generated (with different name ofcourse)and which must be extending Service class
//getOnlineServicePort will be a method (with different name ofcourse) in your service class which will give you the interface referrer using which you'll call the webservice method
OnlinePort service= new MyService(wsdlurl).getOnlineServicePort();
Client proxy = ClientProxy.getClient(service);
//configure security user name password as required
//Add interceptors if needed
//Now you can directly call the webservice methods using the service object
service.method(parameter)
you can also refer to some example one is here

Related

WSO2 ESB Identity Server and Web Service Client

I'm refering to the following article
http://wso2.com/library/articles/2010/10/using-xacml-fine-grained-authorization-wso2-platform/
I would like to use the sample echoService from the WSO2 AS over a secured proxy in WSO2 ESB in combination with the Identity Server for fine-grained authorization. All the settings mentioned on this page seem to work, however I am stuck concerning the client part. I use NetBeans and the given client code, but the .jars in the classpath there have older versions then the ones in the current version of WSO2 IS, so I started to exchange them manually. Now I get some exceptions like
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.xml.security.transforms.Transform.init()V
and I am stuck again. I just want to test the echoService in this constellation and send some string over the ESB via IS and receive the response(if I have the appropriate role) from the AS, is there not another client or how could I test it else?
Thank you!
I can suggest you 3 options:
Use SoapUI to test the service which is the easiest way to test a web service.
Generate the stub for the service and have stub as the dependency in your client. You can use the WSDL2Java tool that ship with AS. Loging to AS --> Tools in left pane --> WSDL2Java --> Provide the wsdl URL and generate the stub jar.
Generate correct dependency libs. Go to [IS-Home]/bin folder, and issue that command "ant" to run the build.xml, this will copy all required libs to [IS-HOME]/repository/lib/ folder. Have them in your class path.

IP-addresses Log file by all requests in MULE CE 3.3.0?

in MULE CE 3.3.0 I want to implement this process:
1- Post- office has a service for giving postal-code to clients. So post-office creates a WSDL-file for its service.
2- Here, our company is a connector between post-office and clients. Our company using mule and create another WSDL file based on post-office’s WSDL file and published out the WSDL for client usage.
3- Company-A and Company-B, get the WSDL-file URL and for instance in My-eclipse IDE or any other IDEs create a portlet and deploy it in a liferay portal as a web-service for displaying postal-code to its clients.
During this process I want to have a log file of ip-addresses. It means, I want to after each request that Company-A’s client or Company-B’s client sent to the server(Our company), it’s Ip-address insert into a database or in a file.
I illustrated my position in the image by a red Arrow. Now I want to put an script in MULE server that and gather all the ip addresses that Company-A's and Company-B's customers who use post-code webservice.
Can I use cxf-interceptor for this issuse ? and how? guide me?
As genjosanzo has suggested in https://stackoverflow.com/a/15993127/387927, you can access all the Mule headers in a CXF interceptor. This means that yes, you can achieve your goal with a CXF interceptor.
Here is an example of such an interceptor: https://github.com/mulesoft/mule/blob/mule-3.3.1/modules/cxf/src/main/java/org/mule/module/cxf/support/MuleHeadersInInterceptor.java
Here is a configuration sample that shows how to use Spring to instantiate and configure CXF interceptors: https://github.com/mulesoft/mule/blob/mule-3.3.1/modules/cxf/src/test/resources/header-conf.xml
The gist of it is:
<cxf:inInterceptors>
<spring:bean id="foo1" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
</cxf:inInterceptors>

how to write unit tests for REST web services developed using apache-cxf with spring without using Maven

I have developed a REST web application using apache cxf library. I am able to access them using a browser and test. I want to write unit tests for the services.
I tried testing it using embedded Jetty server with cxf.
After starting the Jetty server, I am getting a proxy using WebClient API of cxf. The relevant code is
WebClient client = WebClient.create(ENDPOINT_ADDRESS);
client.accept("text/xml");
client.path("/studyservice/topimstudydetails");
client.replaceQuery("pnlId=3&lclId=30&indId=442&maxImStudies=99&rName=DEV");
TopIMStudyDetailsResponse resp = client.get(TopIMStudyDetailsResponse.class);
assertEquals(resp.getStatus().getReturnCode(),0);
The call to the service is successful as I can see the logs, but I'm get a null pointer where the service is trying to make a dao call. The dao is declared as a dependency for the service. How can we get a proxy with all the dependencies injected ?
May be you'll find the following posts helpful:
http://tarlogonjava.blogspot.co.il/2011/12/running-integration-test-using-with.html
http://tarlogonjava.blogspot.co.il/2011/12/running-integration-tests-using-with.html
The second post is about replacing the data source with a special in-memory data source for tests.

How do I use CXFNonSpringJaxrsServlet with a serviceBean instead of a serviceClass?

I am trying to use Apache CXF with JAX-RS to serve as an embedded REST endpoint within a larger application. I cannot use spring configured CXF because my application needs to manage the lifecycle of the Jetty instance and servlets.
The example here shows how to do this with a service class name, but in my application it will be roundabout and ugly to pass a classname rather than a bean. Can anyone point me toward a way to use a bean here?
You have to leave CXFNonSpringJaxrsServlet create your instance, but you can configure it (=bind it to outside world) by extending CXFNonSpringJaxrsServlet#configureSingleton
See this post: CXF/Jetty equivalent of the following Jersey/Jetty code for a solution. Tested with CXF 3.0.3 and Jety 9.2.5.v20141112.
This is how it's done
Object serviceObject = // your JAX-RS service object
JAXRSServerFactoryBean rs = new JAXRSServerFactoryBean();
rs.setServiceBeanObjects(serviceObject);
Server server = rs.create();

Is there a way to connect to a soap webservice without using a web reference in C#?

is there some library out there that will accept the url of the soap wsdl file, and then generate me the proxy classes and what not at runtime? Web references are getting to be somewhat inconvenient for my particular situation.
Thanks.
Web Services Description Language Tool (Wsdl.exe) is what you want.
The following command creates a client proxy class in the C# language for an XML Web service located at the specified URL. The tool saves the client proxy class in the file myProxyClass.cs.
wsdl /out:myProxyClass.cs http://hostServer/WebserviceRoot/WebServiceName.asmx?WSDL