Author: ito
Date: Wed Feb  3 11:31:42 2010
New Revision: 906014

URL: http://svn.apache.org/viewvc?rev=906014&view=rev
Log:
CLEREZZA-87: UserLoginNode added

Added:
    
incubator/clerezza/issues/CLEREZZA-87/org.apache.clerezza.platform.usermanager/src/main/java/org/apache/clerezza/platform/usermanager/UserLoginNode.java

Added: 
incubator/clerezza/issues/CLEREZZA-87/org.apache.clerezza.platform.usermanager/src/main/java/org/apache/clerezza/platform/usermanager/UserLoginNode.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-87/org.apache.clerezza.platform.usermanager/src/main/java/org/apache/clerezza/platform/usermanager/UserLoginNode.java?rev=906014&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-87/org.apache.clerezza.platform.usermanager/src/main/java/org/apache/clerezza/platform/usermanager/UserLoginNode.java
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-87/org.apache.clerezza.platform.usermanager/src/main/java/org/apache/clerezza/platform/usermanager/UserLoginNode.java
 Wed Feb  3 11:31:42 2010
@@ -0,0 +1,94 @@
+/*
+ * 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.usermanager;
+
+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;
+import org.apache.clerezza.rdf.core.Resource;
+
+
+import org.apache.clerezza.rdf.utils.GraphNode;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.clerezza.platform.typerendering.UserContextProvider;
+import org.apache.clerezza.rdf.core.BNode;
+import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.clerezza.rdf.ontologies.FOAF;
+import org.apache.clerezza.rdf.ontologies.PLATFORM;
+import org.apache.clerezza.rdf.utils.UnionMGraph;
+
+/**
+ * The login name is added to the user context node. The name is accessable via
+ * ssp template by using the context node (e.g. 
context/platform("user")/foaf("name")).
+ *
+ * @author tio
+ */
+...@component(enabled=true, immediate=true)
+...@service(UserContextProvider.class)
+
+public class UserLoginNode implements UserContextProvider {
+
+       @Reference
+       protected UserManager userManager;
+
+       @Override
+       public GraphNode addUserContext(GraphNode node) {
+               String userName = getUserName();
+               GraphNode agent = userManager.getUserGraphNode(userName);
+               if(!(node.getObjects(PLATFORM.user).hasNext())) {
+                       node.addProperty(PLATFORM.user, agent.getNode());
+               }
+               node = new GraphNode(node.getNode(), new 
UnionMGraph(node.getGraph(),
+                               agent.getGraph()));
+               return node;
+       }
+
+       private String getUserName() {
+               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;
+       }
+}


Reply via email to