Add a semicolon at the end of this statement
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / ControllersUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.controller;
23
24 import static org.onap.vid.utils.Logging.getMethodCallerName;
25
26 import java.util.Optional;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpSession;
29 import javax.ws.rs.WebApplicationException;
30 import org.apache.commons.lang3.exception.ExceptionUtils;
31 import org.onap.portalsdk.core.domain.User;
32 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
33 import org.onap.portalsdk.core.util.SystemProperties;
34 import org.onap.vid.model.ExceptionResponse;
35 import org.onap.vid.utils.SystemPropertiesWrapper;
36 import org.springframework.http.ResponseEntity;
37
38 public final class ControllersUtils {
39
40     private final SystemPropertiesWrapper systemPropertiesWrapper;
41
42     public ControllersUtils(SystemPropertiesWrapper systemPropertiesWrapper) {
43         this.systemPropertiesWrapper = systemPropertiesWrapper;
44     }
45
46     public static ExceptionResponse handleException(Exception e, EELFLoggerDelegate logger) {
47         logger.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodCallerName(), ExceptionUtils.getMessage(e), e);
48
49         ExceptionResponse exceptionResponse = new ExceptionResponse(e);
50         return exceptionResponse;
51     }
52
53     public static ResponseEntity handleWebApplicationException(WebApplicationException e, EELFLoggerDelegate logger) {
54         return ResponseEntity.status(e.getResponse().getStatus()).body(ControllersUtils.handleException(e, logger));
55     }
56
57     public String extractUserId(HttpServletRequest request) {
58         Optional<User> user = Optional.ofNullable(request.getSession())
59             .map((HttpSession he) -> (User) he
60                 .getAttribute(systemPropertiesWrapper.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)));
61
62         return user.map(User::getLoginId).isPresent()
63             ? user.map(User::getLoginId).orElse("") : user.map(User::getOrgUserId).orElse("");
64     }
65 }