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