e016db0769e8a66a2b4ad1f9966a45430db66a14
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalsdk.core.onboarding.crossapi;
39
40 import java.io.BufferedReader;
41 import java.io.InputStreamReader;
42 import java.net.HttpURLConnection;
43 import java.net.URL;
44
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47
48 public class SessionCommunicationService {
49         private static String portalApiVersion = "/v3";
50
51         private static final Log logger = LogFactory.getLog(SessionCommunicationService.class);
52
53         /**
54          * Calls the ECOMP Portal to retrieve the session slot check interval.
55          * 
56          * @param ecompRestURL
57          *            Remote system URL
58          * @param userName
59          *            application user name used for authentication at Portal
60          * @param password
61          *            application password used for authentication at Portal
62          * @param uebKey
63          *            application UEB key (basically application ID) used for
64          *            authentication at Portal
65          * @return Content read from the remote REST endpoint
66          */
67         public static String getSessionSlotCheckInterval(String ecompRestURL, String userName, String password,
68                         String uebKey) {
69                 try {
70                         String url = ecompRestURL + portalApiVersion + "/getSessionSlotCheckInterval";
71
72                         URL obj = new URL(url);
73
74                         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
75
76                         // optional default is GET
77                         con.setRequestMethod("GET");
78                         con.setConnectTimeout(3000);
79                         con.setReadTimeout(8000);
80                         // add request header
81                         con.setRequestProperty("username", userName);
82                         con.setRequestProperty("password", password);
83                         con.setRequestProperty("uebkey", uebKey);
84
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);
89                         }
90
91                         StringBuilder response = new StringBuilder();
92                         try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"))) {
93                                 String inputLine;
94                                 while ((inputLine = in.readLine()) != null)
95                                         response.append(inputLine);
96                         } catch (Exception ex) {
97                                 logger.error("getSessionSlotCheckInterval failed to read stream", ex);
98                         }
99                         return response.toString();
100                 } catch (Exception e) {
101                         logger.error("getSessionSlotCheckInterval: failed to fetch the session slot check", e);
102                         return null;
103                 }
104
105         }
106
107         /**
108          * Calls the ECOMP Portal to request an extension of the current session.
109          * 
110          * @param ecompRestURL
111          *            Remote system URL
112          * @param userName
113          *            application user name used for authentication at Portal
114          * @param password
115          *            application password used for authentication at Portal
116          * @param uebKey
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
122          */
123         public static String requestPortalSessionTimeoutExtension(String ecompRestURL, String userName, String password,
124                         String uebKey, String sessionTimeoutMap) {
125
126                 try {
127                         String url = ecompRestURL + "/extendSessionTimeOuts";
128                         URL obj = new URL(url);
129
130                         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
131
132                         con.setRequestMethod("POST");
133                         con.setConnectTimeout(3000);
134                         con.setReadTimeout(15000);
135
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();
146
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);
151                         }
152
153                         StringBuilder response = new StringBuilder();
154                         try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
155                                 String inputLine;
156                                 while ((inputLine = in.readLine()) != null) {
157                                         response.append(inputLine);
158                                 }
159                         } catch (Exception ex) {
160                                 logger.error("requestPortalSessionTimeoutExtension failed", ex);
161                         }
162                         return response.toString();
163                 } catch (Exception e) {
164                         logger.error("requestPortalSessionTimeoutExtension: failed to request Portal to extend time out ", e);
165                         return null;
166                 }
167
168         }
169
170 }