package org.safehaus.jettyjam.rest;

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

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Singleton;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMultipart;

/**
 * REST operation to upload (a.k.a. deploy) a project war file.
 */
@Singleton
@Produces(MediaType.APPLICATION_JSON)
@Path(UploadResource.ENDPOINT_URL)
public class UploadResource {

    public final static String ENDPOINT_URL = "/upload";
    private final static Logger LOG = LoggerFactory.getLogger(UploadResource.class);
    public static final String FILENAME_PARAM = "file";
    public static final String CONTENT = "content";

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response upload(MimeMultipart multipart) {
        try {
            String filename = multipart.getBodyPart(0).getContent().toString();
            LOG.warn("FILENAME: " + filename);
//            LOG.warn("CONTENT: " + convertStreamToString());
            InputStream in = multipart.getBodyPart(1).getInputStream();
            String fileLocation = "/home/bahadyr/temp/" + filename;
            writeToFile(in, fileLocation);
        } catch (MessagingException ex) {
            LOG.error("upload", ex);
        } catch (IOException ex) {
            LOG.error("upload", ex);
        }
        return Response.status(Response.Status.CREATED).entity("ok").build();
    }

    static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }

    private void writeToFile(InputStream in, String fileLocation) {
        OutputStream out = null;

        try {
            int read;
            byte[] bytes = new byte[1024];

            out = new FileOutputStream(fileLocation);

            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            out.flush();
        } catch (IOException e) {
            LOG.error("Failed to write out file: " + fileLocation, e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    LOG.error("Failed while trying to close output stream for {}", fileLocation);
                }
            }
        }
    }
}

@@ -1194,71 +1194,8 @@
 e);%0A
-//            LOG.warn(%22CONTENT: %22 + convertStreamToString());%0A
     
@@ -1300,31 +1300,41 @@
 n = 
-%22/home/bahadyr/temp/%22 +
+/* config.getWarUploadPath() + */
  fil
@@ -1536,32 +1536,32 @@
  ex);%0A        %7D%0A
+
         return R
@@ -1633,202 +1633,8 @@
  %7D%0A%0A
-    static String convertStreamToString(java.io.InputStream is) %7B%0A        java.util.Scanner s = new java.util.Scanner(is).useDelimiter(%22%5C%5CA%22);%0A        return s.hasNext() ? s.next() : %22%22;%0A    %7D%0A%0A
     

Write to file applied.