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: PAOSResponse.java,v 1.3 2008/06/25 05:47:20 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.liberty.ws.paos;
031
032import com.sun.identity.liberty.ws.soapbinding.Utils;
033import com.sun.identity.shared.xml.XMLUtils;
034import org.w3c.dom.Document;
035import org.w3c.dom.Element;
036
037/**
038 * The <code>PAOSResponse</code> class is used by a web application on
039 * HTTP server side to receive and parse a <code>PAOS</code> response via an
040 * HTTP request from the user agent side.
041 *
042 * From this class, the original <code>PAOSRequest</code> object could obtained
043 * to correlate with this response.
044 *
045 * @supported.all.api
046 */
047public class PAOSResponse {
048    
049    private String refToMessageID = null;
050    private Boolean mustUnderstand;
051    private String actor;
052    
053    /**
054     * Constructs the <code>PAOSResponse</code> Object.
055     *
056     * @param refToMessageID the value of the refToMessageID attribute
057     * @param mustUnderstand the value of the mustUnderstand attribute
058     * @param actor the value of the actor attribute
059     * @throws PAOSException if <code>PAOSResponse</code> cannot be created.
060     */
061    public PAOSResponse(String refToMessageID, Boolean mustUnderstand,
062        String actor) throws PAOSException {
063
064        this.refToMessageID = refToMessageID;
065        this.mustUnderstand = mustUnderstand;
066        this.actor = actor;
067
068        validateData();
069    }
070
071    /**
072     * Constructs the <code>PAOSResponse</code> Object.
073     *
074     * @param element the Document Element of PAOS <code>Response</code> object.
075     * @throws PAOSException if <code>PAOSResponse</code> cannot be created.
076     */
077    public PAOSResponse(Element element) throws PAOSException {
078        parseElement(element);
079    }
080
081    /**
082     * Constructs the <code>PAOSResponse</code> Object.
083     *
084     * @param xmlString the XML String representation of this object.
085     * @throws PAOSException if <code>PAOSResponse</code> cannot be created.
086     */
087    public PAOSResponse(String xmlString) throws PAOSException {
088        Document xmlDocument =
089            XMLUtils.toDOMDocument(xmlString, PAOSUtils.debug);
090        if (xmlDocument == null) {
091            throw new PAOSException(
092                PAOSUtils.bundle.getString("errorPAOSResponseElement"));
093        }
094        parseElement(xmlDocument.getDocumentElement());
095    }
096
097    /**
098     * Returns the value of the refToMessageID attribute.
099     *
100     * @return the value of the refToMessageID attribute.
101     * @see #setRefToMessageID(String)
102     */
103    public String getRefToMessageID() {
104        return refToMessageID;
105    }
106    
107    /**
108     * Sets the value of the refToMessageID attribute.
109     *
110     * @param refToMessageID the value of the refToMessageID attribute
111     * @see #getRefToMessageID
112     */
113    public void setRefToMessageID(String refToMessageID) {
114        this.refToMessageID = refToMessageID;
115    }
116
117    /** 
118     * Returns value of <code>mustUnderstand</code> attribute.
119     *
120     * @return value of <code>mustUnderstand</code> attribute.
121     */
122    public Boolean isMustUnderstand() {
123        return mustUnderstand;
124    }
125    
126    /** 
127     * Sets the value of the <code>mustUnderstand</code> attribute.
128     *
129     * @param mustUnderstand the value of <code>mustUnderstand</code>
130     *     attribute.
131     */
132    public void setMustUnderstand(Boolean mustUnderstand) {
133        this.mustUnderstand = mustUnderstand;
134    }
135
136    /**
137     * Returns value of <code>actor</code> attribute.
138     *
139     * @return value of <code>actor</code> attribute
140     */
141    public String getActor() {
142        return actor;
143    }
144
145    /**
146     * Sets the value of <code>actor</code> attribute.
147     *
148     * @param actor the value of <code>actor</code> attribute
149     */
150    public void setActor(String actor) {
151        this.actor = actor;
152    }
153
154    /**
155     * Returns a String representation of this Object.
156     * @return a  String representation of this Object.
157     * @exception PAOSException if it could not create String object
158     */
159    public String toXMLString() throws PAOSException {
160        return toXMLString(true, false);
161    }
162
163    /**
164     *  Returns a String representation
165     *  @param includeNSPrefix determines whether or not the namespace
166     *      qualifier is prepended to the Element when converted
167     *  @param declareNS determines whether or not the namespace is declared
168     *      within the Element.
169     *  @return a String representation of this Object.
170     *  @exception PAOSException ,if it could not create String object.
171     */
172    public String toXMLString(boolean includeNSPrefix,boolean declareNS)
173        throws PAOSException {
174
175        validateData();
176
177        StringBuffer xml = new StringBuffer(300);
178
179        xml.append("<");
180        if (includeNSPrefix) {
181            xml.append(PAOSConstants.PAOS_PREFIX).append(":");
182        }
183        xml.append(PAOSConstants.RESPONSE);
184
185        if (declareNS) {
186            xml.append(" xmlns:").append(PAOSConstants.PAOS_PREFIX)
187               .append("=\"").append(PAOSConstants.PAOS_NAMESPACE)
188               .append("\" xmlns:").append(PAOSConstants.SOAP_ENV_PREFIX)
189               .append("=\"").append(PAOSConstants.SOAP_ENV_NAMESPACE)
190               .append("\"");
191        }
192        if (refToMessageID != null) {
193            xml.append(" ").append(PAOSConstants.REF_TO_MESSAGE_ID)
194               .append("=\"").append(refToMessageID).append("\"");
195        }
196        xml.append(" ").append(PAOSConstants.SOAP_ENV_PREFIX).append(":")
197           .append(PAOSConstants.MUST_UNDERSTAND).append("=\"")
198           .append(mustUnderstand.toString()).append("\"")
199           .append(" ").append(PAOSConstants.SOAP_ENV_PREFIX).append(":")
200           .append(PAOSConstants.ACTOR).append("=\"")
201           .append(actor).append("\"></");
202        if (includeNSPrefix) {
203            xml.append(PAOSConstants.PAOS_PREFIX).append(":");
204        }
205        xml.append(PAOSConstants.RESPONSE).append(">");
206
207        return xml.toString();
208    }
209
210    private void parseElement(Element element) throws PAOSException {
211        if (element == null) {
212            if (PAOSUtils.debug.messageEnabled()) {
213                PAOSUtils.debug.message("PAOSResponse.parseElement:" +
214                    " Input is null.");
215            }
216            throw new PAOSException(
217                PAOSUtils.bundle.getString("nullInput"));
218        }
219        String localName = element.getLocalName();
220        if (!PAOSConstants.RESPONSE.equals(localName)) {
221            if (PAOSUtils.debug.messageEnabled()) {
222                PAOSUtils.debug.message("PAOSResponse.parseElement:" +
223                    " element local name should be " + PAOSConstants.RESPONSE);
224            }
225            throw new PAOSException(
226                PAOSUtils.bundle.getString("invalidPAOSResponse"));
227        }
228        String namespaceURI = element.getNamespaceURI();
229        if (!PAOSConstants.PAOS_NAMESPACE.equals(namespaceURI)) {
230            if (PAOSUtils.debug.messageEnabled()) {
231                PAOSUtils.debug.message("PAOSResponse.parseElement:" +
232                    " element namespace should be " +
233                    PAOSConstants.PAOS_NAMESPACE);
234            }
235            throw new PAOSException(
236                PAOSUtils.bundle.getString("invalidPAOSNamesapce"));
237        }
238
239        refToMessageID = XMLUtils.getNodeAttributeValue(element,
240            PAOSConstants.REF_TO_MESSAGE_ID);
241
242        String str = XMLUtils.getNodeAttributeValueNS(element,
243            PAOSConstants.SOAP_ENV_NAMESPACE, PAOSConstants.MUST_UNDERSTAND);
244        try {
245            mustUnderstand = Utils.StringToBoolean(str);
246        } catch (Exception ex) {
247            throw new PAOSException(PAOSUtils.bundle.getString(
248                "invalidValueMustUnderstand"));
249        }
250        actor = XMLUtils.getNodeAttributeValueNS(element,
251            PAOSConstants.SOAP_ENV_NAMESPACE, PAOSConstants.ACTOR);
252
253        validateData();
254    }
255
256    private void validateData() throws PAOSException {
257        if (mustUnderstand == null) {
258            if (PAOSUtils.debug.messageEnabled()) {
259                PAOSUtils.debug.message("PAOSResponse.validateData: " +
260                    "mustUnderstand is missing in the paos:Response");
261            }
262            throw new PAOSException(PAOSUtils.bundle.getString(
263                "missingMustUnderstandPAOSResponse"));
264        }
265
266        if (actor == null) {
267            if (PAOSUtils.debug.messageEnabled()) {
268                PAOSUtils.debug.message("PAOSResponse.validateData: " +
269                    "actor is missing in the paos:Response");
270            }
271            throw new PAOSException(PAOSUtils.bundle.getString(
272                "missingActorPAOSResponse"));
273        }
274    }
275}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.