Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
feat(plan): API Publisher can ask for a mandatory comment while consu…
Browse files Browse the repository at this point in the history
…mer is subscribing to the plan

Closes gravitee-io/issues#1660
  • Loading branch information
brasseld authored and NicolasGeraud committed Nov 21, 2018
1 parent 3794c6b commit bacf496
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public class NewPlanEntity {
@JsonProperty("excluded_groups")
private List<String> excludedGroups;

@JsonProperty("comment_required")
private boolean commentRequired;

public String getName() {
return name;
}
Expand Down Expand Up @@ -147,6 +150,14 @@ public void setSecurityDefinition(String securityDefinition) {
this.securityDefinition = securityDefinition;
}

public boolean isCommentRequired() {
return commentRequired;
}

public void setCommentRequired(boolean commentRequired) {
this.commentRequired = commentRequired;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public class PlanEntity {
@JsonProperty("excluded_groups")
private List<String> excludedGroups;

@JsonProperty("comment_required")
private boolean commentRequired;

public String getId() {
return id;
}
Expand Down Expand Up @@ -217,6 +220,14 @@ public void setExcludedGroups(List<String> excludedGroups) {
this.excludedGroups = excludedGroups;
}

public boolean isCommentRequired() {
return commentRequired;
}

public void setCommentRequired(boolean commentRequired) {
this.commentRequired = commentRequired;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public class UpdatePlanEntity {

private String securityDefinition;

@JsonProperty("comment_required")
private boolean commentRequired;

public String getName() {
return name;
}
Expand Down Expand Up @@ -126,6 +129,14 @@ public void setSecurityDefinition(String securityDefinition) {
this.securityDefinition = securityDefinition;
}

public boolean isCommentRequired() {
return commentRequired;
}

public void setCommentRequired(boolean commentRequired) {
this.commentRequired = commentRequired;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ public Response createSubscription(
if (newSubscriptionEntity == null) {
newSubscriptionEntity = new NewSubscriptionEntity();
}

PlanEntity planEntity = planService.findById(plan);

if (planEntity.isCommentRequired() &&
(newSubscriptionEntity.getRequest() == null || newSubscriptionEntity.getRequest().isEmpty())) {
return Response.status(Response.Status.BAD_REQUEST)
.entity("Plan requires a consumer comment when subscribing")
.build();
}

newSubscriptionEntity.setApplication(application);
newSubscriptionEntity.setPlan(plan);
Subscription subscription = convert(subscriptionService.create(newSubscriptionEntity));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public PlanEntity create(NewPlanEntity newPlan) {
plan.setSecurityDefinition(newPlan.getSecurityDefinition());
plan.setStatus(Plan.Status.valueOf(newPlan.getStatus().name()));
plan.setExcludedGroups(newPlan.getExcludedGroups());
plan.setCommentRequired(newPlan.isCommentRequired());

if (plan.getSecurity() == Plan.PlanSecurityType.KEY_LESS) {
// There is no need for a validation when authentication is KEY_LESS, force to AUTO
Expand Down Expand Up @@ -223,6 +224,7 @@ public PlanEntity update(UpdatePlanEntity updatePlan) {
newPlan.setDescription(updatePlan.getDescription());
newPlan.setUpdatedAt(new Date());
newPlan.setSecurityDefinition(updatePlan.getSecurityDefinition());
newPlan.setCommentRequired(updatePlan.isCommentRequired());

String planPolicies = objectMapper.writeValueAsString(updatePlan.getPaths());
newPlan.setDefinition(planPolicies);
Expand Down Expand Up @@ -532,6 +534,7 @@ private PlanEntity convert(Plan plan) {
entity.setPublishedAt(plan.getPublishedAt());
entity.setValidation(PlanValidationType.valueOf(plan.getValidation().name()));
entity.setCharacteristics(plan.getCharacteristics());
entity.setCommentRequired(plan.isCommentRequired());

return entity;
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<gravitee-definition.version>1.13.2</gravitee-definition.version>
<gravitee-common.version>1.13.0</gravitee-common.version>
<gravitee-plugin.version>1.4.0</gravitee-plugin.version>
<gravitee-repository.version>1.20.0</gravitee-repository.version>
<gravitee-repository.version>1.21.0-SNAPSHOT</gravitee-repository.version>
<gravitee-gateway-api.version>1.11.0</gravitee-gateway-api.version>
<gravitee-fetcher-api.version>1.0.0</gravitee-fetcher-api.version>
<gravitee-expression-language.version>1.0.0</gravitee-expression-language.version>
Expand Down

0 comments on commit bacf496

Please sign in to comment.