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============================================
38 package org.onap.portalsdk.core.onboarding.crossapi;
40 import java.io.BufferedReader;
41 import java.io.InputStreamReader;
42 import java.net.HttpURLConnection;
44 import java.util.Base64;
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
49 public class SessionCommunicationService {
50 private static String portalApiVersion = "/v3";
52 private static final Log logger = LogFactory.getLog(SessionCommunicationService.class);
55 * Calls the ECOMP Portal to retrieve the session slot check interval.
60 * application user name used for authentication at Portal
62 * application password used for authentication at Portal
64 * application UEB key (basically application ID) used for
65 * authentication at Portal
66 * @return Content read from the remote REST endpoint
68 public static String getSessionSlotCheckInterval(String ecompRestURL, String userName, String password,
71 String url = ecompRestURL + portalApiVersion + "/getSessionSlotCheckInterval";
73 URL obj = new URL(url);
75 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
77 // optional default is GET
78 con.setRequestMethod("GET");
79 con.setConnectTimeout(3000);
80 con.setReadTimeout(8000);
82 con.setRequestProperty("username", userName);
83 con.setRequestProperty("password", password);
84 con.setRequestProperty("uebkey", uebKey);
86 String encoding = Base64.getEncoder().encodeToString((userName + ":" + password).getBytes());
87 con.setRequestProperty("Authorization", "Basic " + encoding);
89 int responseCode = con.getResponseCode();
90 if (logger.isDebugEnabled()) {
91 logger.debug("getSessionSlotCheckInterval: Sending 'GET' request to URL : " + url);
92 logger.debug("getSessionSlotCheckInterval: Response Code : " + responseCode);
95 StringBuilder response = new StringBuilder();
96 try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"))) {
98 while ((inputLine = in.readLine()) != null)
99 response.append(inputLine);
100 } catch (Exception ex) {
101 logger.error("getSessionSlotCheckInterval failed to read stream", ex);
103 return response.toString();
104 } catch (Exception e) {
105 logger.error("getSessionSlotCheckInterval: failed to fetch the session slot check", e);
112 * Calls the ECOMP Portal to request an extension of the current session.
114 * @param ecompRestURL
117 * application user name used for authentication at Portal
119 * application password used for authentication at Portal
121 * application UEB key (basically application ID) used for
122 * authentication at Portal
123 * @param sessionTimeoutMap
124 * Session timeout map
125 * @return Content read from the remote REST endpoint
127 public static String requestPortalSessionTimeoutExtension(String ecompRestURL, String userName, String password,
128 String uebKey, String sessionTimeoutMap) {
131 String url = ecompRestURL + "/extendSessionTimeOuts";
132 URL obj = new URL(url);
134 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
136 con.setRequestMethod("POST");
137 con.setConnectTimeout(3000);
138 con.setReadTimeout(15000);
140 // add request header
141 con.setRequestProperty("username", userName);
142 con.setRequestProperty("password", password);
143 con.setRequestProperty("uebkey", uebKey);
144 con.setRequestProperty("sessionMap", sessionTimeoutMap);
146 String encoding = Base64.getEncoder().encodeToString((userName + ":" + password).getBytes());
147 con.setRequestProperty("Authorization", "Basic " + encoding);
149 con.setDoInput(true);
150 con.setDoOutput(true);
151 con.getOutputStream().write(sessionTimeoutMap.getBytes());
152 con.getOutputStream().flush();
153 con.getOutputStream().close();
155 int responseCode = con.getResponseCode();
156 if (logger.isDebugEnabled()) {
157 logger.debug("requestPortalSessionTimeoutExtension: Sending 'GET' request to URL : " + url);
158 logger.debug("requestPortalSessionTimeoutExtension: Response Code : " + responseCode);
161 StringBuilder response = new StringBuilder();
162 try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
164 while ((inputLine = in.readLine()) != null) {
165 response.append(inputLine);
167 } catch (Exception ex) {
168 logger.error("requestPortalSessionTimeoutExtension failed", ex);
170 return response.toString();
171 } catch (Exception e) {
172 logger.error("requestPortalSessionTimeoutExtension: failed to request Portal to extend time out ", e);