Generate a webservice on the fly (dynamically) - How to? - java

Here's a scenario:
I have a webservice, let's call this StockQuoteService deployed on tomcat (axis).
There is this method getStockQuote() exposed via this webservice.
Now, I would like to build a GUI tool which would build a webservice called StockQuoteServiceEx on the fly. The new webservice would expose the same methods as StockQuoteService. However, when getStockQuote() is invoked on StockQuoteServiceEx, this method act like a webservices client, thereby invoking getStockQuote() on StockQuoteService, obtaining the result from it. The purpose of doing this is in manipulating(data masked/shuffled/encrypted) the original result.
Once the webservice is generated, existing clients would update end points from StockQuoteService to StockQuoteServiceEx. So, the question here is, what would be the steps to dynamically generating a web service on the fly?

Since your willing to expose the same webservice interface/operations: Wouldn't it be easier to let your GUI tool act as a HTTP proxy and place that tool between the client and the actual webservice? Like:
Client(s) ==> GUI Tool (http proxy) ==> StockQuoteService.
That way the client(s) are build against the WSDL of the actual StockQuoteService, but make use of the endpoint address of the GUI tool. You can implement the HTTP proxy in your GUI tool as a simple Servlet which dispatches the request (after manipulation) to the actual webservice using Apache HTTPClient.

Related

can i mock a SOAP and rest webservice reponses with file stream from local directiry

I have a requirement to mock webservice call. Here instead of actual webservice call to dummy server and server should reply my response xml response file from local directory.
Our project used that approach (used for mocking external Webservices which were not available on local workspace).
Short answer is yes, but how to implement would depend on your code.
First , we created a interface to call webservice, so that we can create 2 implementations, one actual( which calls external service) and one Test ( which calls an internal WebService which reads from file).
Next, we created a test webservice which we hosted on the same server. Now the logic was simple. Based on input , we used to read the correct response from file. You can build whatever logic you want, our framework was designed to support multiple webservices.
Lastly, in our code, kind of like spring, we used to point to test service ( via interface) instead of actual service. So we could read from file and provide a response. When external interface was available, we would simply switch the configuration and we were good.
Ofcourse, this is over simplification, but I hope you get the gist.
You have two ways of proceeding :
mock the ws from the client side by providing an mocked implementation of the client ws.
mock the the ws from the server side by creating a mocking instance of the ws.
Mocking the call from the client side is simple. You create an common interface with the ws methods you want to call. And you create two implementations of this.
One with the effective call to the ws and another with stubbed responses coming from local files.
Mocking or simulating the ws from the server side is not much complicated but it is not the same approach. You can hard code it but it is not a secure solution when you must mock it again. You also can use a webservice simulator instead of your webservice. You will not query you webservice but another.
SOAPUi can help you to achieve this task by mocking your webservice responses.
Example for SOAP mocking
To use a local file as response, you should use SOAPUI response scripting.
SOAP reponse mocking

How to create API client from wsdl file?

I have wsdl
http://www.webservicex.net/genericbarcode.asmx?WSDL .
I am trying to create a client, looking for logic in wsdl.
I think it should start from this:
BarCode barCode=new BarCode();
BarCodeSoap barCodeSoap=barCode.getBarCodeSoap();
Do I need to input any parameters there?
Executable code is generated from WSDL of the Webservice. The client then uses this code to access the Webservice.
For example, GlassFish includes vsimport utility to generate Java code from WSDL.
This utility generates a lot of compiled Java classes that allows client applications to access to Webservices. These classes are further to be added to classpath of the client.
In addition, GlassFish includes custom Ant task (also there are Maven plugin for that).
Then you need to use #WebServiceRef annotation that inject the instance of the Webservice to you client.
#WebServiceRef(wsdlLocation="...?wsdl")
private static NameOfYouServiceService nameOfYouServiceService;
...
NameOfYouService - this is convention, the name of Webservice that you develop.
Then this call nameOfYouServiceService.getNameOfYouServicePort() return you instance of the Webservice. Then you can free use methods of the instance.
Here, the name NameOfYouService - also convention.
In short, something like that.
Your Webservice is written in .NET technology, but you can also using Java client (or PHP or something else) to access it. In fact, a web service created with one technology can be accessed by clients in any other technology.
Here is a good article for your case: Java Client for WebServiceX.Net Web Service (NetBeans IDE)

Creating a client for a existing web service out of a WSDL file with Java

I have a given WSDL file (a SOAP web service in the internet) and I want to use this service. For that I want to write a Java client, that sends the required datas (as XML) to the web service and the service itself sends an response.
Unfortunately, I am totaly new in web services. This is why I want you to ask for help.
I found a lot of ways to use SOAP in Java. One way is the AXIS2 framework from Apache, another way is the Eclipse Web Tools Project (WTP). I've tried both but couldn't get it run..
First of all.. What do I need to use such an SOAP web service?
I think:
- generate Java source out of the WSDL file
- write an client that uses this code
- with this client you can send datas to the web service (the client itself sends the data via XML) and the client can process the response
Am I right or do I have a fallacy?
Thank you for your help!!
I think: - generate Java source out of the WSDL file - write an client that uses this code - with this client you can send datas to the web service (the client itself sends the data via XML) and the client can process the response
That is correct. You can use Axis2 as you mentioned, write a JAX-WS client, you can even write the SOAP message by hand (not that I would recommend that though).
You should try to get a successful call from SoapUI first then write your client application. Use wsimport to get a simple client working (if that's all you need a framework like Axis2 could be overkill).

How To Use WSDL Url

I wrote a java web service on Netbeans 6.9.1 and deployed on GlassFish 3.0.1
I have a wsdl url like this "http://localhost:8080/web2/service2Service?wsdl".
How can I use this url to access this web service from another java application.
Thanks
You need to generate some Java that represents the client's view of the Web Service and then invoke that Java. Here's an article which explains some of the detail.
Generating client from WSDL in Eclipse
The general idea is that you generate some Java classes from the WSDL. Those classes act as a proxy for the service you want to call. Your java invokes methods on the proxy objects, the generated code creates the appropriate SOAP messages, sends the HTTP request, interprets the response and your code just sees a Java result.
I just use the tooling built into Eclipse, but you will also find other suitable generators, for example in Apache's Axis
1º U must save the content in a "myWebServices.wsdl" file
2º Run your Wsdl converter, all compilers have one of this, normally the name is WDSL.EXE
This process will create a new file with NameSpace or Package with the definitions of webservices built in.
3º Then imports this package or built a library.
Develop web service Client in second application.
You can use Netbeans to create web service client by giving your wsdl url
The document at this url is the actual wsdl (a description of the actual webservice, written in WebService Definfition Language).
The description includes information about the services url, the protocol(s), the method names and and data objects. Your application will use that information to call the remote methods of that service.
The protocol may or may not be SOAP, and without knowing the webservice details, it's quite impossible to recommend a toolset or methodology to use the webservice, there is no general approach. In most cases we see SOAP, for those services I recomment soapUI as a general tool to use and test SOAP based webservices and apache axis to implement java based service consumers.

GWT - Invoke the default 'greetServer' web service from a browser

I'm absolutely new to GWT, java and eclipse, but I'm an experienced MS programmer.
I installed eclipse and GWT and created a default project (called it test2) with pre-built sample gwt code. It comes with one java web service on the backend called 'GreetingService' which has a single web method called 'greetServer' that takes a single parameter called 'input'. I tried to invoke it in the web browser with many different combinations, but with no success (my project name is called 'test2):
http://127.0.0.1:8888/test2/greetServer?input=hello
http://127.0.0.1:8888/greetServer?input=hello
http://127.0.0.1:8888/test2/greetingService/greetServer?input=hello
http://127.0.0.1:8888/test2/greetingService?input=hello
etc
but I get http error 404 not found.
My question is, how can I invoke the web service from a browser and see the return data? I know this can be done easily with WCF or asmx web services but I'm not familiar with java web services.
Thanks a bunch!
The GWT Remote Service Servlet only uses HTTP Post. If you want to see the data (which will be encoded by gwt anyway) use could use firebug and take a look at the traffic.
The greeting service isn't intended to be a web service. It's a demo of GWT's remote procedure call (RPC) facility over AJAX. As with most RPC frameworks, the server side is only intended to be called from the generated client stub.

Categories