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();
43 * @return The user name
45 public static String getUserName() {
46 String name = userNameHandler.retrieveUserName(securityContext);
47 Date startTime = new Date();
48 LoggingUtils.setTargetContext("CLDS", "getUserName");
49 LoggingUtils.setTimeContext(startTime, new Date());
54 * Get the userId from AAF/CSP.
58 public static String getUserId() {
63 * Get the principal name.
65 * @return The principal name
67 public static String getPrincipalName() {
68 String principal = ((UserDetails)securityContext.getAuthentication().getPrincipal()).getUsername();
69 String name = "Not found";
70 if (principal != null) {
76 public static void setSecurityContext(SecurityContext securityContext) {
77 PrincipalUtils.securityContext = securityContext;
80 public static SecurityContext getSecurityContext() {
81 return securityContext;