2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.filters;
22 import java.util.Arrays;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Optional;
27 import javax.servlet.http.HttpServletRequest;
28 import org.onap.sdc.security.AuthenticationCookie;
29 import org.onap.sdc.security.IUsersThreadLocalHolder;
30 import org.onap.sdc.security.PortalClient;
31 import org.onap.sdc.security.RestrictionAccessFilterException;
32 import org.openecomp.sdc.be.model.User;
33 import org.openecomp.sdc.be.user.UserBusinessLogic;
34 import org.openecomp.sdc.common.api.Constants;
35 import org.openecomp.sdc.common.datastructure.UserContext;
36 import org.openecomp.sdc.common.log.wrappers.Logger;
37 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
38 import org.springframework.beans.factory.annotation.Autowired;
40 public class ThreadLocalUtils implements IUsersThreadLocalHolder {
42 private static final Logger log = Logger.getLogger(ThreadLocalUtils.class);
44 PortalClient portalClient;
46 UserBusinessLogic userBusinessLogic;
49 public void setUserContext(AuthenticationCookie authenticationCookie) {
50 UserContext userContext;
51 userContext = new UserContext(authenticationCookie.getUserID(), authenticationCookie.getRoles(), authenticationCookie.getFirstName(),
52 authenticationCookie.getLastName());
53 ThreadLocalsHolder.setUserContext(userContext);
56 protected void setUserContext(HttpServletRequest httpRequest) {
57 final String userId = httpRequest.getHeader(Constants.USER_ID_HEADER);
59 Set<String> roles = null;
61 final Optional<String> userRolesFromPortalOptional = portalClient.fetchUserRolesFromPortal(userId);
62 if (userRolesFromPortalOptional.isPresent()){
63 roles = new HashSet<>(List.of(userRolesFromPortalOptional.get()));
65 } catch (RestrictionAccessFilterException e) {
66 log.debug("Failed to fetch user ID - {} from portal", userId);
67 log.debug(e.getMessage());
69 final UserContext userContext = new UserContext(userId, roles, null, null);
70 ThreadLocalsHolder.setUserContext(userContext);
72 log.debug("user_id value in req header is null, userContext will not be initialized");
76 protected void setUserContextFromDB(HttpServletRequest httpRequest) {
77 String user_id = httpRequest.getHeader(Constants.USER_ID_HEADER);
78 //there are some internal request that have no user_id header e.g. healthcheck
79 if (user_id != null) {
80 updateUserContext(user_id);
82 log.debug("user_id value in req header is null, userContext will not be initialized");
86 private void updateUserContext(String user_id) {
87 User user = userBusinessLogic.getUser(user_id, false);
88 Set<String> roles = new HashSet<>(Arrays.asList(user.getRole()));
89 UserContext userContext = new UserContext(user.getUserId(), roles, user.getFirstName(), user.getLastName());
90 ThreadLocalsHolder.setUserContext(userContext);