Returning bytes[] and Java Object in Jersey in same Http Response - jersey-2.0

I have to return byte[] as file data and JSON object both in a http response from a REST service.
Also I have to process this response in Angular 6 at front end side
The REST service uses Jersey 2.0
I have already implemented the Jersey Rest Service and it returns Multipart Response
But I am unable to process the Multipart response in Angular 6 as HttpClient in Angular cannot process MultiPart Response as per Handling a multipart Response body in Angular
Now , I have done exactly same thing in Spring by using org.springframework.util.MultiValueMap as below -
MultiValueMap<String, Object> responseData = new LinkedMultiValueMap<String, Object>();
SomeDto someDto = new SomeDto();
responseData.add("results", someDto); // this is JSON
responseData.add("fileDetails", fileData); // where fileData has one of the field
as byte[]
So I am able to return byte[] as well as Java Object in same Http Response.
Finally the response received at client side is JSON
How can we achieve the same thing in Jersey 2 ?
Is there any MultiValueMap counterpart in Jersey 2 which can be used to send both byte[] and Java Object in Jersey 2 ?

Related

How to read Gzipped payload in a POST request in SpringBoot

I need to read gzipped json payload in a POST request in my SPringBoot app which accepts json data. How to do that in order to keep the application generic as there may be other clients in future sending data in plain json or other compression formats? I suppose this should be handled by the server itself so is there any way to instruct the embedded Tomcat to unzip the payload?
My SpringBoot application runs on embedded Tomcat 9.0.17.
The controller accepts JSON payload in a POST request.
#RequestMapping(value = "/update/v1", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
public ResponseEntity<String> receiveUpdates(#RequestBody String update) {
We recently changed our content provider and the new one is sending payload in "gzip" format (with header content-encoding=gzip) and without any content-type header. As a result it gives the following error
'error': 'Unsupported Media Type', 'message': "Content type '' not supported"
If I change my consume type to MediaType.ALL_VALUE, my controller starts receiving the request but the payload itself is gzipped. I can handle it in my service layer but that would make it specific to gzipped data.
This problem could be solved by introducing a Filter to handle gzipped payload as mentioned here and here.
But I believe there should be a way to instruct the Tomcat to handle this and serve unzipped data.

How to create a SOAP message from JSON payload in mule

I need to transform the following json payload into a soap message and send the message to a consumer, the consumer edits the data and sends back the soap message.
I haven't done much in soap. I only have REST experience. what steps do I need to take in a process like this?
what is the best approach?
[{"salesOrderId":"00004-5-6","saleName":"House Sale","status":"processing"}, {"salesOrderId":"00001-2-3","saleName":"Car Sale","status":"processing"}]
There are various way to perform this transformation, for example:
PATH - 1
Json To XML (with transformer or string set payload)
Xml To SOAP Request using XSLT, transformer or string set payload.
Send SOAP Request sobre HTTP (POST / Content-type: applicacion/xml / soapAction)
PATH - 2
Json To SOAP Request using Groovy, XSLT or string set payload.
Send SOAP Request sobre HTTP-OUTBOUND (POST / Content-type: applicacion/xml / soapAction)
PATH - 3
Json To SOAP Request Proxy (WSDL To Java).
Send SOAP Request sobre HTTP-OUTBOUND (POST / Content-type: applicacion/xml / soapAction)
The easiest way of doing it is extract the JSON elements from the JSON payload by using <json:json-to-object-transformer/>and store each node value in variables like flow variable in Mule.
Then You can create the SOAP request using XSLT and passing the flow variables value into XSLT as <mulexml:context-property/>
ref:- https://developer.mulesoft.com/docs/display/current/XSLT+Transformer
Once your SOAP XML is created, you can simply post them to your HTTP outbound endpoint pointing to your external web service you need to consume
Try using Mule DataMapper. That helps you to convert a JSON to XML in the more easier way. You can try it in Anypoint Studio of Mule.

Calling Camlet RESTLET Server with Apache HTTp Client

This one should be trivial but somehow it is not working for me. I am trying to make a call to a RESTLET server hosted on Camel. I get it working with Camel client but I am trying to get the same to work with a bare Apache HTTP Client. The Camel is client is as follows:
CamelContext context = new DefaultCamelContext();
Map<String, Object> headers = new HashMap<String, Object>();
context.createProducerTemplate().requestBodyAndHeaders("restlet:http://localhost:8086/Bookmarkee/boolean/V2?restletMethod=post", "Halleluia",headers);
The HTTP Client I am trying to make work is this:
HttpPost httppost = new HttpPost("http://localhost:8086/Bookmarkee/boolean/V2?restletMethod=post");
StringEntity e= new StringEntity("Halleluia",ContentType.create("text/plain", "UTF-8"));
httppost.setEntity(e);
CloseableHttpResponse response = httpclient.execute(httppost);
On the server however, the response is not read from the stream by the servlet engine. Resulting in my component being passed an input stream instead of a string.
Any clue?
Experienced same problem when my camel route tried to process empty response from server (200 OK, Content-Length:0, Content-Type:application/json)
Fixed after I found and fixed bug in URL which I used for requesting with http component

How to make use of GWT Serialization in RequestBuilder and Servlets

I'm using GWT RPC Calls for Server Side Request so far and it's pretty good. I'm planning on separating my Code into Servlets and GWT Client Side. Since i'm using RPC calls, it seems impossible. The Reason i want to do like this is , i'm planning to provide white labeling option for my App. So if i could separate the code to client code and servlets, i can simply provide the White Labeled client code to my Partners to host on their server. I have checked with GWT RequestBuilder and Access-Control Allow-Origin : Origin from Client Header and it works fine.
However i need to implement gwt-serialization over RequestBuilder request and Servlet Responses. How can i do this ..?
Scenario I like to make:
RequestBuilder sending Serializable String(Which is a IsSerialiazible object) to Servlet.
Servlet deserializes the String to Java Object,Processes and Returns the String Response of a 'IsSerialiazable' Object.
The Response String recieved in GWT RequestBuilder deserialzes it back to a Java Object(JS after Compiling).
I have checked on RemoteServiceServlet class which seems to have some info on serializing and deserializing request and response. But i couldn't get it right to get it to work with RequestBuilder. Any ideas , Hope it will be helpful for everyone.
public final void processPost(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException,SerializationException
{
// Read the request fully.
//
String requestPayload = readContent(request);
// Let subclasses see the serialized request.
//
onBeforeRequestDeserialized(requestPayload);
// Invoke the core dispatching logic, which returns the serialized
// result.
//
String responsePayload = processCall(requestPayload);
// Let subclasses see the serialized response.
//
onAfterResponseSerialized(responsePayload);
// Write the response.
//
writeResponse(request, response, responsePayload);
}
GWT RPC and RequestBuilder serve different purposes. We cannot mix/match them.
GWT RPC - Services which fetch Data
GWT Request Builder - fire requests for static resources like js,css, json objects, querying soap services etc
The only feasible solution at the top of my mind for your approach of servicing requests is by using JSON - https://developers.google.com/web-toolkit/doc/latest/tutorial/JSON
You can keep your servlets code as is and then use RequestBuilder to query URL mapped to these servlets for JSON objects. Process the JSON objects using JSNI or Overlay concepts.
I'm trying to get the GWT Serialization & Deserialization source.Unfortunately, i have been stuck with other works and couldn't look on this right now. When i get the GWT Serialization Classes , i will update.

Use GWT RPC Serialization to send an object from client to server in a POST

I prefer to use RPC Serialization but then send data using servlets.
We have strangely found that this performs better and it allows us to have general logic for retrying all servlet calls for example.
I have figured out how to send an object from server to client but can't find a way to serialize on client and deserialize on server.
SERVER TO CLIENT
Server:
serializedObj = RPC.encodeResponseForSuccess(DUMMY_METHOD_OF_TYPE_SERIZABLE, object);
Client:
GWT.create(MyRpc.class).createStreamReader(serializedObj).readObject();
CLIENT TO SERVER
Client:
SerializationStreamWriter streamWriter = streamFactory.createStreamWriter();
streamWriter.writeObject(object);
serializedObj = streamWriter.toString();
But how can I unserialize this after sending it by POST to the server?
Thanks!
http://softteco.blogspot.com/2010/02/serializing-objects-in-gwt-and.html
Note the server side deserialize option
// Getting parameter from request
String content = request.getParameter("content");
// Initializing stream reader
ServerSerializationStreamReader streamReader = new ServerSerializationStreamReader(
Thread.currentThread().getContextClassLoader(), null);
// Filling stream reader with data
streamReader.prepareToRead(content);
// Reading deserialized object from the stream
dto = (D) streamReader.readObject();