2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * Modifications copyright (c) 2018 Nokia
21 * ===================================================================
25 package org.onap.clamp.util;
27 import java.util.Date;
29 import org.onap.clamp.clds.service.DefaultUserNameHandler;
30 import org.onap.clamp.clds.service.UserNameHandler;
31 import org.onap.clamp.clds.util.LoggingUtils;
32 import org.springframework.security.core.context.SecurityContext;
33 import org.springframework.security.core.context.SecurityContextHolder;
34 import org.springframework.security.core.userdetails.UserDetails;
36 public class PrincipalUtils {
37 private static UserNameHandler userNameHandler = new DefaultUserNameHandler();
38 private static SecurityContext securityContext = SecurityContextHolder.getContext();
41 * Private constructor to avoid creating instances of util class.
43 private PrincipalUtils(){
49 * @return The user name
51 public static String getUserName() {
52 String name = userNameHandler.retrieveUserName(securityContext);
53 Date startTime = new Date();
54 LoggingUtils.setTargetContext("CLDS", "getUserName");
55 LoggingUtils.setTimeContext(startTime, new Date());
60 * Get the userId from AAF/CSP.
64 public static String getUserId() {
69 * Get the principal name.
71 * @return The principal name
73 public static String getPrincipalName() {
74 String principal = ((UserDetails)securityContext.getAuthentication().getPrincipal()).getUsername();
75 String name = "Not found";
76 if (principal != null) {
82 public static void setSecurityContext(SecurityContext securityContext) {
83 PrincipalUtils.securityContext = securityContext;
86 public static SecurityContext getSecurityContext() {
87 return securityContext;