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;
25 import javax.servlet.http.HttpServletRequest;
26 import org.onap.sdc.security.AuthenticationCookie;
27 import org.onap.sdc.security.IUsersThreadLocalHolder;
28 import org.onap.sdc.security.PortalClient;
29 import org.onap.sdc.security.RestrictionAccessFilterException;
30 import org.openecomp.sdc.be.model.User;
31 import org.openecomp.sdc.be.user.UserBusinessLogic;
32 import org.openecomp.sdc.common.api.Constants;
33 import org.openecomp.sdc.common.datastructure.UserContext;
34 import org.openecomp.sdc.common.log.wrappers.Logger;
35 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
36 import org.springframework.beans.factory.annotation.Autowired;
38 public class ThreadLocalUtils implements IUsersThreadLocalHolder {
40 private static final Logger log = Logger.getLogger(ThreadLocalUtils.class);
42 PortalClient portalClient;
44 UserBusinessLogic userBusinessLogic;
47 public void setUserContext(AuthenticationCookie authenticationCookie) {
48 UserContext userContext;
49 userContext = new UserContext(authenticationCookie.getUserID(), authenticationCookie.getRoles(), authenticationCookie.getFirstName(),
50 authenticationCookie.getLastName());
51 ThreadLocalsHolder.setUserContext(userContext);
54 protected void setUserContext(HttpServletRequest httpRequest) {
55 String user_id = httpRequest.getHeader(Constants.USER_ID_HEADER);
56 if (user_id != null) {
57 String userRolesFromPortal = null;
58 Set<String> roles = null;
60 userRolesFromPortal = portalClient.fetchUserRolesFromPortal(user_id);
61 roles = new HashSet<>(Arrays.asList(userRolesFromPortal));
62 } catch (RestrictionAccessFilterException e) {
63 log.debug("Failed to fetch user ID - {} from portal", user_id);
64 log.debug(e.getMessage());
66 UserContext userContext = new UserContext(user_id, roles, null, null);
67 ThreadLocalsHolder.setUserContext(userContext);
69 log.debug("user_id value in req header is null, userContext will not be initialized");
73 protected void setUserContextFromDB(HttpServletRequest httpRequest) {
74 String user_id = httpRequest.getHeader(Constants.USER_ID_HEADER);
75 //there are some internal request that have no user_id header e.g. healthcheck
76 if (user_id != null) {
77 updateUserContext(user_id);
79 log.debug("user_id value in req header is null, userContext will not be initialized");
83 private void updateUserContext(String user_id) {
84 User user = userBusinessLogic.getUser(user_id, false);
85 Set<String> roles = new HashSet<>(Arrays.asList(user.getRole()));
86 UserContext userContext = new UserContext(user.getUserId(), roles, user.getFirstName(), user.getLastName());
87 ThreadLocalsHolder.setUserContext(userContext);