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: Directive.java,v 1.2 2008/06/25 05:47:10 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.liberty.ws.disco;
031
032import java.util.Iterator;
033import java.util.List;
034import java.util.ArrayList;
035import java.util.StringTokenizer;
036
037import org.w3c.dom.Element;
038
039import com.sun.identity.liberty.ws.disco.common.DiscoConstants;
040import com.sun.identity.liberty.ws.disco.common.DiscoUtils;
041
042/**
043 * The class <code>Directive</code> represents a discovery service
044 * <code>DirectiveType</code> element. Current implementation supports the
045 * following four directive types: <code>AUTHENTICATE_REQUESTER</code>,
046 * <code>AUTHORIZE_REQUESTER</code>, <code>AUTHENTICATE_SESSION_CONTEXT</code>,
047 * and <code>ENCRYPT_RESOURCEID</code>.
048 * <p>
049 * The following schema fragment specifies the expected content within the
050 * <code>DirectiveType</code> object.
051 * <pre>
052 * &lt;complexType name="DirectiveType">
053 *   &lt;complexContent>
054 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
055 *       &lt;attribute name="descriptionIDRefs" type="{http://www.w3.org/2001/XMLSchema}IDREFS" />
056 *     &lt;/restriction>
057 *   &lt;/complexContent>
058 * &lt;/complexType>
059 * </pre>
060 * 
061 * @supported.all.api
062 */
063public class Directive {
064
065    /**
066     * <code>DirectiveType AuthenticateRequester</code>.
067     */
068    public static final String AUTHENTICATE_REQUESTER =
069                                                "AuthenticateRequester";
070
071    /**
072     * <code>DirectiveType AuthorizeRequester</code>.
073     */
074    public static final String AUTHORIZE_REQUESTER = "AuthorizeRequester";
075
076
077    /**
078     * <code>DirectiveType AuthenticateSessionContext</code>.
079     */
080    public static final String AUTHENTICATE_SESSION_CONTEXT = 
081                                                "AuthenticateSessionContext";
082
083    /**
084     * <code>DirectiveType EncryptResourceID</code>.
085     */
086    public static final String ENCRYPT_RESOURCEID = "EncryptResourceID";
087
088
089    /**
090     * <code>DirectiveType GenerateBearerToken</code>.
091     */
092    public static final String GENERATE_BEARER_TOKEN = "GenerateBearerToken";
093
094    private String type = null;
095    private List descIDRefs = null;
096
097    /**
098     * Constructs a directive instance for a type of directive.
099     * @param directiveType Type of the directive.
100     */
101    public Directive(String directiveType) {
102        type = directiveType;
103    }
104
105    /**
106     * Constructs a directive instance from DOM element.
107     * @param elem <code>DirectiveType</code> DOM element.
108     * @exception DiscoveryException if error occurs.
109     */
110    public Directive(Element elem) throws DiscoveryException {
111        String tag = null;
112        if (elem == null) {
113            DiscoUtils.debug.message("Directive(Element): null input.");
114            throw new DiscoveryException(
115                DiscoUtils.bundle.getString("nullInput"));
116        }
117        if ((tag = elem.getLocalName()) == null) {
118            DiscoUtils.debug.message("Directive(Element): wrong input");
119            throw new DiscoveryException(
120                DiscoUtils.bundle.getString("wrongInput"));
121        }
122        if (tag.equals(AUTHENTICATE_REQUESTER)) {
123            type = AUTHENTICATE_REQUESTER;
124            setDescIDRefs(elem);
125        } else if (tag.equals(AUTHORIZE_REQUESTER)) {
126            type = AUTHORIZE_REQUESTER;
127            setDescIDRefs(elem);
128        } else if (tag.equals(AUTHENTICATE_SESSION_CONTEXT)) {
129            type = AUTHENTICATE_SESSION_CONTEXT;
130            setDescIDRefs(elem);
131        } else if (tag.equals(ENCRYPT_RESOURCEID)) {
132            type = ENCRYPT_RESOURCEID;
133            setDescIDRefs(elem);
134        } else if (tag.equals(GENERATE_BEARER_TOKEN)) {
135            type = GENERATE_BEARER_TOKEN;
136            setDescIDRefs(elem);
137        } else {
138            if (DiscoUtils.debug.messageEnabled()) {
139                DiscoUtils.debug.message("Directive(Element): not supported:"
140                    + tag);
141            }
142            throw new DiscoveryException(
143                DiscoUtils.bundle.getString("directiveNotSupported"));
144        }
145    }
146
147    private void setDescIDRefs(Element elem) {
148        String ids = elem.getAttribute("descriptionIDRefs");
149        if ((ids != null) && (ids.length() != 0)) {
150            StringTokenizer st = new StringTokenizer(ids);
151            if (st.countTokens() > 0) {
152                if (descIDRefs == null) {
153                    descIDRefs = new ArrayList();
154                }
155                while (st.hasMoreTokens()) {
156                    descIDRefs.add(st.nextToken());
157                }
158            }
159        }
160    }
161
162    /**
163     * Returns a list of description ID references. 
164     * @return a list of description ID references.
165     * @see #setDescriptionIDRef(List)
166     */
167    public List getDescriptionIDRef() {
168        return descIDRefs;
169    }
170
171    /**
172     * Sets a list of description ID references.
173     * @param idrefs a list of description ID references to be set.
174     * @see #getDescriptionIDRef()
175     */
176    public void setDescriptionIDRef(List idrefs) {
177        descIDRefs = idrefs;
178    }
179
180
181    /**
182     * Returns type of directive.
183     * @return type of directive.
184     * @see #setDirectiveType(String)
185     */
186    public String getDirectiveType() {
187        return type;
188    }
189
190    /**
191     * Sets type of the directive.
192     * @param directiveType type of the directive to be set.
193     * @see #getDirectiveType()
194     */
195    public void setDirectiveType(String directiveType) {
196        type = directiveType;
197    }
198
199
200    /**
201     * Returns the directive object in string format.
202     * @return the directive object in string format.
203     */ 
204    public String toString() {
205        String ns = null;
206        if (type.equals(GENERATE_BEARER_TOKEN)) {
207            ns = DiscoConstants.DISCO11_NS;
208        } else {
209            ns = DiscoConstants.DISCO_NS;
210        }
211
212        StringBuffer sb = new StringBuffer(300);
213        sb.append("<").append(type).append(" xmlns=\"").
214            append(ns).append("\"");
215        if (descIDRefs != null) {
216            sb.append(" descriptionIDRefs=\"");
217            Iterator iter = descIDRefs.iterator();
218            if (iter.hasNext()) {
219                sb.append((String) iter.next());
220            }
221            while (iter.hasNext()) {
222                sb.append(" ").append((String) iter.next());
223            }
224            sb.append("\"");
225        }
226        sb.append(">");
227        sb.append("</").append(type).append(">");
228        return sb.toString();
229    }
230}