--- /dev/null
+/* Copyright (c) 2019 AT&T Intellectual Property. #\r
+# #\r
+# Licensed under the Apache License, Version 2.0 (the "License"); #\r
+# you may not use this file except in compliance with the License. #\r
+# You may obtain a copy of the License at #\r
+# #\r
+# http://www.apache.org/licenses/LICENSE-2.0 #\r
+# #\r
+# Unless required by applicable law or agreed to in writing, software #\r
+# distributed under the License is distributed on an "AS IS" BASIS, #\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #\r
+# See the License for the specific language governing permissions and #\r
+# limitations under the License. #\r
+##############################################################################*/\r
+\r
+\r
+package org.oran.otf.api.handler;\r
+\r
+import org.oran.otf.api.Utilities;\r
+import org.oran.otf.common.utility.http.ResponseUtility;\r
+import java.io.InputStream;\r
+import java.util.Base64;\r
+import javax.ws.rs.core.Response;\r
+import org.apache.http.HttpEntity;\r
+import org.apache.http.client.ClientProtocolException;\r
+import org.apache.http.client.ResponseHandler;\r
+import org.apache.http.client.methods.HttpUriRequest;\r
+import org.apache.http.client.methods.RequestBuilder;\r
+import org.apache.http.conn.HttpHostConnectException;\r
+import org.apache.http.conn.ssl.NoopHostnameVerifier;\r
+import org.apache.http.entity.ContentType;\r
+import org.apache.http.entity.mime.MultipartEntityBuilder;\r
+import org.apache.http.impl.client.CloseableHttpClient;\r
+import org.apache.http.impl.client.HttpClients;\r
+import org.apache.http.util.EntityUtils;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class CamundaProcessDeploymentHandler {\r
+ private static final Logger logger =\r
+ LoggerFactory.getLogger(CamundaProcessDeploymentHandler.class);\r
+\r
+ private CamundaProcessDeploymentHandler() {\r
+ // prevent instantiation\r
+ }\r
+\r
+ public Response start(InputStream bpmn, InputStream compressedResources) {\r
+ // Read necessary environment variables - Avoiding using Spring dependencies (@Value)\r
+ String host = System.getenv("otf.camunda.host");\r
+ String path = System.getenv("otf.camunda.uri.deploy-test-strategy-zip");\r
+ int port = Utilities.TryGetEnvironmentVariable("otf.camunda.port");\r
+ String aafCredentialsDecoded =\r
+ System.getenv("AAF_ID") + ":" + System.getenv("AAF_MECH_PASSWORD");\r
+\r
+ if (!Utilities.isHostValid(host)) {\r
+ logger.error("Host (%s) must use either the http or https protocol.", host);\r
+ return null;\r
+ }\r
+\r
+ if (!Utilities.isPortValid(port)) {\r
+ logger.error(\r
+ "Invalid port (%s) specified as environment variable 'otf.camunda.port'.",\r
+ System.getenv("otf.camunda.port"));\r
+ return null;\r
+ }\r
+\r
+ // Form the full url\r
+ String postUrl = String.format("%s:%s/%s", host, port, path);\r
+\r
+ try (CloseableHttpClient httpclient =\r
+ HttpClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build()) {\r
+\r
+ // build multipart upload request\r
+ MultipartEntityBuilder builder =\r
+ MultipartEntityBuilder.create()\r
+ .addBinaryBody("bpmn", bpmn, ContentType.DEFAULT_BINARY, "bpmn");\r
+\r
+ // add resources to the request if they were supplied\r
+ if (compressedResources != null) {\r
+ builder.addBinaryBody(\r
+ "resources", compressedResources, ContentType.DEFAULT_BINARY, "resources");\r
+ }\r
+\r
+ HttpEntity data = builder.build();\r
+\r
+ // build http request and assign multipart upload data\r
+ HttpUriRequest request =\r
+ RequestBuilder.post(postUrl)\r
+ .addHeader(\r
+ "Authorization",\r
+ "Basic " + Base64.getEncoder().encodeToString(aafCredentialsDecoded.getBytes()))\r
+ .setEntity(data)\r
+ .build();\r
+\r
+ System.out.println("Executing request " + request.getRequestLine());\r
+\r
+ // Create a custom response handler\r
+ ResponseHandler<Response> responseHandler =\r
+ response -> {\r
+ int status = response.getStatusLine().getStatusCode();\r
+ if (status >= 200 && status < 300) {\r
+ HttpEntity entity = response.getEntity();\r
+ String message = entity != null ? EntityUtils.toString(entity) : null;\r
+ return Response.ok(message).build();\r
+ } else if (status == 400) {\r
+ HttpEntity entity = response.getEntity();\r
+ String message =\r
+ entity != null\r
+ ? EntityUtils.toString(entity)\r
+ : "Supplied bpmn file is not deployable.";\r
+ return Utilities.Http.BuildResponse.badRequestWithMessage(message);\r
+ } else {\r
+ throw new ClientProtocolException("Unexpected response status: " + status);\r
+ }\r
+ };\r
+\r
+ Response responseBody = httpclient.execute(request, responseHandler);\r
+ System.out.println("----------------------------------------");\r
+ System.out.println(responseBody.getEntity().toString());\r
+\r
+ return responseBody;\r
+ } catch (HttpHostConnectException e) {\r
+ return ResponseUtility.Build.serviceUnavailableWithMessage(e.getMessage());\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ return ResponseUtility.Build.internalServerErrorWithMessage("Unable to deploy definition.");\r
+ }\r
+ }\r
+}\r