1f9bfd07fd8ffdeb1da77fa71ae0f06a249aca09
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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         protected 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                         StringBuffer response = new StringBuffer();                     
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                         }
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
128                         String url = ecompRestURL + "/extendSessionTimeOuts";
129                         URL obj = new URL(url);
130
131                         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
132
133                         con.setRequestMethod("POST");
134                         con.setConnectTimeout(3000);
135                         con.setReadTimeout(15000);
136
137                         // add request header
138                         con.setRequestProperty("username", userName);
139                         con.setRequestProperty("password", password);
140                         con.setRequestProperty("uebkey", uebKey);
141                         con.setRequestProperty("sessionMap", sessionTimeoutMap);
142                         con.setDoInput(true);
143                         con.setDoOutput(true);
144                         con.getOutputStream().write(sessionTimeoutMap.getBytes());
145                         con.getOutputStream().flush();
146                         con.getOutputStream().close();
147
148                         // con.set
149
150                         int responseCode = con.getResponseCode();
151                         if (logger.isDebugEnabled()) {
152                                 logger.debug("requestPortalSessionTimeoutExtension: Sending 'GET' request to URL : " + url);
153                                 logger.debug("requestPortalSessionTimeoutExtension: Response Code : " + responseCode);
154                         }
155
156                         StringBuffer response = new StringBuffer();
157                         try (BufferedReader       in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
158                                 String inputLine;
159                                 while ((inputLine = in.readLine()) != null) {
160                                         response.append(inputLine);
161                                 }
162                         } catch (Exception ex) {
163                                 logger.error("requestPortalSessionTimeoutExtension failed", ex);
164                         }
165                         return response.toString();
166                 } catch (Exception e) {
167                         logger.error("requestPortalSessionTimeoutExtension: failed to request Portal to extend time out ", e);
168                         return null;
169                 }
170
171         }
172
173 }