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