public ConfigurableServletWebServerFactory webServerFactory() {
return new JettyServletWebServerFactory();
}
+
}
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
+import org.springframework.validation.Validator;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
public class WorkflowVersionController {
private final WorkflowVersionManager workflowVersionManager;
+ private Validator validator;
+
+ @InitBinder
+ private void initBinder(WebDataBinder binder) {
+ binder.addValidators(validator);
+ }
@Autowired
public WorkflowVersionController(
- @Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager) {
+ @Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager,
+ @Qualifier("workflowVersionValidator") Validator validator ) {
this.workflowVersionManager = workflowVersionManager;
+ this.validator = validator;
}
@GetMapping
@PostMapping
@ApiOperation("Create workflow version")
- public ResponseEntity<WorkflowVersion> create(@RequestBody WorkflowVersion version,
+ public ResponseEntity<WorkflowVersion> create(@RequestBody @Validated WorkflowVersion version,
@PathVariable("workflowId") String workflowId,
@RequestParam(value = "baseVersionId", required = false) String baseVersionId,
@RequestHeader(USER_ID_HEADER_PARAM) String user) {
@PutMapping("/{versionId}")
@ApiOperation("Update workflow version")
- public void update(@RequestBody WorkflowVersion version, @PathVariable("workflowId") String workflowId,
+ public void update(@RequestBody @Validated WorkflowVersion version, @PathVariable("workflowId") String workflowId,
@PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER_PARAM) String user) {
version.setId(versionId);
workflowVersionManager.update(workflowId, version);
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
+import org.springframework.validation.FieldError;
+import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
return new ResponseEntity<>(exception.getMessage(), BAD_REQUEST);
}
+ //For workflowVersionValidator exception
+ @Override
+ protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException e,
+ final HttpHeaders headers,
+ final HttpStatus status,
+ final WebRequest request) {
+
+ FieldError result = e.getBindingResult().getFieldError();
+ return new ResponseEntity<>(result.getDefaultMessage(), BAD_REQUEST);
+ }
+
//For missing header exceptions
@Override
public ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex,
--- /dev/null
+/*
+ * Copyright © 2018 European Support Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.sdc.workflow.api.validator;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+import org.onap.sdc.workflow.persistence.types.ParameterEntity;
+import org.onap.sdc.workflow.persistence.types.WorkflowVersion;
+import org.springframework.stereotype.Component;
+import org.springframework.validation.Errors;
+import org.springframework.validation.Validator;
+
+
+@Component("workflowVersionValidator")
+public class WorkflowVersionValidator implements Validator{
+
+ @Override
+ public boolean supports(Class<?> aClass) {
+ return WorkflowVersion.class.equals(aClass);
+ }
+
+ @Override
+ public void validate(Object o, Errors errors) {
+
+ WorkflowVersion workflowVersion = (WorkflowVersion) o;
+ Collection<ParameterEntity> inputs = workflowVersion.getInputs();
+ Collection<ParameterEntity> outputs = workflowVersion.getOutputs();
+
+ if(containsDuplicates(inputs)){
+ errors.rejectValue("inputs", "duplicateName", new Object[] {inputs}, "Input name must be unique");
+ }
+
+ if(containsDuplicates(outputs)){
+ errors.rejectValue("outputs", "duplicateName", new Object[] {outputs}, "Output name must be unique");
+ }
+ }
+
+ private boolean containsDuplicates(Collection<ParameterEntity> parameters){
+ if(Objects.isNull(parameters) || parameters.size() < 2 ) {
+ return false;
+ }
+ Set<String> testSet = new HashSet<>();
+ return parameters.stream().anyMatch(s -> !testSet.add(s.getName()));
+ }
+}
package org.onap.sdc.workflow.persistence.types;
+import javax.validation.constraints.Pattern;
import lombok.Data;
+import javax.validation.constraints.NotNull;
@Data
public class ParameterEntity {
private String id;
+ @NotNull(message = "Parameter name may not be null")
+ @Pattern(regexp = "[A-Za-z0-9_]*", message = "The field must contain only letters, digits and underscores")
private String name;
+ @NotNull
private ParameterType type;
+ @NotNull
private boolean mandatory;
}
import java.util.Collection;
import java.util.Set;
import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Pattern;
+import javax.validation.constraints.Size;
import lombok.Data;
+
@Data
public class Workflow {
private String id;
- @NotNull
+ @NotNull(message = "Workflow name may not be null")
+ @Size(min = 6, max = 30, message = "The field must be at least 6 characters, and less than 30 characters")
+ @Pattern(regexp = "[A-Za-z0-9_]*", message = "The field must contain only letters, digits and underscores")
private String name;
private String description;
private Set<WorkflowVersionState> versionStates;