1cb535c7a97141ad6ee5a72d0e8d64252c84d2ea
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.workflow.services;
39
40 import java.io.BufferedReader;
41 import java.io.InputStreamReader;
42 import java.io.OutputStreamWriter;
43 import java.net.HttpURLConnection;
44 import java.net.URL;
45 import java.net.URLConnection;
46 import java.nio.charset.Charset;
47
48 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
49
50 public class WorkflowScheduleExecutor {
51
52         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WorkflowScheduleExecutor.class);
53
54         private String serverURL;
55         private String workflowKey;
56         private String myUrl;
57         private String payload;
58
59         public WorkflowScheduleExecutor(String serverURL, String workflowKey) {
60                 this.serverURL = serverURL;
61                 this.workflowKey = workflowKey;
62                 this.myUrl = this.serverURL + "/engine-rest/process-definition/key/" + this.workflowKey + "/submit-form";
63                 ;
64                 this.payload = "{\"variables\":{}}";
65         }
66
67         public void execute() {
68                 POST_fromURL(myUrl, payload);
69         }
70
71         public static String get_fromURL(String myURL) {
72                 logger.debug(EELFLoggerDelegate.debugLogger, "get_fromURL: Requested URL {}", myURL);
73                 StringBuilder sb = new StringBuilder();
74                 URLConnection urlConn = null;
75                 InputStreamReader in = null;
76                 try {
77                         URL url = new URL(myURL);
78                         urlConn = url.openConnection();
79                         if (urlConn != null)
80                                 urlConn.setReadTimeout(60 * 1000);
81                         if (urlConn != null && urlConn.getInputStream() != null) {
82                                 in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset());
83                                 BufferedReader bufferedReader = new BufferedReader(in);
84                                 int cp;
85                                 while ((cp = bufferedReader.read()) != -1)
86                                         sb.append((char) cp);
87                                 bufferedReader.close();
88                                 if(in != null)
89                                         in.close();
90                         }
91                 } catch (Exception e) {
92                         logger.error(EELFLoggerDelegate.errorLogger, "get_fromURL failed", e);
93                         throw new RuntimeException("Exception while calling URL:" + myURL, e);
94                 } finally {
95                         try {
96                                 if (in != null)
97                                         in.close();
98                         } catch (Exception e) {
99                                 logger.error(EELFLoggerDelegate.errorLogger, "get_fromURL close failed", e);
100                         }
101                 }
102                 return sb.toString();
103         }
104
105         public static String POST_fromURL(String myURL, String payload) {
106                 String line;
107                 StringBuilder jsonString = new StringBuilder();
108                 try {
109                         URL url = new URL(myURL);
110                         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
111                         connection.setDoInput(true);
112                         connection.setDoOutput(true);
113                         connection.setRequestMethod("POST");
114                         connection.setRequestProperty("Accept", "application/json");
115                         connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
116                         OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
117                         writer.write(payload);
118                         writer.close();
119                         BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
120                         while ((line = br.readLine()) != null) {
121                                 jsonString.append(line);
122                         }
123                         br.close();
124                         connection.disconnect();
125                 } catch (Exception e) {
126                         logger.error(EELFLoggerDelegate.errorLogger, "POST_fromURL failed", e);
127                         throw new RuntimeException(e.getMessage());
128                 }
129                 return jsonString.toString();
130         }
131 }