What is the best way to consume a web service in Grails? - web-services

I know there are a few web service plug-ins for Grails, some of them look like they aren't maintained. I have a jar with all the stubs generated from a wsdl and now I need to start integrating. Which plugin would serve best for this? Also, the web service uses SOAP, not REST.

I know your question asked about a plugin for consuming, but I've never used one of the Grails-WS plugins, so I can't comment there. Instead, if your stubs are compatible with JAXB marshalling you can use the Spring Web Services project. You'll just have to add a dependency in BuildConfig.groovy to import the appropriate jars.
http://static.springsource.org/spring-ws/sites/2.0/reference/html/client.html
So you just define some JAXB marshaller/unmarshaller beans and web service handlers. You can get as detailed as you want with this from the documentation above and define timeouts and security if you specify your own connection handler or interceptors.
myJaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) {
classesToBeBound = ['my.class.Class1','my.class.Class2']
}
myWebServiceMessageFactory(org.springframework.ws.soap.saaj.SaajSoapMessageFactory)
myWebServiceTemplate(org.springframework.ws.client.core.WebServiceTemplate, ref('myWebServiceMessageFactory')) {
marshaller = ref('myJaxb2Marshaller')
unmarshaller = ref('myJaxb2Marshaller')
}
At that point you can use Grail's dependency injection to use the WebServiceTemplate in your Grails code:
class myService {
def myWebServiceTemplate
void myMethod {
...
Class1 myRequestObject = new Class1(data:myData)
Class2 myResponseObject = myWebServiceTemplate.marshalSendAndReceive(mySoapEndpoint, myRequestObject)
...
}
}

I would recommend using Spring's way.
You're developing in grails after all, so maybie you don't need a plugin.
Read Spring docs on Remoting and webservices, chapter 19.5.2 "Accessing web services using JAX-RPC".
No need for any of your jar's stubs. Spring will generate everything for you...
P.S. : I assume you know how to declare spring beans in grails...

Related

Unit Test Swagger Output

I am using Swagger in an ASP.NET MVC WebAPI project. This project has Swashbuckle nugget package installed and generates Swagger UI and Swagger/docs/v1. A consistent problem I'm having is developers will break the swagger file by not carefully naming their webAPI operations. I would like to add a unit test to prevent me from finding out that swagger/docs/v1 isn't available by going to the Swagger UI site after deployment and seeing an HTTP 500 displayed in the swagger UI. Does anybody know how to write a unit test to validate that Swashbuckle can successfully generate the swagger docs?
Found a great way to do what I want:
[Fact]
public async Task TestSwagger()
{
var webHostBuilder = new WebHostBuilder()
.UseEnvironment("Test") // You can set the environment you want (development, staging, production)
.UseStartup<Startup>(); // Startup class of your web app project
using (var server = new TestServer(webHostBuilder))
{
using (var client = server.CreateClient())
{
string result = await client.GetStringAsync("/swagger/v1/swagger.json");
JObject root = JObject.Parse(result);
}
}
}
This uses the following nuget package:
Microsoft.AspNetCore.TestHost
Look at the UnitTests we have on Swashbuckle :
https://github.com/domaindrivendev/Swashbuckle/tree/master/Swashbuckle.Tests/Swagger
I'm sure one of those is really close to what you are looking for.
...But if is just the naming of the webAPI operations you could just loop over those using GetApiExplorer and make sure they follow your rules
To whom may be looking for a Asp.Net Core Swagger testing solution, I suggest taking a closer look at Cristophe Blin's aproach

developing custom WSO2 Carbon authenticator clients

I am implementing new authentication methods for WSO2 Carbon. I know there is a pretty good explanation and sample piece of code in this post.
The problem is, what if I want to generate stub and service client components from the sample BE authenticator? What are the steps to follow? Any tools (java2wsdl / wsdl2java, maven plugins,...) or reference tutorial that can help to achieve this in the most straightforward way?
I know there are several existing authenticators (IWA, webseal,...), but they already come with some stub/ui built in the existing repositories. I would be rather interested in being able to develop/generate all the components more or less from scratch rather than having to modify existing code, which is often prone to errors.
Thanks
This is some high level thing.. but hope this is useful.
1st you can develop the BE component as OSGI bundle. Then you can deploy it in /repository/components/dropins directory. Here you need to have a service.xml file to expose as web service (Please refer WebSeal BE component)
Then configure following property in the carbon.xml file.. if BE service has been defined as admin service
true
Open browser and locate your WSDL of new service
https://{ip}:{port}/services/{service name}.wsdl
Then use wsdl2java tool to create the stub class for you. There is a UI tool in WSO2AS product to do it.
Use stub as a dependency for your FE

EJB as WebService context-root gone

I'm working on a project with JAX-WS.
When I annotate my endpoint class with #WebService the WSDL is marked in console like
.../<context-root>/XXXService?wsdl
When I add #Stateless on those endpoints the WSDL is not marked in console and the actual address is
.../XXXService/XXXEndpoint?wsdl
Is this normal or expected?
Update
For further readers.
I couldn't find any resolution. I decided not to use mixed #Stateless+#WebService. I split those #EJBs and #WebServices for clear module separation.
What youre experiencing is expected behaviour. It's a different matter if the service s not functional. When an EJB 3.x stateless bean is deployed as a WS, it's naming defaults to what you see there,
Servername/SIBnameService/SIBName.
The reason for this is obvious: EJBs don't operate within the context of a web application and so cannot be addressed as such. You can customise the default name using the serviceName attribute on the #WebService annotation
Look at this from apache

Inject generated beans

Let's i have simply #Stateless session bean. (EJB 3.0)
#Stateless
public class SomeBeanWS implements ISomeBeanWS {
// ...
}
And I have many WebServices (that are beans also) that have already deployed on the app server.
I have WSDLs of this Services.
I can generate Interfaces if this WebServices in my project using Maven.
Can I inject this Webservieces (beans) to my SomeBeanWS?
May you're using NetBeans, it provides a "generate WebService from WSDL" tool.
It helped me accessing external WS from within my ManagedBean (by using #WebServiceRef annotation there). Only injecting the WS through a Session Bean doesn't work (see here).
Edit says: I've found a solution, follow the link ;)

How can I inject a data source dependency into a RESTful web service with Jersey (Test Framework)?

I'm building a RESTful web service using Jersey that relies on MongoDB for persistence.
The web service itself connects to the default database, but for the unit tests, I would like to use a separate test database. I would populate this test database in setUp, run my tests, and then destroy it in tearDown.
Normally, I would use dependency injection here to supply the data source to an entity manager that the service would use, but in this case the web service is running independent of the unit tests. I'm using the Jersey Test Framework, which starts up a Grizzly container to provide the web service interface, and provides a web service client to the unit testing class.
What is the best way to inject a dependency from my unit test class into the server instance (which Jersey Test Framework sets up in a Grizzly container)?
After digging through the Jersey Test Framework source, I've discovered an elegant way to inject dependencies into my RESTful resource classes.
In my test class (which extends JerseyTest), I've added only an implementation for the configure() method:
public AppDescriptor configure() {
return new WebAppDescriptor.Builder()
.contextListenerClass(ContextLoaderListener.class)
.contextParam("contextConfigLocation", "classpath:applicationContext.xml")
.initParam("com.sun.jersey.config.property.packages", "[resource package]")
.build();
}
This effectively provides a custom built WebAppDescriptor instead of relying on Jersey Test's Grizzly Web container to build one.
This will use the "applicationContext.xml" file on the classpath, which can be configured differently for running JUnit tests. Effectively, I have two different applicationContext.xml files: one for my JUnit tests, and the other for production code.
The test's applicationContext.xml will configure the data access dependency object differently.