Fix check style issues
[clamp.git] / src / main / java / org / onap / clamp / util / PrincipalUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.util;
26
27 import java.util.Date;
28
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;
35
36 public class PrincipalUtils {
37     private static UserNameHandler userNameHandler = new DefaultUserNameHandler();
38     private static SecurityContext securityContext = SecurityContextHolder.getContext();
39
40     /**
41      * Get the Full name.
42      *
43      * @return The user name
44      */
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());
50         return name;
51     }
52
53     /**
54      * Get the userId from AAF/CSP.
55      *
56      * @return The user ID
57      */
58     public static String getUserId() {
59         return getUserName();
60     }
61
62     /**
63      * Get the principal name.
64      *
65      * @return The principal name
66      */
67     public static String getPrincipalName() {
68         String principal = ((UserDetails)securityContext.getAuthentication().getPrincipal()).getUsername();
69         String name = "Not found";
70         if (principal != null) {
71             name = principal;
72         }
73         return name;
74     }
75
76     public static void setSecurityContext(SecurityContext securityContext) {
77         PrincipalUtils.securityContext = securityContext;
78     }
79
80     public static SecurityContext getSecurityContext() {
81         return securityContext;
82     }
83 }