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: GetComplete.java,v 1.2 2008/06/25 05:46:47 qcheng Exp $
026 *
027 */
028
029package com.sun.identity.federation.message.common;
030
031import com.sun.identity.federation.message.common.EntityName;
032
033import org.w3c.dom.Element;
034import org.w3c.dom.Node;
035import org.w3c.dom.NodeList;
036import org.w3c.dom.Document;
037
038import com.sun.identity.shared.xml.XMLUtils;
039import com.sun.identity.saml.protocol.AbstractRequest;
040import com.sun.identity.saml.common.SAMLConstants;
041import com.sun.identity.saml.common.SAMLUtils;
042import com.sun.identity.saml.common.SAMLException;
043import com.sun.identity.saml.common.SAMLRequestVersionTooHighException;
044import com.sun.identity.saml.common.SAMLRequestVersionTooLowException;
045import com.sun.identity.federation.common.IFSConstants;
046import com.sun.identity.federation.message.common.FSMsgException;
047import com.sun.identity.federation.common.FSUtils;
048
049import java.util.List;
050import java.util.Collections;
051import java.util.Iterator;
052import java.util.ArrayList;
053
054/**
055 * This class contains methods for the <code>GetComplete</code>
056 * Element. This element specifies a URI which resolves to
057 * the complete IDPList.
058 *
059 * @supported.all.api
060 */
061
062public class GetComplete extends EntityName {
063    
064    /**
065     * Default Constructor.
066     */
067    public GetComplete() {
068    }
069    
070    /**
071     * Constructor create <code>GetComplete</code> object.
072     *
073     * @param uri the value of the <code>URI</code>.
074     */
075    public GetComplete(String uri) {
076        super(uri);
077    }
078    
079    /**
080     * Constructor create <code>GetComplete</code> object.
081     *
082     * @param uri the value of the <code>URI</code>.
083     * @param otherElements list of other elements.
084     */
085    public GetComplete(String uri, List otherElements) {
086        super(uri, otherElements);
087    }
088    
089    /**
090     * Constructor creates <code>GetComplete</code> object from
091     * the Document Element.
092     *
093     * @param root the Document Element object.
094     * @throws FSMsgException if error creating this object.
095     */
096    public GetComplete(Element root) throws FSMsgException {
097        String tag = null;
098        if (root == null) {
099            FSUtils.debug.message("GetComplete(Element): null input.");
100            throw new FSMsgException("nullInput",null);
101        }
102        if (((tag = root.getLocalName()) == null) ||
103                (!tag.equals("GetComplete"))) {
104            FSUtils.debug.message("GetComplete(Element): wrong input");
105            throw new FSMsgException("wrongInput",null);
106        }
107        
108        int length = 0;
109        // get the contents of the request
110        NodeList contentnl = root.getChildNodes();
111        Node child;
112        String nodeName;
113        length = contentnl.getLength();
114        for (int i = 0; i < length; i++) {
115            child = contentnl.item(i);
116            nodeName = child.getLocalName();
117            if ((nodeName != null) &&  nodeName.equals("URI")) {
118                // make sure the providerId is not assigned already
119                if (uri != null) {
120                    if (FSUtils.debug.messageEnabled()) {
121                        FSUtils.debug.message("GetComplete(Element): should"
122                                + "contain only one URI.");
123                    }
124                    throw new FSMsgException("wrongInput",null);
125                }
126                uri = XMLUtils.getElementValue((Element) child);
127            } else {
128                if (FSUtils.debug.messageEnabled()) {
129                    FSUtils.debug.message("GetComplete(Element): invalid"
130                            + " node" + nodeName);
131                }
132                throw new FSMsgException("wrongInput",null);
133            }
134        }
135    }
136
137/**
138 * Returns <code>GetComplete</code> object. This
139 * object is created by parsing the <code>XML</code> string.
140 *
141 * @param xml <code>XML</code> String
142 * @return the <code>GetComplete</code> object.
143 * @throws FSMsgException if there is an error creating this object.
144 */
145public static GetComplete parseXML(String xml) throws FSMsgException {
146    Document doc = XMLUtils.toDOMDocument(xml, FSUtils.debug);
147    if (doc == null) {
148        if (FSUtils.debug.messageEnabled()) {
149            FSUtils.debug.message("GetComplete.parseXML:Error "
150                    + "while parsing input xml string");
151        }
152        throw new FSMsgException("parseError",null);
153    }
154    Element root = doc.getDocumentElement();
155    return new GetComplete(root);
156}
157
158/**
159 * Returns the string representation of this object.
160 *
161 * @return An XML String representing this object.
162 *
163 */
164public String toXMLString() throws FSMsgException {
165    return toXMLString(true, true);
166}
167
168    /**
169     * Returns a String representation of the &lt;samlp:Response&gt; element.
170     *
171     * @param includeNS : Determines whether or not the namespace qualifier
172     *        is prepended to the Element when converted
173     * @param declareNS : Determines whether or not the namespace is declared
174     *        within the Element.
175     * @return a string containing the valid XML for this element
176     * @throws FSMsgException if there is an error converting
177     *         this object ot a string.
178     */
179    public String toXMLString(boolean includeNS, boolean declareNS)
180    throws FSMsgException {
181        return toXMLString(includeNS, declareNS, false);
182    }
183    
184    /**
185     * Returns a String representation of the &lt;samlp:Response&gt; element.
186     *
187     * @param includeNS Determines whether or not the namespace qualifier
188     *        is prepended to the Element when converted
189     * @param declareNS Determines whether or not the namespace is declared
190     *        within the Element.
191     * @param includeHeader Determines whether the output include the xml
192     *        declaration header.
193     * @return a string containing the valid XML for this element
194     * @throws FSMsgException if there is an error converting
195     *         this object ot a string.
196     */
197
198    public String toXMLString(boolean includeNS,boolean declareNS,
199            boolean includeHeader) throws FSMsgException {
200        StringBuffer xml = new StringBuffer(300);
201        if (includeHeader) {
202            xml.append("<?xml version=\"1.0\" encoding=\"").
203                    append(IFSConstants.DEFAULT_ENCODING).append("\" ?>");
204        }
205        String prefix = "";
206        String uri = "";
207        if (includeNS) {
208            prefix = IFSConstants.LIB_PREFIX;
209        }
210        if (declareNS) {
211            uri = IFSConstants.LIB_NAMESPACE_STRING;
212        }
213        
214        xml.append("<").append(prefix).append("GetComplete").append(uri).
215                append(">\n");
216        
217        xml.append("<").append(prefix).append("URI").append(uri).append(">").
218                append(this.uri).
219                append("</").append(prefix).append("URI").append(">");
220        
221        xml.append("</").append(prefix).append("GetComplete>");
222        
223        return xml.toString();
224    }
225}