2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nokia. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
23 package org.onap.vid.controller;
25 import static org.onap.vid.utils.Logging.getMethodCallerName;
27 import java.util.Optional;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpSession;
30 import javax.ws.rs.WebApplicationException;
31 import org.apache.commons.lang3.exception.ExceptionUtils;
32 import org.onap.portalsdk.core.domain.User;
33 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
34 import org.onap.portalsdk.core.util.SystemProperties;
35 import org.onap.vid.model.ExceptionResponse;
36 import org.onap.vid.utils.SystemPropertiesWrapper;
37 import org.springframework.http.ResponseEntity;
39 public final class ControllersUtils {
41 private final SystemPropertiesWrapper systemPropertiesWrapper;
43 public ControllersUtils(SystemPropertiesWrapper systemPropertiesWrapper) {
44 this.systemPropertiesWrapper = systemPropertiesWrapper;
47 public static ExceptionResponse handleException(Exception e, EELFLoggerDelegate logger) {
48 logger.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodCallerName(), ExceptionUtils.getMessage(e), e);
49 return new ExceptionResponse(e);
52 public static ResponseEntity handleWebApplicationException(WebApplicationException e, EELFLoggerDelegate logger) {
53 return ResponseEntity.status(e.getResponse().getStatus()).body(ControllersUtils.handleException(e, logger));
56 public String extractUserId(HttpServletRequest request) {
57 Optional<User> user = Optional.ofNullable(request.getSession())
58 .map((HttpSession he) -> (User) he
59 .getAttribute(systemPropertiesWrapper.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)));
61 return user.map(User::getLoginId).isPresent()
62 ? user.map(User::getLoginId).orElse("") : user.map(User::getOrgUserId).orElse("");