-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadCredentialImpl.java
73 lines (60 loc) · 2.39 KB
/
LoadCredentialImpl.java
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
package com.loadfocus.jenkins.impl;
import com.cloudbees.plugins.credentials.CredentialsDescriptor;
import com.loadfocus.jenkins.AbstractCredential;
import com.loadfocus.jenkins.api.LoadAPI;
import hudson.Extension;
import hudson.util.FormValidation;
import hudson.util.Secret;
import jenkins.model.Jenkins;
import net.sf.json.JSONException;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.verb.POST;
import javax.mail.MessagingException;
import javax.servlet.ServletException;
import java.io.IOException;
public class LoadCredentialImpl extends AbstractCredential {
private static final long serialVersionUID = 1L;
private final Secret apiKey;
private final String description;
@DataBoundConstructor
public LoadCredentialImpl(String apiKey, String description) {
this.apiKey = Secret.fromString(apiKey);
this.description = description;
}
public String getDescription() {
return description;
}
public String getApiKey() {
return apiKey.getPlainText();
}
@Extension
public static class DescriptorImpl extends CredentialsDescriptor {
@Override
public String getDisplayName() {
return Messages.LoadCredential_DisplayName();
}
@POST
public FormValidation doTestConnection(@QueryParameter("apiKey") final Secret apiKey) throws MessagingException, IOException, JSONException, ServletException {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return FormValidation.ok();
}
return checkLoadKey(apiKey);
}
@POST
public FormValidation doTestExistingConnection(@QueryParameter("apiKey") final Secret apiKey) throws MessagingException, IOException, JSONException, ServletException {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return FormValidation.ok();
}
return checkLoadKey(apiKey);
}
private FormValidation checkLoadKey(final Secret apiKey) throws JSONException, IOException, ServletException {
LoadAPI ldr = new LoadAPI(apiKey);
if (ldr.isValidApiKey()) {
return FormValidation.okWithMarkup("Valid API Key");
} else {
return FormValidation.errorWithMarkup("Invalid API Key");
}
}
}
}