You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by it...@apache.org on 2010/02/09 17:11:14 UTC
svn commit: r908096 -
/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.webapp.parent/org.apache.clerezza.platform.security/src/main/java/org/apache/clerezza/platform/security/UserUtil.java
Author: ito
Date: Tue Feb 9 16:11:13 2010
New Revision: 908096
URL: http://svn.apache.org/viewvc?rev=908096&view=rev
Log:
CLEREZZA-110: provide method for retrieving current subjects name
Added:
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.webapp.parent/org.apache.clerezza.platform.security/src/main/java/org/apache/clerezza/platform/security/UserUtil.java
Added: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.webapp.parent/org.apache.clerezza.platform.security/src/main/java/org/apache/clerezza/platform/security/UserUtil.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.webapp.parent/org.apache.clerezza.platform.security/src/main/java/org/apache/clerezza/platform/security/UserUtil.java?rev=908096&view=auto
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.webapp.parent/org.apache.clerezza.platform.security/src/main/java/org/apache/clerezza/platform/security/UserUtil.java (added)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.webapp.parent/org.apache.clerezza.platform.security/src/main/java/org/apache/clerezza/platform/security/UserUtil.java Tue Feb 9 16:11:13 2010
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.clerezza.platform.security;
+
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.Principal;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import java.util.Iterator;
+import javax.security.auth.Subject;
+
+/**
+ * Utility methods for retrieving user information.
+ *
+ *
+ * @author mir, tio
+ */
+public class UserUtil {
+
+ /**
+ *
+ * @return the name of user which is associated to the current thread
+ */
+ public static String getCurrentUserName() {
+ Subject subject;
+ final AccessControlContext context = AccessController.getContext();
+ try {
+ subject = AccessController.doPrivileged(new PrivilegedExceptionAction<Subject>() {
+
+ @Override
+ public Subject run() throws Exception {
+ return Subject.getSubject(context);
+ }
+ });
+ } catch (PrivilegedActionException ex) {
+ Exception cause = (Exception)ex.getCause();
+ if (cause instanceof RuntimeException) {
+ throw (RuntimeException) cause;
+ }
+ throw new RuntimeException(cause);
+ }
+ Iterator<Principal> iter = subject.getPrincipals().iterator();
+ String name = null;
+ if (iter.hasNext()) {
+ name = iter.next().getName();
+ }
+ return name;
+ }
+}