Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>5.18</version>
<version>6.2116.v7501b_67dc517</version>
<relativePath />
</parent>

Expand All @@ -22,6 +22,7 @@
~ hpi-plugin.version: The HPI Maven Plugin version used by the plugin..
~ stapler-plugin.version: The Stapler Maven plugin version required by the plugin.
-->
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
</properties>

<name>REST List Parameter</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.jenkins.plugins.restlistparam.util.PathExpressionValidationUtils;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import org.jenkinsci.Symbol;
import org.kohsuke.stapler.*;
import org.kohsuke.stapler.verb.POST;
Expand Down Expand Up @@ -75,364 +75,364 @@
this.restEndpoint = restEndpoint;
this.mimeType = mimeType;
this.valueExpression = valueExpression;
this.credentialId = StringUtils.isNotBlank(credentialId) ? credentialId : "";
this.credentialId = credentialId != null && !credentialId.trim().isEmpty() ? credentialId : "";
if (mimeType == MimeType.APPLICATION_JSON) {
this.displayExpression = StringUtils.isNotBlank(displayExpression) ? displayExpression : "$";
this.displayExpression = !displayExpression.isBlank() ? displayExpression : "$";
}
this.defaultValue = StringUtils.isNotBlank(defaultValue) ? defaultValue : "";
this.defaultValue = defaultValue != null && !defaultValue.trim().isEmpty() ? defaultValue : "";
this.valueOrder = valueOrder != null ? valueOrder : ValueOrder.NONE;
this.filter = StringUtils.isNotBlank(filter) ? filter : ".*";
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 = StringUtils.isNotBlank(credentialId) ? credentialId : "";
this.credentialId = credentialId != null && !credentialId.trim().isEmpty() ? credentialId : "";
if (mimeType == MimeType.APPLICATION_JSON) {
this.displayExpression = StringUtils.isNotBlank(displayExpression) ? displayExpression : "$";
this.displayExpression = !displayExpression.isBlank() ? displayExpression : "$";
}
this.defaultValue = StringUtils.isNotBlank(defaultValue) ? defaultValue : "";
this.defaultValue = defaultValue != null && !defaultValue.trim().isEmpty() ? defaultValue : "";
this.valueOrder = valueOrder != null ? valueOrder : ValueOrder.NONE;
this.filter = StringUtils.isNotBlank(filter) ? filter : ".*";
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 StringUtils.isNotBlank(displayExpression) ? displayExpression : "$";
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 (StringUtils.isNotBlank(value)) {
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 (StringUtils.isNotBlank(value)) {
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 (StringUtils.isBlank(restEndpoint)) {
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 (StringUtils.isBlank(valueExpression)) {
if (valueExpression == null || valueExpression.isBlank()) {
return FormValidation.error(Messages.RLP_DescriptorImpl_ValidationErr_ExpressionEmpty());
}
Optional<StandardCredentials> credentials = CredentialsUtils.findCredentials(context, credentialId);
if (StringUtils.isNotBlank(credentialId) && !credentials.isPresent()) {
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,
StringUtils.isNotBlank(displayExpression) ? displayExpression : "$",
!displayExpression.isBlank() ? displayExpression : "$",

Check warning on line 435 in src/main/java/io/jenkins/plugins/restlistparam/RestListParameterDefinition.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 78-435 are not covered by tests
filter,
valueOrder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang.StringUtils;

import org.jenkinsci.plugins.plaincredentials.StringCredentials;

import java.io.IOException;
Expand Down Expand Up @@ -311,7 +311,7 @@
}

private static boolean isFilterSet(String filter) {
return StringUtils.isNotBlank(filter) && !filter.equalsIgnoreCase(".*");
return filter != null && !filter.isBlank() && !filter.equalsIgnoreCase(".*");

Check warning on line 314 in src/main/java/io/jenkins/plugins/restlistparam/logic/RestValueService.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 314 is only partially covered, 5 branches are missing
}

private static boolean isOrderSet(ValueOrder order) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import hudson.util.ListBoxModel;
import io.jenkins.plugins.restlistparam.Messages;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;

import org.jenkinsci.plugins.plaincredentials.StringCredentials;

import java.util.Collections;
Expand Down Expand Up @@ -65,22 +65,22 @@
return FormValidation.ok();
}

if (StringUtils.isBlank(credentialsId)) {
if (credentialsId == null || credentialsId.isBlank()) {
return FormValidation.ok();
}
if (credentialsId.startsWith("${") && credentialsId.endsWith("}")) {
return FormValidation.warning(Messages.RLP_CredentialsUtils_ValidationWrn_ExpressionBased());
}
if (!findCredentials(context, credentialsId).isPresent()) {
return FormValidation.error(Messages.RLP_CredentialsUtils_ValidationErr_CannotFind());
}
return FormValidation.ok();
}

public static Optional<StandardCredentials> findCredentials(final Item context,
final String credentialsId)
{
if (StringUtils.isBlank(credentialsId)) {
if (credentialsId == null || credentialsId.isBlank()) {

Check warning on line 83 in src/main/java/io/jenkins/plugins/restlistparam/util/CredentialsUtils.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 68-83 are not covered by tests
return Optional.empty();
}
List<StandardCredentials> lookupCredentials = CredentialsProvider.lookupCredentials(
Expand Down
Loading