Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / mso-infrastructure-bpmn / src / main / java / org / onap / so / bpmn / common / workflow / service / WorkflowMessageResource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.common.workflow.service;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.HeaderParam;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import org.onap.so.bpmn.common.workflow.service.CallbackHandlerService.CallbackError;
36 import org.onap.so.bpmn.common.workflow.service.CallbackHandlerService.CallbackResult;
37 import org.onap.so.logger.ErrorCode;
38 import org.onap.so.logger.MessageEnum;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Component;
43 import io.swagger.annotations.Api;
44 import io.swagger.annotations.ApiOperation;
45
46 /**
47  * Generalized REST interface that injects a message event into a waiting BPMN process. Examples:
48  * 
49  * <pre>
50  *     /WorkflowMessage/SDNCAResponse/6d10d075-100c-42d0-9d84-a52432681cae-1478486185286
51  *     /WorkflowMessage/SDNCAEvent/USOSTCDALTX0101UJZZ01
52  * </pre>
53  */
54 @Path("/")
55 @Api(description = "Provides a generic service to inject messages into a waiting BPMN Proccess")
56 @Component
57 public class WorkflowMessageResource {
58     private static final Logger logger = LoggerFactory.getLogger(WorkflowMessageResource.class);
59     private static final String LOGMARKER = "[WORKFLOW-MESSAGE]";
60
61     @Autowired
62     CallbackHandlerService callback;
63
64     @POST
65     @Path("/WorkflowMessage/{messageType}/{correlator}")
66     @ApiOperation(value = "Workflow message correlator", notes = "")
67     @Consumes("*/*")
68     @Produces(MediaType.TEXT_PLAIN)
69     public Response deliver(@HeaderParam("Content-Type") String contentType,
70             @PathParam("messageType") String messageType, @PathParam("correlator") String correlator, String message) {
71
72         String method = "receiveWorkflowMessage";
73
74         logger.debug(LOGMARKER + " Received workflow message" + " type='" + messageType + "'" + " correlator='"
75                 + correlator + "'" + (contentType == null ? "" : " contentType='" + contentType + "'") + " message="
76                 + System.lineSeparator() + message);
77
78         if (messageType == null || messageType.isEmpty()) {
79             String msg = "Missing message type";
80             logger.debug(LOGMARKER + " " + msg);
81             logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION.toString(), "BPMN",
82                     ErrorCode.DataError.getValue(), LOGMARKER + ":" + msg);
83             return Response.status(400).entity(msg).build();
84         }
85
86         if (correlator == null || correlator.isEmpty()) {
87             String msg = "Missing correlator";
88             logger.debug(LOGMARKER + " " + msg);
89             logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION.toString(), "BPMN",
90                     ErrorCode.DataError.getValue(), LOGMARKER + ":" + msg);
91             return Response.status(400).entity(msg).build();
92         }
93
94         String messageEventName = "WorkflowMessage";
95         String messageVariable = messageType + "_MESSAGE";
96         String correlationVariable = messageType + "_CORRELATOR";
97         String correlationValue = correlator;
98         String contentTypeVariable = messageType + "_CONTENT_TYPE";
99
100         Map<String, Object> variables = new HashMap<>();
101
102         if (contentType != null) {
103             variables.put(contentTypeVariable, contentType);
104         }
105
106         CallbackResult result = callback.handleCallback(method, message, messageEventName, messageVariable,
107                 correlationVariable, correlationValue, LOGMARKER, variables);
108
109         if (result instanceof CallbackError) {
110             return Response.status(500).entity(((CallbackError) result).getErrorMessage()).build();
111         } else {
112             return Response.status(204).build();
113         }
114     }
115 }