forked from jenkinsci/rest-list-parameter-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestListParameterDefinition.java
More file actions
450 lines (396 loc) · 15.8 KB
/
RestListParameterDefinition.java
File metadata and controls
450 lines (396 loc) · 15.8 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package io.jenkins.plugins.restlistparam;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import hudson.Extension;
import hudson.model.Item;
import io.jenkins.plugins.restlistparam.logic.ValueResolver;
import io.jenkins.plugins.restlistparam.model.ValueItem;
import hudson.model.ParameterDefinition;
import hudson.model.ParameterValue;
import hudson.model.SimpleParameterDefinition;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import io.jenkins.plugins.restlistparam.logic.RestValueService;
import io.jenkins.plugins.restlistparam.model.MimeType;
import io.jenkins.plugins.restlistparam.model.ResultContainer;
import io.jenkins.plugins.restlistparam.model.ValueOrder;
import io.jenkins.plugins.restlistparam.util.CredentialsUtils;
import io.jenkins.plugins.restlistparam.util.PathExpressionValidationUtils;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.*;
import org.kohsuke.stapler.verb.POST;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
public final class RestListParameterDefinition extends SimpleParameterDefinition {
private static final long serialVersionUID = 3453376762337829455L;
private static final RestListParameterGlobalConfig config = RestListParameterGlobalConfig.get();
private final String restEndpoint;
private final String credentialId;
private final MimeType mimeType;
private final String valueExpression;
private String displayExpression;
private ValueOrder valueOrder;
private String defaultValue;
private String filter;
private Integer cacheTime;
private String errorMsg;
private List<ValueItem> values;
@DataBoundConstructor
public RestListParameterDefinition(final String name,
final String description,
final String restEndpoint,
final String credentialId,
final MimeType mimeType,
final String valueExpression,
final String displayExpression)
{
this(name, description, restEndpoint, credentialId, mimeType, valueExpression,
displayExpression, ValueOrder.NONE, ".*", config.getCacheTime(), "");
}
public RestListParameterDefinition(final String name,
final String description,
final String restEndpoint,
final String credentialId,
final MimeType mimeType,
final String valueExpression,
final String displayExpression,
final ValueOrder valueOrder,
final String filter,
final Integer cacheTime,
final String defaultValue)
{
super(name);
setDescription(description);
this.restEndpoint = restEndpoint;
this.mimeType = mimeType;
this.valueExpression = valueExpression;
this.credentialId = credentialId != null && !credentialId.trim().isEmpty() ? credentialId : "";
if (mimeType == MimeType.APPLICATION_JSON) {
this.displayExpression = !displayExpression.isBlank() ? displayExpression : "$";
}
this.defaultValue = defaultValue != null && !defaultValue.trim().isEmpty() ? defaultValue : "";
this.valueOrder = valueOrder != null ? valueOrder : ValueOrder.NONE;
this.filter = !filter.isBlank() ? filter : ".*";
this.cacheTime = cacheTime != null ? cacheTime : config.getCacheTime();
this.errorMsg = "";
this.values = Collections.emptyList();
}
private RestListParameterDefinition(final String name,
final String description,
final String restEndpoint,
final String credentialId,
final MimeType mimeType,
final String valueExpression,
final String displayExpression,
final ValueOrder valueOrder,
final String filter,
final Integer cacheTime,
final String defaultValue,
final List<ValueItem> values)
{
super(name);
setDescription(description);
this.restEndpoint = restEndpoint;
this.mimeType = mimeType;
this.valueExpression = valueExpression;
this.credentialId = credentialId != null && !credentialId.trim().isEmpty() ? credentialId : "";
if (mimeType == MimeType.APPLICATION_JSON) {
this.displayExpression = !displayExpression.isBlank() ? displayExpression : "$";
}
this.defaultValue = defaultValue != null && !defaultValue.trim().isEmpty() ? defaultValue : "";
this.valueOrder = valueOrder != null ? valueOrder : ValueOrder.NONE;
this.filter = !filter.isBlank() ? filter : ".*";
this.cacheTime = cacheTime != null ? cacheTime : config.getCacheTime();
this.errorMsg = "";
this.values = values;
}
public String getRestEndpoint() {
return restEndpoint;
}
public String getCredentialId() {
return credentialId;
}
public MimeType getMimeType() {
return mimeType;
}
public String getValueExpression() {
return valueExpression;
}
public String getFilter() {
return filter;
}
public String getDisplayExpression() {
if (mimeType == MimeType.APPLICATION_JSON) {
return !displayExpression.isBlank() ? displayExpression : "$";
}
return "";
}
@DataBoundSetter
public void setDisplayExpression(final String displayExpression) {
this.displayExpression = displayExpression;
}
@DataBoundSetter
public void setValueOrder(final ValueOrder valueOrder) {
this.valueOrder = valueOrder;
}
public ValueOrder getValueOrder() {
return valueOrder != null ? valueOrder : ValueOrder.NONE;
}
@DataBoundSetter
public void setFilter(final String filter) {
this.filter = filter;
}
public Integer getCacheTime() {
return cacheTime != null ? cacheTime : config.getCacheTime();
}
@DataBoundSetter
public void setCacheTime(final Integer cacheTime) {
this.cacheTime = cacheTime;
}
public String getDefaultValue() {
return defaultValue;
}
@DataBoundSetter
public void setDefaultValue(final String defaultValue) {
this.defaultValue = defaultValue;
}
void setErrorMsg(final String errorMsg) {
this.errorMsg = errorMsg;
}
public String getErrorMsg() {
return errorMsg;
}
public List<ValueItem> getValues() {
Item context = null;
if (Stapler.getCurrentRequest2() != null) {
context = Stapler.getCurrentRequest2().findAncestorObject(Item.class);
}
Optional<StandardCredentials> credentials = CredentialsUtils.findCredentials(context, credentialId);
ResultContainer<List<ValueItem>> container = RestValueService.get(
getRestEndpoint(),
credentials.orElse(null),
getMimeType(),
getCacheTime(),
getValueExpression(),
getDisplayExpression(),
getFilter(),
getValueOrder());
setErrorMsg(container.getErrorMsg().orElse(""));
values = container.getValue();
return values;
}
@Override
public ParameterDefinition copyWithDefaultValue(final ParameterValue defaultValue) {
if (defaultValue instanceof RestListParameterValue) {
RestListParameterValue value = (RestListParameterValue) defaultValue;
return new RestListParameterDefinition(
getName(), getDescription(), getRestEndpoint(), getCredentialId(), getMimeType(),
getValueExpression(), getDisplayExpression(), getValueOrder(), getFilter(), getCacheTime(),
ValueResolver.parseDisplayValue(getMimeType(), value.getValue(), displayExpression), getValues());
}
else {
return this;
}
}
@Override
public ParameterValue createValue(final String value) {
RestListParameterValue parameterValue = new RestListParameterValue(getName(), value, getDescription());
checkValue(parameterValue);
return parameterValue;
}
@Override
public ParameterValue createValue(final StaplerRequest2 req,
final JSONObject jo)
{
RestListParameterValue value = req.bindJSON(RestListParameterValue.class, jo);
checkValue(value);
return value;
}
private void checkValue(final RestListParameterValue value) {
if (!isValid(value)) {
throw new IllegalArgumentException(Messages.RLP_Definition_ValueException(getName(), value.getValue()));
}
}
@Override
public boolean isValid(ParameterValue value) {
if(value == null || value.getValue() == null) {
return false;
}
getValues();
return values.stream()
.map(ValueItem::getValue)
.filter(Objects::nonNull)
.anyMatch(val -> value.getValue().equals(val));
}
@Override
public int hashCode() {
return Objects.hash(
getName(), getDescription(), getRestEndpoint(), getCredentialId(),
getMimeType(), getValueExpression(), getFilter());
}
@Override
public boolean equals(Object obj) {
if (obj == null || this.getClass() != obj.getClass()) {
return false;
}
if (this == obj) {
return true;
}
RestListParameterDefinition other = (RestListParameterDefinition) obj;
if (!Objects.equals(getName(), other.getName())) {
return false;
}
if (!Objects.equals(getDescription(), other.getDescription())) {
return false;
}
if (!Objects.equals(getRestEndpoint(), other.getRestEndpoint())) {
return false;
}
if (!Objects.equals(getCredentialId(), other.getCredentialId())) {
return false;
}
if (!Objects.equals(getMimeType(), other.getMimeType())) {
return false;
}
if (!Objects.equals(getValueExpression(), other.getValueExpression())) {
return false;
}
if (!Objects.equals(getFilter(), other.getFilter())) {
return false;
}
return Objects.equals(defaultValue, other.defaultValue);
}
@Symbol({"RESTList", "RestList", "RESTListParam"})
@Extension
public static class DescriptorImpl extends ParameterDescriptor {
@Override
@Nonnull
public String getDisplayName() {
return Messages.RLP_DescriptorImpl_DisplayName();
}
public Integer getDefaultCacheTime() {
return config.getCacheTime();
}
@POST
public FormValidation doCheckRestEndpoint(@AncestorInPath final Item context,
@QueryParameter final String value,
@QueryParameter final String credentialId)
{
if (context == null) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
}
else {
context.checkPermission(Item.CONFIGURE);
}
if (value != null && !value.trim().isEmpty()) {
if (value.matches("^http(s)?://.+")) {
Optional<StandardCredentials> credentials = CredentialsUtils.findCredentials(context, credentialId);
return RestValueService.doBasicValidation(value, credentials.orElse(null));
}
return FormValidation.error(Messages.RLP_DescriptorImpl_ValidationErr_EndpointUrl());
}
return FormValidation.error(Messages.RLP_DescriptorImpl_ValidationErr_EndpointEmpty());
}
@POST
public FormValidation doCheckValueExpression(@AncestorInPath final Item context,
@QueryParameter final String value,
@QueryParameter final MimeType mimeType)
{
if (context == null) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
}
else {
context.checkPermission(Item.CONFIGURE);
}
if (value != null && !value.trim().isEmpty()) {
switch (mimeType) {
case APPLICATION_JSON:
return PathExpressionValidationUtils.doCheckJsonPathExpression(value);
case APPLICATION_XML:
return PathExpressionValidationUtils.doCheckXPathExpression(value);
default:
return FormValidation.error(Messages.RLP_DescriptorImpl_ValidationErr_UnknownMime());
}
}
return FormValidation.error(Messages.RLP_DescriptorImpl_ValidationErr_ExpressionEmpty());
}
@POST
public ListBoxModel doFillCredentialIdItems(@AncestorInPath final Item context,
@QueryParameter final String credentialId)
{
return CredentialsUtils.doFillCredentialsIdItems(context, credentialId);
}
@POST
public FormValidation doCheckCredentialId(@AncestorInPath final Item context,
@QueryParameter final String value)
{
return CredentialsUtils.doCheckCredentialsId(context, value);
}
@POST
public FormValidation doCheckCacheTime(@AncestorInPath final Item context,
@QueryParameter final Integer cacheTime)
{
if (context == null) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
}
else {
context.checkPermission(Item.CONFIGURE);
}
if (cacheTime != null && cacheTime >= 0) {
return FormValidation.ok();
}
return FormValidation.error(Messages.RLP_DescriptorImpl_ValidationErr_CacheTime());
}
@POST
public FormValidation doTestConfiguration(@AncestorInPath final Item context,
@QueryParameter final String restEndpoint,
@QueryParameter final String credentialId,
@QueryParameter final MimeType mimeType,
@QueryParameter final String valueExpression,
@QueryParameter final String displayExpression,
@QueryParameter final String filter,
@QueryParameter final ValueOrder valueOrder)
{
if (context == null) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
}
else {
context.checkPermission(Item.CONFIGURE);
}
if (restEndpoint == null || restEndpoint.trim().isEmpty()) {
return FormValidation.error(Messages.RLP_DescriptorImpl_ValidationErr_EndpointEmpty());
}
if (mimeType == null) {
return FormValidation.error(Messages.RLP_DescriptorImpl_ValidationErr_UnknownMime());
}
if (valueExpression == null || valueExpression.isBlank()) {
return FormValidation.error(Messages.RLP_DescriptorImpl_ValidationErr_ExpressionEmpty());
}
Optional<StandardCredentials> credentials = CredentialsUtils.findCredentials(context, credentialId);
if (credentialId != null && !credentialId.trim().isEmpty() && !credentials.isPresent()) {
return FormValidation.error(Messages.RLP_CredentialsUtils_ValidationErr_CannotFind());
}
ResultContainer<List<ValueItem>> container = RestValueService.get(
restEndpoint,
credentials.orElse(null),
mimeType,
0,
valueExpression,
!displayExpression.isBlank() ? displayExpression : "$",
filter,
valueOrder);
Optional<String> errorMsg = container.getErrorMsg();
List<ValueItem> values = container.getValue();
if (errorMsg.isPresent()) {
return FormValidation.error(errorMsg.get());
}
// values should NEVER be empty here
// due to all the filtering and error handling done in the RestValueService
return FormValidation.ok(Messages.RLP_DescriptorImpl_ValidationOk_ConfigValid(values.size(), values.get(0).getDisplayValue()));
}
}
}