Jersey Client - send gzip file got error: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream - jersey-2.0

I have to call a webservice and it requires to set header("x-ms-blob-type","blockblob") and put a file in gzip. I am trying to use the FormDataMultiple to set the file and send it as an Entity. This is what I did.
The server responded the they got the file but when they tried to process the file it gave me back with this error:
"Error parsing file: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.”
final FileDataBodyPart filePart = new FileDataBodyPart("file", new File("/Users/Downloads/bulk.gz"),MediaType.APPLICATION_OCTET_STREAM_TYPE);
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
final FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart.bodyPart(filePart);
final Response response = target.request()
.header("Content-Type", "application/x-gzip")
.header("x-ms-blob-type","blockblob")
.put(Entity.entity(multipart,multipart.getMediaType()));
What should I do? I think it would be better if I can pass a gzip input stream to the request instead of using the Multiple Part Form.
Best Regards,
Robert

Related

Explicitly setting `Content-Length` header in `java.net.http.HttpRequest`

I want to upload a file from InputStream over HTTP, and for this, I am using the new HttpClient provided as part of JDK11. When I try to set the Content-Length while creating a post request I get an IllegalArgumentException saying restricted header name: "Content-Length" and if I try to upload a file from InputStream without the Content-Length header I get an internal server error from the server where I want to upload the file. Is there any option to set the Content-Length in the request in Java 11?
CodeI am using for creating HttpRequest:
var postRequest = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofInputStream(() -> inputStream))
.uri(new URI(url))
.header(HttpHeaders.CONTENT_LENGTH, Long.toString(inputStreamSupplier.getFileSize()))
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE)
.build();
Note: It won't be possible to update to Java 12 to allow the restricted headers.
I could also use another library, just wanted to know if there is an option to use the classes from JDK before switching to RestTemplate from Spring. (yes, it's deprecated, As the alternative uses spring boot can't use it at the moment)
Simply use fromPublisher:
var bodyPublisher = BodyPublishers
.fromPublisher(BodyPublishers.ofInputStream(()-> inputStream)), length);
Note that you must ensure that the InputStream delivers exactly length bytes.

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.

Soap message encoded in base64 binary data multiple files

Recently I got a new project to work on a SOAP service and to Get and Post messages to a ASP.NET service based on xml.
The issue is that I managed to make the soap request and get the message.
The message looks like this:
UEsDBBQAAAAIAAdUe06+NXE0kR4AADLSAQALAAAAUHJvZHVzZS54bWzUXW1z48YN5k/h5EMnmbMsvomSpmkzFCXbjERJoSTb52/p9dq5mbxN2svczy/........
The message is Base64 Binary on RFC 4648 with multiple xml documents on it.
How I can construct this documents from the code in php?
The documents encrypted in this request are 3 xml files.
I managed to get them from an online decryptor called freeformatter with download function.
If I try to decode the result I get something like:
PKT{N�5q4�2�Produse.xml�]ms��
�O��C'��,����i3%یDI�$��o��ڹ��M����/�,��|vL�O�$�/�xv,,�u�s>9?;?....
Is there a solution for this?
I'm new to SOAP so I don't understand too much of it.
Thank you but i mannged to solve it.
I gonna post here the sollution so everyone who facing the same issue, get the response.
The first thing you need to do when you have an .zip file in a base64 binary string is to catch the response to a txt file.
Let's say the response from soap it's called ' $response ' and we need to catch this to an file. We do like this :
$response = $client -> _getLastResponse();
$fille = "response.xml";
fille_put_contents($fille,$response);
Now we got the response to an xml file.
The next thing to do is to get the response from xml values.
Lets say our value is <ResponseFromServer> .
`$b64 = "b64.txt";
$dom = new DomDocument();
$dom = load("response.xml");
$data = $dom->getElementByTagName("ResponseFromServer");
$catchb64 = $data;
fille_put_content($b64,$catchb64);`
Now we got the clean Base64 Binary string in one fille.
The next thing we need is to create the document ( in this case is a .zip fille)
`$input_fille = "response.txt"; // the fille with clean base64 binary data on it
$output_fille = "result.zip"; //the fille we need to create on system with the
documents decrypted
$content = fille_get_contents($input_fille); // Reading the content of input fille
$binary = base64_decode($content); // Decoding to binary
fille_put_contents($output_fille,$binary); // Writing to fille the vallues`
We dont need the ZipArchive() function, because is allready a zip archive, all we need to do is to create a empty document and after to send the binary data to it.
Cheer's and goodluck!

Get the name of an uploaded file

I'm uploading a file using curl:
curl -X POST --data-binary #/home/me/my_file.jpb localhost:9001/upload
And here is how to store it:
def upload = Action(parse.temporaryFile) {
request =>
import java.io.File
val f = new File("tmp/someName") // how do I get the name of the file being uploaded?
request.body.moveTo(f, true)
Ok("File uploaded\n")
}
Note that files can be in any format. I want to get the name of the actually uploaded file. I tried request.body.file.getName but it returns gibberish.
How do I do that?
I am fairly certain you cannot get the file name from the binary stream you are uploading via curl. You need to explicitly provide the file name separately.
The options I can think of are these:
If your Content-Type header is instead multipart/form-data, then the process is quite simple as described here
Upload JSON with a String for the file name and a binary portion for the file.

Sending Soap with attachment with Mule ESB

I am using Mule ESB version 1.3 and as soap engine it has AXIS1.4 embedded.Mule uses the code below to send stream as ws attachment. I checked they are still using it in newer versions.
File temp = File.createTempFile("soap", ".tmp");
temp.deleteOnExit(); // TODO cleanup files earlier (IOUtils
// has a file tracker)
FileOutputStream fos = new FileOutputStream(temp);
msgContext.getRequestMessage().writeTo(fos);
fos.close();
contentLength = (int)temp.length();
payload = new FileInputStream(temp);
Here the content of soap message with attachment is saved on a file and handle for this file is given to the rest of system. I did not get the logic behind this. Why not just use
javax.activation.DataHandler
inputstream and instead first saving and then reading message from file. As far as I understand Axis1.4 itself not working that way. Am I missing something here ?
After some modifications to make mule send file I came accross the below difference between axis and mule client generated messages. Except this line generated messages are same.
Mule generate this header:
Content-Type: multipart/related;type="application/xop+xml"
Axis generate this header which is correct:
Content-Type: multipart/related;type="application/xop+xml"; start="<2F4952A019F62AB6704A0C06DB5E8AA1>"; start-info="text/xml; charset=utf-8"; .boundary="----=_Part_0_1836994030.1387284330292"
What is causing this difference ?