package com.redhat.jenkins.plugins.buildrequester;

import hudson.maven.MavenModuleSetBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Failure;
import hudson.security.ACL;
import hudson.security.Permission;
import hudson.util.HttpResponses;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.HttpRedirect;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.interceptor.RequirePOST;

import javax.servlet.ServletException;
import java.util.List;

/**
 * @author vdedik@redhat.com
 */
public class BuildRequesterAction implements Action {
    public static final Permission BUILD_REQUEST = AbstractProject.BUILD;

    private MavenModuleSetBuild build;
    private String url;

    // Props
    private String name;
    private String gav;
    private String javaVersion;
    private String mavenVersion;
    private String buildCommand;
    private String commandLineParameters;
    private String scm;
    private List<String> tags;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setJavaVersion(String javaVersion) {
        this.javaVersion = javaVersion;
    }

    public String getJavaVersion() {
        return this.javaVersion;
    }

    public String getGav() {
        return gav;
    }

    public void setGav(String gav) {
        this.gav = gav;
    }

    public String getMavenVersion() {
        return mavenVersion;
    }

    public void setMavenVersion(String mavenVersion) {
        this.mavenVersion = mavenVersion;
    }

    public String getBuildCommand() {
        return buildCommand;
    }

    public void setBuildCommand(String buildCommand) {
        this.buildCommand = buildCommand;
    }

    public String getCommandLineParameters() {
        return commandLineParameters;
    }

    public void setCommandLineParameters(String commandLineParameters) {
        this.commandLineParameters = commandLineParameters;
    }

    public String getScm() {
        return scm;
    }

    public void setScm(String scm) {
        this.scm = scm;
    }

    public List<String> getTags() {
        return tags;
    }

    public void setTags(List<String> tags) {
        this.tags = tags;
    }

    public void setBuild(MavenModuleSetBuild build) {
        this.build = build;
    }

    public MavenModuleSetBuild getBuild() {
        return this.build;
    }

    public ACL getACL() {
        return this.build.getACL();
    }

    @RequirePOST
    public HttpResponse doBuildRequestSubmit(StaplerRequest req) {
        getACL().checkPermission(BUILD_REQUEST);

        try {
            JSONObject form = req.getSubmittedForm();
            // Remove keys that are empty (i.e. inputs without name)
            form.remove("");

            System.out.println(form.toString());
        } catch (ServletException e) {
            throw new Failure("Exception: " + e.getMessage());
        }
        return new HttpRedirect("..");
    }

    @Override
    public String getIconFileName() {
        return "/images/24x24/redo.png";
    }

    @Override
    public String getDisplayName() {
        return "Handover to Prod";
    }

    @Override
    public String getUrlName() {
        return "handover";
    }
}

@@ -247,42 +247,8 @@
 on;%0A
-import hudson.util.HttpResponses;%0A
 impo
@@ -487,24 +487,180 @@
 tException;%0A
+import java.io.DataOutputStream;%0Aimport java.io.IOException;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL;%0A
 import java.
@@ -3002,24 +3002,25 @@
 ttedForm();%0A
+%0A
             
@@ -3122,69 +3122,1061 @@
     
-System.out.println(form.toString());%0A        %7D catch (Servlet
+// Send the request%0A            URL nclUrl = new URL(this.url);%0A            HttpURLConnection conn = (HttpURLConnection) nclUrl.openConnection();%0A            conn.setRequestMethod(%22POST%22);%0A            conn.setRequestProperty(%22User-Agent%22, %22Mozilla/5.0%22);%0A            conn.setRequestProperty(%22Accept-Language%22, %22en-US,en;q=0.5%22);%0A            conn.setRequestProperty(%22Content-Type%22, %22application/json%22);%0A            conn.setDoOutput(true);%0A            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());%0A            wr.writeBytes(form.toString());%0A            wr.flush();%0A            wr.close();%0A%0A            int responseCode = conn.getResponseCode();%0A%0A            if (responseCode / 100 != 2) %7B%0A                throw new Failure(%22Failed to send the build request: %22 + conn.getResponseMessage());%0A            %7D%0A        %7D catch (ServletException e) %7B%0A            throw new Failure(%22Exception: %22 + e.getMessage());%0A        %7D catch (MalformedURLException e) %7B%0A            throw new Failure(%22Exception: %22 + e.getMessage());%0A        %7D catch (IO
 Exce

Send request