/*
* Author:: Joe Williams (j@boundary.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

/*
* some of this was based on https://github.com/jenkinsci/hudson-notifo-plugin
*/

package hudson.plugins.boundary;

import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.Hudson;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;

public class Boundary
{
    private final String BOUNDARY_URI = "https://api.boundary.com/";
    private HttpClient client;
    private String id;
    private String token;
    private String body;

    public Boundary() {}

    public Boundary( String id, String token )
    {
        this.id = id;
        this.token = token;
    }

    public void sendEvent(AbstractBuild<?, ?> build)
    {

        HashMap<String, Object> event = new HashMap<String, Object>();

        event.put("fingerprintFields", Arrays.asList(new String[]{"build name"}));

        Map<String, String> source = new HashMap<String, String>();
        String hostname = "localhost";

        try {
            hostname = java.net.InetAddress.getLocalHost().getHostName();
        }
        catch(UnknownHostException e) {
            System.out.println("host lookup exception: " + e);
        }

        source.put("ref", hostname);
        source.put("type", "jenkins");
        event.put("source", source);

        Map<String, String> properties = new HashMap<String, String>();
        properties.put("build status", build.getResult().toString());
        properties.put("build number", build.getDisplayName());
        properties.put("build name", build.getProject().getName());
        event.put("properties", properties);

        event.put("title", String.format("Jenkins Build Job - %s - %s", build.getProject().getName(), build.getDisplayName()));

        if ( build.getResult().toString() == "SUCCESS" ) {
            event.put("severity", "INFO");
            event.put("status", "CLOSED");
        }
        else {
            event.put("severity", "WARN");
            event.put("status", "OPEN");
        }

        ObjectMapper mapper = new ObjectMapper();
        String jsonOutput = new String();

        try {
            jsonOutput = mapper.writeValueAsString(event);
            System.out.println(jsonOutput);
        }
        catch(IOException ioe) {
            System.out.println("json error: " + ioe);
        }

        createClient(  );

        PostMethod post = new PostMethod( BOUNDARY_URI + id + "/" + "events");

        post.addRequestHeader("Content-Type", "application/json");
        post.setRequestBody( jsonOutput );

        try
            {
                System.out.println(client.executeMethod( post ));
            }

        catch ( Exception e )
            {
                System.out.println( "Unable to send message to Boundary API: \n" + e);
            }

        finally
            {
                post.releaseConnection(  );
            }
    }

    private void createClient(  )
    {
        client = new HttpClient(  );

        Credentials defaultcreds = new UsernamePasswordCredentials( token, "" );
        client.getState(  ).setCredentials( AuthScope.ANY, defaultcreds );
        client.getParams(  ).setAuthenticationPreemptive( true );
    }
}

@@ -805,16 +805,44 @@
 .Hudson;
+%0Aimport hudson.model.Result;
 %0A%0Aimport
@@ -2858,51 +2858,48 @@
 if (
- build.getResult().toString() == %22SUCCESS%22 
+Result.SUCCESS.equals(build.getResult())
 ) %7B%0A

use result