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: PasswordTransforms.java,v 1.2 2008/06/25 05:47:08 qcheng Exp $
026 *
027 */
028
029package com.sun.identity.liberty.ws.authnsvc.protocol;
030
031import java.util.ArrayList;
032import java.util.Iterator;
033import java.util.List;
034
035import org.w3c.dom.Document;
036import org.w3c.dom.Element;
037import org.w3c.dom.Node;
038import org.w3c.dom.NodeList;
039
040import com.sun.identity.liberty.ws.authnsvc.AuthnSvcConstants;
041import com.sun.identity.liberty.ws.authnsvc.AuthnSvcException;
042
043/**
044 * The <code>PasswordTransforms</code> class represents 'PasswordTransforms'
045 * element defined in Authentication Service schema.
046 *
047 * @supported.all.api
048 */
049public class PasswordTransforms {
050
051    private List transforms = null;
052
053    /**
054     * This is the default constructor.
055     */
056    public PasswordTransforms(List transforms)
057    {
058        this.transforms = transforms;
059    }
060
061    /**
062     * This constructor takes a <code>org.w3c.dom.Element</code>.
063     * @param element a PasswordTransforms element
064     * @exception AuthnSvcException if an error occurs while parsing the
065     *                              PasswordTransforms element
066     */
067    PasswordTransforms(Element element) throws AuthnSvcException
068    {
069        NodeList nl = element.getChildNodes();
070        int length = nl.getLength();
071
072        for(int i = 0; i < length; i++) {
073            Node child = nl.item(i);
074            if (child.getNodeType() == Node.ELEMENT_NODE) {
075                Element childElement = (Element)child;
076                String localName = childElement.getLocalName();
077                String namespaceURI = childElement.getNamespaceURI();
078
079                if (AuthnSvcConstants.NS_AUTHN_SVC.equals(namespaceURI) &&
080                    AuthnSvcConstants.TAG_TRANSFORM.equals(localName)) {
081
082                    Transform tf = Transform.getTransform(childElement);
083                    if (transforms == null) {
084                        transforms = new ArrayList();
085                    }
086                    transforms.add(tf);
087                } else {
088                    throw new AuthnSvcException("invalidChildPT");
089                }
090            }
091        }
092    }
093
094    /**
095     * Returns a list of child 'Transforms' Elements
096     * @return a list of child 'Transforms' Elements
097     * @see #setTransforms(List)
098     */
099    public List getTransforms()
100    {
101        return transforms;
102    }
103
104    /**
105     * Sets a list of child 'Transforms' Elements.
106     * @param transforms a list of child 'Transforms' Element
107     * @see #getTransforms()
108     */
109    public void setTransforms(List transforms)
110    {
111        this.transforms = transforms;
112    }
113
114    /**
115     * Converts this to <code>org.w3c.dom.Element</code> and add to
116     * parent SASLResponse Element.
117     * @param respE parent SASLResponse Element
118     * @exception AuthnSvcException if there is no child
119     */
120    void addToParent(Element respE) throws AuthnSvcException
121    {
122        if (transforms == null || transforms.isEmpty()) {
123            throw new AuthnSvcException("noChildPT");
124        }
125        Document doc = respE.getOwnerDocument();
126        Element ptE = doc.createElementNS(
127                                AuthnSvcConstants.NS_AUTHN_SVC,
128                                AuthnSvcConstants.PTAG_PASSWORD_TRANSFORMS);
129        respE.appendChild(ptE);
130
131        for(Iterator iter = transforms.iterator(); iter.hasNext(); ) {
132            Transform tf = (Transform)iter.next();
133            tf.addToParent(ptE);
134        }
135    }
136}