/*******************************************************************************
 * Copyright (c) 2014 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/

package com.codenvy.client.core;

import com.codenvy.client.FactoryClient;
import com.codenvy.client.Request;
import com.codenvy.client.core.auth.AuthenticationManager;
import com.codenvy.client.core.model.DefaultFactory;
import com.codenvy.client.model.Factory;
import com.codenvy.client.model.ProjectReference;

import org.glassfish.jersey.media.multipart.FormDataMultiPart;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA;

/**
 * The Codenvy factory API client.
 *
 * @author Florent Benoit
 */
public class DefaultFactoryClient extends AbstractClient implements FactoryClient {

    /**
     * Constructs an instance of {@link com.codenvy.client.core.DefaultFactoryClient}.
     *
     * @param url
     *         the Codenvy platform URL.
     * @param authenticationManager
     *         the {@link com.codenvy.client.core.auth.AuthenticationManager}.
     * @throws NullPointerException
     *         if url or authenticationManager parameter is {@code null}.
     */
    DefaultFactoryClient(String url, AuthenticationManager authenticationManager) {
        super(url, "factory", authenticationManager);
    }


    /**
     * Returns factory data
     *
     * @param jsonContent
     *         the content of the JSON factory
     * @return factory information.
     */

    @Override
    public Request<Factory> save(String jsonContent) {
        final FormDataMultiPart formDataMultiPart = new FormDataMultiPart().field("factoryUrl", jsonContent);

        final Invocation request = getWebTarget().request()
                                                 .accept(APPLICATION_JSON)
                                                 .buildPost(Entity.entity(formDataMultiPart, MULTIPART_FORM_DATA));

        return new SimpleRequest<Factory>(request, DefaultFactory.class, getAuthenticationManager());
    }

    /**
     * Returns factory content of a project
     *
     * @param projectReference@return
     *         factory content.
     */
    @Override
    public Request<String> export(ProjectReference projectReference) {

        final Invocation request = getWebTarget().path(projectReference.workspaceId())
                                                 .path(projectReference.name())
                                                 .request()
                                                 .accept(APPLICATION_JSON)
                                                 .buildGet();

        return new SimpleRequest<String>(request, getAuthenticationManager());
    }
}

@@ -881,86 +881,487 @@
 Data
-MultiPart;%0A%0Aimport javax.ws.rs.client.Entity;%0Aimport javax.ws.rs.client.Invoca
+BodyPart;%0Aimport org.glassfish.jersey.media.multipart.FormDataContentDisposition;%0Aimport org.glassfish.jersey.media.multipart.FormDataMultiPart;%0A%0Aimport javax.json.Json;%0Aimport javax.json.JsonObject;%0Aimport javax.json.JsonReader;%0Aimport javax.ws.rs.client.Entity;%0Aimport javax.ws.rs.client.Invocation;%0Aimport javax.ws.rs.core.MediaType;%0Aimport java.io.IOException;%0Aimport java.io.StringReader;%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL;%0Aimport java.net.URLConnec
 tion
@@ -2416,117 +2416,2267 @@
 ) %7B%0A
-        final FormDataMultiPart formDataMultiPart = new FormDataMultiPart().field(%22factoryUrl%22, jsonContent);
+%0A        // ok there we need to check if there is a logo element in the json content%0A        // and if it's there we will add the image as binary content of the form%0A        URL logoURL = null;%0A        JsonReader jSonReader = Json.createReader(new StringReader(jsonContent));%0A        JsonObject jsonObject = jSonReader.readObject();%0A        if (jsonObject != null) %7B%0A            JsonObject button = jsonObject.getJsonObject(%22button%22);%0A            if (button != null) %7B%0A                String type = button.getString(%22type%22);%0A                if (%22logo%22.equals(type)) %7B%0A                    JsonObject attributes = button.getJsonObject(%22attributes%22);%0A                    if (attributes != null) %7B%0A                        String logoPath = attributes.getString(%22logo%22);%0A                        if (logoPath != null && !logoPath.isEmpty()) %7B%0A                            // found a logo%0A                            try %7B%0A                                logoURL = new URL(logoPath);%0A                            %7D catch (MalformedURLException e) %7B%0A                                throw new IllegalStateException(%22Invalid logo content found in the JSON file.%22, e);%0A                            %7D%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A%0A%0A        final FormDataMultiPart formDataMultiPart = new FormDataMultiPart().field(%22factoryUrl%22, jsonContent);%0A%0A        // do we have a logo%0A        if (logoURL != null) %7B%0A            try  %7B%0A                URLConnection urlConnection = logoURL.openConnection();%0A                String contentType = urlConnection.getContentType();%0A                FormDataBodyPart formDataBodyPart = new FormDataBodyPart(FormDataContentDisposition%0A                                                                                 .name(%22image%22).fileName(%22example%22).build(),%0A                                                                         logoURL.openStream(),%0A                                                                         MediaType.valueOf(contentType));%0A%0A                formDataMultiPart.bodyPart(formDataBodyPart);%0A            %7D catch (IOException e) %7B%0A                throw new IllegalStateException(%22Invalid logo content found in the JSON file.%22, e);%0A            %7D%0A        %7D
 %0A%0A  

send image defined in the factory file if available