1ce031460e06b8faab6fce203b71a6a683bc8669
[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.IOException;
42 import java.io.InputStream;
43 import java.io.InputStreamReader;
44 import java.io.PrintWriter;
45 import java.io.StringWriter;
46 import java.util.List;
47 import java.util.Map;
48
49 import javax.servlet.ServletException;
50 import javax.servlet.annotation.WebServlet;
51 import javax.servlet.http.HttpServlet;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54
55 import org.apache.commons.logging.Log;
56 import org.apache.commons.logging.LogFactory;
57 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
58 import org.onap.portalsdk.core.onboarding.listener.PortalTimeoutHandler;
59 import org.onap.portalsdk.core.onboarding.rest.RestWebServiceClient;
60 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
61 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
62 import org.onap.portalsdk.core.restful.domain.EcompRole;
63 import org.onap.portalsdk.core.restful.domain.EcompUser;
64 import org.owasp.esapi.ESAPI;
65
66 import com.fasterxml.jackson.core.JsonProcessingException;
67 import com.fasterxml.jackson.core.type.TypeReference;
68 import com.fasterxml.jackson.databind.DeserializationFeature;
69 import com.fasterxml.jackson.databind.ObjectMapper;
70
71 /**
72  * This servlet performs the functions described below. It listens on a path
73  * like "/api" (see {@link PortalApiConstants#API_PREFIX}). The servlet checks
74  * for authorized access and rejects unauthorized requests.
75  * <OL>
76  * <LI>Proxies user (i.e., browser) requests for web analytics. The GET method
77  * fetches javascript from the Portal and returns it. The POST method forwards
78  * data sent by the browser on to Portal. These requests are checked for a valid
79  * User UID in a header; these requests do NOT use the application
80  * username-password header.</LI>
81  * <LI>Responds to ECOMP Portal API requests to query and update user, role and
82  * user-role information. The servlet proxies all requests on to a local Java
83  * class that implements {@link IPortalRestAPIService}. These requests must have
84  * the application username-password header.</LI>
85  * </OL>
86  * This servlet will not start if the required portal.properties file is not
87  * found on the classpath.
88  */
89
90 @WebServlet(urlPatterns = { PortalApiConstants.API_PREFIX + "/*" })
91 public class PortalRestAPIProxy extends HttpServlet implements IPortalRestAPIService {
92         
93         private static final long serialVersionUID = 1L;
94
95         private static final String APPLICATION_JSON = "application/json";
96
97         private static final Log logger = LogFactory.getLog(PortalRestAPIProxy.class);
98
99         /**
100          * Mapper for JSON to object etc.
101          */
102         private final ObjectMapper mapper = new ObjectMapper();
103
104         /**
105          * Client-supplied class that implements our interface.
106          */
107         private static IPortalRestAPIService portalRestApiServiceImpl;
108         private static final String isAccessCentralized = PortalApiProperties
109                         .getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED);
110         private static final String errorMessage = "Access Management is not allowed for Centralized applications." ;
111         private static final String isCentralized = "remote";
112
113
114         public PortalRestAPIProxy() {
115                 // Ensure that any additional fields sent by the Portal
116                 // will be ignored when creating objects.
117                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
118         }
119
120         @Override
121         public void init() throws ServletException {
122                 String className = PortalApiProperties.getProperty(PortalApiConstants.PORTAL_API_IMPL_CLASS);
123                 if (className == null)
124                         throw new ServletException(
125                                         "init: Failed to find class name property " + PortalApiConstants.PORTAL_API_IMPL_CLASS);
126                 try {
127                         logger.debug("init: creating instance of class " + className);
128                         Class<?> implClass = Class.forName(className);
129                         if (!isCentralized.equals(isAccessCentralized))
130                                 portalRestApiServiceImpl = (IPortalRestAPIService) (implClass.getConstructor().newInstance());
131                         else {
132                                 portalRestApiServiceImpl = new PortalRestAPICentralServiceImpl();                               
133                         }
134                 } catch (Exception ex) {
135                         throw new ServletException("init: Failed to find or instantiate class " + className, ex);
136                 }
137         }
138
139         @Override
140         protected void doPost(HttpServletRequest request, HttpServletResponse response)
141                         throws IOException, ServletException {
142                 if (portalRestApiServiceImpl == null) {
143                         // Should never happen due to checks in init()
144                         logger.error("doPost: no service class instance");
145                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
146                         response.getWriter().write(buildJsonResponse(false, "Misconfigured - no instance of service class"));
147                         return;
148                 }
149                 String requestUri = request.getRequestURI();
150                 String responseJson = "";
151                 String storeAnalyticsContextPath = "/storeAnalytics";
152                 if (requestUri.endsWith(PortalApiConstants.API_PREFIX + storeAnalyticsContextPath)) {
153                         String userId;
154                         try {
155                                 userId = getUserId(request);
156                         } catch (PortalAPIException e) {
157                                 logger.error("Issue with invoking getUserId implemenation !!! ", e);
158                                 throw new ServletException(e);
159                         }
160                         if (userId == null || userId.length() == 0) {
161                                 logger.debug("doPost: userId is null or empty");
162                                 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
163                                 responseJson = buildJsonResponse(false, "Not authorized for " + storeAnalyticsContextPath);
164                         } else {
165                                 // User ID obtained from request
166                                 try {
167                                         String appUserName = "";
168                                         String appPassword = "";
169                                         String appName = "";
170
171                                         for (Map.Entry<String, String> entry : getCredentials().entrySet()) {
172
173                                                 if (entry.getKey().equalsIgnoreCase("username")) {
174                                                         appUserName = entry.getValue();
175                                                 } else if (entry.getKey().equalsIgnoreCase("password")) {
176                                                         appPassword = entry.getValue();
177                                                 } else {
178                                                         appName = entry.getValue();
179                                                 }
180                                         }
181                                         
182                                         String credential = PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY);
183                                         // for now lets also pass uebkey as user name and password
184                                         String requestBody = readRequestBody(request);
185                                         @SuppressWarnings("unchecked")
186                                         Map<String, String> bodyMap = mapper.readValue(requestBody, Map.class);
187                                         // add user ID
188                                         bodyMap.put("userid", userId);
189                                         requestBody = mapper.writeValueAsString(bodyMap);
190                                         logger.debug("doPost:  StoreAnalytics requestbody: "+ requestBody);
191                                         responseJson = RestWebServiceClient.getInstance().postPortalContent(storeAnalyticsContextPath,
192                                                         userId, appName, null, appUserName, appPassword, "application/json", requestBody, true);
193                                         logger.debug("doPost: postPortalContent returns " + responseJson);
194                                         response.setStatus(HttpServletResponse.SC_OK);
195                                 } catch (Exception ex) {
196                                         logger.error("doPost: " + storeAnalyticsContextPath + " caught exception", ex);
197                                         responseJson = buildJsonResponse(ex);
198                                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
199                                 }
200                         }
201                         writeAndFlush(response, APPLICATION_JSON, responseJson);
202                         return;
203                 } // post analytics
204
205                 boolean secure = false;
206                 try {
207                         secure = isAppAuthenticated(request);
208                 } catch (PortalAPIException ex) {
209                         logger.error("doPost: isAppAuthenticated threw exception", ex);
210                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
211                         response.getWriter().write(buildJsonResponse(false, "Failed to authenticate request"));
212                         return;
213                 }
214                 if (!secure) {
215                         logger.debug("doPost: isAppAuthenticated answered false");
216                         response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
217                         writeAndFlush(response, APPLICATION_JSON, buildJsonResponse(false, "Not authorized"));
218                         return;
219                 }
220
221                 try {
222                         String requestBody = readRequestBody(request);
223                         if (logger.isDebugEnabled())
224                                 logger.debug("doPost: URI =  " + requestUri + ", payload = " + requestBody);
225
226                         /*
227                          * All APIs:
228                          * 
229                          * 1. /user <-- save user
230                          * 
231                          * 2. /user/{loginId} <-- edit user
232                          * 
233                          * 3. /user/{loginId}/roles <-- save roles for user
234                          */
235
236                         // On success return the empty string.
237
238                         if (requestUri.endsWith("/updateSessionTimeOuts")) {
239                                 if (updateSessionTimeOuts(requestBody)) {
240                                         logger.debug("doPost: updated session timeouts");
241                                         response.setStatus(HttpServletResponse.SC_OK);
242                                 } else {
243                                         String msg = "Failed to update session time outs";
244                                         logger.error("doPost: " + msg);
245                                         responseJson = buildJsonResponse(false, msg);
246                                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
247                                 }
248                         } else if (requestUri.endsWith("/timeoutSession")) {
249                                 String portalJSessionId = request.getParameter("portalJSessionId");
250                                 if (portalJSessionId == null) {
251                                         portalJSessionId = "";
252                                 }
253                                 if (timeoutSession(portalJSessionId)) {
254                                         logger.debug("doPost: timed out session");
255                                         response.setStatus(HttpServletResponse.SC_OK);
256                                 } else {
257                                         String msg = "Failed to timeout session";
258                                         logger.error("doPost: " + msg);
259                                         responseJson = buildJsonResponse(false, msg);
260                                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
261                                 }
262                         } else
263                         // Example: /user <-- create user
264                         if (requestUri.endsWith(PortalApiConstants.API_PREFIX + "/user")) {
265                                 try {
266                                         EcompUser user = mapper.readValue(requestBody, EcompUser.class);
267                                         pushUser(user);
268                                         if (logger.isDebugEnabled())
269                                                 logger.debug("doPost: pushUser: success");
270                                         responseJson = buildJsonResponse(true, "user saved successfully");
271                                         response.setStatus(HttpServletResponse.SC_OK);
272                                 } catch (Exception ex) {
273                                         responseJson = buildJsonResponse(ex);
274                                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
275                                         logger.error("doPost: pushUser: caught exception", ex);
276                                 }
277                         } else
278                         // Example: /user/abc <-- edit user abc 
279                         if (requestUri.contains(PortalApiConstants.API_PREFIX + "/user/") && !(requestUri.endsWith("/roles"))) {
280                                 String loginId = requestUri.substring(requestUri.lastIndexOf('/') + 1);
281                                 try {
282                                         EcompUser user = mapper.readValue(requestBody, EcompUser.class);
283                                         editUser(loginId, user);
284                                         if (logger.isDebugEnabled())
285                                                 logger.debug("doPost: editUser: success");
286                                         responseJson = buildJsonResponse(true, "user saved successfully");
287                                         response.setStatus(HttpServletResponse.SC_OK);
288                                 } catch (Exception ex) {
289                                         responseJson = buildJsonResponse(ex);
290                                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
291                                         logger.error("doPost: editUser: caught exception", ex);
292                                 }
293                         } else
294                         // Example: /user/{loginId}/roles <-- save roles for user
295                         if (requestUri.contains(PortalApiConstants.API_PREFIX + "/user/") && requestUri.endsWith("/roles")) {
296                                 String loginId = requestUri.substring(requestUri.indexOf("/user/") + ("/user").length() + 1,
297                                                 requestUri.lastIndexOf('/'));
298                                 try {
299                                         if (isCentralized.equals(isAccessCentralized)) {
300                                                 responseJson = buildJsonResponse(true, errorMessage);
301                                                 response.setStatus(HttpServletResponse.SC_OK);
302                                         } else {
303                                                 TypeReference<List<EcompRole>> typeRef = new TypeReference<List<EcompRole>>() {
304                                                 };
305                                                 List<EcompRole> roles = mapper.readValue(requestBody, typeRef);
306                                                 pushUserRole(loginId, roles);
307                                                 if (logger.isDebugEnabled())
308                                                         logger.debug("doPost: pushUserRole: success");
309                                                 responseJson = buildJsonResponse(true, "saveRoles is successful");
310                                                 response.setStatus(HttpServletResponse.SC_OK);
311                                         }
312                                 } catch (Exception ex) {
313                                         responseJson = buildJsonResponse(ex);
314                                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
315                                         logger.error("doPost: pushUserRole: caught exception", ex);
316                                 }
317                         } else {
318                                 String msg = "doPost: no match for request " + requestUri;
319                                 logger.warn( ESAPI.encoder().encodeForHTML(msg));
320                                 responseJson = buildJsonResponse(false, msg);
321                                 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
322                         }
323                 } catch (Exception ex) {
324                         logger.error("doPost: Failed to process request " + ESAPI.encoder().encodeForHTML(requestUri), ex);
325                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
326                         responseJson = buildJsonResponse(ex);
327                 }
328
329                 writeAndFlush(response, APPLICATION_JSON, responseJson);
330
331         }
332
333         @Override
334         protected void doGet(HttpServletRequest request, HttpServletResponse response)
335                         throws IOException, ServletException {
336
337                 if (portalRestApiServiceImpl == null) {
338                         // Should never happen due to checks in init()
339                         logger.error("doGet: no service class instance");
340                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
341                         writeAndFlush(response, APPLICATION_JSON,
342                                         buildJsonResponse(false, "Misconfigured - no instance of service class"));
343                         return;
344                 }
345
346                 String requestUri = request.getRequestURI();
347                 String contentType = APPLICATION_JSON;
348                 String webAnalyticsContextPath = "/analytics";
349                 if (requestUri.endsWith(PortalApiConstants.API_PREFIX + webAnalyticsContextPath)) {
350                         String responseString;
351                         String userId;
352                         try {
353                                 userId = getUserId(request);
354                         } catch (PortalAPIException e) {
355                                 logger.error("Issue with invoking getUserId implemenation !!! ", e);
356                                 throw new ServletException(e);
357                         }
358                         if (userId == null || userId.length() == 0) {
359                                 logger.debug("doGet: userId is null or empty");
360                                 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
361                                 responseString = buildJsonResponse(false, "Not authorized for " + webAnalyticsContextPath);
362                         } else {
363                                 // User ID obtained from request
364                                 try {
365                                         String appUserName = "";
366                                         String appPassword = "";
367                                         String appName = "";
368
369                                         for (Map.Entry<String, String> entry : getCredentials().entrySet()) {
370
371                                                 if (entry.getKey().equalsIgnoreCase("username")) {
372                                                         appUserName = entry.getValue();
373                                                 } else if (entry.getKey().equalsIgnoreCase("password")) {
374                                                         appPassword = entry.getValue();
375                                                 } else {
376                                                         appName = entry.getValue();
377                                                 }
378                                         }
379                                         String credential = PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY);
380                                         // for now lets also pass uebkey as user name and password
381                                         contentType = "text/javascript";
382
383                                         responseString = RestWebServiceClient.getInstance().getPortalContent(webAnalyticsContextPath,
384                                                         userId, appName, null, appUserName, appPassword, true);
385                                         
386                                         if (logger.isDebugEnabled())
387                                                 logger.debug("doGet: " + webAnalyticsContextPath + ": " + responseString);
388                                         response.setStatus(HttpServletResponse.SC_OK);
389                                 } catch (Exception ex) {
390                                         responseString = buildJsonResponse(ex);
391                                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
392                                         logger.error("doGet: " + webAnalyticsContextPath + " caught exception", ex);
393                                 }
394                         }
395                         writeAndFlush(response, contentType, responseString);
396                         return;
397                 }
398
399                 boolean secure = false;
400                 try {
401                         secure = isAppAuthenticated(request);
402                 } catch (PortalAPIException ex) {
403                         logger.error("doGet: isAppAuthenticated threw exception", ex);
404                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
405                         writeAndFlush(response, APPLICATION_JSON, buildJsonResponse(false, "Failed to authenticate request"));
406                         return;
407                 }
408
409                 if (!secure) {
410                         if (logger.isDebugEnabled())
411                                 logger.debug("doGet: isAppAuthenticated answered false");
412                         response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
413                         writeAndFlush(response, APPLICATION_JSON, buildJsonResponse(false, "Not authorized"));
414                         return;
415                 }
416
417                 String responseJson = null;
418                 try {
419                         // Ignore any request body in a GET.
420                         logger.debug("doGet: URI =  " + requestUri);
421
422                         /*
423                          * 1. /roles <-- get roles
424                          * 
425                          * 2. /user/{loginId} <-- get user
426                          * 
427                          * 3. /users <-- get all users
428                          * 
429                          * 4. /user/{loginId}/roles <-- get roles for user
430                          */
431
432                         if (requestUri.endsWith("/sessionTimeOuts")) {
433                                 try  {
434                                         responseJson = getSessionTimeOuts();
435                                         logger.debug("doGet: got session timeouts");
436                                         response.setStatus(HttpServletResponse.SC_OK);
437                                 } catch(Exception ex) {
438                                         String msg = "Failed to get session time outs";
439                                         logger.error("doGet: " + msg);
440                                         responseJson = buildJsonResponse(false, msg);
441                                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
442                                 }
443                         } else
444                         // Example: /users <-- get all users
445                         if (requestUri.endsWith(PortalApiConstants.API_PREFIX + "/users")) {
446                                 try {
447                                         List<EcompUser> users = getUsers();
448                                         responseJson = mapper.writeValueAsString(users);
449                                         if (logger.isDebugEnabled())
450                                                 logger.debug("doGet: getUsers: " + responseJson);
451                                 } catch (Exception ex) {
452                                         responseJson = buildShortJsonResponse(ex);
453                                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
454                                         logger.error("doGet: getUsers: caught exception", ex);
455                                 }
456                         } else
457                         // Example: /roles <-- get all roles
458
459                         if (requestUri.endsWith(PortalApiConstants.API_PREFIX + "/roles")) {
460                                 try {
461                                         List<EcompRole> roles = getAvailableRoles(getUserId(request));
462                                         responseJson = mapper.writeValueAsString(roles);
463                                         if (logger.isDebugEnabled())
464                                                 logger.debug("doGet: getAvailableRoles: " + responseJson);
465                                 } catch (Exception ex) {
466                                         responseJson = buildJsonResponse(ex);
467                                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
468                                         logger.error("doGet: getAvailableRoles: caught exception", ex);
469                                 }
470                         } else
471                         // Example: /user/abc <-- get user abc
472                         if (requestUri.contains(PortalApiConstants.API_PREFIX + "/user/") && !requestUri.endsWith("/roles")) {
473                                 String loginId = requestUri.substring(requestUri.lastIndexOf('/') + 1);
474                                 try {
475                                         EcompUser user = getUser(loginId);
476                                         responseJson = mapper.writeValueAsString(user);
477                                         if (logger.isDebugEnabled())
478                                                 logger.debug("doGet: getUser: " + responseJson);
479                                 } catch (Exception ex) {
480                                         responseJson = buildJsonResponse(ex);
481                                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
482                                         logger.error("doGet: getUser: caught exception", ex);
483                                 }
484                         }
485                         // Example: /user/abc/roles <-- get roles for user abc
486                         else if (requestUri.contains(PortalApiConstants.API_PREFIX + "/user/") && requestUri.endsWith("/roles")) {
487                                 String loginId = requestUri.substring(requestUri.indexOf("/user/") + ("/user").length() + 1,
488                                                 requestUri.lastIndexOf('/'));
489                                 try {
490                                         List<EcompRole> roles = getUserRoles(loginId);
491                                         responseJson = mapper.writeValueAsString(roles);
492                                         if (logger.isDebugEnabled())
493                                                 logger.debug("doGet: getUserRoles: " + responseJson);
494                                 } catch (Exception ex) {
495                                         responseJson = buildJsonResponse(ex);
496                                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
497                                         logger.error("doGet: getUserRoles: caught exception", ex);
498                                 }
499                         }
500                         else {
501                                 logger.warn("doGet: no match found for request");
502                                 responseJson = buildJsonResponse(false, "No match for request");
503                                 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
504                         }
505                 } catch (Exception ex) {
506                         logger.error("doGet: Failed to process request", ex);
507                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
508                         responseJson = buildJsonResponse(ex);
509                 }
510                 writeAndFlush(response, APPLICATION_JSON, responseJson);
511         }
512
513         public String getSessionTimeOuts() {
514                 return PortalTimeoutHandler.gatherSessionExtensions();
515         }
516
517         public boolean timeoutSession(String portalJSessionId) {
518                 return PortalTimeoutHandler.invalidateSession(portalJSessionId);
519         }
520
521         public boolean updateSessionTimeOuts(String sessionMap) {
522                 return PortalTimeoutHandler.updateSessionExtensions(sessionMap);
523         }
524
525         @Override
526         public void pushUser(EcompUser user) throws PortalAPIException {
527                 portalRestApiServiceImpl.pushUser(user);
528         }
529
530         @Override
531         public void editUser(String loginId, EcompUser user) throws PortalAPIException {
532                 portalRestApiServiceImpl.editUser(loginId, user);
533         }
534
535         @Override
536         public EcompUser getUser(String loginId) throws PortalAPIException {
537                 return portalRestApiServiceImpl.getUser(loginId);
538         }
539
540         @Override
541         public List<EcompUser> getUsers() throws PortalAPIException {
542                 return portalRestApiServiceImpl.getUsers();
543         }
544
545         @Override
546         public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
547                 return portalRestApiServiceImpl.getAvailableRoles(requestedLoginId);
548         }
549
550         @Override
551         public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
552                 portalRestApiServiceImpl.pushUserRole(loginId, roles);
553         }
554
555         @Override
556         public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
557                 return portalRestApiServiceImpl.getUserRoles(loginId);
558         }
559
560         @Override
561         public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
562                 return portalRestApiServiceImpl.isAppAuthenticated(request);
563         }
564
565         /**
566          * Sets the content type and writes the response.
567          * 
568          * @param response
569          * @param contentType
570          * @param responseBody
571          * @throws IOException
572          */
573         private void writeAndFlush(HttpServletResponse response, String contentType, String responseBody)
574                         throws IOException {
575                 response.setContentType(contentType);
576                 PrintWriter out = response.getWriter();
577                 out.print(responseBody);
578                 out.flush();
579         }
580
581         /**
582          * Reads the request body and closes the input stream.
583          * 
584          * @param request
585          * @return String read from the request, the empty string if nothing is read.
586          * @throws IOException
587          */
588         private static String readRequestBody(HttpServletRequest request) throws IOException {
589
590                 String body = null;
591                 StringBuilder stringBuilder = new StringBuilder();
592
593                 try(InputStream inputStream = request.getInputStream()) {
594                         if (inputStream != null) {
595                                 try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));){
596                                         char[] charBuffer = new char[1024];
597                                         int bytesRead = -1;
598                                         while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
599                                                 stringBuilder.append(charBuffer, 0, bytesRead);
600                                         }
601                                 } catch(IOException e) {
602                                         logger.error("readRequestBody", e);
603                                         throw e;
604                                 }
605                         } else {
606                                 stringBuilder.append("");
607                         }
608                 } catch(IOException e) {
609                         logger.error("readRequestBody", e);
610                         throw e;
611                 }
612                 body = stringBuilder.toString();
613                 return body;
614         }
615
616         /**
617          * Builds JSON object with status + message response body.
618          * 
619          * @param success
620          *            True to indicate success, false to signal failure.
621          * @param msg
622          *            Message to include in the response object; ignored if null.
623          * @return
624          * 
625          *         <pre>
626          * { "status" : "ok" (or "error"), "message": "some explanation" }
627          *         </pre>
628          */
629         private String buildJsonResponse(boolean success, String msg) {
630                 PortalAPIResponse response = new PortalAPIResponse(success, msg);
631                 String json = null;
632                 try {
633                         json = mapper.writeValueAsString(response);
634                 } catch (JsonProcessingException ex) {
635                         // Truly should never, ever happen
636                         logger.error("buildJsonResponse", ex);
637                         json = "{ \"status\": \"error\",\"message\":\"" + ex.toString() + "\" }";
638                 }
639                 return json;
640         }
641
642         /**
643          * Builds JSON object with status of error and message containing stack trace
644          * for the specified throwable.
645          * 
646          * @param t
647          *            Throwable with stack trace to use as message
648          * 
649          * @return
650          * 
651          *         <pre>
652          * { "status" : "error", "message": "some-big-stacktrace" }
653          *         </pre>
654          */
655         private String buildJsonResponse(Throwable t) {
656                 StringWriter sw = new StringWriter();
657                 PrintWriter pw = new PrintWriter(sw);
658                 t.printStackTrace(pw);
659                 return buildJsonResponse(false, sw.toString());
660         }
661         
662         private String buildShortJsonResponse(Throwable t)
663         {
664                 String errorMessage = t.getMessage();
665                 return buildJsonResponse(false, errorMessage);
666         }
667
668         @Override
669         public String getUserId(HttpServletRequest request) throws PortalAPIException {
670                 return portalRestApiServiceImpl.getUserId(request);
671         }
672
673         public static IPortalRestAPIService getPortalRestApiServiceImpl() {
674                 return portalRestApiServiceImpl;
675         }
676
677         public static void setPortalRestApiServiceImpl(IPortalRestAPIService portalRestApiServiceImpl) {
678                 PortalRestAPIProxy.portalRestApiServiceImpl = portalRestApiServiceImpl;
679         }
680         
681         @Override
682         public  Map<String, String> getCredentials() throws PortalAPIException {
683                 return portalRestApiServiceImpl.getCredentials();
684         }
685
686 }