String actionURIBase = "urn:oasis:names:tc:xacml:2.0:request-param:attribute:";
Principal principal = request.getUserPrincipal();
RequestContext requestCtx = RequestResponseContextFactory.createRequestCtx();
//Create a subject type
SubjectType subject = new SubjectType();
subject.getAttribute().add(
RequestAttributeFactory.createStringAttributeType(
XACMLConstants.ATTRIBUTEID_SUBJECT_ID,
"jboss.org",
principal.getName()));
List<Role> rolesList = callerRoles.getRoles();
if(rolesList != null)
{
for(Role role:rolesList)
{
String roleName = role.getRoleName();
AttributeType attSubjectID = RequestAttributeFactory.createStringAttributeType(
XACMLConstants.ATTRIBUTEID_ROLE, "jboss.org", roleName);
subject.getAttribute().add(attSubjectID);
}
}
//Create a resource type
ResourceType resourceType = new ResourceType();
resourceType.getAttribute().add(
RequestAttributeFactory.createAnyURIAttributeType(
XACMLConstants.ATTRIBUTEID_RESOURCE_ID,
null,
new URI(request.getRequestURI())));
//Create an action type
ActionType actionType = new ActionType();
actionType.getAttribute().add(
RequestAttributeFactory.createStringAttributeType(
XACMLConstants.ATTRIBUTEID_ACTION_ID,
"jboss.org",
action));
Enumeration<String> enumer = request.getParameterNames();
while(enumer.hasMoreElements())
{
String paramName = enumer.nextElement();
String paramValue = request.getParameter(paramName);
URI actionUri = new URI(actionURIBase + paramName);
actionType.getAttribute().add(
RequestAttributeFactory.createStringAttributeType(
actionUri.toASCIIString(),
"jboss.org",
paramValue));
}
//Create an Environment Type (Optional)
EnvironmentType environmentType = new EnvironmentType();
environmentType.getAttribute().add( RequestAttributeFactory.createDateTimeAttributeType(
XACMLConstants.ATTRIBUTEID_CURRENT_TIME, null));
//Create a Request Type
RequestType requestType = new RequestType();
requestType.getSubject().add(subject);
requestType.getResource().add(resourceType);
requestType.setAction(actionType);
requestType.setEnvironment(environmentType);
requestCtx.setRequest(requestType);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if(trace)
{
requestCtx.marshall(baos);
log.trace(new String(baos.toByteArray()));
}
return requestCtx;
}