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: DSTQueryItem.java,v 1.2 2008/06/25 05:47:13 qcheng Exp $
026 *
027 */
028
029package com.sun.identity.liberty.ws.dst;
030
031import java.text.ParseException;
032import java.util.Date;
033import java.util.StringTokenizer;
034import com.sun.identity.shared.DateUtils;
035import com.sun.identity.shared.xml.XMLUtils;
036import org.w3c.dom.NodeList;
037import org.w3c.dom.Element;
038
039/**
040 * The class <code>DSTQueryItem</code> is the wrapper for one query item
041 * for Data service.
042 * The following schema fragment specifies the expected content within the
043 * <code>DSTQueryItem</code> object.
044 * <pre>
045 * &lt;element name="QueryItem" maxOccurs="unbounded">
046 *   &lt;complexType>
047 *     &lt;complexContent>
048 *       &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
049 *         &lt;sequence>
050 *           &lt;element name="Select"
051 *           type="{urn:liberty:id-sis-pp:2003-08}SelectType"/>
052 *         &lt;/sequence>
053 *         &lt;attribute name="itemID"
054 *         type="{urn:liberty:id-sis-pp:2003-08}IDType" />
055 *         &lt;attribute name="changedSince"
056 *         type="{http://www.w3.org/2001/XMLSchema}dateTime" />
057 *         &lt;attribute name="includeCommonAttributes" 
058 *         type="{http://www.w3.org/2001/XMLSchema}boolean" />
059 *         &lt;attribute name="id" 
060 *         type="{http://www.w3.org/2001/XMLSchema}ID" />
061 *       &lt;/restriction>
062 *     &lt;/complexContent>
063 *   &lt;/complexType>
064 * &lt;/element>
065 * </pre>
066 *
067 * @supported.all.api
068 */
069public class DSTQueryItem {
070
071    private String select; 
072    private boolean includeCommonAttribute;
073    private Date changedSince;
074    private String itemID = null;
075    private String id = null;
076    private String nameSpaceURI = null;
077    private String prefix = null;
078
079    /**
080     * Constructor
081     * @param select specifies the data the query  should return 
082     * @param serviceNS service Name space
083     */
084    public DSTQueryItem (String select, String serviceNS) { 
085        this.select = select;
086        this.nameSpaceURI = serviceNS;
087    }
088   
089    /**
090     * Constructor
091     * @param select specifies the data the query  should return 
092     * @param includeCommonAttribute if true, query response will 
093     *        contains common attributes (attribute id and modification 
094     *        time)
095     * @param changedSince Only match entries changed after the specified
096     *        date
097     * @param serviceNS service Name space
098     */
099    public DSTQueryItem (String select, 
100                         boolean includeCommonAttribute,
101                         Date changedSince,
102                         String serviceNS) {
103        this.select = select;
104        this.includeCommonAttribute = includeCommonAttribute;
105        this.changedSince = changedSince; 
106        this.nameSpaceURI = serviceNS;
107    }
108
109    /**
110     * Constructor
111     * @param element <code>DOM</code> Element 
112     * @throws DSTException
113     */
114    public DSTQueryItem(org.w3c.dom.Element element) throws DSTException{
115        if(element == null) {
116           DSTUtils.debug.error("DSTQueryItem(element):null input");
117           throw new DSTException(DSTUtils.bundle.getString("nullInputParams"));
118        }
119        String elementName = element.getLocalName();
120        if(elementName == null || !elementName.equals("QueryItem")) {
121           DSTUtils.debug.error("DSTQueryItem(element):Invalid elementName");
122           throw new DSTException(DSTUtils.bundle.getString("invalidElement"));
123        }
124        nameSpaceURI = element.getNamespaceURI();
125        if(nameSpaceURI == null) {
126           DSTUtils.debug.error("DSTQueryItem(element): Namespace is null");
127           throw new DSTException(DSTUtils.bundle.getString("noNameSpace"));
128        }
129        prefix = element.getPrefix();
130        id = element.getAttribute("id");
131        String attr = element.getAttribute("includeCommonAttributes");
132        if(attr != null) {
133           includeCommonAttribute = Boolean.valueOf(attr).booleanValue();
134        }
135        attr = element.getAttribute("changedSince");
136
137        if (attr != null && attr.length() != 0) {
138            try {
139                changedSince = DateUtils.stringToDate(attr);
140            } catch(ParseException ex) {
141                DSTUtils.debug.error(
142                    "DSTQueryItem(element): date can not be parsed.", ex);
143            }
144        }
145
146        NodeList list = element.getElementsByTagNameNS(
147                        nameSpaceURI, "Select");
148
149        if((list.getLength() != 1)) {
150           DSTUtils.debug.error("DSTQueryItem(element): Select is null" +
151           " or more than one select found.");
152           throw new DSTException(
153           DSTUtils.bundle.getString("invalidSelect"));
154        }
155        select = XMLUtils.getElementValue((Element)list.item(0));
156        if(select == null) {
157           DSTUtils.debug.error("DSTQueryItem(element): Select is null" );
158           throw new DSTException(
159           DSTUtils.bundle.getString("invalidSelect"));
160        }
161    }
162
163
164    /**
165     * Returns data selection string 
166     * @return String
167     */
168    public String getSelect() {
169        return select;
170    }
171
172    /**
173     * Gets <code>itemID</code> attribute
174     * @return String
175     */
176    public String getItemID() {
177        return itemID;
178    }
179
180    /**
181     * Sets <code>itemID</code> attribute
182     * @param itemID item ID to be set
183     */
184     public void setItemID(String itemID) {
185         this.itemID = itemID;
186     }
187
188    /**
189     * Gets id attribute.
190     *
191     * @return id attribute.
192     */
193    public String getId() {
194        return id;
195    }
196
197    /**
198     * Sets id attribute
199     * @param id id attribute to be set
200     */
201    public void setId(String id) {
202        this.id = id;
203    }
204    
205    /**
206     * Checks include common attribute for the <code>DST</code> query item.
207     * @return boolean
208     */
209    public boolean isIncludeCommonAttributes() { 
210        return includeCommonAttribute;
211    }
212
213   /**
214    * Gets changed since attribute
215    * @return Date
216    */
217    public Date getChangedSince() {
218        return changedSince;
219    }
220
221    /**
222     * Gets the name space.
223     * @return Name space.
224     */
225    public java.lang.String getNameSpaceURI() {
226        return nameSpaceURI;
227    }
228
229    /**
230     * Sets the name space.
231     *
232     * @param nameSpace Name space URI.
233     */
234    public void setNameSpaceURI(String nameSpace) {
235        this.nameSpaceURI = nameSpace;
236    }
237
238    /**
239     * Sets the name space prefix.
240     * @param prefix Name space prefix.
241     */
242    public void setNameSpacePrefix(String prefix) {
243        this.prefix = prefix;
244    }
245
246    /**
247     * Gets the name space prefix.
248     * @return String Name space prefix.
249     */
250    public java.lang.String getNameSpacePrefix() {
251        return prefix;
252    }
253
254    /**
255     * Creates a String representation of this object.
256     * By default name space name is prepended to the element name
257     * @return String A string containing the valid XML for this element
258     */
259    public java.lang.String toString() {
260        return toString(true, false);
261    }
262
263    /**
264     * Creates a String representation of this object.
265     * @param includeNS if true prepends all elements by their Name space prefix
266     * @param declareNS if true includes the Name space within the
267     *                  generated.
268     * @return String A string containing the valid XML for this element
269     */
270    public java.lang.String toString(boolean includeNS, boolean declareNS) {
271
272        if(select == null) {
273           DSTUtils.debug.error("DSTQueryItem.toString: Select cannot be null");
274           return "";
275        }
276        String tempPrefix = "";
277        if(includeNS) { 
278           if(prefix == null) {
279              prefix = DSTConstants.DEFAULT_NS_PREFIX;
280           }
281           tempPrefix = prefix + ":";   
282        }
283        if (declareNS && nameSpaceURI == null) {
284            DSTUtils.debug.error("DSTQueryItem.toString: Name Space is " +
285              "not defined");
286            return "";
287        }
288        StringBuffer sb = new StringBuffer(300);
289        sb.append("<").append(tempPrefix).append("QueryItem");
290        if(id != null && id.length() != 0) {
291           sb.append(" id=\"").append(id).append("\"");
292        }
293        sb.append(" includeCommonAttributes=\"");
294        if(includeCommonAttribute) {
295           sb.append("true").append("\"");
296        } else {
297           sb.append("false").append("\"");
298        }
299        if(itemID != null && itemID.length() != 0) {
300           sb.append(" itemID=\"").append(itemID).append("\"");
301        }
302
303        if (changedSince != null) {
304            sb.append(" changedSince=\"")
305                .append(DateUtils.toUTCDateFormat(changedSince))
306                .append("\"");
307        }
308
309        if(declareNS) {
310           sb.append(" xmlns:").append(prefix).append("=\"")
311             .append(nameSpaceURI).append("\"")
312             .append(" xmlns=\"").append(nameSpaceURI).append("\"");
313        }
314        sb.append(">").append("<").append(tempPrefix).append("Select")
315          .append(">").append(appendPrefix(select, prefix)).append("</")
316          .append(tempPrefix).append("Select").append(">")
317          .append("</").append(tempPrefix).append("QueryItem").append(">");
318
319        return sb.toString();
320    }
321
322    private String appendPrefix(String select, String prefix) {
323        if(select.indexOf(":") != -1) {
324           // prefix is already defined.
325           return select;
326        }
327        StringBuffer sb = new StringBuffer(100);
328        StringTokenizer st = new StringTokenizer(select, "/");
329        while(st.hasMoreTokens()) {
330           String temp = (String)st.nextToken();
331           temp = "/" + prefix + ":" + temp;
332           sb.append(temp);
333        }
334        return sb.toString();
335    }
336
337}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.