abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r441629 - in /incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server: ./ auth/ servlet/ util/
Date Fri, 08 Sep 2006 20:03:26 GMT
Author: jmsnell
Date: Fri Sep  8 13:03:25 2006
New Revision: 441629

URL: http://svn.apache.org/viewvc?view=rev&rev=441629
Log:
Provide a new SubjectResolver that can be used to resolve a javax.security.auth.Subject
from the authenticated Principal provided by the HttpServletRequest.  This allows 
APP server implementations the ability to configure their own user profile mapping
implementation and have the authenticated identity passed down through the 
RequestContext.

Added:
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/auth/
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/auth/SubjectResolver.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/SimpleSubjectResolver.java
Modified:
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/AbderaServer.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestContext.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/ServletRequestContext.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/ServerConstants.java

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/AbderaServer.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/AbderaServer.java?view=diff&rev=441629&r1=441628&r2=441629
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/AbderaServer.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/AbderaServer.java
Fri Sep  8 13:03:25 2006
@@ -18,6 +18,7 @@
 package org.apache.abdera.protocol.server;
 
 import org.apache.abdera.Abdera;
+import org.apache.abdera.protocol.server.auth.SubjectResolver;
 import org.apache.abdera.protocol.server.target.TargetResolver;
 import org.apache.abdera.protocol.server.util.ServerConstants;
 import org.apache.abdera.util.ServiceUtil;
@@ -27,32 +28,38 @@
   private final Abdera abdera;
   private final RequestHandlerFactory handlerFactory;
   private final TargetResolver targetResolver;
+  private final SubjectResolver subjectResolver;
   private final String defaultHandlerFactory;
   private final String defaultTargetResolver;
+  private final String defaultSubjectResolver;
   
   public AbderaServer() {
-    this(new Abdera(),"","");
+    this(new Abdera(),"","",DEFAULT_SUBJECT_RESOLVER);
   }
   
   public AbderaServer(
     String defaultTargetResolver, 
-    String defaultHandlerFactory) {
-      this(new Abdera(), defaultTargetResolver, defaultHandlerFactory);
+    String defaultHandlerFactory,
+    String defaultSubjectResolver) {
+      this(new Abdera(), defaultTargetResolver, defaultHandlerFactory, defaultSubjectResolver);
   }
   
   public AbderaServer(Abdera abdera) {
-    this(abdera,"","");
+    this(abdera,"","",DEFAULT_SUBJECT_RESOLVER);
   }
   
   public AbderaServer(
     Abdera abdera, 
     String defaultTargetResolver, 
-    String defaultHandlerFactory) {
+    String defaultHandlerFactory,
+    String defaultSubjectResolver) {
       this.abdera = abdera;
       this.handlerFactory = newRequestHandlerFactory(defaultHandlerFactory);
       this.targetResolver = newTargetResolver(defaultTargetResolver);
+      this.subjectResolver = newSubjectResolver(defaultSubjectResolver);
       this.defaultHandlerFactory = defaultHandlerFactory;
       this.defaultTargetResolver = defaultTargetResolver;
+      this.defaultSubjectResolver = defaultSubjectResolver;
   }
   
   public Abdera getAbdera() {
@@ -68,7 +75,7 @@
       HANDLER_FACTORY, (_default != null) ? _default : "", abdera);
   }
   
-  public RequestHandlerFactory getRequestHandlerFactory(String _default) {
+  public RequestHandlerFactory getRequestHandlerFactory() {
     return handlerFactory;
   }
   
@@ -81,7 +88,20 @@
       TARGET_RESOLVER, (_default != null) ? _default : "", abdera);
   }
   
-  public TargetResolver getTargetResolver(String _default) {
+  public TargetResolver getTargetResolver() {
     return targetResolver;
+  }
+  
+  public SubjectResolver newSubjectResolver() {
+    return newSubjectResolver(defaultSubjectResolver);
+  }
+  
+  public SubjectResolver newSubjectResolver(String _default) {
+    return (SubjectResolver) ServiceUtil.newInstance(
+        SUBJECT_RESOLVER, (_default != null) ? _default : DEFAULT_SUBJECT_RESOLVER, abdera);
+  }
+  
+  public SubjectResolver getSubjectResolver() {
+    return subjectResolver;
   }
 }

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestContext.java?view=diff&rev=441629&r1=441628&r2=441629
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestContext.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestContext.java
Fri Sep  8 13:03:25 2006
@@ -22,12 +22,16 @@
 import java.net.URI;
 import java.util.List;
 
+import javax.security.auth.Subject;
+
 import org.apache.abdera.protocol.Request;
 import org.apache.abdera.protocol.server.target.Target;
 
 public interface RequestContext extends Request {
   
   Target getTarget();
+  
+  Subject getSubject();
   
   String getMethod();
   

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/auth/SubjectResolver.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/auth/SubjectResolver.java?view=auto&rev=441629
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/auth/SubjectResolver.java
(added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/auth/SubjectResolver.java
Fri Sep  8 13:03:25 2006
@@ -0,0 +1,30 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.server.auth;
+
+import java.security.Principal;
+
+import javax.security.auth.Subject;
+
+public interface SubjectResolver {
+
+  Subject resolve(Principal principal);
+  
+  Subject resolve(String userid);
+  
+}

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java?view=diff&rev=441629&r1=441628&r2=441629
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java
Fri Sep  8 13:03:25 2006
@@ -34,6 +34,7 @@
 import org.apache.abdera.protocol.server.RequestHandler;
 import org.apache.abdera.protocol.server.RequestHandlerFactory;
 import org.apache.abdera.protocol.server.ResponseContext;
+import org.apache.abdera.protocol.server.auth.SubjectResolver;
 import org.apache.abdera.protocol.server.exceptions.AbderaServerException;
 import org.apache.abdera.protocol.server.target.TargetResolver;
 import org.apache.abdera.protocol.server.util.ServerConstants;
@@ -63,9 +64,12 @@
   private RequestContext getRequestContext(HttpServletRequest request) {
     RequestContext context = 
       (RequestContext) request.getAttribute(REQUESTCONTEXT);
-    TargetResolver resolver = getTargetResolver();
     if (context == null) {
-      context = new ServletRequestContext(abdera,resolver,request);
+      context = new ServletRequestContext(
+        abdera,
+        getTargetResolver(),
+        getSubjectResolver(),
+        request);
       request.setAttribute(REQUESTCONTEXT, context);
     }
     return context;
@@ -97,6 +101,21 @@
         String s = getServletConfig().getInitParameter(TARGET_RESOLVER);
         resolver = abderaServer.newTargetResolver(s);
         context.setAttribute(TARGET_RESOLVER, resolver);
+      }
+      return resolver;
+    }
+  }
+  
+  private SubjectResolver getSubjectResolver() {
+    ServletContext context = getServletContext();
+    synchronized(context) {
+      SubjectResolver resolver = 
+        (SubjectResolver) context.getAttribute(
+          SUBJECT_RESOLVER);
+      if (resolver  == null) {
+        String s = getServletConfig().getInitParameter(SUBJECT_RESOLVER);
+        resolver = abderaServer.newSubjectResolver(s);
+        context.setAttribute(SUBJECT_RESOLVER, resolver);
       }
       return resolver;
     }

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/ServletRequestContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/ServletRequestContext.java?view=diff&rev=441629&r1=441628&r2=441629
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/ServletRequestContext.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/ServletRequestContext.java
Fri Sep  8 13:03:25 2006
@@ -26,10 +26,12 @@
 import java.util.Date;
 import java.util.List;
 
+import javax.security.auth.Subject;
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.abdera.Abdera;
 import org.apache.abdera.protocol.server.RequestContext;
+import org.apache.abdera.protocol.server.auth.SubjectResolver;
 import org.apache.abdera.protocol.server.target.Target;
 import org.apache.abdera.protocol.server.target.TargetResolver;
 import org.apache.abdera.protocol.server.util.ServerConstants;
@@ -42,6 +44,7 @@
   
   private final Target target;
   private final Abdera abdera;
+  private final Subject subject;
   private final HttpServletRequest servletRequest;
   private final String method;
   private final URI uri;
@@ -51,6 +54,7 @@
   public ServletRequestContext(
     Abdera abdera,
     TargetResolver resolver,
+    SubjectResolver subjectResolver,
     HttpServletRequest request) {
       this.abdera = abdera;
       this.servletRequest = request;
@@ -60,6 +64,12 @@
       pathInfo = initPathInfo();
       target = resolver.resolve(getUri().toString());
       method = request.getMethod();
+      subject = (subjectResolver != null) ?
+        subjectResolver.resolve(request.getUserPrincipal()) : null;
+  }
+  
+  public Subject getSubject() {
+    return subject;
   }
   
   public Target getTarget() {

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/ServerConstants.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/ServerConstants.java?view=diff&rev=441629&r1=441628&r2=441629
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/ServerConstants.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/ServerConstants.java
Fri Sep  8 13:03:25 2006
@@ -28,6 +28,12 @@
   public static final String TARGET_RESOLVER = 
     "org.apache.abdera.protocol.server.target.TargetResolver";
   
+  public static final String SUBJECT_RESOLVER = 
+    "org.apache.abdera.protocol.server.auth.SubjectResolver";
+  
+  public static final String DEFAULT_SUBJECT_RESOLVER = 
+    "org.apache.abdera.protocol.server.util.SimpleSubjectResolver";
+  
   public static final String X_OVERRIDE_HEADER = 
     "X-HTTP-Method-Override";
   

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/SimpleSubjectResolver.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/SimpleSubjectResolver.java?view=auto&rev=441629
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/SimpleSubjectResolver.java
(added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/SimpleSubjectResolver.java
Fri Sep  8 13:03:25 2006
@@ -0,0 +1,94 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.server.util;
+
+import java.security.Principal;
+
+import javax.security.auth.Subject;
+
+import org.apache.abdera.protocol.server.auth.SubjectResolver;
+
+public class SimpleSubjectResolver 
+  implements SubjectResolver {
+
+  public static final Principal ANONYMOUS = new AnonymousPrincipal();
+  
+  public Subject resolve(Principal principal) {
+    Subject subject = new Subject();
+    subject.getPrincipals().add((principal != null) ? principal : ANONYMOUS);
+    return subject;
+  }
+
+  public Subject resolve(String userid) {
+    if (userid == null) return resolve(ANONYMOUS);
+    return resolve(new SimplePrincipal(userid));
+  }
+
+  static class SimplePrincipal implements Principal {
+    final String name;
+    SimplePrincipal(String name) {
+      this.name = name;
+    }
+    public String getName() {
+      return name;
+    }
+    public String toString() {
+      return name;
+    }
+    @Override
+    public int hashCode() {
+      final int PRIME = 31;
+      int result = 1;
+      result = PRIME * result + ((name == null) ? 0 : name.hashCode());
+      return result;
+    }
+    @Override
+    public boolean equals(Object obj) {
+      if (this == obj) return true;
+      if (obj == null) return false;
+      if (getClass() != obj.getClass()) return false;
+      final SimplePrincipal other = (SimplePrincipal) obj;
+      if (name == null) {
+        if (other.name != null) return false;
+      } else if (!name.equals(other.name))
+        return false;
+      return true;
+    }
+    
+  }
+  
+  public static final class AnonymousPrincipal implements Principal {
+    final String name = "Anonymous";
+    public String getName() {
+      return name;
+    }
+    public String toString() {
+      return name;
+    }
+    public boolean equals(Object other) {
+      if (other == null) return false;
+      return this == other;
+    }
+    public int hashCode() {
+      final int PRIME = 31;
+      int result = 1;
+      result = PRIME * result + ((name == null) ? 0 : name.hashCode());
+      return result;
+    }
+  }
+}



Mime
View raw message