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: Description.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;
036import javax.xml.namespace.QName;
037
038import org.w3c.dom.*;
039
040import com.sun.identity.liberty.ws.disco.common.DiscoConstants;
041import com.sun.identity.liberty.ws.disco.common.DiscoUtils;
042import com.sun.identity.shared.xml.XMLUtils;
043
044/**
045 * The class <code>Description</code> represents a 
046 * Description Type of a service instance.
047 * <p>The following schema fragment specifies the expected content within the
048 * <code>Description</code> object.
049 * <p>
050 * <pre>
051 * &lt;complexType name="DescriptionType">
052 *   &lt;complexContent>
053 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
054 *       &lt;sequence>
055 *         &lt;element name="SecurityMechID" type="{http://www.w3.org/2001/XMLSchema}anyURI" maxOccurs="unbounded"/>
056 *         &lt;element name="CredentialRef" type="{http://www.w3.org/2001/XMLSchema}IDREF" maxOccurs="unbounded" minOccurs="0"/>
057 *         &lt;choice>
058 *           &lt;group ref="{urn:liberty:disco:2003-08}WsdlRef"/>
059 *           &lt;group ref="{urn:liberty:disco:2003-08}BriefSoapHttpDescription"/>
060 *         &lt;/choice>
061 *       &lt;/sequence>
062 *       &lt;attribute name="id" type="{http://www.w3.org/2001/XMLSchema}ID" />
063 *     &lt;/restriction>
064 *   &lt;/complexContent>
065 * &lt;/complexType>
066 * </pre>
067 * 
068 * @supported.all.api
069 */
070public class Description {
071
072    private String id = null;
073    private List mechID = null;
074    private List credentialRef = new ArrayList();
075    private String soapEndpoint = null;
076    private String soapAction = null;
077    private QName serviceNameRef = null;
078    private String wsdlURI = null;
079
080    /**
081     * Default constructor.
082     */
083     public Description() {}
084
085    /**
086     * Constructor.
087     * @param securityMechID List of supported security mechanism ID as String
088     * @param credentialRef List of credential references
089     * @param endPoint SOAP endpoint URI
090     */
091    public Description (java.util.List securityMechID,
092                        java.util.List credentialRef,
093                        String endPoint)
094    {
095        mechID = securityMechID;
096        this.credentialRef = credentialRef;
097        soapEndpoint = endPoint;
098    }
099
100    /**
101     * Constructs a Description object from DOM element.
102     * @param elem DOM Element of Description.
103     * @exception DiscoveryException if error occurs.
104     */
105    public Description(Element elem) throws DiscoveryException {
106        if (elem == null) {
107            DiscoUtils.debug.message("Description(Element): null input.");
108            throw new DiscoveryException(
109                DiscoUtils.bundle.getString("nullInput"));
110        }
111        String nodeName;
112        String nameSpaceURI;
113        if (((nodeName = elem.getLocalName()) == null) ||
114            (!nodeName.equals("Description")) ||
115            ((nameSpaceURI = elem.getNamespaceURI()) == null) ||
116            (!nameSpaceURI.equals(DiscoConstants.DISCO_NS)))
117        {
118            DiscoUtils.debug.message("Description(Element): wrong input");
119            throw new DiscoveryException(
120                DiscoUtils.bundle.getString("wrongInput"));
121        }
122
123        id = elem.getAttribute("id");
124
125        NodeList contentnl = elem.getChildNodes();
126        Node child;
127        for (int i = 0, length = contentnl.getLength(); i < length; i++) {
128            child = contentnl.item(i);
129            if ((nodeName = child.getLocalName()) != null) {
130                nameSpaceURI = ((Element) child).getNamespaceURI();
131                if ((nameSpaceURI == null) ||
132                    (!nameSpaceURI.equals(DiscoConstants.DISCO_NS)))
133                {
134                    if (DiscoUtils.debug.messageEnabled()) {
135                        DiscoUtils.debug.message("Description(Element): "
136                            + "invalid namespace for node " + nodeName);
137                    }
138                    throw new DiscoveryException(
139                        DiscoUtils.bundle.getString("wrongInput"));
140                }
141                if (nodeName.equals("SecurityMechID")) {
142                    String mID = XMLUtils.getElementValue((Element) child);
143                    if ((mID == null) || (mID.length() == 0)) {
144                        if (DiscoUtils.debug.messageEnabled()) {
145                            DiscoUtils.debug.message("Description(Element): "
146                                + "missing SecurityMechID value.");
147                        }
148                        throw new DiscoveryException(
149                            DiscoUtils.bundle.getString("emptyElement"));
150                    }
151                    if (mechID == null) {
152                        mechID = new ArrayList();
153                    }
154                    mechID.add(mID);
155                } else if (nodeName.equals("CredentialRef")) {
156                    String ref = XMLUtils.getElementValue((Element) child);
157                    if ((ref == null) || (ref.length() == 0)) {
158                        if (DiscoUtils.debug.messageEnabled()) {
159                            DiscoUtils.debug.message("Description(Element): "
160                                + "missing CredentialRef value.");
161                        }
162                        throw new DiscoveryException(
163                            DiscoUtils.bundle.getString("emptyElement"));
164                    }
165                    if (credentialRef == null) {
166                        credentialRef = new ArrayList();
167                    }
168                    credentialRef.add(ref);
169                } else if (nodeName.equals("Endpoint")) {
170                    parseEndpoint((Element) child);
171                } else if (nodeName.equals("SoapAction")) {
172                    parseSoapAction((Element) child);
173                } else if (nodeName.equals("WsdlURI")) {
174                    parseWsdlURI((Element) child);
175                } else if (nodeName.equals("ServiceNameRef")) {
176                    parseServiceNameRef((Element) child);
177                } else {
178                    if (DiscoUtils.debug.messageEnabled()) {
179                        DiscoUtils.debug.message("Description(Element): "
180                            + "invalid node" + nodeName);
181                    }
182                    throw new DiscoveryException(
183                        DiscoUtils.bundle.getString("wrongInput"));
184                }
185            }
186        }
187        if ((mechID == null) || (mechID.size() == 0)) {
188            if (DiscoUtils.debug.messageEnabled()) {
189                DiscoUtils.debug.message("ServiceInstance(Element): missing "
190                    + "SecurityMechID element.");
191            }
192            throw new DiscoveryException(
193                DiscoUtils.bundle.getString("missingSecurityMechID"));
194        }
195
196        if ((soapEndpoint == null) && (wsdlURI == null)) {
197            if (DiscoUtils.debug.messageEnabled()) {
198                DiscoUtils.debug.message("ServiceInstance(Element): missing "
199                    + "WsdlRef or BriefSoapHttpDescription.");
200            }
201            throw new DiscoveryException(
202                DiscoUtils.bundle.getString("missingWsdlOrBrief"));
203        }
204    }
205
206    private void parseEndpoint(Element child) throws DiscoveryException {
207        if ((soapEndpoint != null) || (wsdlURI != null) ||
208            (serviceNameRef != null))
209        {
210            if (DiscoUtils.debug.messageEnabled()) {
211                DiscoUtils.debug.message("Description(Element): "
212                    + "included more Endpoint.");
213            }
214            throw new DiscoveryException(
215                DiscoUtils.bundle.getString("moreElement"));
216        }
217        soapEndpoint = XMLUtils.getElementValue((Element) child);
218        if ((soapEndpoint == null) || (soapEndpoint.length() == 0)) {
219            if (DiscoUtils.debug.messageEnabled()) {
220                DiscoUtils.debug.message("Description(Element): "
221                        + "missing Endpoint value.");
222            }
223            throw new DiscoveryException(
224                    DiscoUtils.bundle.getString("emptyElement"));
225        }
226    }
227
228    private void parseSoapAction(Element child) throws DiscoveryException {
229        if ((soapAction != null) || (wsdlURI != null) ||
230            (serviceNameRef != null))
231        {
232            if (DiscoUtils.debug.messageEnabled()) {
233                DiscoUtils.debug.message("Description(Element): "
234                    + "included more SoapAction.");
235            }
236            throw new DiscoveryException(
237                DiscoUtils.bundle.getString("moreElement"));
238        }
239        soapAction = XMLUtils.getElementValue((Element) child);
240        if ((soapAction == null) || (soapAction.length() == 0)) {
241            if (DiscoUtils.debug.messageEnabled()) {
242                DiscoUtils.debug.message("Description(Element): "
243                        + "missing SoapAction value.");
244            }
245            throw new DiscoveryException(
246                    DiscoUtils.bundle.getString("emptyElement"));
247        }
248    }
249
250    private void parseWsdlURI(Element child) throws DiscoveryException {
251        if ((soapEndpoint != null) || (wsdlURI != null) ||
252            (soapAction != null))
253        {
254            if (DiscoUtils.debug.messageEnabled()) {
255                DiscoUtils.debug.message("Description(Element): "
256                    + "included more WsdlURI.");
257            }
258            throw new DiscoveryException(
259                DiscoUtils.bundle.getString("moreElement"));
260        }
261        wsdlURI = XMLUtils.getElementValue((Element) child);
262        if ((wsdlURI == null) || (wsdlURI.length() == 0)) {
263            if (DiscoUtils.debug.messageEnabled()) {
264                DiscoUtils.debug.message("Description(Element): "
265                        + "missing WsdlURI value.");
266            }
267            throw new DiscoveryException(
268                    DiscoUtils.bundle.getString("emptyElement"));
269        }
270    }
271
272    private void parseServiceNameRef(Element child) throws DiscoveryException {
273        if ((soapEndpoint != null) || (serviceNameRef != null) ||
274            (soapAction != null))
275        {
276            if (DiscoUtils.debug.messageEnabled()) {
277                DiscoUtils.debug.message("Description(Element): "
278                    + "included more WsdlURI.");
279            }
280            throw new DiscoveryException(
281                DiscoUtils.bundle.getString("moreElement"));
282        }
283        String eleValue = XMLUtils.getElementValue(child);
284        if ((eleValue == null) || (eleValue.length() == 0)) {
285            if (DiscoUtils.debug.messageEnabled()) {
286                DiscoUtils.debug.message("Description(Element): "
287                        + "missing ServiceNameRef value.");
288            }
289            throw new DiscoveryException(
290                    DiscoUtils.bundle.getString("emptyElement"));
291        }
292        String localPart = eleValue;
293        String prefix = null;
294        String attrName = "xmlns";
295        if (eleValue.indexOf(":") != -1) {
296            StringTokenizer st = new StringTokenizer(localPart, ":");
297            if (st.countTokens() != 2) {
298                if (DiscoUtils.debug.messageEnabled()) {
299                    DiscoUtils.debug.message("Description(Element): "
300                        + "wrong ServiceNameRef value.");
301                }
302                throw new DiscoveryException(
303                    DiscoUtils.bundle.getString("wrongInput"));
304            }
305            prefix = st.nextToken();
306            attrName = attrName + ":" + prefix;
307            localPart = st.nextToken();
308        }
309        String namespaceURI = child.getAttribute(attrName);
310        if ((namespaceURI != null) && (namespaceURI.length() != 0)) {
311            if ((prefix != null) && (prefix.length() != 0)) {
312                serviceNameRef = new QName(namespaceURI, localPart, prefix);
313            } else {
314                serviceNameRef = new QName(namespaceURI, localPart);
315            }
316        } else {
317            serviceNameRef = new QName(localPart);
318        }
319    }
320
321    /**
322     * Gets id attribute.
323     *
324     * @return id attribute.
325     * @see #setId(String)
326     */
327    public String getId() {
328        return id;
329    }
330
331    /**
332     * Sets id attribute.
333     *
334     * @param id id attribute.
335     * @see #getId()
336     */
337    public void setId(String id) {
338        this.id = id;
339    }
340
341    /**
342     * Gets SOAP action.
343     *
344     * @return SOAP action.
345     * @see #setSoapAction(String)
346     */
347    public String getSoapAction() {
348        return soapAction;
349    }
350
351    /**
352     * Sets SOAP action.
353     * @param value SOAP action to be set 
354     * @see #getSoapAction()
355     */
356    public void setSoapAction(String value) {
357        soapAction = value;
358    }
359
360    /**
361     * Gets supported Security Mechanism IDs.
362     * 
363     * @return List of IDs as String for security mechanism 
364     * 
365     */
366    public List getSecurityMechID() {
367        return mechID;
368    }
369
370    /**
371     * Sets supported Security Mechanism IDs.
372     * 
373     * @param mechIDs List of IDs as String for security mechanism 
374     * 
375     */
376    public void getSecurityMechID(List mechIDs) {
377        mechID = mechIDs;
378    }
379
380    /**
381     * Gets WSDL service name reference.
382     *
383     * @return WSDL service name reference.
384     * @see #setServiceNameRef(QName)
385     */
386    public QName getServiceNameRef() {
387        return serviceNameRef;
388    }
389
390    /**
391     * Sets WSDL service name reference.
392     *
393     * @param nameRef service name reference. 
394     * @see #getServiceNameRef()
395     */
396    public void setServiceNameRef(QName nameRef) {
397        serviceNameRef = nameRef;
398    }
399
400    /**
401     * Gets URI to WSDL resource containing the service description.
402     *
403     * @return URI to WSDL resource containing the service description.
404     * @see #setWsdlURI(String)
405     */
406    public String getWsdlURI() {
407        return wsdlURI;
408    }
409
410    /**
411     * Sets URI to WSDL resource containing the service description.
412     *
413     * @param uri URI to the WSDL resource 
414     * @see #getWsdlURI()
415     */
416    public void setWsdlURI(String uri) {
417        wsdlURI = uri;
418    }
419
420    /**
421     * Gets the value of the <code>CredentialRef</code> property.
422     * 
423     * @return value of the <code>CredentialRef</code> property.
424     * @see #setCredentialRef(List)
425     */
426    public List getCredentialRef() {
427        return credentialRef;
428    }
429
430    /**
431     * Sets the value of the <code>CredentialRef</code> property.
432     * 
433     * @param refs List of String value of the <code>CredentialRef</code>
434     *                property.
435     * @see #getCredentialRef()
436     * 
437     */
438    public void setCredentialRef(List refs) {
439        credentialRef = refs;
440    }
441
442    /**
443     * Gets SOAP end point URI. 
444     * @return SOAP end point URI 
445     * @see #setEndpoint(String)
446     */
447    public String getEndpoint() {
448        return soapEndpoint;
449    }
450
451    /**
452     * Sets SOAP end point URI. 
453     * @param uri end point URI to be set 
454     * @see #getEndpoint()
455     */
456    public void setEndpoint(String uri) {
457        soapEndpoint = uri;
458    }
459
460    /**
461     * Returns formatted string of the service description.
462     *
463     * @return formatted string of the service description.
464     */ 
465    public String toString() {
466        StringBuffer sb = new StringBuffer(500);
467        sb.append("<Description xmlns=\"").append(DiscoConstants.DISCO_NS).
468                append("\"");
469        if ((id != null) && id.length() != 0) {
470            sb.append(" id=\"").append(id).append("\"");
471        }
472        sb.append(">");
473        if (mechID != null) {
474            Iterator iter = mechID.iterator();
475            while (iter.hasNext()) {
476                sb.append("<SecurityMechID>").append((String) iter.next()).
477                    append("</SecurityMechID>");
478            }
479        }
480        if (credentialRef != null) {
481            Iterator iter2 = credentialRef.iterator();
482            while (iter2.hasNext()) {
483                sb.append("<CredentialRef>").append((String) iter2.next()).
484                    append("</CredentialRef>");
485            }
486        }
487        if (soapEndpoint != null) {
488            sb.append("<Endpoint>").append(soapEndpoint).append("</Endpoint>");
489            if (soapAction != null) {
490                sb.append("<SoapAction>").append(soapAction).
491                        append("</SoapAction>");
492            }
493        } else {
494            sb.append("<WsdlURI>").append(wsdlURI).append("</WsdlURI>");
495            sb.append("<ServiceNameRef");
496            String prefix = null;
497            String namespace = serviceNameRef.getNamespaceURI();
498            if ((namespace != null) && namespace.length() != 0) {
499                sb.append(" xmlns:");
500                prefix = serviceNameRef.getPrefix();
501                if ((prefix == null) || prefix.length() == 0) {
502                    prefix = "ns1"; // our default
503                }
504                sb.append(prefix).append("=\"").append(namespace).append("\"");
505            }
506            sb.append(">");
507            if (prefix != null) {
508                sb.append(prefix).append(":");
509            }
510            sb.append(serviceNameRef.getLocalPart()).
511                append("</ServiceNameRef>");
512        }
513        sb.append("</Description>");
514        return sb.toString();
515    }
516}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.