Hi everyone,
First, sorry for my english. Secondly, I know there are a lot of existing posts about what I am going to talk about, but as far as I've searched I found nothing (really) relevant on my problem. So here we go.
I am currently developping a JAVA RESTful web service. The goal of this web service is to send some XML files (more precisely, some OVAL definitions) to connected clients. I have seen 4 'solutions' :
- put all the XML file content in a String (encode this String in base64 if needed) and send the String to the client
- put all the XML file content in a Byte[] and send the Byte[] to the client
- use a FTP auxiliary server
- send/receive the file by streaming on the socket (InputStream/OutputStream)
For the moment, I've decided to choose the solution n°4. You can see below some parts of my code concerning the file transfer.
Server side:
@GET @Path("/sendXML") @Produces(MediaType.TEXT_PLAIN) @GET @Path("/sendXML") @Produces(MediaType.TEXT_PLAIN) public String getHtml(@Context HttpServletResponse serv) throws IOException { ServletOutputStream o =serv.getOutputStream(); try { FileInputStream fis = new FileInputStream(new File("xml file to send")); int c; while ((c = fis.read()) != -1) { o.write(c); } fis.close(); o.close(); } catch (Exception e) { System.err.println("e"); } return "OK" ; }
Client side:
try { URL u = new URL("http://localhost:8080/ws.example.filetransfer/rest/sendXML"); HttpURLConnection uc = (HttpURLConnection) u.openConnection(); InputStream is = uc.getInputStream(); try { FileOutputStream fos = new FileOutputStream(new File("path where the file will be saved")); int c; while ((c = is.read()) != -1) { fos.write(c); } is.close(); fos.close(); } catch (Exception e) { System.err.println("FileStreamsTest: " + e); } } catch (Exception e) { e.printStackTrace(); }
I am developping and testing my programs under Eclipse. Everything seems OK, the XML file is well sent from server to client, which saves it on the disk. Here is an console output when I launch the Client program (Server is already started):
<?xml version="1.0"?><hello> Hello Jersey</hello> <html> <title>Hello Jersey</title><body><h1>Hello Jersey</body></h1></html> <?xml version="1.0" encoding="UTF-8"?> <oval_definitions xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-definitions-5 oval-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#hpux hpux-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#unix unix-definitions-schema.xsd" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5"> <generator> <oval:product_name>The OVAL Repository</oval:product_name> <oval:schema_version>5.10.1</oval:schema_version> <oval:timestamp>2012-06-29T05:09:57.714-04:00</oval:timestamp> </generator> [...]
As you can see, when the client is receiving the XML file, it writes the content on stdout in addition to write this content on the disk (in the file which is specified in code). Writing on stdout is a minor problem, since the content is also written on disk, but is this normal, or a kind of Eclipse bug, or a bug from me ..?
To conclude, here are my questions:
- according to you, what is the best method to transfer XML file with a REST web service ?
- do you think the streaming solution is a good method?
- have I well implemented this solution (I'm in doubt with some pieces of code, like using the @Contex attribute)?
Thanks to all for your attention and your answers!
Heisen89