Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-building-blocks / src / main / java / org / onap / so / bpmn / infrastructure / bpmn / activity / DeployActivitySpecs.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.bpmn.activity;
22
23 import java.io.File;
24 import java.nio.file.Files;
25 import java.nio.file.Paths;
26 import javax.ws.rs.core.UriBuilder;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.StatusLine;
29 import org.apache.http.client.HttpClient;
30 import org.apache.http.client.methods.HttpPost;
31 import org.apache.http.entity.StringEntity;
32 import org.apache.http.impl.client.HttpClientBuilder;
33 import org.springframework.stereotype.Component;
34
35 @Component
36 public class DeployActivitySpecs {
37     private static final String ACTIVITY_FILE_LOCATION = "src/main/resources/ActivitySpec/";
38     private static final String ACTIVITY_SPEC_URI = "/activityspec-api/v1.0/activity-spec";
39     private static final String CONTENT_TYPE_JSON = "application/json";
40
41     public static void main(String[] args) throws Exception {
42
43         if (args == null || args.length == 0) {
44             System.out.println("Please specify hostname argument");
45             return;
46         }
47
48         String hostname = args[0];
49
50         File dir = new File(ACTIVITY_FILE_LOCATION);
51         if (!dir.isDirectory()) {
52             System.out.println("ActivitySpec store is not a directory");
53             return;
54         }
55
56         for (File f : dir.listFiles()) {
57             String activitySpecName = f.getName();
58             String errorMessage = deployActivitySpec(hostname, activitySpecName);
59             if (errorMessage == null) {
60                 System.out.println("Deployed Activity Spec: " + activitySpecName);
61             } else {
62                 System.out.println("Error deploying Activity Spec: " + activitySpecName + " : " + errorMessage);
63             }
64         }
65         return;
66     }
67
68     protected static String deployActivitySpec(String hostname, String activitySpecName) throws Exception {
69         String payload = new String(Files.readAllBytes(Paths.get(ACTIVITY_FILE_LOCATION + activitySpecName)));
70         try {
71             HttpClient client = HttpClientBuilder.create().build();
72
73             String url = UriBuilder.fromUri(hostname).path(ACTIVITY_SPEC_URI).build().toString();
74             HttpPost post = new HttpPost(url);
75
76             StringEntity input = new StringEntity(payload);
77             input.setContentType(CONTENT_TYPE_JSON);
78             post.setEntity(input);
79
80             HttpResponse response = client.execute(post);
81             StatusLine statusLine = response.getStatusLine();
82
83             if (statusLine != null) {
84                 if (statusLine.getStatusCode() != 200) {
85                     return (statusLine.toString());
86                 } else {
87                     return null;
88                 }
89             } else {
90                 return ("Empty response from the remote endpoint");
91             }
92
93         } catch (Exception e) {
94             return e.getMessage();
95         }
96
97     }
98 }