Spring Content 1.2.5 JPA(Postgres) .docx file mutates to zip archive - spring

Saved ".docx" file with mimeType "application/vnd.openxmlformats-officedocument.wordprocessingml.document". But when I access it's endpoint in Spring Content and download it's not exactly Word document, but Zip archive(application/zip). Spring Content 1.2.5 supports ".docx" files, how can we fix it?
Demo project to reproduce issue(.docx file attached):
https://github.com/leonaugust/docx-problem
EDIT
Although I understand that resulting file is docx after all and we can choose to open it as Word document, but is there a way to make it less confusing for customers and return back as ".docx" format? In my case a huge amount of documents will most likely be sent in that format

As you have spring-content-rest on the classpath and use the #StoreRestResource annotation I am assuming that you are using that to fetch your content? Please let me know if that is not the case and I will edit.
There are a couple of annotations that, if present on the entity, the spring content rest post/put endpoints would set for you and that are then used later on by the GET endpoint; #MimeType and #OriginalFileName.
If you add these annotations to your entity and set them appropriately in your post endpoint then:-
#PostMapping
public UUID create(#RequestParam("file") MultipartFile multipartFile) {
File file = new File();
file.setMimeType(multipartFile.getContentType());
store.setContent(file, multipartFile.getResource());
file.setMimeType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
file.setOriginalFileName(multipartFile.getOriginalFilename());
UUID id = repository.save(file).getId();
log.info("id {}", id);
return id;
}
When your client fetches content (again, I assume) via the Spring Content REST endpoint it will set the following headers:
Content-type header
content-dispostion form-data attachment header
on the response.
Both of which will direct the browser app as to handle the content appropriately.
This should allow you to make the following get request from your browser.
curl -H 'Accept: application/vnd.openxmlformats-officedocument.wordprocessingml.document' http://localhost:8080/storage/b9ca6fbe-dede-4a51-b444-9e22b798e922
And it should download the attachments as test.docx
Seperately, I'd be curious to know why you added your own "create" endpoint, rather than using the Spring Data REST/Spring Content REST endpoint. It will do this for you automatically. I assume it is because you do not want to use Spring Data REST?

Related

Why is XML response returned by default in Web API?

When nothing is specified in "accept" header in the request, why is XML response returned by default in Web API? I mean is there anything which makes the framework to do so?
XmlMediaTypeFormatter is the default formatter, as this is the order in which they are found in the 'Formatters' collection of the HttpConfiguration object
https://stackoverflow.com/a/20192316/1538039
You can change the behaviour here by clearing down and only adding in a Jsonformatter, e.g.
configuration.Formatters.Clear();
configuration.Formatters.Add(new JsonMediaTypeFormatter());
The link to StrathWeb contains additional information

How to get Uploaded Url of a Video File in android using Parse REST API

I am using Parse Mobile Backend for my android app,but whenever I want to upload a large video more than the default 10mb limit for a ParseFileby getting the bytes from it into a ParseFile I keep running into the dreaded OutOfMemory Exception .So,I want to use the Parse REST API since I can easily use the setChunkedStreamingMode(1024) in HttpUrlConnectionto send the bytes in a chunked manner.The trouble is,how do I get the uploaded url of the file uploaded.Thanks for your help in advance.
i think you can use getUrl() method
ParseFile file = new ParseFile(byte[] data);
file.getUrl();
ParseFile object has getUrl() method to getting url of file. this method return URL in string format.

Send XML file as response to ajax

i have created a xml file from database.
this xml file i need to send as a response from my servlet to ajax.
I have checked various forums and blogs over the web and found that the response xml is created at the time when the servlet is called.
In this case i already have a xml file in my server, i just need to send it as a response to ajax.
Help !!
In your doGet() or doPost() method make sure you set the content-type before writing anything to the response. Like this...
PrintWriter pr = response.getWriter();
response.setContentType("application/xml");
//parse your data to XML
String xml = parseXml(root);
pr.write(xml);
Note: A content-type of "text/xml" should also be valid. Frameworks like JQuery and Prototype support both.

How to add for format=json in oracle weblogic server

I have installed oracle weblogic server 11g.Implemented RESTFul but as per document when I place there format=json. It should works but it is not working.
Can you please let me know, how to resolve response as json.
you can get a working example of how to do this here: https://www.samplecode.oracle.com/sf/projects/oracle-parcel-svc/ and we have a webcast series that covers JAX-RS on WLS in Session 4 at this link:http://www.oracle.com/technetwork/middleware/weblogic/learnmore/weblogic-javaee6-webcasts-358613.html
When you configure your method that you want to return JSON from you have to specify that it produces JSON. Here's one way to do it:
#GET
#Path("{id}.json")
#Consumes({MediaType.APPLICATION_JSON})
#Produces({MediaType.APPLICATION_JSON})
public Parcel getParcelById_json(#PathParam("id") int id)
{
return getParcelById(id);
}
You also need to put the right HTTP headers in the client in order to specify that it expects a JSON response. Some test clients like SOAP-UI auto-convert JSON to XML such that you can do an XPATH on it, even though the actual transmission of data is JSON.

send file from website to windows application

I have a file in the website and i m trying to send this file to windows application using the Response.BinaryWrite (getContent)( Where getContent is the byte array having the file which I need to send) vis HTTP post method only. Also I m adding a Header and Content-Type as application/octet-stream in the Response.
Now while reading the (httpWebResponse) response in the stream at client side(windows application) all the things (header + content-type + file + some extra bytes) are getting added. so when I try to read the file in stream it cannot be loaded since the content has chnged
Is there any way to separate the file from rest contents present in the response object..
How sahll I save this file in directory
Use System.Net.WebClient.DownloadData or DownloadFile method instead.
What language / version are you using?
If you are using a reasonably up-to-date version of C# you can use the WebClient class, and its DownloadFile method

Resources