2 * ================================================================================
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
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 * ================================================================================
20 package org.openecomp.portalsdk.workflow.services;
22 import java.io.BufferedReader;
23 import java.io.InputStreamReader;
24 import java.io.OutputStreamWriter;
25 import java.net.HttpURLConnection;
27 import java.net.URLConnection;
28 import java.nio.charset.Charset;
30 public class WorkflowScheduleExecutor {
31 private String serverURL;
32 private String workflowKey;
34 private String payload;
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\":{}}";
44 public static void main(String [] args) throws Exception {
48 public void execute() {
49 POST_fromURL(myUrl,payload);
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;
58 URL url = new URL(myURL);
59 urlConn = url.openConnection();
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) {
68 while ((cp = bufferedReader.read()) != -1) {
71 bufferedReader.close();
75 } catch (Exception e) {
76 throw new RuntimeException("Exception while calling URL:"+ myURL, e);
82 public static String POST_fromURL(String myURL, String payload) {
84 StringBuffer jsonString = new StringBuffer();
86 URL url = new URL(myURL);
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);
97 BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
98 while ((line = br.readLine()) != null) {
99 jsonString.append(line);
102 connection.disconnect();
103 } catch (Exception e) {
104 throw new RuntimeException(e.getMessage());
106 return jsonString.toString();