added svcapi ui and camunda code
[it/otf.git] / otf-service-api / src / main / java / org / oran / otf / api / handler / CamundaProcessDeploymentHandler.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 package org.oran.otf.api.handler;\r
18 \r
19 import org.oran.otf.api.Utilities;\r
20 import org.oran.otf.common.utility.http.ResponseUtility;\r
21 import java.io.InputStream;\r
22 import java.util.Base64;\r
23 import javax.ws.rs.core.Response;\r
24 import org.apache.http.HttpEntity;\r
25 import org.apache.http.client.ClientProtocolException;\r
26 import org.apache.http.client.ResponseHandler;\r
27 import org.apache.http.client.methods.HttpUriRequest;\r
28 import org.apache.http.client.methods.RequestBuilder;\r
29 import org.apache.http.conn.HttpHostConnectException;\r
30 import org.apache.http.conn.ssl.NoopHostnameVerifier;\r
31 import org.apache.http.entity.ContentType;\r
32 import org.apache.http.entity.mime.MultipartEntityBuilder;\r
33 import org.apache.http.impl.client.CloseableHttpClient;\r
34 import org.apache.http.impl.client.HttpClients;\r
35 import org.apache.http.util.EntityUtils;\r
36 import org.slf4j.Logger;\r
37 import org.slf4j.LoggerFactory;\r
38 import org.springframework.stereotype.Component;\r
39 \r
40 @Component\r
41 public class CamundaProcessDeploymentHandler {\r
42   private static final Logger logger =\r
43       LoggerFactory.getLogger(CamundaProcessDeploymentHandler.class);\r
44 \r
45   private CamundaProcessDeploymentHandler() {\r
46     // prevent instantiation\r
47   }\r
48 \r
49   public Response start(InputStream bpmn, InputStream compressedResources) {\r
50     // Read necessary environment variables - Avoiding using Spring dependencies (@Value)\r
51     String host = System.getenv("otf.camunda.host");\r
52     String path = System.getenv("otf.camunda.uri.deploy-test-strategy-zip");\r
53     int port = Utilities.TryGetEnvironmentVariable("otf.camunda.port");\r
54     String aafCredentialsDecoded =\r
55         System.getenv("AAF_ID") + ":" + System.getenv("AAF_MECH_PASSWORD");\r
56 \r
57     if (!Utilities.isHostValid(host)) {\r
58       logger.error("Host (%s) must use either the http or https protocol.", host);\r
59       return null;\r
60     }\r
61 \r
62     if (!Utilities.isPortValid(port)) {\r
63       logger.error(\r
64           "Invalid port (%s) specified as environment variable 'otf.camunda.port'.",\r
65           System.getenv("otf.camunda.port"));\r
66       return null;\r
67     }\r
68 \r
69     // Form the full url\r
70     String postUrl = String.format("%s:%s/%s", host, port, path);\r
71 \r
72     try (CloseableHttpClient httpclient =\r
73         HttpClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build()) {\r
74 \r
75       // build multipart upload request\r
76       MultipartEntityBuilder builder =\r
77           MultipartEntityBuilder.create()\r
78               .addBinaryBody("bpmn", bpmn, ContentType.DEFAULT_BINARY, "bpmn");\r
79 \r
80       // add resources to the request if they were supplied\r
81       if (compressedResources != null) {\r
82         builder.addBinaryBody(\r
83             "resources", compressedResources, ContentType.DEFAULT_BINARY, "resources");\r
84       }\r
85 \r
86       HttpEntity data = builder.build();\r
87 \r
88       // build http request and assign multipart upload data\r
89       HttpUriRequest request =\r
90           RequestBuilder.post(postUrl)\r
91               .addHeader(\r
92                   "Authorization",\r
93                   "Basic " + Base64.getEncoder().encodeToString(aafCredentialsDecoded.getBytes()))\r
94               .setEntity(data)\r
95               .build();\r
96 \r
97       System.out.println("Executing request " + request.getRequestLine());\r
98 \r
99       // Create a custom response handler\r
100       ResponseHandler<Response> responseHandler =\r
101           response -> {\r
102             int status = response.getStatusLine().getStatusCode();\r
103             if (status >= 200 && status < 300) {\r
104               HttpEntity entity = response.getEntity();\r
105               String message = entity != null ? EntityUtils.toString(entity) : null;\r
106               return Response.ok(message).build();\r
107             } else if (status == 400) {\r
108               HttpEntity entity = response.getEntity();\r
109               String message =\r
110                   entity != null\r
111                       ? EntityUtils.toString(entity)\r
112                       : "Supplied bpmn file is not deployable.";\r
113               return Utilities.Http.BuildResponse.badRequestWithMessage(message);\r
114             } else {\r
115               throw new ClientProtocolException("Unexpected response status: " + status);\r
116             }\r
117           };\r
118 \r
119       Response responseBody = httpclient.execute(request, responseHandler);\r
120       System.out.println("----------------------------------------");\r
121       System.out.println(responseBody.getEntity().toString());\r
122 \r
123       return responseBody;\r
124     } catch (HttpHostConnectException e) {\r
125       return ResponseUtility.Build.serviceUnavailableWithMessage(e.getMessage());\r
126     } catch (Exception e) {\r
127       e.printStackTrace();\r
128       return ResponseUtility.Build.internalServerErrorWithMessage("Unable to deploy definition.");\r
129     }\r
130   }\r
131 }\r