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: CorrelationHeader.java,v 1.3 2008/06/25 05:47:22 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.liberty.ws.soapbinding; 
031
032
033import java.text.ParseException;
034
035import java.util.Date;
036
037import org.w3c.dom.Document;
038import org.w3c.dom.Element;
039
040import com.sun.identity.shared.DateUtils;
041import com.sun.identity.shared.xml.XMLUtils;
042import com.sun.identity.saml.common.SAMLUtils;
043
044/**
045 * The <code>CorrelationHeader</code> class represents <code>Correlation</code>
046 * element defined in SOAP binding schema. The <code>messageID</code> is a 
047 * required attribute and will be generated automatically when a constructor
048 * is called.
049 *
050 * @supported.all.api
051 */
052
053public class CorrelationHeader {   
054    private String messageID = null;
055    private String id = null;
056    private String refToMessageID = null;
057    private Date timestamp = null;
058    private Boolean mustUnderstand = null;
059    private String actor = null;
060
061    /**
062     * Default Construtor
063     */
064    public CorrelationHeader() {
065        messageID = SAMLUtils.generateID();
066        id = messageID;
067        timestamp = new Date();
068        actor = SOAPBindingConstants.DEFAULT_SOAP_ACTOR;
069        mustUnderstand = Boolean.TRUE;
070    }
071
072    /**
073     * This constructor takes a <code>org.w3c.dom.Element</code>.
074     *
075     * @param correlationElement a Correlation element
076     * @throws SOAPBindingException if an error occurs while parsing
077     *                              the Correlation element
078     */
079    CorrelationHeader(Element correlationElement) throws SOAPBindingException {
080        messageID = XMLUtils.getNodeAttributeValue(
081                                    correlationElement,
082                                    SOAPBindingConstants.ATTR_MESSAGE_ID);
083        if (messageID == null) {
084            String msg = Utils.bundle.getString("missingMessageID");
085            Utils.debug.error("CorrelationHeader: " + msg);
086            throw new SOAPBindingException(msg);
087        }
088
089        id = XMLUtils.getNodeAttributeValue(
090                                     correlationElement,
091                                     SOAPBindingConstants.ATTR_id);
092        refToMessageID = XMLUtils.getNodeAttributeValue(
093                                correlationElement,
094                                SOAPBindingConstants.ATTR_REF_TO_MESSAGE_ID);
095        String str = XMLUtils.getNodeAttributeValueNS(
096                                     correlationElement,
097                                     SOAPBindingConstants.NS_SOAP,
098                                     SOAPBindingConstants.ATTR_MUSTUNDERSTAND);
099        if (str != null && str.length() > 0) {
100            try {
101                mustUnderstand = Utils.StringToBoolean(str);
102            } catch (Exception pe) {
103                String msg = Utils.bundle.getString("invalidMustUnderstand");
104                Utils.debug.error("CorrelationHeader: " + msg, pe);
105                throw new SOAPBindingException(msg);
106            }
107        }
108
109        str = XMLUtils.getNodeAttributeValue(
110                                     correlationElement,
111                                     SOAPBindingConstants.ATTR_TIMESTAMP);
112        if (str == null || str.length() == 0) {
113            String msg = Utils.bundle.getString("missingCorrelationTimestamp");
114            Utils.debug.error("CorrelationHeader: " + msg);
115            throw new SOAPBindingException(msg);
116        }
117        try {
118            timestamp = DateUtils.stringToDate(str);
119        } catch (ParseException pe) {
120            String msg = Utils.bundle.getString("invalidTimestamp");
121            Utils.debug.error("CorrelationHeader: " + msg, pe);
122            throw new SOAPBindingException(msg);
123        }
124        actor = XMLUtils.getNodeAttributeValueNS(
125                                       correlationElement,
126                                       SOAPBindingConstants.NS_SOAP,
127                                       SOAPBindingConstants.ATTR_ACTOR);
128    }
129
130    /**
131     * Returns value of <code>messageID</code> attribute.
132     *
133     * @return value of <code>messageID</code> attribute
134     */
135    public String getMessageID() {
136        return messageID;
137    }
138
139    /**
140     * Returns value of <code>refToMessageID</code> attribute.
141     *
142     * @return value of <code>refToMessageID</code> attribute
143     */
144    public String getRefToMessageID() {
145        return refToMessageID;
146    }
147
148    /**
149     * Returns value of <code>timestamp</code> attribute.
150     *
151     * @return value of <code>timestamp</code> attribute
152     */
153    public Date getTimestamp() {
154        return timestamp;
155    }
156
157    /**
158     * Returns value of <code>id</code> attribute.
159     *
160     * @return value of <code>id</code> attribute
161     */
162    public String getId() {
163        return messageID;
164    }
165
166    /**
167     * Returns value of <code>mustUnderstand</code> attribute.
168     *
169     * @return value of <code>mustUnderstand</code> attribute
170     */
171    public Boolean getMustUnderstand() {
172        return mustUnderstand;
173    }
174
175    /**
176     * Returns value of <code>actor</code> attribute.
177     *
178     * @return value of <code>actor</code> attribute
179     */
180    public String getActor() {
181        return actor;
182    }
183
184    /**
185     * Sets value of <code>mustUnderstand</code> attribute.
186     *
187     * @param mustUnderstand value of <code>mustUnderstand</code> attribute
188     */
189    public void setMustUnderstand(Boolean mustUnderstand) {
190        this.mustUnderstand = mustUnderstand;
191    }
192
193    /**
194     * Sets value of <code>actor</code> attribute.
195     *
196     * @param actor value of <code>actor</code> attribute
197     */
198    public void setActor(String actor) {
199        this.actor = actor;
200    }
201
202    /**
203     * Sets value of <code>refToMessageID</code> attribute.
204     *
205     * @param refToMessageID value of <code>refToMessageID</code> attribute
206     *
207     */
208    public void setRefToMessageID(String refToMessageID) {
209        this.refToMessageID = refToMessageID;
210    }
211
212    /**
213     * Converts this header to <code>org.w3c.dom.Element</code> and add to
214     * parent Header Element.
215     *
216     * @param headerE parent Header Element
217     */
218    public void addToParent(Element headerE) {
219        Document doc = headerE.getOwnerDocument();
220        Element correlationHeaderE = doc.createElementNS(
221                                        SOAPBindingConstants.NS_SOAP_BINDING,
222                                        SOAPBindingConstants.PTAG_CORRELATION);
223        headerE.appendChild(correlationHeaderE);
224        correlationHeaderE.setAttributeNS(null,
225                                          SOAPBindingConstants.ATTR_MESSAGE_ID,
226                                          messageID);
227        if (refToMessageID != null) {
228            correlationHeaderE.setAttributeNS(null,
229                                  SOAPBindingConstants.ATTR_REF_TO_MESSAGE_ID,
230                                  refToMessageID);
231        }
232
233        correlationHeaderE.setAttributeNS(null,
234            SOAPBindingConstants.ATTR_TIMESTAMP,
235            DateUtils.toUTCDateFormat(timestamp));
236
237        correlationHeaderE.setAttributeNS(null, SOAPBindingConstants.ATTR_id,
238                                          messageID);
239        if (mustUnderstand != null) {
240            correlationHeaderE.setAttributeNS(SOAPBindingConstants.NS_SOAP,
241                                    SOAPBindingConstants.PATTR_MUSTUNDERSTAND,
242                                    Utils.BooleanToString(mustUnderstand));
243        }
244        if (actor != null) {
245            correlationHeaderE.setAttributeNS(SOAPBindingConstants.NS_SOAP,
246                                              SOAPBindingConstants.PATTR_ACTOR,
247                                              actor);
248        }
249    }
250}