be3a7d5482d879edeb2cb1001b872111bf77013c
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalsdk.core.onboarding.crossapi;
21
22 import java.io.BufferedReader;
23 import java.io.InputStreamReader;
24 import java.net.HttpURLConnection;
25 import java.net.URL;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 public class SessionCommunicationService {
31
32         protected static final Log logger = LogFactory.getLog(SessionCommunicationService.class);
33
34         /**
35          * Calls the ECOMP Portal to retrieve the session slot check interval.
36          * 
37          * @param ecompRestURL
38          * @param userName
39          *            application user name used for authentication at Portal
40          * @param password
41          *            application password used for authentication at Portal
42          * @param uebKey
43          *            application UEB key (basically application ID) used for
44          *            authentication at Portal
45          * @return Content read from the remote REST endpoint
46          */
47         public static String getSessionSlotCheckInterval(String ecompRestURL, String userName, String password,
48                         String uebKey) {
49                 try {
50                         String url = ecompRestURL + "/getSessionSlotCheckInterval";
51
52                         URL obj = new URL(url);
53
54                         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
55
56                         // optional default is GET
57                         con.setRequestMethod("GET");
58                         con.setConnectTimeout(3000);
59                         con.setReadTimeout(8000);
60                         // add request header
61                         con.setRequestProperty("username", userName);
62                         con.setRequestProperty("password", password);
63                         con.setRequestProperty("uebkey", uebKey);
64
65                         int responseCode = con.getResponseCode();
66                         if (logger.isDebugEnabled()) {
67                                 logger.debug("getSessionSlotCheckInterval: Sending 'GET' request to URL : " + url);
68                                 logger.debug("getSessionSlotCheckInterval: Response Code : " + responseCode);
69                         }
70
71                         StringBuffer response = new StringBuffer();
72
73                         BufferedReader in = null;
74                         try {
75                                 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
76                                 String inputLine;
77                                 while ((inputLine = in.readLine()) != null)
78                                         response.append(inputLine);
79                         } finally {
80                                 in.close();
81                         }
82                         return response.toString();
83                 } catch (Exception e) {
84                         logger.error("getSessionSlotCheckInterval: failed to fetch the session slot check", e);
85                         return null;
86                 }
87
88         }
89
90         /**
91          * Calls the ECOMP Portal to request an extension of the current session.
92          * 
93          * @param ecompRestURL
94          * @param userName
95          *            application user name used for authentication at Portal
96          * @param password
97          *            application password used for authentication at Portal
98          * @param uebKey
99          *            application UEB key (basically application ID) used for
100          *            authentication at Portal
101          * @param sessionTimeoutMap
102          * @return Content read from the remote REST endpoint
103          * @throws Exception
104          */
105         public static String requestPortalSessionTimeoutExtension(String ecompRestURL, String userName, String password,
106                         String uebKey, String sessionTimeoutMap) throws Exception {
107
108                 try {
109
110                         String url = ecompRestURL + "/extendSessionTimeOuts";
111                         // String decreptedPwd =
112                         // app.appPassword;//CipherUtil.decrypt(encriptedPwdDB,
113                         // SystemProperties.getProperty(SystemProperties.SECRET_KEY));
114
115                         URL obj = new URL(url);
116
117                         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
118
119                         con.setRequestMethod("POST");
120                         con.setConnectTimeout(3000);
121                         con.setReadTimeout(15000);
122
123                         // add request header
124                         con.setRequestProperty("username", userName);
125                         con.setRequestProperty("password", password);
126                         con.setRequestProperty("uebkey", uebKey);
127                         con.setRequestProperty("sessionMap", sessionTimeoutMap);
128                         con.setDoInput(true);
129                         con.setDoOutput(true);
130                         con.getOutputStream().write(sessionTimeoutMap.getBytes());
131                         con.getOutputStream().flush();
132                         con.getOutputStream().close();
133
134                         // con.set
135
136                         int responseCode = con.getResponseCode();
137                         if (logger.isDebugEnabled()) {
138                                 logger.debug("requestPortalSessionTimeoutExtension: Sending 'GET' request to URL : " + url);
139                                 logger.debug("requestPortalSessionTimeoutExtension: Response Code : " + responseCode);
140                         }
141
142                         StringBuffer response = new StringBuffer();
143                         BufferedReader in = null;
144                         try {
145                                 in = new BufferedReader(new InputStreamReader(con.getInputStream()));
146                                 String inputLine;
147                                 while ((inputLine = in.readLine()) != null) {
148                                         response.append(inputLine);
149                                 }
150                         } finally {
151                                 in.close();
152                         }
153                         return response.toString();
154                 } catch (Exception e) {
155                         logger.error("requestPortalSessionTimeoutExtension: failed to request Portal to extend time out ", e);
156                         return null;
157                 }
158
159         }
160
161 }