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;
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";
64 this.payload = "{\"variables\":{}}";
67 public void execute() {
68 POST_fromURL(myUrl, payload);
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;
77 URL url = new URL(myURL);
78 urlConn = url.openConnection();
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);
85 while ((cp = bufferedReader.read()) != -1)
87 bufferedReader.close();
90 } catch (Exception e) {
91 logger.error(EELFLoggerDelegate.errorLogger, "get_fromURL failed", e);
92 throw new RuntimeException("Exception while calling URL:" + myURL, e);
97 } catch (Exception e) {
98 logger.error(EELFLoggerDelegate.errorLogger, "get_fromURL close failed", e);
101 return sb.toString();
104 public static String POST_fromURL(String myURL, String payload) {
106 StringBuilder jsonString = new StringBuilder();
108 URL url = new URL(myURL);
109 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
110 connection.setDoInput(true);
111 connection.setDoOutput(true);
112 connection.setRequestMethod("POST");
113 connection.setRequestProperty("Accept", "application/json");
114 connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
115 OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
116 writer.write(payload);
118 BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
119 while ((line = br.readLine()) != null) {
120 jsonString.append(line);
123 connection.disconnect();
124 } catch (Exception e) {
125 logger.error(EELFLoggerDelegate.errorLogger, "POST_fromURL failed", e);
126 throw new RuntimeException(e.getMessage());
128 return jsonString.toString();