90b9e96ac6113111aadd881606c8693775fc15b7
[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         //constructor
60         public WorkflowScheduleExecutor(String serverURL,String workflowKey){
61                 this.serverURL = serverURL;
62                 this.workflowKey = workflowKey;
63                 this.myUrl = this.serverURL + "/engine-rest/process-definition/key/" + this.workflowKey + "/submit-form";;
64         this.payload="{\"variables\":{}}";
65         }
66         
67         public static void main(String [] args)  throws Exception {             
68
69         }
70         
71         public void execute() {
72                 POST_fromURL(myUrl,payload);
73         }
74         
75         public static String get_fromURL(String myURL) {
76                 logger.debug(EELFLoggerDelegate.debugLogger, "get_fromURL: Requested URL {}", myURL);
77                 StringBuilder sb = new StringBuilder();
78                 URLConnection urlConn = null;
79                 InputStreamReader in = null;
80                 try {
81                         URL url = new URL(myURL);
82                         urlConn = url.openConnection();
83                         if (urlConn != null)
84                                 urlConn.setReadTimeout(60 * 1000);
85                         if (urlConn != null && urlConn.getInputStream() != null) {
86                                 in = new InputStreamReader(urlConn.getInputStream(),
87                                                 Charset.defaultCharset());
88                                 BufferedReader bufferedReader = new BufferedReader(in);
89                                 int cp;
90                                 while ((cp = bufferedReader.read()) != -1)
91                                         sb.append((char) cp);
92                                 bufferedReader.close();
93                                 in.close();
94                         }
95                 } catch (Exception e) {
96                         logger.error(EELFLoggerDelegate.errorLogger, "get_fromURL failed", e);
97                         throw new RuntimeException("Exception while calling URL:"+ myURL, e);
98                 } 
99                 finally {
100                         try {
101                                 if (in != null)
102                                         in.close();
103                                 } catch (Exception e) {
104                                         logger.error(EELFLoggerDelegate.errorLogger, "get_fromURL close failed", e);
105                                 }
106                 }
107                 return sb.toString();
108         }
109         
110         
111         public static String POST_fromURL(String myURL, String payload) {
112                         String line;
113                     StringBuffer jsonString = new StringBuffer();
114                     try {
115                         URL url = new URL(myURL);
116
117                         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
118                         connection.setDoInput(true);
119                         connection.setDoOutput(true);
120                         connection.setRequestMethod("POST");
121                         connection.setRequestProperty("Accept", "application/json");
122                         connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
123                         OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
124                         writer.write(payload);
125                         writer.close();
126                         BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
127                         while ((line = br.readLine()) != null) {
128                                 jsonString.append(line);
129                         }
130                         br.close();
131                         connection.disconnect();
132                     } catch (Exception e) {
133                         logger.error(EELFLoggerDelegate.errorLogger, "POST_fromURL failed", e);
134                         throw new RuntimeException(e.getMessage());
135                     }
136                     return jsonString.toString();
137                 }                                       
138 }