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;
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
48 public class SessionCommunicationService {
49 private static String portalApiVersion = "/v3";
51 private static final Log logger = LogFactory.getLog(SessionCommunicationService.class);
54 * Calls the ECOMP Portal to retrieve the session slot check interval.
59 * application user name used for authentication at Portal
61 * application password used for authentication at Portal
63 * application UEB key (basically application ID) used for
64 * authentication at Portal
65 * @return Content read from the remote REST endpoint
67 public static String getSessionSlotCheckInterval(String ecompRestURL, String userName, String password,
70 String url = ecompRestURL + portalApiVersion + "/getSessionSlotCheckInterval";
72 URL obj = new URL(url);
74 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
76 // optional default is GET
77 con.setRequestMethod("GET");
78 con.setConnectTimeout(3000);
79 con.setReadTimeout(8000);
81 con.setRequestProperty("username", userName);
82 con.setRequestProperty("password", password);
83 con.setRequestProperty("uebkey", uebKey);
85 int responseCode = con.getResponseCode();
86 if (logger.isDebugEnabled()) {
87 logger.debug("getSessionSlotCheckInterval: Sending 'GET' request to URL : " + url);
88 logger.debug("getSessionSlotCheckInterval: Response Code : " + responseCode);
91 StringBuilder response = new StringBuilder();
92 try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"))) {
94 while ((inputLine = in.readLine()) != null)
95 response.append(inputLine);
96 } catch (Exception ex) {
97 logger.error("getSessionSlotCheckInterval failed to read stream", ex);
99 return response.toString();
100 } catch (Exception e) {
101 logger.error("getSessionSlotCheckInterval: failed to fetch the session slot check", e);
108 * Calls the ECOMP Portal to request an extension of the current session.
110 * @param ecompRestURL
113 * application user name used for authentication at Portal
115 * application password used for authentication at Portal
117 * application UEB key (basically application ID) used for
118 * authentication at Portal
119 * @param sessionTimeoutMap
120 * Session timeout map
121 * @return Content read from the remote REST endpoint
123 public static String requestPortalSessionTimeoutExtension(String ecompRestURL, String userName, String password,
124 String uebKey, String sessionTimeoutMap) {
127 String url = ecompRestURL + "/extendSessionTimeOuts";
128 URL obj = new URL(url);
130 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
132 con.setRequestMethod("POST");
133 con.setConnectTimeout(3000);
134 con.setReadTimeout(15000);
136 // add request header
137 con.setRequestProperty("username", userName);
138 con.setRequestProperty("password", password);
139 con.setRequestProperty("uebkey", uebKey);
140 con.setRequestProperty("sessionMap", sessionTimeoutMap);
141 con.setDoInput(true);
142 con.setDoOutput(true);
143 con.getOutputStream().write(sessionTimeoutMap.getBytes());
144 con.getOutputStream().flush();
145 con.getOutputStream().close();
147 int responseCode = con.getResponseCode();
148 if (logger.isDebugEnabled()) {
149 logger.debug("requestPortalSessionTimeoutExtension: Sending 'GET' request to URL : " + url);
150 logger.debug("requestPortalSessionTimeoutExtension: Response Code : " + responseCode);
153 StringBuilder response = new StringBuilder();
154 try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
156 while ((inputLine = in.readLine()) != null) {
157 response.append(inputLine);
159 } catch (Exception ex) {
160 logger.error("requestPortalSessionTimeoutExtension failed", ex);
162 return response.toString();
163 } catch (Exception e) {
164 logger.error("requestPortalSessionTimeoutExtension: failed to request Portal to extend time out ", e);