How to deliver .jad file using Jersey Rest service? - java

I have to return a JavaME .jad file from restful web service (using Jersey), which is then used to install an app on a mobile phone. Before delivering the .jad file some values in there have to be changed. I was thinking of reading the original file, change the read input and writing it back to some outputstream. Can I just return the output stream in a Jersey Rest services? Is there anything special I have to take of in terms of mime-type, etc.? Does anyone know of some kind of example code or tutorial for this purpose?
Thanks in advance

Either like here:
Input and Output binary streams using JERSEY?
or you could send the StreamingOutput as an entity like this:
return Response.ok(streamingOutput).type("text/vnd.sun.j2me.app-descriptor");
Point is you have to use the OutputStream handed to you via the StreamingOutput.write(OutputStream outputStream) method.

Related

How to download a file in chunks using camel?

I want to download a very large file using camel, but I don't want to hold the entire file in memory and THEN save it to file.
I want to stream the file in and save or write to a file in chunks.
Is this possible with Camel, and if so, how do I do this?
Note: Is it possible that the endpoint I am downloading the file does not support streaming/chunking? If yes, how can I verify this?
Camel's HTTP component uses Netty to make the request. Netty reads the entire response into memory, so there is no way to do what you are asking for.
You would need to implement your own endpoint for Camel that utilizes another HTTP library which has support for HTTP response streaming.
More documentation is available here :
https://cwiki.apache.org/confluence/display/CAMEL/Netty4+HTTP
You can 3 option to download the file i.e. using:
ftp://[username#]hostname[:port]/directoryname[?options]
sftp://[username#]hostname[:port]/directoryname[?options]
ftps://[username#]hostname[:port]/directoryname[?options]
There is a option of streamDownload in it.
For more check out http://camel.apache.org/ftp.html

RESTful Webservices with PDF as an input file

Is is possible to exchange PDF file as a input for RESTful webservices and also I would like to send PNG image as a response to it.
If we can do it through REST services please provide me the references link to implement it using REST services.
I was able to achieve a similar output by converting the file to string/byte[] data and sending it via REST.
My implementation was in Java and the steps used is outline below
Convert the file on disk to byte[] array (apache common-io can convert the file to byte[] in easy step. Try the IOUtils class)
Encoded the byte[] as String (apache common-codec was used for the encoding)
Wrapped the string data in a model class
Converted the model class to json format (GSON was used for the conversion)
Sent the json data over to the server
The server application reversed the process, and the file was available on the server
A rest service isn't the right way for what you want. The input for this kind of services are HTTP request attributes or some kind of pushed data. Maybe it's possible to implement a file upload but it's not typical.
For restful services is also common to tell your service how to handle the requested resource via the used request method (GET, POST, PUT, DELETE)
The response of rest services is normally some kind of structured text output - for instance json.
All in all rest services seems to me not the way to implement your desired scenario.
What about a normal cgi or servlet solution?

Apache Axis2 Web service : Gzip resquet and response

I am developed a web service that return simple string using axis2 . But i want to apply gzip compression techniques , in my web service. can anyboby give me server code and client code for it. I developed web service from this reference
http://blog.sencide.com/2011/06/create-web-service-using-apache-axis2.html
Please provide proper guidance.
Thanx
Are you trying to do the Zip yourself, or just trying to compress the bits as they travel through the wire?
If you just want to compress the responses from your server, I know Apache has several ways of doing it. I would suggest looking here as a start.
If you are trying to create your own zip file, which you are sending in the response of your WS, and the client is expecting a zip file, then you want to look at java.util.zip. It provides several classes that read/write various ZIP formats. I would look at GZipOutputStream. It appears to work like any other OutputStream, allowing you to write out some byte[] array.

Sending multiple files to a servlet with a single connection

I'm writing a Java desktop client which will send multiple files over the wire to a servlet using a post request. In the servlet I'm getting the input stream from the request to receive the files. The servlet will write the files to disk, one by one as they're read from the stream.
The implementation has a couple of requirements:
Only one HTTP request must be used to the server (so only a single stream)
The servlet must use a reasonable fixed amount of memory, no matter what the size of the files.
I had considered inserting markers into the stream so I know when one file ends and the next one begins. I'd then write some code to parse the stream in the servlet, and start writing the next file as appropriate.
Here's the thing... surely there's a library to do that. I've looked through apache commons and found nothing. Commons File Upload is interesting but since the upload comes from a Java app, not a browser it only solves the receiving end, not the sending.
Any ideas for a library which easily allows multiple file transfers across a single stream with fixed memory expectations even for very large files?
Thanks.
Just use HTTP multipart/form-data encoding on the POST request body. It's described in RFC-2388 and a standard way of uploading (multiple) files by HTTP.
You can do it with just java.net.URLConnection as described in this mini-tutorial, although it would generate lot of boilerplate code. A more convenienced approach would be using Apache Commons HttpClient.
In the servlet side you can then just use Apache Commons Fileupload to process the uploaded files the usual HTTP way (or when you're already on Servlet 3.0, the HttpServletRequest#getParts(), see also this answer for examples).

Send file with REST?

Do Java REST frameworks like Restlet and Jersey allow one to send a file as input to a web service?
This would encompass the client sending the file and the server then receiving and processing the file.
If you're asking if you can do an HTTP PUT to a Restlet service, then, absolutely, yes you can. You can use the Directory class if you just want to store the file, or you can implement your own ServerResource to handle the new resource.
I haven't used Jersey, but Restlet is a fantastic api.
Well, there's no restriction for them not to do it. REST handles files without any problems.
Take a look at:
http://mikedesjardins.us/wordpress/2007/04/restful-services-on-ftp/
You can also use REST Assured which builds on HTTP Client. It's very simple:
given().multiPart(new File("/somedir/file.bin")).when().post("/fileUpload");

Categories