847707bc11ff2a4a41bc46a97376139292c4aff9
[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 import java.util.Base64;
45
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48
49 public class SessionCommunicationService {
50         private static String portalApiVersion = "/v3";
51
52         private static final Log logger = LogFactory.getLog(SessionCommunicationService.class);
53
54         /**
55          * Calls the ECOMP Portal to retrieve the session slot check interval.
56          * 
57          * @param ecompRestURL
58          *            Remote system URL
59          * @param userName
60          *            application user name used for authentication at Portal
61          * @param password
62          *            application password used for authentication at Portal
63          * @param uebKey
64          *            application UEB key (basically application ID) used for
65          *            authentication at Portal
66          * @return Content read from the remote REST endpoint
67          */
68         public static String getSessionSlotCheckInterval(String ecompRestURL, String userName, String password,
69                         String uebKey) {
70                 try {
71                         String url = ecompRestURL + portalApiVersion + "/getSessionSlotCheckInterval";
72
73                         URL obj = new URL(url);
74
75                         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
76
77                         // optional default is GET
78                         con.setRequestMethod("GET");
79                         con.setConnectTimeout(3000);
80                         con.setReadTimeout(8000);
81                         // add request header
82                         con.setRequestProperty("username", userName);
83                         con.setRequestProperty("password", password);
84                         con.setRequestProperty("uebkey", uebKey);
85                         
86                         String encoding = Base64.getEncoder().encodeToString((userName + ":" + password).getBytes());
87                         con.setRequestProperty("Authorization", "Basic " + encoding);
88
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);
93                         }
94
95                         StringBuilder response = new StringBuilder();
96                         try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"))) {
97                                 String inputLine;
98                                 while ((inputLine = in.readLine()) != null)
99                                         response.append(inputLine);
100                         } catch (Exception ex) {
101                                 logger.error("getSessionSlotCheckInterval failed to read stream", ex);
102                         }
103                         return response.toString();
104                 } catch (Exception e) {
105                         logger.error("getSessionSlotCheckInterval: failed to fetch the session slot check", e);
106                         return null;
107                 }
108
109         }
110
111         /**
112          * Calls the ECOMP Portal to request an extension of the current session.
113          * 
114          * @param ecompRestURL
115          *            Remote system URL
116          * @param userName
117          *            application user name used for authentication at Portal
118          * @param password
119          *            application password used for authentication at Portal
120          * @param uebKey
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
126          */
127         public static String requestPortalSessionTimeoutExtension(String ecompRestURL, String userName, String password,
128                         String uebKey, String sessionTimeoutMap) {
129
130                 try {
131                         String url = ecompRestURL + "/extendSessionTimeOuts";
132                         URL obj = new URL(url);
133
134                         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
135
136                         con.setRequestMethod("POST");
137                         con.setConnectTimeout(3000);
138                         con.setReadTimeout(15000);
139
140                         // add request header
141                         con.setRequestProperty("username", userName);
142                         con.setRequestProperty("password", password);
143                         con.setRequestProperty("uebkey", uebKey);
144                         con.setRequestProperty("sessionMap", sessionTimeoutMap);
145                         
146                         String encoding = Base64.getEncoder().encodeToString((userName + ":" + password).getBytes());
147                         con.setRequestProperty("Authorization", "Basic " + encoding);
148
149                         con.setDoInput(true);
150                         con.setDoOutput(true);
151                         con.getOutputStream().write(sessionTimeoutMap.getBytes());
152                         con.getOutputStream().flush();
153                         con.getOutputStream().close();
154
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);
159                         }
160
161                         StringBuilder response = new StringBuilder();
162                         try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
163                                 String inputLine;
164                                 while ((inputLine = in.readLine()) != null) {
165                                         response.append(inputLine);
166                                 }
167                         } catch (Exception ex) {
168                                 logger.error("requestPortalSessionTimeoutExtension failed", ex);
169                         }
170                         return response.toString();
171                 } catch (Exception e) {
172                         logger.error("requestPortalSessionTimeoutExtension: failed to request Portal to extend time out ", e);
173                         return null;
174                 }
175
176         }
177
178 }