title: JAVA RESTful Webservice and XML files transfer
body: <p>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 'specific' problem. So here we go.</p>
  
  <p>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' :</p>
  
  <ul>
  <li>put all the XML file content in a String (encode this String in base64 if needed) and send the String to the client</li>
  <li>put all the XML file content in a Byte[] and send the Byte[] to the client</li>
  <li>use a FTP auxiliary server</li>
  <li>send/receive the file by streaming on the socket (InputStream/OutputStream)</li>
  </ul>
  
  <p>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.</p>
  
  <hr>
  
  <p>Server side:</p>
  
  <pre><code>@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" ;
  }
  </code></pre>
  
  <hr>
  
  <p>Client side:</p>
  
  <pre><code>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();
      }
  </code></pre>
  
  <hr>
  
  <p>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):</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;&lt;hello&gt; Hello Jersey&lt;/hello&gt;
  &lt;html&gt; &lt;title&gt;Hello Jersey&lt;/title&gt;&lt;body&gt;&lt;h1&gt;Hello Jersey&lt;/body&gt;&lt;/h1&gt;&lt;/html&gt; 
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  &lt;generator&gt;
  &lt;oval:product_name&gt;The OVAL Repository&lt;/oval:product_name&gt;
  &lt;oval:schema_version&gt;5.10.1&lt;/oval:schema_version&gt;
  &lt;oval:timestamp&gt;2012-06-29T05:09:57.714-04:00&lt;/oval:timestamp&gt;
  &lt;/generator&gt;
  [...]
  </code></pre>
  
  <p>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 ..?</p>
  
  <p>To conclude, here are my questions:</p>
  
  <ul>
  <li>according to you, what is the best method to transfer XML file with a JAVA REST web service ?</li>
  <li>do you think the streaming solution is a good method?</li>
  <li>have I well implemented this solution (I'm in doubt with some pieces of code, like using the @Contex attribute)?</li>
  </ul>
  
  <p>Thanks to all for your attention and your answers!
  Heisen89 </p>