/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* https://opensso.dev.java.net/public/CDDLv1.0.html or
* opensso/legal/CDDLv1.0.txt
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at opensso/legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* $Id: ResourceImpl.java,v 1.3 2008/06/25 05:48:13 qcheng Exp $
*
*/
package com.sun.identity.xacml.context.impl;
import com.sun.identity.xacml.common.XACMLSDKUtils;
import com.sun.identity.shared.xml.XMLUtils;
import com.sun.identity.xacml.common.XACMLSDKUtils;
import com.sun.identity.xacml.common.XACMLConstants;
import com.sun.identity.xacml.common.XACMLException;
import com.sun.identity.xacml.context.Attribute;
import com.sun.identity.xacml.context.ContextFactory;
import com.sun.identity.xacml.context.Resource;
import com.sun.identity.xacml.context.ResourceContent;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* The Resource
element specifies information about the
* resource to which access is requested by listing a
* sequence of Attribute
elements associated with the
* resource. it may include ResourceContent
*
*
* <xs:element name="Resource" type="xacml-context:ResourceType"/> * <xs:complexType name="ResourceType"> * <xs:sequence> * <xs:element ref="xacml-context:ResourceContent" minOccurs="0"/> * <xs:element ref="xacml-context:Attribute" minOccurs="0" * maxOccurs="unbounded"/> * <xs:sequence> * <xs:complexType> **@supported.all.api */ public class ResourceImpl implements Resource { private List attributes; private Element resourceContent; private boolean isMutable = true; /** * Default constructor */ public ResourceImpl() { } /** * This constructor is used to build
Resource
object from a
* XML string.
*
* @param xml A java.lang.String
representing
* a Resource
object
* @exception XACMLException if it could not process the XML string
*/
public ResourceImpl(String xml) throws XACMLException {
Document document = XMLUtils.toDOMDocument(xml, XACMLSDKUtils.debug);
if (document != null) {
Element rootElement = document.getDocumentElement();
processElement(rootElement);
makeImmutable();
} else {
XACMLSDKUtils.debug.error(
"SubjectImpl.processElement(): invalid XML input");
throw new XACMLException(
XACMLSDKUtils.xacmlResourceBundle.getString(
"errorObtainingElement"));
}
}
/**
* This constructor is used to build resource
object from a
* block of existing XML that has already been built into a DOM.
*
* @param element A org.w3c.dom.Element
representing
* DOM tree for Resource
object
* @exception XACML2Exception if it could not process the Element
*/
public ResourceImpl(Element element) throws XACMLException {
processElement(element);
makeImmutable();
}
private void processElement(Element element) throws XACMLException {
if (element == null) {
XACMLSDKUtils.debug.error(
"ResourceImpl.processElement(): invalid root element");
throw new XACMLException(
XACMLSDKUtils.xacmlResourceBundle.getString(
"invalid_element"));
}
String elemName = element.getLocalName();
if (elemName == null) {
XACMLSDKUtils.debug.error(
"ResourceImpl.processElement(): local name missing");
throw new XACMLException(
XACMLSDKUtils.xacmlResourceBundle.getString(
"missing_local_name"));
}
if (!elemName.equals(XACMLConstants.RESOURCE)) {
XACMLSDKUtils.debug.error(
"ResourceImpl.processElement(): invalid local name " +
elemName);
throw new XACMLException(
XACMLSDKUtils.xacmlResourceBundle.getString(
"invalid_local_name"));
}
// starts processing subelements
NodeList nodes = element.getChildNodes();
int numOfNodes = nodes.getLength();
if (numOfNodes > 0) {
ContextFactory factory = ContextFactory.getInstance();
for (int i=0; i< numOfNodes; i++) {
Node child = (Node)nodes.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
String childName = child.getLocalName();
// The child nodes should be immutable
if
* makeImmutable()
has been invoked on it. It can
* be determined by calling isMutable
on the object.
*/
public void setResourceContent(Element resourceContent)
throws XACMLException {
if (!isMutable) {
throw new XACMLException(
XACMLSDKUtils.xacmlResourceBundle.getString(
"objectImmutable"));
}
String elemName = resourceContent.getLocalName();
if (elemName == null
|| !elemName.equals(XACMLConstants.RESOURCE_CONTENT)) {
XACMLSDKUtils.debug.error(
"StatusMessageImpl.processElement():"
+ "local name missing or incorrect");
throw new XACMLException(
XACMLSDKUtils.xacmlResourceBundle.getString(
"missing_local_name"));
}
this.resourceContent = resourceContent;
}
/**
* Returns zero to many Attribute
elements of this object
* If no attributes and present, empty List
will be returned.
* Typically a Resource
element will contain an
* Attribute
with an AttributeId
of
* "urn:oasis:names:tc:xacml:1.0:resource:resource-id". Each such
* Attribute
SHALL be an absolute abd fully resolved
* representation of the identity of the single resource to which
* access is requested.
*
* @return List
containing the Attribute
* elements of this object
*/
public List getAttributes() {
return attributes;
}
/**
* Sets the Attribute
elements of this object
*
* @param attributes Attribute
elements of this object
* attributes could be an empty List
, if no attributes
* are present.
*
* @exception XACMLException if the object is immutable
* An object is considered immutable
if
* makeImmutable()
has been invoked on it. It can
* be determined by calling isMutable
on the object.
*/
public void setAttributes(List attributes) throws XACMLException {
if (!isMutable) {
throw new XACMLException(
XACMLSDKUtils.xacmlResourceBundle.getString(
"objectImmutable"));
}
if (attributes != null && !attributes.isEmpty()) {
if (this.attributes == null) {
this.attributes = new ArrayList();
}
this.attributes.addAll(attributes);
}
}
/**
* Returns a String
representation of this object
* @param includeNSPrefix Determines whether or not the namespace qualifier
* is prepended to the Element when converted
* @param declareNS Determines whether or not the namespace is declared
* within the Element.
* @return a string representation of this object
* @exception XACMLException if conversion fails for any reason
*/
public String toXMLString(boolean includeNSPrefix, boolean declareNS)
throws XACMLException
{
StringBuffer sb = new StringBuffer(2000);
StringBuffer NS = new StringBuffer(100);
String appendNS = "";
if (declareNS) {
NS.append(XACMLConstants.CONTEXT_NS_DECLARATION)
.append(XACMLConstants.SPACE);
NS.append(XACMLConstants.XSI_NS_URI)
.append(XACMLConstants.SPACE)
.append(XACMLConstants.CONTEXT_SCHEMA_LOCATION);
}
if (includeNSPrefix) {
appendNS = XACMLConstants.CONTEXT_NS_PREFIX + ":";
}
sb.append("<").append(appendNS).append(XACMLConstants.RESOURCE)
.append(NS);
sb.append(">");
int length = 0;
if (attributes != null) {
sb.append("\n");
length = attributes.size();
for (int i = 0; i < length; i++) {
Attribute attr = (Attribute)attributes.get(i);
sb.append(attr.toXMLString(includeNSPrefix, false));
}
}
if (resourceContent != null) {
sb.append("\n");
// ignore trailing ":"
if (includeNSPrefix && (resourceContent.getPrefix() == null)) {
resourceContent.setPrefix(appendNS.substring(0, appendNS.length()-1));
}
if(declareNS) {
int index = NS.indexOf("=");
String namespaceName = NS.substring(0, index);
String namespaceURI = NS.substring(index+1);
if (resourceContent.getNamespaceURI() == null) {
resourceContent.setAttribute(namespaceName, namespaceURI);
// does not seem to work to append namespace TODO
}
}
sb.append(XMLUtils.print(resourceContent));
}
sb.append("").append(appendNS).append(XACMLConstants.RESOURCE);
sb.append(">\n");
return sb.toString();
}
/**
* Returns a string representation of this object
*
* @return a string representation of this object
* @exception XACMLException if conversion fails for any reason
*/
public String toXMLString() throws XACMLException {
return toXMLString(true, false);
}
/*
* Makes the object immutable
*/
public void makeImmutable() {// TODO
}
/**
* Checks if the object is mutable
*
* @return true
if the object is mutable,
* false
otherwise
*/
public boolean isMutable() {
return isMutable;
}
}