package org.lumongo.server.rest;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

import org.lumongo.LumongoConstants;
import org.lumongo.server.indexing.LumongoIndexManager;
import org.lumongo.util.StreamHelper;

@Path(LumongoConstants.ASSOCIATED_DOCUMENTS_URL)
public class AssociatedResource {
	
	private LumongoIndexManager indexManager;
	
	public AssociatedResource(LumongoIndexManager indexManager) {
		this.indexManager = indexManager;
	}
	
	@GET
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public StreamingOutput get(@QueryParam(LumongoConstants.UNIQUE_ID) final String uniqueId, @QueryParam(LumongoConstants.FILE_NAME) final String fileName,
					@QueryParam(LumongoConstants.INDEX) final String indexName) {
		return new StreamingOutput() {
			
			@Override
			public void write(OutputStream output) throws IOException, WebApplicationException {
				if (uniqueId != null && fileName != null && indexName != null) {
					InputStream is = indexManager.getAssociatedDocumentStream(indexName, uniqueId, fileName);
					if (is != null) {
						StreamHelper.copyStream(is, output);
						
					}
					else {
						throw new WebApplicationException("Cannot find associated document with uniqueId <" + uniqueId + "> with fileName <" + fileName + ">",
										LumongoConstants.NOT_FOUND);
					}
				}
				else {
					throw new WebApplicationException(LumongoConstants.UNIQUE_ID + " and " + LumongoConstants.FILE_NAME + " are required",
									LumongoConstants.BAD_REQUEST);
				}
			}
			
		};
		
	}
	
	@POST
	@Produces({ MediaType.TEXT_XML })
	public Response post(@QueryParam(LumongoConstants.UNIQUE_ID) final String uniqueId, @QueryParam(LumongoConstants.FILE_NAME) final String fileName,
					@QueryParam(LumongoConstants.INDEX) final String indexName, final InputStream is) {
		if (uniqueId != null && fileName != null && indexName != null) {
			
			try {
				indexManager.storeAssociatedDocument(indexName, uniqueId, fileName, is, false, null);
				
				return Response.status(LumongoConstants.SUCCESS)
								.entity("Stored associated document with uniqueId <" + uniqueId + "> and fileName <" + fileName + ">").build();
			}
			catch (Exception e) {
				return Response.status(LumongoConstants.INTERNAL_ERROR).entity(e.getMessage()).build();
			}
		}
		else {
			throw new WebApplicationException(LumongoConstants.UNIQUE_ID + " and " + LumongoConstants.FILE_NAME + " are required", LumongoConstants.BAD_REQUEST);
		}
		
	}
}

@@ -283,32 +283,65 @@
 ationException;%0A
+import javax.ws.rs.core.Context;%0A
 import javax.ws.
@@ -359,16 +359,16 @@
 iaType;%0A
-
 import j
@@ -650,18 +650,17 @@
 ource %7B%0A
-%09
 %0A
+
 %09private
@@ -694,18 +694,17 @@
 anager;%0A
-%09
 %0A
+
 %09public 
@@ -797,18 +797,17 @@
 ger;%0A%09%7D%0A
-%09
 %0A
+
 %09@GET%0A%09@
@@ -886,28 +886,49 @@
 lic 
-StreamingOutput get(
+Response get(@Context Response response, 
 @Que
@@ -977,33 +977,38 @@
 String uniqueId,
- 
+%0A%09%09%09%09%09
 @QueryParam(Lumo
@@ -1045,38 +1045,33 @@
 String fileName,
-%0A%09%09%09%09%09
+ 
 @QueryParam(Lumo
@@ -1116,24 +1116,43 @@
 Name) %7B%0A
+%0A
 %09%09
-return
+StreamingOutput stream =
  new Str
@@ -1164,27 +1164,24 @@
 gOutput() %7B%0A
-%09%09%09
 %0A%09%09%09@Overrid
@@ -1504,15 +1504,9 @@
 t);%0A
-%09%09%09%09%09%09
 %0A
+
 %09%09%09%09
@@ -1903,25 +1903,126 @@
 %09%09%7D%0A
-%09%09%09
 %0A
+
 %09%09%7D;%0A
+%0A
 %09%09
+return Response.ok(stream).header(%22content-disposition%22, %22attachment; filename = %22 + fileName).build();%0A
 %0A%09%7D%0A
-%09
 %0A
+
 %09@PO
@@ -2363,20 +2363,17 @@
 null) %7B%0A
-%09%09%09
 %0A
+
 %09%09%09try %7B
@@ -2467,13 +2467,9 @@
 l);%0A
-%09%09%09%09
 %0A
+
 %09%09%09%09
@@ -2938,12 +2938,9 @@
 %09%09%7D%0A
-%09%09
 %0A
+
 %09%7D%0A
+
 %7D
-%0A

make associated resource rest trigger a download in a web browser