Author: sohil.shah(a)jboss.com
Date: 2009-02-05 20:22:36 -0500 (Thu, 05 Feb 2009)
New Revision: 12783
Added:
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/xacml/JAXBEncoder.java
Modified:
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/model/Action.java
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/model/Attribute.java
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/model/Environment.java
modules/authorization/trunk/enforcement/pom.xml
modules/authorization/trunk/enforcement/src/main/java/org/jboss/security/authz/enforcement/Request.java
modules/authorization/trunk/policy-server/src/test/java/org/jboss/security/authz/policy/server/plugin/TestEnterprisePolicyFinderModule.java
Log:
JAXB Encoding of the Object Model started
Modified:
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/model/Action.java
===================================================================
---
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/model/Action.java 2009-02-05
20:54:56 UTC (rev 12782)
+++
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/model/Action.java 2009-02-06
01:22:36 UTC (rev 12783)
@@ -23,6 +23,7 @@
package org.jboss.security.authz.model;
import java.util.Set;
+import java.util.HashSet;
/**
* Represents a protected Action within a system
@@ -43,7 +44,7 @@
*/
public Action()
{
-
+ this.attributes = new HashSet<Attribute>();
}
/**
@@ -62,5 +63,10 @@
public void setAttributes(Set<Attribute> attributes)
{
this.attributes = attributes;
- }
+ }
+
+ public void addAttribute(Attribute attribute)
+ {
+ this.attributes.add(attribute);
+ }
}
Modified:
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/model/Attribute.java
===================================================================
---
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/model/Attribute.java 2009-02-05
20:54:56 UTC (rev 12782)
+++
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/model/Attribute.java 2009-02-06
01:22:36 UTC (rev 12783)
@@ -119,5 +119,5 @@
public void setValue(String value)
{
this.value = value;
- }
+ }
}
Modified:
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/model/Environment.java
===================================================================
---
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/model/Environment.java 2009-02-05
20:54:56 UTC (rev 12782)
+++
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/model/Environment.java 2009-02-06
01:22:36 UTC (rev 12783)
@@ -23,6 +23,7 @@
package org.jboss.security.authz.model;
import java.util.Set;
+import java.util.HashSet;
/**
* Represents Environment information in the context of an Authroization Request
@@ -43,7 +44,7 @@
*/
public Environment()
{
-
+ this.attributes = new HashSet<Attribute>();
}
/**
@@ -62,5 +63,10 @@
public void setAttributes(Set<Attribute> attributes)
{
this.attributes = attributes;
- }
+ }
+
+ public void addAttribute(Attribute attribute)
+ {
+ this.attributes.add(attribute);
+ }
}
Added:
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/xacml/JAXBEncoder.java
===================================================================
---
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/xacml/JAXBEncoder.java
(rev 0)
+++
modules/authorization/trunk/common/src/main/java/org/jboss/security/authz/xacml/JAXBEncoder.java 2009-02-06
01:22:36 UTC (rev 12783)
@@ -0,0 +1,152 @@
+/*
+* JBoss, a division of Red Hat
+* Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+*/
+package org.jboss.security.authz.xacml;
+
+import org.jboss.security.xacml.core.model.context.AttributeType;
+import org.jboss.security.xacml.core.model.context.AttributeValueType;
+import org.jboss.security.xacml.core.model.context.SubjectType;
+import org.jboss.security.xacml.core.model.context.ResourceType;
+import org.jboss.security.xacml.core.model.context.ActionType;
+import org.jboss.security.xacml.core.model.context.EnvironmentType;
+
+import org.jboss.security.authz.model.Attribute;
+import org.jboss.security.authz.model.Subject;
+import org.jboss.security.authz.model.Resource;
+import org.jboss.security.authz.model.Action;
+import org.jboss.security.authz.model.Environment;
+
+/**
+ * A Utility for encoding the developer friendly domain objects to the XACML JAXB
Objects
+ *
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ */
+public class JAXBEncoder
+{
+ /**
+ * Encodes an Attribute object to its correspoding JAXB object
+ *
+ * @param attribute
+ * @return
+ */
+ public static AttributeType encode(Attribute attribute)
+ {
+ AttributeType jaxbObject = new AttributeType();
+
+ jaxbObject.setAttributeId(attribute.getUri());
+ jaxbObject.setDataType(attribute.getDatatType());
+ AttributeValueType jaxbValue = new AttributeValueType();
+ jaxbValue.getContent().add(attribute.getValue());
+ jaxbObject.getAttributeValue().add(jaxbValue);
+
+ return jaxbObject;
+ }
+
+ /**
+ * Encodes a Subject object to its corresponding JAXB object
+ *
+ * @param subject
+ * @return
+ */
+ public static SubjectType encode(Subject subject)
+ {
+ SubjectType jaxbObject = new SubjectType();
+
+ jaxbObject.setSubjectCategory(subject.getCategory());
+
+ if(subject.getAttributes() != null)
+ {
+ for(Attribute attribute: subject.getAttributes())
+ {
+ AttributeType jaxbAttribute = JAXBEncoder.encode(attribute);
+ jaxbObject.getAttribute().add(jaxbAttribute);
+ }
+ }
+
+ return jaxbObject;
+ }
+
+ /**
+ * Encodes a Resource object to its corresponding JAXB object
+ *
+ * @param resource
+ * @return
+ */
+ public static ResourceType encode(Resource resource)
+ {
+ ResourceType jaxbObject = new ResourceType();
+
+ if(resource.getAttributes() != null)
+ {
+ for(Attribute attribute: resource.getAttributes())
+ {
+ AttributeType jaxbAttribute = JAXBEncoder.encode(attribute);
+ jaxbObject.getAttribute().add(jaxbAttribute);
+ }
+ }
+
+ return jaxbObject;
+ }
+
+ /**
+ * Encodes a Action object to its corresponding JAXB object
+ *
+ * @param resource
+ * @return
+ */
+ public static ActionType encode(Action action)
+ {
+ ActionType jaxbObject = new ActionType();
+
+ if(action.getAttributes() != null)
+ {
+ for(Attribute attribute: action.getAttributes())
+ {
+ AttributeType jaxbAttribute = JAXBEncoder.encode(attribute);
+ jaxbObject.getAttribute().add(jaxbAttribute);
+ }
+ }
+
+ return jaxbObject;
+ }
+
+ /**
+ * Encodes an Environment object to its corresponding JAXB object
+ *
+ * @param resource
+ * @return
+ */
+ public static EnvironmentType encode(Environment environment)
+ {
+ EnvironmentType jaxbObject = new EnvironmentType();
+
+ if(environment.getAttributes() != null)
+ {
+ for(Attribute attribute: environment.getAttributes())
+ {
+ AttributeType jaxbAttribute = JAXBEncoder.encode(attribute);
+ jaxbObject.getAttribute().add(jaxbAttribute);
+ }
+ }
+
+ return jaxbObject;
+ }
+}
Modified: modules/authorization/trunk/enforcement/pom.xml
===================================================================
--- modules/authorization/trunk/enforcement/pom.xml 2009-02-05 20:54:56 UTC (rev 12782)
+++ modules/authorization/trunk/enforcement/pom.xml 2009-02-06 01:22:36 UTC (rev 12783)
@@ -20,12 +20,10 @@
<version>${project.version}</version>
</dependency>
- <!-- test dependencies -->
<!-- jboss xacml -->
<dependency>
<groupId>org.jboss.security</groupId>
- <artifactId>jboss-xacml</artifactId>
- <scope>test</scope>
+ <artifactId>jboss-xacml</artifactId>
</dependency>
</dependencies>
Modified:
modules/authorization/trunk/enforcement/src/main/java/org/jboss/security/authz/enforcement/Request.java
===================================================================
---
modules/authorization/trunk/enforcement/src/main/java/org/jboss/security/authz/enforcement/Request.java 2009-02-05
20:54:56 UTC (rev 12782)
+++
modules/authorization/trunk/enforcement/src/main/java/org/jboss/security/authz/enforcement/Request.java 2009-02-06
01:22:36 UTC (rev 12783)
@@ -29,7 +29,10 @@
import org.jboss.security.authz.model.Subject;
import org.jboss.security.authz.model.Action;
import org.jboss.security.authz.model.Environment;
+import org.jboss.security.authz.xacml.JAXBEncoder;
+import org.jboss.security.xacml.core.model.context.RequestType;
+
/**
* An Authorization Request
*
@@ -97,4 +100,41 @@
{
this.subjects.add(subject);
}
+
+ public RequestType encode()
+ {
+ RequestType jaxbObject = new RequestType();
+
+ //Encode Resources
+ if(this.resources != null)
+ {
+ for(Resource resource: this.resources)
+ {
+ jaxbObject.getResource().add(JAXBEncoder.encode(resource));
+ }
+ }
+
+ //Encode Subjects
+ if(this.subjects != null)
+ {
+ for(Subject subject: this.subjects)
+ {
+ jaxbObject.getSubject().add(JAXBEncoder.encode(subject));
+ }
+ }
+
+ //Encode Action
+ if(this.action != null)
+ {
+ jaxbObject.setAction(JAXBEncoder.encode(this.action));
+ }
+
+ //Encode Environment
+ if(this.environment != null)
+ {
+ jaxbObject.setEnvironment(JAXBEncoder.encode(this.environment));
+ }
+
+ return jaxbObject;
+ }
}
Modified:
modules/authorization/trunk/policy-server/src/test/java/org/jboss/security/authz/policy/server/plugin/TestEnterprisePolicyFinderModule.java
===================================================================
---
modules/authorization/trunk/policy-server/src/test/java/org/jboss/security/authz/policy/server/plugin/TestEnterprisePolicyFinderModule.java 2009-02-05
20:54:56 UTC (rev 12782)
+++
modules/authorization/trunk/policy-server/src/test/java/org/jboss/security/authz/policy/server/plugin/TestEnterprisePolicyFinderModule.java 2009-02-06
01:22:36 UTC (rev 12783)
@@ -21,27 +21,21 @@
*/
package org.jboss.security.authz.policy.server.plugin;
-import java.util.Set;
import junit.framework.TestCase;
import org.apache.log4j.Logger;
import org.jboss.security.authz.model.Policy;
import org.jboss.security.authz.model.PolicyMetaData;
import org.jboss.security.authz.model.Resource;
+import org.jboss.security.authz.model.Subject;
+import org.jboss.security.authz.model.Action;
import org.jboss.security.authz.model.Attribute;
-import org.jboss.security.authz.model.Rule;
-import org.jboss.security.authz.model.DroolsRuleExpression;
import org.jboss.security.authz.components.http.HttpResource;
+import org.jboss.security.authz.enforcement.Request;
import org.jboss.security.authz.policy.server.PolicyServer;
import org.jboss.security.authz.policy.server.Server;
-import org.jboss.security.xacml.core.model.context.ActionType;
-import org.jboss.security.xacml.core.model.context.AttributeType;
-import org.jboss.security.xacml.core.model.context.AttributeValueType;
-import org.jboss.security.xacml.core.model.context.ObjectFactory;
-import org.jboss.security.xacml.core.model.context.RequestType;
-import org.jboss.security.xacml.core.model.context.ResourceType;
-import org.jboss.security.xacml.core.model.context.SubjectType;
+
import org.jboss.security.xacml.factories.RequestResponseContextFactory;
import org.jboss.security.xacml.interfaces.RequestContext;
import org.jboss.security.xacml.interfaces.ResponseContext;
@@ -126,57 +120,32 @@
log.info("Decision="+responseContext.getDecision());
}
private RequestContext createRequestContext(HttpResource httpResource, boolean
mustBePermitted) throws Exception
- {
- //Create ObjectFactory
- ObjectFactory objectFactory = new ObjectFactory();
-
- //Create RequestContext
- RequestContext requestContext = RequestResponseContextFactory.createRequestCtx();
-
+ {
//Create a RequestType
- RequestType requestType = objectFactory.createRequestType();
+ Request request = new Request();
//Create Subjects
- SubjectType subject = objectFactory.createSubjectType();
- AttributeType subjectAttribute = objectFactory.createAttributeType();
- subjectAttribute.setAttributeId(XACMLConstants.ATTRIBUTEID_ROLE);
- subjectAttribute.setDataType(XMLSchemaConstants.DATATYPE_STRING);
- AttributeValueType subjectId = objectFactory.createAttributeValueType();
-
subjectId.getContent().add(mustBePermitted?"Admin":"Anonymous");
- subjectAttribute.getAttributeValue().add(subjectId);
- subject.getAttribute().add(subjectAttribute);
- requestType.getSubject().add(subject);
+ Subject subject = new Subject();
+ Attribute subjectAttr = new Attribute(XACMLConstants.ATTRIBUTEID_ROLE,
+ XMLSchemaConstants.DATATYPE_STRING,
mustBePermitted?"Admin":"Anonymous");
+ subject.addAttribute(subjectAttr);
+ request.addSubject(subject);
//Create Resource
Resource urlResource = httpResource.getURLResource();
- ResourceType resource = objectFactory.createResourceType();
- Set<Attribute> attributes = urlResource.getAttributes();
- for(Attribute attribute: attributes)
- {
- AttributeType resourceAttribute = objectFactory.createAttributeType();
- resourceAttribute.setAttributeId(attribute.getUri());
- resourceAttribute.setDataType(attribute.getDatatType());
- AttributeValueType resourceId = objectFactory.createAttributeValueType();
- resourceId.getContent().add(attribute.getValue());
- resourceAttribute.getAttributeValue().add(resourceId);
- resource.getAttribute().add(resourceAttribute);
- }
- requestType.getResource().add(resource);
+ request.addResource(urlResource);
//Create Action
- ActionType action = objectFactory.createActionType();
- AttributeType actionAttribute = objectFactory.createAttributeType();
- actionAttribute.setAttributeId(XACMLConstants.ATTRIBUTEID_ACTION_ID);
- actionAttribute.setDataType(XMLSchemaConstants.DATATYPE_STRING);
- AttributeValueType actionId = objectFactory.createAttributeValueType();
- actionId.getContent().add("GET");
- actionAttribute.getAttributeValue().add(actionId);
- action.getAttribute().add(actionAttribute);
- requestType.setAction(action);
+ Action action = new Action();
+ Attribute actionAttr = new Attribute(XACMLConstants.ATTRIBUTEID_ACTION_ID,
+ XMLSchemaConstants.DATATYPE_STRING, "GET");
+ action.addAttribute(actionAttr);
+ request.setAction(action);
- //Spit out RequestContext
- requestContext.setRequest(requestType);
+ //Create RequestContext
+ RequestContext requestContext = RequestResponseContextFactory.createRequestCtx();
+ requestContext.setRequest(request.encode());
return requestContext;
}