/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.muse.ws.notification.impl;
import java.text.ParseException;
import java.util.Date;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.apache.muse.util.messages.Messages;
import org.apache.muse.util.messages.MessagesFactory;
import org.apache.muse.util.xml.XmlSerializable;
import org.apache.muse.util.xml.XmlUtils;
import org.apache.muse.util.xml.XsdUtils;
import org.apache.muse.ws.addressing.EndpointReference;
import org.apache.muse.ws.addressing.soap.SoapFault;
import org.apache.muse.ws.notification.WsnConstants;
import org.apache.muse.ws.resource.WsResource;
import org.apache.muse.ws.resource.ext.faults.InvalidMessageFormatFault;
/**
*
* SubscribeResponse is a serializer/deserializer for the WS-Notification
* Subscribe operation's response content.
*
* @author Dan Jemiolo (danj)
*
*/
public class SubscribeResponse implements XmlSerializable
{
//
// Used to look up all exception messages
//
private static Messages _MESSAGES = MessagesFactory.get(SubscribeResponse.class);
private Date _currentTime = new Date();
private EndpointReference _subscriptionEPR = null;
private Date _terminationTime = null;
public SubscribeResponse(Element xml)
throws InvalidMessageFormatFault
{
Element eprXML = XmlUtils.getElement(xml, WsnConstants.SUBSCRIPTION_EPR_QNAME);
if (eprXML == null)
throw new InvalidMessageFormatFault(_MESSAGES.get("NoSubscriptionEPR"));
try
{
_subscriptionEPR = new EndpointReference(eprXML);
}
catch (SoapFault error)
{
throw new InvalidMessageFormatFault(error);
}
String currentString = XmlUtils.getElementText(xml, WsnConstants.CURRENT_TIME_QNAME);
String terminationString = XmlUtils.getElementText(xml, WsnConstants.TERMINATION_TIME_QNAME);
try
{
if (currentString != null)
_currentTime = XsdUtils.getLocalTime(currentString);
if (terminationString != null)
_terminationTime = XsdUtils.getLocalTime(terminationString);
}
catch (ParseException error)
{
throw new InvalidMessageFormatFault(error);
}
}
public SubscribeResponse(WsResource subscription, Date terminationTime)
{
_subscriptionEPR = subscription.getEndpointReference();
_terminationTime = terminationTime;
}
public Date getCurrentTime()
{
return _currentTime;
}
public EndpointReference getSubscriptionReference()
{
return _subscriptionEPR;
}
public Date getTerminationTime()
{
return _terminationTime;
}
public Element toXML()
{
return toXML(XmlUtils.EMPTY_DOC);
}
public Element toXML(Document doc)
{
Element root = XmlUtils.createElement(doc, WsnConstants.SUBSCRIBE_RESPONSE_QNAME);
Element eprXML = getSubscriptionReference().toXML();
XmlUtils.setElement(root, WsnConstants.SUBSCRIPTION_EPR_QNAME, eprXML);
Date currentTime = getCurrentTime();
Date termTime = getTerminationTime();
if (currentTime != null)
XmlUtils.setElement(root, WsnConstants.CURRENT_TIME_QNAME, currentTime);
if (termTime != null)
XmlUtils.setElement(root, WsnConstants.TERMINATION_TIME_QNAME, termTime);
return root;
}
}