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: RequestedService.java,v 1.2 2008/06/25 05:47:11 qcheng Exp $ 026 * 027 */ 028 029 030package com.sun.identity.liberty.ws.disco; 031 032import java.util.Iterator; 033import java.util.List; 034import org.w3c.dom.*; 035import com.sun.identity.liberty.ws.disco.common.DiscoConstants; 036import com.sun.identity.liberty.ws.disco.common.DiscoUtils; 037import com.sun.identity.shared.xml.XMLUtils; 038 039/** 040 * The class <code>RequestedService</code> enables the requester to specify 041 * that all the resource offerings returned must be offered via a service 042 * instance complying with one of the specified service type. 043 * <p>The following schema fragment specifies the expected content 044 * within the <code>RequestedService</code> object. 045 * <p> 046 * <pre> 047 * <complexType> 048 * <complexContent> 049 * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 050 * <sequence> 051 * <element ref="{urn:liberty:disco:2003-08}ServiceType"/> 052 * <element ref="{urn:liberty:disco:2003-08}Options" minOccurs="0"/> 053 * * </sequence> 054 * </restriction> 055 * </complexContent> 056 * </complexType> 057 * </pre> 058 * <p> 059 * <pre> 060 * In this implementation, the value of Options has the following meanings: 061 * When the List of options is null, no Options element will be created; 062 * When the List of options is an empty List, or is Collection.EMPTY_LIST, 063 * empty Options element <Options></Options> will be created; 064 * When the List of options is not empty, 065 * Options element with child Option element(s) will be created. 066 * </pre> 067 * @supported.all.api 068 */ 069public class RequestedService { 070 071 private List options = null; 072 private String serviceType = null; 073 074 /** 075 * Constructor. 076 * @param options List of String, each is a URI specifying an option the 077 * returned resource offering should support. 078 * @param serviceType URI specifying the type of service to be returned 079 */ 080 public RequestedService (java.util.List options, 081 java.lang.String serviceType) 082 { 083 this.options = options; 084 this.serviceType = serviceType; 085 } 086 087 /** 088 * Constructor. 089 * @param elem <code>RequestedService</code> DOM element 090 * @exception DiscoveryException if error occurs 091 */ 092 public RequestedService(Element elem) throws DiscoveryException { 093 if (elem == null) { 094 DiscoUtils.debug.message("RequestedService(Element): null input."); 095 throw new DiscoveryException( 096 DiscoUtils.bundle.getString("nullInput")); 097 } 098 String nodeName; 099 String nameSpaceURI; 100 if (((nodeName = elem.getLocalName()) == null) || 101 (!nodeName.equals("RequestedServiceType")) || 102 ((nameSpaceURI = elem.getNamespaceURI()) == null) || 103 (!nameSpaceURI.equals(DiscoConstants.DISCO_NS))) 104 { 105 DiscoUtils.debug.message("RequestedService(Element): wrong input"); 106 throw new DiscoveryException( 107 DiscoUtils.bundle.getString("wrongInput")); 108 } 109 110 NodeList contentnl = elem.getChildNodes(); 111 Node child; 112 for (int i = 0, length = contentnl.getLength(); i < length; i++) { 113 child = contentnl.item(i); 114 if ((nodeName = child.getLocalName()) != null) { 115 nameSpaceURI = ((Element) child).getNamespaceURI(); 116 if ((nameSpaceURI == null) || 117 (!nameSpaceURI.equals(DiscoConstants.DISCO_NS))) 118 { 119 if (DiscoUtils.debug.messageEnabled()) { 120 DiscoUtils.debug.message("RequestedService(Element): " 121 + "invalid namespace for node " + nodeName); 122 } 123 throw new DiscoveryException( 124 DiscoUtils.bundle.getString("wrongInput")); 125 } 126 if (nodeName.equals("ServiceType")) { 127 if (serviceType != null) { 128 if (DiscoUtils.debug.messageEnabled()) { 129 DiscoUtils.debug.message("RequestedService(Element)" 130 + ": Included more than one ServiceType " 131 + "element."); 132 } 133 throw new DiscoveryException( 134 DiscoUtils.bundle.getString("moreElement")); 135 } 136 serviceType = XMLUtils.getElementValue((Element)child); 137 if ((serviceType == null) || (serviceType.length() == 0)) { 138 if (DiscoUtils.debug.messageEnabled()) { 139 DiscoUtils.debug.message("RequestedService(Element)" 140 + ": missing ServiceType element value."); 141 } 142 throw new DiscoveryException( 143 DiscoUtils.bundle.getString("emptyElement")); 144 } 145 } else if (nodeName.equals("Options")) { 146 if (options != null) { 147 if (DiscoUtils.debug.messageEnabled()) { 148 DiscoUtils.debug.message("RequestedService(Element)" 149 + ": Included more than one Options element."); 150 } 151 throw new DiscoveryException( 152 DiscoUtils.bundle.getString("moreElement")); 153 } 154 options = DiscoUtils.parseOptions((Element) child); 155 } else { 156 if (DiscoUtils.debug.messageEnabled()) { 157 DiscoUtils.debug.message("RequestedService(Element): " 158 + "invalid node" + nodeName); 159 } 160 throw new DiscoveryException( 161 DiscoUtils.bundle.getString("wrongInput")); 162 } 163 } 164 } 165 166 if (serviceType == null) { 167 if (DiscoUtils.debug.messageEnabled()) { 168 DiscoUtils.debug.message("RequestedService(Element): missing " 169 + "ServiceType element."); 170 } 171 throw new DiscoveryException( 172 DiscoUtils.bundle.getString("missingServiceType")); 173 } 174 } 175 176 /** 177 * Gets list of options. 178 * @return List of options 179 * @see #setOptions(List) 180 */ 181 public List getOptions() { 182 return options; 183 } 184 185 /** 186 * Sets options. 187 * @param options List of option to be set 188 * @see #getOptions() 189 */ 190 public void setOptions(List options) { 191 this.options = options; 192 } 193 194 /** 195 * Gets service type. 196 * @return service type String 197 * @see #setServiceType(String) 198 */ 199 public String getServiceType() { 200 return serviceType; 201 } 202 203 /** 204 * Sets service type. 205 * @param serviceType String 206 * @see #getServiceType() 207 */ 208 public void setServiceType(String serviceType) { 209 this.serviceType = serviceType; 210 } 211 212 /** 213 * Returns string format of object <code>RequestedServiceType</code>. 214 * 215 * @return formatted string. 216 */ 217 public java.lang.String toString() { 218 StringBuffer sb = new StringBuffer(1000); 219 sb.append("<RequestedServiceType xmlns=\""). 220 append(DiscoConstants.DISCO_NS).append("\"><ServiceType>"); 221 if (serviceType != null) { 222 sb.append(serviceType); 223 } 224 sb.append("</ServiceType>"); 225 if (options != null) { 226 sb.append("<Options>"); 227 if (!options.isEmpty()) { 228 Iterator iter = options.iterator(); 229 String option = null; 230 while (iter.hasNext()) { 231 option = (String) iter.next(); 232 if ((option != null) && option.length() != 0) { 233 sb.append("<Option>").append(option). 234 append("</Option>"); 235 } 236 } 237 } 238 sb.append("</Options>"); 239 } 240 sb.append("</RequestedServiceType>"); 241 return sb.toString(); 242 } 243}