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: PAOSRequest.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>PAOSRequest</code> class is used by a web application on
039 * HTTP server side to construct a <code>PAOS</code> request message and send
040 * it via an HTTP response to the user agent side.
041 *
042 * @supported.all.api
043 */
044public class PAOSRequest {
045
046    private String responseConsumerURL;
047    private String service;
048    private String messageID;
049    private Boolean mustUnderstand;
050    private String actor;
051    
052    /**
053     * Constructs the <code>PAOSRequest</code> Object.
054     *
055     * @param responseConsumerURL the value of the responseConsumerURL
056     *     attribute
057     * @param service the value of the service attribute
058     * @param messageID the value of the messageID attribute
059     * @param mustUnderstand the value of the mustUnderstand attribute
060     * @param actor the value of the actor attribute
061     * @throws PAOSException if <code>PAOSRequest</code> cannot be created.
062     */
063    public PAOSRequest(String responseConsumerURL, String service,
064        String messageID, Boolean mustUnderstand, String actor)
065        throws PAOSException {
066
067        this.responseConsumerURL = responseConsumerURL;
068        this.service = service;
069        this.messageID = messageID;
070        this.mustUnderstand = mustUnderstand;
071        this.actor = actor;
072
073        validateData();
074    }
075
076    /**
077     * Constructs the <code>PAOSRequest</code> Object.
078     *
079     * @param element the Document Element of PAOS <code>Request</code> object.
080     * @throws PAOSException if <code>PAOSRequest</code> cannot be created.
081     */
082    public PAOSRequest(Element element) throws PAOSException {
083        parseElement(element);
084    }
085
086    /**
087     * Constructs the <code>PAOSRequest</code> Object.
088     *
089     * @param xmlString the XML String representation of this object.
090     * @throws PAOSException if <code>PAOSRequest</code> cannot be created.
091     */
092    public PAOSRequest(String xmlString) throws PAOSException {
093        Document xmlDocument =
094            XMLUtils.toDOMDocument(xmlString, PAOSUtils.debug);
095        if (xmlDocument == null) {
096            throw new PAOSException(
097                PAOSUtils.bundle.getString("errorPAOSRequestElement"));
098        }
099        parseElement(xmlDocument.getDocumentElement());
100    }
101
102    /**
103     * Returns the value of the responseConsumerURL attribute.
104     *
105     * @return the value of the responseConsumerURL attribute.
106     * @see #setResponseConsumerURL(String)
107     */
108    public String getResponseConsumerURL() {
109        return responseConsumerURL;
110    }
111
112    /**
113     * Sets the value of the responseConsumerURL attribute.
114     *
115     * @param responseConsumerURL the value of the responseConsumerURL
116     *     attribute
117     * @see #getResponseConsumerURL
118     */
119    public void setResponseConsumerURL(String responseConsumerURL) {
120        this.responseConsumerURL = responseConsumerURL;
121    }
122
123    /**
124     * Returns the value of the service attribute.
125     *
126     * @return the value of the service attribute.
127     * @see #setService(String)
128     */
129    public String getService() {
130        return service;
131    }
132
133    /**
134     * Sets the value of the service attribute.
135     *
136     * @param service the value of the service attribute
137     * @see #getService
138     */
139    public void setService(String service) {
140        this.service = service;
141    }
142
143    
144    /**
145     * Returns the value of the messageID attribute.
146     *
147     * @return the value of the messageID attribute.
148     * @see #setMessageID(String)
149     */
150    public String getMessageID() {
151        return messageID;
152    }
153    
154    /**
155     * Sets the value of the messageID attribute.
156     *
157     * @param messageID the value of the messageID attribute
158     * @see #getMessageID
159     */
160    public void setMessageID(String messageID) {
161        this.messageID = messageID;
162    }
163
164    /** 
165     * Returns value of <code>mustUnderstand</code> attribute.
166     *
167     * @return value of <code>mustUnderstand</code> attribute.
168     */
169    public Boolean isMustUnderstand() {
170        return mustUnderstand;
171    }
172    
173    /** 
174     * Sets the value of the <code>mustUnderstand</code> attribute.
175     *
176     * @param mustUnderstand the value of <code>mustUnderstand</code>
177     *     attribute.
178     */
179    public void setMustUnderstand(Boolean mustUnderstand) {
180        this.mustUnderstand = mustUnderstand;
181    }
182
183    /**
184     * Returns value of <code>actor</code> attribute.
185     *
186     * @return value of <code>actor</code> attribute
187     */
188    public String getActor() {
189        return actor;
190    }
191
192    /**
193     * Sets the value of <code>actor</code> attribute.
194     *
195     * @param actor the value of <code>actor</code> attribute
196     */
197    public void setActor(String actor) {
198        this.actor = actor;
199    }
200
201    /**
202     * Returns a String representation of this Object.
203     * @return a  String representation of this Object.
204     * @exception PAOSException if it could not create String object
205     */
206    public String toXMLString() throws PAOSException {
207        return toXMLString(true, false);
208    }
209
210    /**
211     *  Returns a String representation
212     *  @param includeNSPrefix determines whether or not the namespace
213     *      qualifier is prepended to the Element when converted
214     *  @param declareNS determines whether or not the namespace is declared
215     *      within the Element.
216     *  @return a String representation of this Object.
217     *  @exception PAOSException ,if it could not create String object.
218     */
219    public String toXMLString(boolean includeNSPrefix,boolean declareNS)
220        throws PAOSException {
221
222        validateData();
223
224        StringBuffer xml = new StringBuffer(300);
225
226        xml.append("<");
227        if (includeNSPrefix) {
228            xml.append(PAOSConstants.PAOS_PREFIX).append(":");
229        }
230        xml.append(PAOSConstants.REQUEST);
231
232        if (declareNS) {
233            xml.append(" xmlns:").append(PAOSConstants.PAOS_PREFIX)
234               .append("=\"").append(PAOSConstants.PAOS_NAMESPACE)
235               .append("\" xmlns:").append(PAOSConstants.SOAP_ENV_PREFIX)
236               .append("=\"").append(PAOSConstants.SOAP_ENV_NAMESPACE)
237               .append("\"");
238        }
239        xml.append(" ").append(PAOSConstants.RESPONSE_CONSUMER_URL)
240           .append("=\"").append(responseConsumerURL).append("\"")
241           .append(" ").append(PAOSConstants.SERVICE).append("=\"")
242           .append(service).append("\"");
243        if (messageID != null) {
244            xml.append(" ").append(PAOSConstants.MESSAGE_ID).append("=\"")
245               .append(messageID).append("\"");
246        }
247        xml.append(" ").append(PAOSConstants.SOAP_ENV_PREFIX).append(":")
248           .append(PAOSConstants.MUST_UNDERSTAND).append("=\"")
249           .append(mustUnderstand.toString()).append("\"")
250           .append(" ").append(PAOSConstants.SOAP_ENV_PREFIX).append(":")
251           .append(PAOSConstants.ACTOR).append("=\"")
252           .append(actor).append("\"></");
253        if (includeNSPrefix) {
254            xml.append(PAOSConstants.PAOS_PREFIX).append(":");
255        }
256        xml.append(PAOSConstants.REQUEST).append(">");
257
258        return xml.toString();
259    }
260
261    private void parseElement(Element element) throws PAOSException {
262        if (element == null) {
263            if (PAOSUtils.debug.messageEnabled()) {
264                PAOSUtils.debug.message("PAOSRequest.parseElement:" +
265                    " Input is null.");
266            }
267            throw new PAOSException(
268                PAOSUtils.bundle.getString("nullInput"));
269        }
270        String localName = element.getLocalName();
271        if (!PAOSConstants.REQUEST.equals(localName)) {
272            if (PAOSUtils.debug.messageEnabled()) {
273                PAOSUtils.debug.message("PAOSRequest.parseElement:" +
274                    " element local name should be " + PAOSConstants.REQUEST);
275            }
276            throw new PAOSException(
277                PAOSUtils.bundle.getString("invalidPAOSRequest"));
278        }
279        String namespaceURI = element.getNamespaceURI();
280        if (!PAOSConstants.PAOS_NAMESPACE.equals(namespaceURI)) {
281            if (PAOSUtils.debug.messageEnabled()) {
282                PAOSUtils.debug.message("PAOSRequest.parseElement:" +
283                    " element namespace should be " +
284                    PAOSConstants.PAOS_NAMESPACE);
285            }
286            throw new PAOSException(
287                PAOSUtils.bundle.getString("invalidPAOSNamesapce"));
288        }
289
290        responseConsumerURL = XMLUtils.getNodeAttributeValue(element,
291            PAOSConstants.RESPONSE_CONSUMER_URL);
292
293        service = XMLUtils.getNodeAttributeValue(element,
294            PAOSConstants.SERVICE);
295
296        messageID = XMLUtils.getNodeAttributeValue(element,
297            PAOSConstants.MESSAGE_ID);
298
299        String str = XMLUtils.getNodeAttributeValueNS(element,
300            PAOSConstants.SOAP_ENV_NAMESPACE, PAOSConstants.MUST_UNDERSTAND);
301        try {
302            mustUnderstand = Utils.StringToBoolean(str);
303        } catch (Exception ex) {
304            throw new PAOSException(PAOSUtils.bundle.getString(
305                "invalidValueMustUnderstand"));
306        }
307        actor = XMLUtils.getNodeAttributeValueNS(element,
308            PAOSConstants.SOAP_ENV_NAMESPACE, PAOSConstants.ACTOR);
309
310        validateData();
311    }
312
313    private void validateData() throws PAOSException {
314        if (responseConsumerURL == null) {
315            if (PAOSUtils.debug.messageEnabled()) {
316                PAOSUtils.debug.message("PAOSRequest.validateData: " +
317                    "responseConsumerURL is missing in the paos:Request");
318            }
319            throw new PAOSException(PAOSUtils.bundle.getString(
320                "missingResponseConsumerURLPAOSRequest"));
321        }
322
323        if (service == null) {
324            if (PAOSUtils.debug.messageEnabled()) {
325                PAOSUtils.debug.message("PAOSRequest.validateData: " +
326                    "service is missing in the paos:Request");
327            }
328            throw new PAOSException(PAOSUtils.bundle.getString(
329                "missingServicePAOSRequest"));
330        }
331
332        if (mustUnderstand == null) {
333            if (PAOSUtils.debug.messageEnabled()) {
334                PAOSUtils.debug.message("PAOSRequest.validateData: " +
335                    "mustUnderstand is missing in the paos:Request");
336            }
337            throw new PAOSException(PAOSUtils.bundle.getString(
338                "missingMustUnderstandPAOSRequest"));
339        }
340
341        if (actor == null) {
342            if (PAOSUtils.debug.messageEnabled()) {
343                PAOSUtils.debug.message("PAOSRequest.validateData: " +
344                    "actor is missing in the paos:Request");
345            }
346            throw new PAOSException(PAOSUtils.bundle.getString(
347                "missingActorPAOSRequest"));
348        }
349    }
350}