001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: AttributeDesignator.java,v 1.2 2008/06/25 05:47:31 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.saml.assertion;
031
032import java.util.*; 
033import java.net.*;
034import org.w3c.dom.*; 
035import com.sun.identity.saml.common.SAMLUtilsCommon;
036import com.sun.identity.saml.common.SAMLConstants;
037import com.sun.identity.saml.common.SAMLException;
038import com.sun.identity.saml.common.SAMLRequesterException;
039
040/**
041 * The <code>AttributeDesignator</code> element identifies an attribute
042 * name within an attribute namespace. The element is used in an attribute query
043 * to request that attribute values within a specific namespace be returned. 
044 * @supported.all.api
045 */
046public class AttributeDesignator {
047    protected String _attributeName = null;
048    //_attributeNameSpace type should be URI, for now, define as String 
049    protected String _attributeNameSpace = null;      
050    
051    /**
052     *Default constructor
053     */
054    protected AttributeDesignator() {
055    }
056  
057    /**
058     * Constructs an attribute designator element from an existing XML block.
059     *
060     * @param element representing a DOM tree element.
061     * @exception SAMLException if that there is an error in the sender or
062     *            in the element definition.
063     */
064    public AttributeDesignator(Element element) throws SAMLException { 
065        // make sure that the input xml block is not null
066        if (element == null) {
067            SAMLUtilsCommon.debug.message(
068            "AttributeDesignator: Input is null.");
069            throw new SAMLRequesterException(
070                      SAMLUtilsCommon.bundle.getString("nullInput"));
071        }
072        // Make sure this is an AttributeDesignator.
073        String tag = null;
074        tag = element.getLocalName(); 
075        if ((tag == null) || (!tag.equals("AttributeDesignator"))) {
076            SAMLUtilsCommon.debug.message("AttributeDesignator: wrong input");
077            throw new SAMLRequesterException(
078                      SAMLUtilsCommon.bundle.getString("wrongInput"));
079        }
080        
081        // handle attributes  
082        int i = 0; 
083        NamedNodeMap atts = ((Node)element).getAttributes(); 
084        int attrCount = atts.getLength(); 
085        for (i = 0; i < attrCount; i++) {
086            Node att = atts.item(i);
087            if (att.getNodeType() == Node.ATTRIBUTE_NODE) {
088                String attName = att.getLocalName();
089                if (attName == null || attName.length() == 0) {
090                    if (SAMLUtilsCommon.debug.messageEnabled()) {
091                        SAMLUtilsCommon.debug.message("AttributeDesignator:" +
092                                "Attribute Name is either null or empty.");
093                    }
094                    throw new SAMLRequesterException(
095                        SAMLUtilsCommon.bundle.getString("nullInput"));
096                }
097                if (attName.equals("AttributeName")) {
098                    _attributeName =((Attr)att).getValue().trim();
099                } else if (attName.equals("AttributeNamespace")) {
100                    _attributeNameSpace = ((Attr)att).getValue().trim(); 
101                }
102            }
103        }
104        // AttributeName is required 
105        if (_attributeName == null || _attributeName.length() == 0) {
106            if (SAMLUtilsCommon.debug.messageEnabled()) {
107                SAMLUtilsCommon.debug.message("AttributeDesignator: "+
108                                        "AttributeName is required attribute");
109            }
110            throw new SAMLRequesterException(
111                        SAMLUtilsCommon.bundle.getString("missingAttribute"));
112        }
113        
114        // AttributeNamespace is required 
115        if (_attributeNameSpace == null || _attributeNameSpace.length() == 0) {
116            if (SAMLUtilsCommon.debug.messageEnabled()) {
117                SAMLUtilsCommon.debug.message("AttributeDesignator: "+
118                                    "AttributeNamespace is required attribute");
119            }
120            throw new SAMLRequesterException(
121                        SAMLUtilsCommon.bundle.getString("missingAttribute"));
122        }
123        
124        // handle the children of AttributeDesignator element
125        // Since AttributeDesignator does not have any child element_node, 
126        // we will throw exception if we found any such child. 
127        NodeList  nodes = element.getChildNodes();
128        int nodeCount = nodes.getLength(); 
129        if (nodeCount > 0) {
130            for (i = 0; i < nodeCount; i++) {
131                Node currentNode = nodes.item(i);               
132                if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
133                    if (SAMLUtilsCommon.debug.messageEnabled()) {
134                        SAMLUtilsCommon.debug.message(
135                        "AttributeDesignator: illegal input!");
136                    }
137                    throw new SAMLRequesterException(
138                              SAMLUtilsCommon.bundle.getString("wrongInput"));
139                }
140            }
141        }
142    }
143    
144    /**
145     * Constructs an instance of <code>AttributeDesignator</code>.
146     *
147     * @param name the name of the attribute. 
148     * @param nameSpace the namespace in which <code>AttributeName</code>
149     *        elements are interpreted.
150     * @exception SAMLException if there is an error in the sender or in the
151     *            element definition.
152     */
153    public AttributeDesignator(String name, String nameSpace) 
154                              throws SAMLException {
155        if (name == null || name.length() == 0) {
156            if (SAMLUtilsCommon.debug.messageEnabled()) {
157                SAMLUtilsCommon.debug.message("AttributeDesignator:" +
158                                        "AttributeName is required!");
159            }
160            throw new SAMLRequesterException(
161                      SAMLUtilsCommon.bundle.getString("nullInput"));
162        } else {
163            _attributeName = name; 
164        }
165        if (nameSpace == null || nameSpace.length() == 0) {
166            if (SAMLUtilsCommon.debug.messageEnabled()) {
167                SAMLUtilsCommon.debug.message("AttributeDesignator: " +
168                                        "AttributeNamespace is required!");
169            }
170            throw new SAMLRequesterException(
171                      SAMLUtilsCommon.bundle.getString("nullInput"));
172        } else { 
173             _attributeNameSpace = nameSpace;
174        }
175    }   
176    
177    /**
178     * Returns attribute name from the <code>AttributeDesignator</code>.
179     *
180     * @return A String representing the attribute name.
181     */
182    public String getAttributeName() {
183        return _attributeName; 
184    }
185    
186    /**
187     * Returns attribute name space from the
188     * <code>AttributeDesignator</code>.
189     *
190     * @return A String representing the attribute name space.
191     */
192    public String getAttributeNamespace() {
193        return _attributeNameSpace; 
194    }
195    
196    /** 
197     * Returns a String representation of the 
198     * <code>&lt;saml:AttributeDesignator&gt;</code> element.
199     *
200     * @return A string containing the valid XML for this element.
201     */
202    public String toString() {
203        return (toString(true, false)); 
204    }
205    
206    /**
207     * Returns a String representation of the
208     * <code>&lt;saml:AttributeDesignator&gt;</code> element.
209     *
210     * @param includeNS Determines whether or not the namespace qualifier is 
211     *        to the Element when converted
212     * @param declareNS Determines whether or not the namespace is declared
213     *        within the Element.
214     * @return A string containing the valid XML for this element.
215     */
216    public String toString(boolean includeNS, boolean declareNS) {
217        StringBuffer result = new StringBuffer(200);
218        String prefix = "";
219        String uri = "";
220        if (includeNS) {
221            prefix = SAMLConstants.ASSERTION_PREFIX;
222        }
223        if (declareNS) {
224            uri = SAMLConstants.assertionDeclareStr;
225        }    
226        result.append("<").append(prefix).append("AttributeDesignator ").
227               append(uri).append(" AttributeName=\"").append(_attributeName).
228               append("\" AttributeNamespace=\"").append(_attributeNameSpace).
229               append("\" />\n");       
230        return ((String)result.toString());
231    }                       
232}
233