title: JAX-RS file downloads, multiple content types
body: <p>Let me provide some context first. I am working on a system that integrates with Microsoft SharePoint 2010, well not really SharePoint as a system but the virtual representation of it's filesystem, document libraries, etc... Users upload files to SharePoint, and my system monitors these files and indexes them into a search engine (including file content). User can interact with this system by means of REST interfaces. </p>
  
  <p>I have created a REST interface to fetch a file for the user corresponding a certain entry in my search engine. This uses the absolute network path as its identifier. An example would be <code>//corporateserver//library1/filex.docx</code>. Due to the same origin policy however I can not load this file client side. Therefore I am trying to transmit it via the server.</p>
  
  <p>I had some success using JAX-RS to transmit data, however, I am getting stuck at the following: </p>
  
  <p>The file the user wishes to download can be of mutliple content types, most of them will be microsoft office formats. I had a look through the list of registered MIME types and came across <code>application/msword</code> or <code>application/vnd.ms-powerpoint</code></p>
  
  <p>My question: is there a content type that would include Microsoft Office files? If not, how could one proceed to match the correct content types with a file that is being requested. What would happen if one would server a word file with content type <code>text/plain</code>? </p>
  
  <p>Any help on the subject would be greatly appreciated. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The code I use to transmit data:</p>
  
  <pre><code>package com.fujitsu.consensus.rest;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.QueryParam;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.Response;
  import javax.ws.rs.core.StreamingOutput;
  
  import org.apache.commons.io.IOUtils;
  import org.codehaus.jettison.json.JSONException;
  
  @Path("/fetcher")
  public class FetcherService {
  
      @GET
      @Path("/fetch")
      @Produces(MediaType.APPLICATION_OCTET_STREAM)
      public Response fetchFile(@QueryParam("path") String path) 
          throws JSONException, IOException {
  
          final File file = new File(path);
          System.out.println(path);
  
          StreamingOutput stream = new StreamingOutput() {
              @Override
              public void write(OutputStream output) throws IOException {
                  try {
                      output.write(IOUtils.toByteArray(new FileInputStream(file)));
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          };
  
          return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM)
              .header("Content-Disposition", "inline; filename=\"" + file.getName() + "\"") 
              .build();
      }
  }
  </code></pre>
  
  <p>JavaScript code:</p>
  
  <pre><code> $.ajax({
     url: "../rest/fetcher/fetch",
     type: "GET", //send it through get method
     data:{path:obj.id},
     success: function(response) {
     console.log(response);},
     error: function(xhr) {//Do Something to handle error}
   });
  </code></pre>
  
  <p>The response I get on client side: </p>
  
  <p><a href="https://i.stack.imgur.com/MXX3R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MXX3R.png" alt="response on client side"></a></p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I've added a HTTP trace as proof that the headers and data are in fact being transmitted, the download dialogue however is not shown.  </p>
  
  <p>The <code>Content-Disposition</code> header does not appear to be working with either inline or attachment.</p>
  
  <p><a href="https://i.stack.imgur.com/Cr7Vy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Cr7Vy.png" alt="HTTP trace result"></a> </p>