1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\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
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\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
17 package org.oran.otf.api.handler;
\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
41 public class CamundaProcessDeploymentHandler {
\r
42 private static final Logger logger =
\r
43 LoggerFactory.getLogger(CamundaProcessDeploymentHandler.class);
\r
45 private CamundaProcessDeploymentHandler() {
\r
46 // prevent instantiation
\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
57 if (!Utilities.isHostValid(host)) {
\r
58 logger.error("Host (%s) must use either the http or https protocol.", host);
\r
62 if (!Utilities.isPortValid(port)) {
\r
64 "Invalid port (%s) specified as environment variable 'otf.camunda.port'.",
\r
65 System.getenv("otf.camunda.port"));
\r
69 // Form the full url
\r
70 String postUrl = String.format("%s:%s/%s", host, port, path);
\r
72 try (CloseableHttpClient httpclient =
\r
73 HttpClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build()) {
\r
75 // build multipart upload request
\r
76 MultipartEntityBuilder builder =
\r
77 MultipartEntityBuilder.create()
\r
78 .addBinaryBody("bpmn", bpmn, ContentType.DEFAULT_BINARY, "bpmn");
\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
86 HttpEntity data = builder.build();
\r
88 // build http request and assign multipart upload data
\r
89 HttpUriRequest request =
\r
90 RequestBuilder.post(postUrl)
\r
93 "Basic " + Base64.getEncoder().encodeToString(aafCredentialsDecoded.getBytes()))
\r
97 System.out.println("Executing request " + request.getRequestLine());
\r
99 // Create a custom response handler
\r
100 ResponseHandler<Response> responseHandler =
\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
111 ? EntityUtils.toString(entity)
\r
112 : "Supplied bpmn file is not deployable.";
\r
113 return Utilities.Http.BuildResponse.badRequestWithMessage(message);
\r
115 throw new ClientProtocolException("Unexpected response status: " + status);
\r
119 Response responseBody = httpclient.execute(request, responseHandler);
\r
120 System.out.println("----------------------------------------");
\r
121 System.out.println(responseBody.getEntity().toString());
\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