use of org.onap.so.apihandlerinfra.tasksbeans.Variables in project so by onap.
the class ManualTasks method completeTask.
@POST
@Path("/{version:[vV]1}/{taskId}/complete")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Operation(description = "Complete specified task", responses = @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
@Transactional
public Response completeTask(String request, @PathParam("version") String version, @PathParam("taskId") String taskId, @Context ContainerRequestContext requestContext) throws ApiException {
String requestId = requestContext.getProperty("requestId").toString();
logger.info(LoggingAnchor.TWO, MessageEnum.APIH_GENERATED_REQUEST_ID.toString(), requestId);
logger.debug("requestId is: {}", requestId);
TasksRequest taskRequest = null;
String apiVersion = version.substring(1);
try {
ObjectMapper mapper = new ObjectMapper();
taskRequest = mapper.readValue(request, TasksRequest.class);
if (taskRequest.getRequestDetails() == null) {
throw new ValidationException("requestDetails");
}
if (taskRequest.getRequestDetails().getRequestInfo() == null) {
throw new ValidationException("requestInfo");
}
if (empty(taskRequest.getRequestDetails().getRequestInfo().getSource())) {
throw new ValidationException("source");
}
if (empty(taskRequest.getRequestDetails().getRequestInfo().getRequestorId())) {
throw new ValidationException("requestorId");
}
} catch (IOException e) {
ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError).build();
ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed: " + e.getMessage(), HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
throw validateException;
} catch (ValidationException e) {
ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError).errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON Object failed. " + e.getMessage(), HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo).build();
throw validateException;
}
// Transform the request to Camunda-style Complete request
Variables variablesForComplete = new Variables();
Value sourceValue = new Value();
sourceValue.setValue(taskRequest.getRequestDetails().getRequestInfo().getSource());
Value responseValue = new Value();
responseValue.setValue(taskRequest.getRequestDetails().getRequestInfo().getResponseValue().name());
Value requestorIdValue = new Value();
requestorIdValue.setValue(taskRequest.getRequestDetails().getRequestInfo().getRequestorId());
variablesForComplete.setSource(sourceValue);
variablesForComplete.setResponseValue(responseValue);
variablesForComplete.setRequestorId(requestorIdValue);
String camundaJsonReq;
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
camundaJsonReq = mapper.writeValueAsString(variablesForComplete);
} catch (JsonProcessingException e) {
logger.error("Mapping of JSON object to Camunda request failed");
ValidateException validateException = new ValidateException.Builder("Mapping of JSON object to Camunda request failed", HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR).build();
throw validateException;
}
String requestUrl = taskUri + "/" + taskId + "/complete";
ResponseEntity<String> response = camundaClient.post(camundaJsonReq, requestUrl);
int bpelStatus = responseHandler.setStatus(response.getStatusCodeValue());
responseHandler.acceptedOrNoContentResponse(response);
logger.debug("Received good response from Camunda");
TaskRequestReference trr = new TaskRequestReference();
trr.setTaskId(taskId);
String completeResp = null;
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
completeResp = mapper.writeValueAsString(trr);
} catch (JsonProcessingException e) {
logger.error("Unable to map response from Camunda");
ValidateException validateException = new ValidateException.Builder("Request Failed due to bad response format", bpelStatus, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).build();
throw validateException;
}
logger.debug("Response to the caller: {}", completeResp);
logger.debug("End of the transaction, the final response is: {}", completeResp);
return builder.buildResponse(HttpStatus.SC_ACCEPTED, requestId, completeResp, apiVersion);
}
Aggregations