Feedback/Patterns on creating a protocol adaptor - java

I have a backend system that currently returns a domain object. I want to build a REST & SOAP front end using mule. To create responses that are REST or SOAP based, and ensure that the backend is protocol agnostic, I am thinking about creating two protocol adapters (REST and SOAP). It is unclear to me if it is better to create a mule translator for the service, which understands the protocol and delegates to generate an appropriate response, or just create two class interfaces (using JAX-RS/JAX-WS annotations) to the service.
I'm not finding much literature, best practices, or design patterns on the subject. Can someone point me to some useful material or provide their opinions?

Writing transformers for message formatting would be a waste of time.
You should write service classes with JAX-RS/JAX-WS annotations, transports exist for both.
For a REST service you can use the Jersey transport:
http://www.mulesoft.org/documentation/display/JERSEY/User's+Guide
For SOAP service you can use the CXF transport (also supports JAX-RS):
http://www.mulesoft.org/documentation/display/CXF/Building+a+web+service
Note: You don't need mule for this at all, both Jersey and CXF run in a servlet container like tomcat.

Related

How to implement SOAP Endpoints that receive SOAP XML (SAAJ?) Instead of generated objects using CXF

We have currently implemented SOAP endpoints with CXF (Spring Boot). In accordance with the contract-first approach, we provide a WSDL, generate Java objects, and services from it (cxf-codegen-plugin) and finally implement the service interface.
This is all very nice and simple, but we only need the SOAP XML or the corresponding DOM tree. Not only is the conversion to Java objects unnecessary, we also have to convert them back to XML and lose some information from the original XML.
As far as I understand is SAAJ (SOAP with Attachments API for Java) exactly right here, or am I missing something? At most all resources about SAAJ are quite old and "low level". I cannot find good resources for that kind of approach.
In addition, we want to implement new services as microservices soon and are currently reviewing Quarkus and Apache Camel. However, I cannot find a (simple) way to create corresponding endpoints from the WSDLs, via which we then receive the SOAP message as plain XML / DOM tree.
In jax-ws, message-level access can be accomplished on the server side using web service Provider-based endpoints, and on the client side using Dispatch clients.
A web service Provider-based endpoint offers a dynamic alternative to the Java service endpoint interface (SEI)-based endpoint. Unlike the SEI-based endpoint that abstracts the details of converting between Java objects and their XML representation, the Provider interface enables you to access the content directly at the XML message level—without the JAXB binding. web service Provider-based endpoints can be implemented synchronously or asynchronously using the javax.xml.ws.Provider or com.sun.xml.ws.api.server.AsyncProvider interfaces, respectively.
See https://cxf.apache.org/docs/provider-services.html for details

Using webservices with javax.ws or javax.jws

I'm starting to choose a way to create my webservice, so i found 2 ways to do it:
1) Using the package javax.jws, with annotation #WebService:
#WebService(...)
public class MyServiceImpl {
2) The other way is using javax.ws, with annotation #Path:
#Path("/MyService")
public class MyServiceImpl
In my understand using the second solution is more simple, because when i need to create the Client i just need make a HTTP call (GET/POST). Using the first solution i need create a WSDL client, more complex solution.
So, I would like to know which is the advantage in use FIRST SOLUTION.
The SOAP/WSDL style is useful when a formal contract must be established to describe the interface that the web service offers.The Web Services Description Language (WSDL) describes the details such as messages, operations, bindings, and location of the web service.
Also the SOAP/WSDL style is useful when the application architecture needs to handle asynchronous processing and invocation (e.g. using JAX-WS the assynchronous clients can be created).
The disadvantages of SOAP/WSDL style are
its complexity: tools are required to create a client
heavier on Bandwidth : SOAP requires a heavy XML wrapper arround each request or response
complicated security rules
The advantages of REST style are
simplicity: REST client can be accessed from any browser (however, this is only true for GET method. Data creation request requires also the XML wrapper).
lighter on Bandwidth : data on the wire are usually bare xml elements (not wrapped within the <Envelope><Body> tags).
REST application security rules can be setup using the http standards: the administrator (or firewall) can discern the intent of each message by analyzing the HTTP command used in the request.
For example, a GET request can always be considered safe because it can't, by definition, modify any data.
The disadvantage of REST style is that it still doesn't cover all business requirements
There is no common standard accepted yet for the formal REST service description
REST requests (especially GET method) are not suitable for large amount of data
REST doesn't cover all web services standards, like Transactions, Security, Addressing, Trust, Coordination,

REST WADL vs SOAP

I saw these two links:
1.SOAP vs REST
SOAP vs REST 2
I understood the difference between the two. But I also heard about WADL (https://wadl.java.net/), which are used along with REST to provide a contract ( similar to SOAP WSDL does)
Now I have two questions:
REST is also adheres to the data type that is going to receive or transmit by WADL, which now acts very similar to SOAP ( with WSDL ).
If REST with WADL and SOAP are similar, which one to use?
Thanks in advance.
SOAP (Simple Object Access Protocol)
REST (Representation State Transfer)
SOAP and REST can’t be compared directly, since the first is a protocol and the second is an architectural style.
The main difference between SOAP and REST is the degree of coupling between client and server implementations. SOAP works like a custom desktop application, tightly coupled to server. There is rigid contract between client and server and everything seem like breaking is any of the side is changed. But, REST client is more like a browser. It’s a generic client that knows how to use a protocol and standardized methods. If done right, there would be less coupling.
A client is supposed to enter into REST without prior knowledge of the API, except for the entry point and the media type. In SOAP, the clients need to have the prior knowledge of everything it will be using.
REST is protocol independent. It’s not coupled to HTTP. We can use it for any protocol for which there is standardized URI scheme. But, SOAP itself is a protocol.
SOAP only supports XML, but REST supports different format like text, JSON, XML etc. And we know if we use JSON then definitely we will be in better place in terms of payload.
SOAP can use any generic transport (HTTP / HTTPS) or RPC (Remote Procedural Call). But, REST uses only HTTP/HTTPS
SOAP can’t use REST. But, REST can use SOAP similar to HTTP or any other protocols
SOAP used services interfaces to expose the business logic. REST used URI to expose the business logic
In Java, JAX-WS is for SOAP web services. But, Java JAX-RS is for REST web services
SOAP defines the standards to be strictly followed. REST doesn’t define any standards like SOAP
SOAP requires more bandwidth and resource than REST. REST required less bandwidth and resources.
SOAP defines its own security. REST inherits security measures from underlying transport
SOAP is less preferred these days as compared to REST
RESTful and SOAP WebServices are fundamentally different.
In short - SOAP is a typical RPC based call where you hide your request action and details within the XML document of the SOAP body. Wheras pure RESTful webservie totally rely on HTTP methods to perform an action on server.
Although WADL is not yet widely adopted and not really a standard but the theory behind it is to simply help tools to generate some code, and it aims to promote reuse of applications beyond the basic use in a web browser i.e. machine-to-machine communication (still using HTTP protocol).

camel mediate service via jms and rest

In my company we are starting to use web services. My plan is to provide access to services via 2 protocols, http and jms. Any external access (clients outside company network) to services will be typically via http and json through a restful URL, but internally if the service needs to invoke another service, it will do it via jms, mainly to decouple the service from each other.
My question is following
Can camel provide abstraction so that i can write my service code without http, json and jms dependencies? I would like camel to handle http to java, json to java and jms to java conversion through some mediation and invocation of my service should be simply through a java method with accepts a java object as a request. Keep in mind that although http is synchronous model, jms would have to simulate request-response. If yes, can you please point me towards an example which demonstrates this setting.
Likewisely, i would like camel to convert the response from my service, which would be a java object, into json and return the response back to the client.
Lastly, how can i scale in this model. For JMS, it is easy to startup multiple instances and have them listen to a queue. How can i leverage same instances to loadbalance across http interfaces? I would like services to have location transparency, hence, they should not have to care about invoking "jms" specific cluster vs "http" specific cluster?
I think you can achieve all of this with Camel.
Marshalling between different object representations is handled through data formats. You can call POJOs from within Camel routes that can encapsulate your services.
See data formats.
Camel comes with clustering and load-balancing support. But if you only have HTTP type endpoints to load balance, then my personal opinion is that you should use a more http-centric approach, like using httpd as a reverse proxy/load balancer.

Do we need restclient in case if we have to support multiple plateform

I've restful web service. I created a restful java client which can abstract connection details and expose beans to set the data. Now what happen if my web service has to be consumed by different platform than Java ? Wouldn't it had been better to avoid the client itself and let consumer make direct HTTP call ?
WADL (https://wadl.java.net/) is the WSDL-equivalent for RESTful services, however, it is not very widely used by now.
Edit: A reason why it might be not popular can be found in this article: http://bitworking.org/news/193/Do-we-need-WADL

Categories

Resources