2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.workflow.services;
40 import java.io.BufferedReader;
41 import java.io.InputStreamReader;
42 import java.io.OutputStreamWriter;
43 import java.net.HttpURLConnection;
45 import java.net.URLConnection;
46 import java.nio.charset.Charset;
48 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
50 public class WorkflowScheduleExecutor {
52 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WorkflowScheduleExecutor.class);
54 private String serverURL;
55 private String workflowKey;
57 private String payload;
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\":{}}";
67 public static void main(String [] args) throws Exception {
71 public void execute() {
72 POST_fromURL(myUrl,payload);
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;
81 URL url = new URL(myURL);
82 urlConn = url.openConnection();
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);
90 while ((cp = bufferedReader.read()) != -1)
92 bufferedReader.close();
95 } catch (Exception e) {
96 logger.error(EELFLoggerDelegate.errorLogger, "get_fromURL failed", e);
97 throw new RuntimeException("Exception while calling URL:"+ myURL, e);
103 } catch (Exception e) {
104 logger.error(EELFLoggerDelegate.errorLogger, "get_fromURL close failed", e);
107 return sb.toString();
111 public static String POST_fromURL(String myURL, String payload) {
113 StringBuffer jsonString = new StringBuffer();
115 URL url = new URL(myURL);
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);
126 BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
127 while ((line = br.readLine()) != null) {
128 jsonString.append(line);
131 connection.disconnect();
132 } catch (Exception e) {
133 logger.error(EELFLoggerDelegate.errorLogger, "POST_fromURL failed", e);
134 throw new RuntimeException(e.getMessage());
136 return jsonString.toString();