package org.opentosca.csarrepo.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.opentosca.csarrepo.filesystem.FileSystem;
import org.opentosca.csarrepo.model.CsarFile;

public class WineryApiClient {

	private Client client;
	private String url;

	public WineryApiClient(URL url) {
		this.url = url.toExternalForm();
		if(this.url.charAt(this.url.length() -1 ) != '/') {
			this.url += "/";
		}
		this.client = ClientBuilder.newClient(new ClientConfig()
				.register(MultiPartFeature.class));
	}

	public void uploadToWinery(CsarFile file) throws Exception {
		FileSystem fs = new FileSystem();
		File f = fs.getFile(file.getHashedFile().getFilename());

		if (f == null) {
			throw new FileNotFoundException(file.getName() + " not found");
		}

		// build form data
		FormDataMultiPart multiPart = new FormDataMultiPart();
		FormDataContentDisposition.FormDataContentDispositionBuilder dispositionBuilder = FormDataContentDisposition
				.name("file");

		dispositionBuilder.fileName(file.getName());
		dispositionBuilder.size(f.getTotalSpace());
		FormDataContentDisposition formDataContentDisposition = dispositionBuilder
				.build();

		multiPart.bodyPart(new FormDataBodyPart("file", f, MediaType.APPLICATION_OCTET_STREAM_TYPE)
				.contentDisposition(formDataContentDisposition));

		Entity<FormDataMultiPart> entity = Entity.entity(multiPart,
				MediaType.MULTIPART_FORM_DATA_TYPE);
		
		// send request
		WebTarget target = client.target(this.url);
		Builder request = target.request();
		Response response = request.post(entity);
		
		// handle response
		if(Status.NO_CONTENT.getStatusCode() == response.getStatus()) {
			return;
		}
		
		throw new Exception("failed to push to winery");
	}

}

@@ -86,24 +86,52 @@
 dException;%0A
+import java.io.InputStream;%0A
 import java.
@@ -275,25 +275,34 @@
 .client.
-WebTarget
+Invocation.Builder
 ;%0Aimport
@@ -321,34 +321,25 @@
 .client.
-Invocation.Builder
+WebTarget
 ;%0Aimport
@@ -2386,11 +2386,780 @@
 y%22);%0A%09%7D%0A
+%09%0A%09public InputStream pullFromWinery(String id) throws Exception %7B%0A%09%09// send request%0A%09%09WebTarget target = client.target(this.url + %22servicetemplates/%22 + id);%0A%09%09Builder request = target.request();%0A%09%09request.accept(%22application/zip%22);%0A%09%09Response response = request.get();%0A%09%09%0A%09%09if(Status.NOT_FOUND.getStatusCode() == response.getStatus()) %7B%0A%09%09%09// 404%0A%09%09%09throw new Exception(%22No corresponding servicetemplate found%22);%0A%09%09%7D%0A%09%09%0A%09%09if(Status.OK.getStatusCode() == response.getStatus()) %7B%0A%09%09%09// 200%0A%09%09%09try %7B%0A%09%09%09%09InputStream stream = (InputStream) response.getEntity();%0A%09%09%09%09return stream;%0A%09%09%09%7D catch(Exception e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%09System.out.println(e.getMessage());%0A%09%09%09%09return null;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09// other code%0A%09%09throw new Exception(%22Error connecting to winery%22);%0A%09%7D
 %0A%7D%0A

implement pull from winery to apiClient, refs #275