/*=============================================================================*
* Copyright 2006 The Apache Software Foundation
*
* Licensed 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.ws.addressing.EndpointReference;
import org.apache.muse.ws.notification.Filter;
import org.apache.muse.ws.notification.WsnConstants;
import org.apache.muse.ws.notification.faults.*;
import org.apache.muse.ws.notification.topics.WstConstants;
import org.apache.muse.ws.resource.WsResource;
import org.apache.muse.ws.resource.ext.faults.InvalidMessageFormatFault;
import org.apache.muse.ws.addressing.soap.SoapFault;
import org.apache.muse.util.xml.XmlSerializable;
import org.apache.muse.util.xml.XmlUtils;
/**
*
* Subscribe is a serializer/deserializer for the WS-Notification Subscribe
* operation's request content.
*
* @author Dan Jemiolo (danj)
*
*/
public class Subscribe implements XmlSerializable
{
//
// Used to look up all exception messages
//
private static Messages _MESSAGES = MessagesFactory.get(Subscribe.class);
private EndpointReference _consumer = null;
private Filter _filter = null;
private Date _terminationTime = null;
public Subscribe(Element xml, WsResource resource)
throws InvalidMessageFormatFault,
InvalidFilterFault,
TopicExpressionDialectUnknownFault,
InvalidTopicExpressionFault,
InvalidProducerPropertiesExpressionFault,
InvalidMessageContentExpressionFault
{
Element eprXML = XmlUtils.getElement(xml, WsnConstants.CONSUMER_QNAME);
if (eprXML == null)
throw new NullPointerException(_MESSAGES.get("NullConsumerElement"));
try
{
_consumer = new EndpointReference(eprXML);
}
catch (SoapFault error)
{
throw new InvalidMessageFormatFault(error);
}
Element filterXML = XmlUtils.getElement(xml, WsnConstants.FILTER_QNAME);
FilterFactory factory = FilterFactory.getInstance();
_filter = factory.newInstance(filterXML, resource);
Element timeXML = XmlUtils.getElement(xml, WsnConstants.INIT_TERMINATION_TIME_QNAME);
try
{
if (timeXML != null)
_terminationTime = XmlUtils.getDate(timeXML);
}
catch (ParseException error)
{
Object[] filler = { error.getMessage() };
throw new InvalidMessageFormatFault(_MESSAGES.get("InvalidTerminationTime", filler));
}
}
public Subscribe(EndpointReference consumer, Filter filter, Date terminationTime)
{
if (consumer == null)
throw new NullPointerException(_MESSAGES.get("NullConsumerEPR"));
_consumer = new EndpointReference(consumer, WsnConstants.CONSUMER_QNAME);
_filter = filter;
_terminationTime = terminationTime;
}
public EndpointReference getConsumerReference()
{
return _consumer;
}
protected String getDialect(String name)
{
//
// concrete expressions will be paths/with/slashes
//
return name.indexOf('/') >= 0 ? WstConstants.CONCRETE_TOPIC_URI : WstConstants.SIMPLE_TOPIC_URI;
}
public Filter getFilter()
{
return _filter;
}
public Date getTerminationTime()
{
return _terminationTime;
}
public Object[] toArray()
{
//
// use null for the policy - our impl doesn't support policy
//
return new Object[]{
getConsumerReference(), getFilter(), getTerminationTime(), null
};
}
public Element toXML()
{
return toXML(XmlUtils.EMPTY_DOC);
}
public Element toXML(Document doc)
{
if (doc == null)
throw new NullPointerException(_MESSAGES.get("NullDocument"));
Element root = XmlUtils.createElement(doc, WsnConstants.SUBSCRIBE_QNAME);
EndpointReference consumer = getConsumerReference();
XmlUtils.setElement(root, WsnConstants.CONSUMER_QNAME, consumer);
Filter filter = getFilter();
if (filter != null)
XmlUtils.setElement(root, WsnConstants.FILTER_QNAME, filter);
Date termination = getTerminationTime();
if (termination != null)
XmlUtils.setElement(root, WsnConstants.INIT_TERMINATION_TIME_QNAME, termination);
return root;
}
}