aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/linaro/pubapi/LinaroPubAPIKey.java
blob: 4c80ec99dd167c11439bb79c60e300ac1d621b37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package linaro.pubapi;

import java.io.IOException;

import hudson.AbortException;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersAction;
import hudson.model.StringParameterValue;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 * @author Paul Sokolovsky
 */
public class LinaroPubAPIKey extends Builder {

    @DataBoundConstructor
    public LinaroPubAPIKey() {
    }


    @Override
    public Descriptor getDescriptor() {
        return (Descriptor) super.getDescriptor();
    }

    @Extension
    public static class Descriptor extends BuildStepDescriptor<Builder> {

        private String secretKey;
        private String apiURL;
        // Using public members alleviates us from creating getters/setters
        public String defaultNotBefore;
        public String defaultNotAfter;

        public Descriptor() {
            load();
        }

        public String getPubAPIURL() {
            return apiURL;
        }

        public void setPubAPIURL(String apiURL) {
            this.apiURL = apiURL;
        }

        public String getPubAPISecretKey() {
            return secretKey;
        }

        public void setPubAPISecretKey(String secretKey) {
            this.secretKey = secretKey;
        }

        @Override
        public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
            req.bindJSON(this, json.getJSONObject("linaropubapi"));
            save();
            return true;
        }

        @Override
        public boolean isApplicable(Class<? extends AbstractProject> jobType) {
            // Go bold and apply to any possible project type
            return true;
            //return FreeStyleProject.class.isAssignableFrom(jobType);
        }

        @Override
        public String getDisplayName() {
            return "Linaro Publishing API Token";
        }

    }

    @Override
    public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener)
            throws InterruptedException, IOException {
        HttpClient client = new HttpClient();
        String url = getDescriptor().getPubAPIURL();
        PostMethod method = new PostMethod(url);
        method.setRequestHeader("AuthToken", getDescriptor().getPubAPISecretKey());
        method.addParameter("not_valid_til", Integer.toString(Integer.parseInt(getDescriptor().defaultNotBefore) * 60));
        method.addParameter("expires", Integer.toString(Integer.parseInt(getDescriptor().defaultNotAfter) * 60));

        // Auto-retry once on some (connection) errors
        // Note: for some connection issues, there's very long timeouts, so
        // having anything more than 1 here contradicts "fail fast" approach.
        // Disable auto-retry altogether for now - note that there's autoretry
        // by default.
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(0, false));

        String token = null;
        try {
            int statusCode = client.executeMethod(method);
            //System.out.println("Status: " + method.getStatusLine());

            if (statusCode != 201) {
                System.err.println("Method failed: " + method.getStatusLine());
                throw new AbortException("Unable to get publishing key: " + method.getStatusLine());
            }
            String loc = method.getResponseHeader("Location").getValue();
            System.out.println("Token URL: " + loc);
            token = loc.substring(loc.lastIndexOf("/") + 1);

        } catch (HttpException e) {
            System.err.println("Fatal protocol violation: " + e.getMessage());
            e.printStackTrace();
            throw new AbortException("Unable to get publishing key: " + e.getMessage());
        } catch (IOException e) {
            System.err.println("Fatal transport error for '" + url + "': " + e.getMessage());
            e.printStackTrace();
            throw new AbortException("Unable to get publishing key: " + e.getMessage());
        } finally {
            // Release the connection
            method.releaseConnection();
        }

        build.addAction(new ParametersAction(new StringParameterValue("PUBLISH_TOKEN", token)));
        return true;
    }
}