Merge "Logging improvements"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controllers / ControllersUtils.java
1 package org.onap.vid.controllers;
2
3 import org.apache.commons.lang3.exception.ExceptionUtils;
4 import org.onap.portalsdk.core.domain.User;
5 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
6 import org.onap.portalsdk.core.util.SystemProperties;
7 import org.onap.vid.model.ExceptionResponse;
8 import org.slf4j.MDC;
9 import org.springframework.http.ResponseEntity;
10
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpSession;
13 import javax.ws.rs.WebApplicationException;
14
15 import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
16 import static org.onap.vid.utils.Logging.getMethodName;
17
18 public class ControllersUtils {
19
20
21     public static String extractUserId(HttpServletRequest request) {
22         String userId = "";
23         HttpSession session = request.getSession();
24         if (session != null) {
25             User user = (User) session.getAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME));
26             if (user != null) {
27                 //userId = user.getHrid();
28                 userId = user.getLoginId();
29                 if (userId == null)
30                     userId = user.getOrgUserId();
31             }
32         }
33         return userId;
34     }
35
36     public static ExceptionResponse handleException(Exception e, EELFLoggerDelegate logger) {
37         logger.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodName(), ExceptionUtils.getMessage(e), e);
38
39         ExceptionResponse exceptionResponse = new ExceptionResponse(e);
40         return exceptionResponse;
41     }
42
43     public static ResponseEntity handleWebApplicationException(WebApplicationException e, EELFLoggerDelegate logger) {
44         return ResponseEntity.status(e.getResponse().getStatus()).body(ControllersUtils.handleException(e, logger));
45     }
46
47 }