2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.bpmn.activity;
24 import java.nio.file.Files;
25 import java.nio.file.Paths;
27 import javax.ws.rs.core.UriBuilder;
29 import org.apache.http.HttpResponse;
30 import org.apache.http.StatusLine;
31 import org.apache.http.client.HttpClient;
32 import org.apache.http.client.methods.HttpPost;
33 import org.apache.http.entity.StringEntity;
34 import org.apache.http.impl.client.HttpClientBuilder;
35 import org.springframework.stereotype.Component;
38 public class DeployActivitySpecs {
39 private static final String ACTIVITY_FILE_LOCATION = "src/main/resources/ActivitySpec/";
40 private static final String ACTIVITY_SPEC_URI = "/activityspec-api/v1.0/activity-spec";
41 private static final String CONTENT_TYPE_JSON = "application/json";
43 public static void main(String[] args) throws Exception {
45 if (args == null || args.length == 0) {
46 System.out.println("Please specify hostname argument");
50 String hostname = args[0];
52 File dir = new File(ACTIVITY_FILE_LOCATION);
53 if (!dir.isDirectory()) {
54 System.out.println("ActivitySpec store is not a directory");
58 for (File f : dir.listFiles()) {
59 String activitySpecName = f.getName();
60 String errorMessage = deployActivitySpec(hostname, activitySpecName);
61 if (errorMessage == null) {
62 System.out.println("Deployed Activity Spec: " + activitySpecName);
65 System.out.println("Error deploying Activity Spec: " + activitySpecName + " : " + errorMessage);
71 protected static String deployActivitySpec(String hostname, String activitySpecName) throws Exception {
72 String payload = new String(Files.readAllBytes(Paths.get(ACTIVITY_FILE_LOCATION + activitySpecName)));
74 HttpClient client = HttpClientBuilder.create().build();
76 String url = UriBuilder.fromUri(hostname).path(ACTIVITY_SPEC_URI).build().toString();
77 HttpPost post = new HttpPost(url);
79 StringEntity input = new StringEntity(payload);
80 input.setContentType(CONTENT_TYPE_JSON);
81 post.setEntity(input);
83 HttpResponse response = client.execute(post);
84 StatusLine statusLine = response.getStatusLine();
86 if (statusLine != null) {
87 if (statusLine.getStatusCode() != 200) {
88 return (statusLine.toString());
95 return("Empty response from the remote endpoint");
98 } catch (Exception e) {
99 return e.getMessage();