ea04e69ee6171a635df3d6486dcdb4665a609855
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 package org.openecomp.portalsdk.workflow.services;
21
22 import java.io.BufferedReader;
23 import java.io.InputStreamReader;
24 import java.io.OutputStreamWriter;
25 import java.net.HttpURLConnection;
26 import java.net.URL;
27 import java.net.URLConnection;
28 import java.nio.charset.Charset;
29
30 public class WorkflowScheduleExecutor {
31         private String serverURL;
32         private String workflowKey;
33         private String myUrl;
34         private String payload;
35
36         //constructor
37         public WorkflowScheduleExecutor(String serverURL,String workflowKey){
38                 this.serverURL = serverURL;
39                 this.workflowKey = workflowKey;
40                 this.myUrl = this.serverURL + "/engine-rest/process-definition/key/" + this.workflowKey + "/submit-form";;
41         this.payload="{\"variables\":{}}";
42         }
43         
44         public static void main(String [] args)  throws Exception {             
45
46         }
47         
48         public void execute() {
49                 POST_fromURL(myUrl,payload);
50         }
51         
52         public static String get_fromURL(String myURL) {
53                 System.out.println("Requeted URL:" + myURL);
54                 StringBuilder sb = new StringBuilder();
55                 URLConnection urlConn = null;
56                 InputStreamReader in = null;
57                 try {
58                         URL url = new URL(myURL);
59                         urlConn = url.openConnection();
60                         if (urlConn != null)
61                                 urlConn.setReadTimeout(60 * 1000);
62                         if (urlConn != null && urlConn.getInputStream() != null) {
63                                 in = new InputStreamReader(urlConn.getInputStream(),
64                                                 Charset.defaultCharset());
65                                 BufferedReader bufferedReader = new BufferedReader(in);
66                                 if (bufferedReader != null) {
67                                         int cp;
68                                         while ((cp = bufferedReader.read()) != -1) {
69                                                 sb.append((char) cp);
70                                         }
71                                         bufferedReader.close();
72                                 }
73                         }
74                 in.close();
75                 } catch (Exception e) {
76                         throw new RuntimeException("Exception while calling URL:"+ myURL, e);
77                 } 
78                 return sb.toString();
79         }
80         
81         
82         public static String POST_fromURL(String myURL, String payload) {
83                         String line;
84                     StringBuffer jsonString = new StringBuffer();
85                     try {
86                         URL url = new URL(myURL);
87
88                         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
89                         connection.setDoInput(true);
90                         connection.setDoOutput(true);
91                         connection.setRequestMethod("POST");
92                         connection.setRequestProperty("Accept", "application/json");
93                         connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
94                         OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
95                         writer.write(payload);
96                         writer.close();
97                         BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
98                         while ((line = br.readLine()) != null) {
99                                 jsonString.append(line);
100                         }
101                         br.close();
102                         connection.disconnect();
103                     } catch (Exception e) {
104                             throw new RuntimeException(e.getMessage());
105                     }
106                     return jsonString.toString();
107                 }                                       
108 }