/*=============================================================================*
* Copyright 2004 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.ws.eventing.pubsub;
import org.apache.axis.AxisProperties;
import org.apache.axis.configuration.EngineConfigurationFactoryDefault;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.XmlObjectWrapper;
import org.apache.ws.addressing.EndpointReference;
import org.apache.ws.addressing.XmlBeansEndpointReference;
import org.apache.ws.pubsub.NotificationConsumer;
import org.apache.ws.pubsub.NotificationProducer;
import org.apache.ws.pubsub.SubscriptionEndConsumer;
import org.apache.ws.pubsub.TopicFilter;
import org.apache.ws.pubsub.XPathFilter;
import org.apache.ws.pubsub.wsaSOAPConnection;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.ws.util.jndi.XmlBeanJndiUtils;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.xmlsoap.schemas.soap.envelope.EnvelopeDocument;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.ReferencePropertiesType;
import org.xmlsoap.schemas.ws.x2004.x08.eventing.DeliveryType;
import org.xmlsoap.schemas.ws.x2004.x08.eventing.ExpirationType;
import org.xmlsoap.schemas.ws.x2004.x08.eventing.FilterType;
import org.xmlsoap.schemas.ws.x2004.x08.eventing.SubscribeDocument;
import org.xmlsoap.schemas.ws.x2004.x08.eventing.SubscribeResponseDocument;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import java.util.Calendar;
/**
* DOCUMENT_ME
*
* @version $Revision: 1.8 $
* @author $author$
*/
public class Subscriber
implements NotificationProducer
{
/**
* DOCUMENT_ME
*/
private static final Log LOG = LogFactory.getLog( Subscriber.class.getName( ) );
private static String WRAPPED_MODE_URI = "http://ws.apache.org/ws-fx/pubscribe/pubsub/wrapped";
private EndpointReference epr;
private SubscriptionHome sH;
// private SubscriberStore store;
public Subscriber( String url,
String configfile )
{
//Axis stuff to enable Addressing HandlerChain
AxisProperties.setProperty( EngineConfigurationFactoryDefault.OPTION_CLIENT_CONFIG_FILE,
"/org/apache/ws/eventing/client-config.wsdd" );
try
{
//read jndi-config.xml
init( configfile );
//create EPR TODO clean
org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType e =
org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType.Factory.newInstance( );
org.xmlsoap.schemas.ws.x2004.x08.addressing.AttributedURI auri =
org.xmlsoap.schemas.ws.x2004.x08.addressing.AttributedURI.Factory.newInstance( );
auri.setStringValue( url );
e.setAddress( auri );
//wrapper
this.epr = new XmlBeansEndpointReference( e );
//getlocal SubscriptionHome from jndi-config
Context initialContext = new InitialContext( );
sH = (SubscriptionHome) initialContext.lookup( SubscriptionHome.HOME_LOCATION );
}
catch ( Exception e )
{
e.printStackTrace( );
}
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public EndpointReference getEPR( )
{
return epr;
}
/**
* runs resource discovery.
*
*/
public void init( String configfile )
throws Exception
{
LOG.info( this.getClass( ).getName( ) + " : initialize with " + configfile );
XmlBeanJndiUtils.initFromInputStream( Thread.currentThread( ).getContextClassLoader( ).getResourceAsStream( configfile ) );
}
/**
* DOCUMENT_ME
*
* @param notificationConsumer DOCUMENT_ME
* @param subscriptionEndConsumer DOCUMENT_ME
* @param tf DOCUMENT_ME
* @param xf DOCUMENT_ME
* @param initialTerminationTime DOCUMENT_ME
* @param UseNotify DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public org.apache.ws.pubsub.Subscription subscribe( NotificationConsumer notificationConsumer,
SubscriptionEndConsumer subscriptionEndConsumer,
TopicFilter tf,
XPathFilter xf,
Calendar initialTerminationTime,
boolean UseNotify )
{
//create local Subscription
//add to local SubscriptionHome
//create ID
Subscription ls = this.sH.create( );
ls.setNotificationConsumer( notificationConsumer );
ls.setSubscriptionEndConsumer( subscriptionEndConsumer );
SubscribeDocument sdom = SubscribeDocument.Factory.newInstance( );
SubscribeDocument.Subscribe s = sdom.addNewSubscribe( );
//create notifyTo EPR
EndpointReferenceType nepr =
(EndpointReferenceType) ( (XmlObjectWrapper) notificationConsumer.getEPR( ) ).getXmlObject( );
//add ReferenceProperties
ReferencePropertiesType refProps = nepr.addNewReferenceProperties( );
XmlObject xmlObject = XmlBeanUtils.addChildElement( refProps,
new QName( "TODO" ) );
XmlCursor xmlCursor = xmlObject.newCursor( );
xmlCursor.setTextValue( String.valueOf( ls.getID( ) ) );
xmlCursor.dispose( );
//notifications are send to:
DeliveryType delivery = s.addNewDelivery( );
delivery.setNotifyTo( nepr );
if ( UseNotify == true )
{
delivery.setMode( WRAPPED_MODE_URI );
}
//create endto EPR
EndpointReferenceType eepr =
(EndpointReferenceType) ( (XmlObjectWrapper) subscriptionEndConsumer.getEPR( ) ).getXmlObject( );
//add ReferenceProperties we already created
eepr.setReferenceProperties( refProps );
//subscription ends are send to:
s.setEndTo( eepr );
//TODO check Calendar serializing
ExpirationType expires = ExpirationType.Factory.newInstance( );
expires.setObjectValue( initialTerminationTime );
s.setExpires( expires );
//TODO multiple filters
//XPathFilter
if ( xf != null )
{
FilterType filter = s.addNewFilter( );
java.net.URI uri = xf.getURI( );
if ( uri != null )
{
filter.setDialect( uri.toString( ) );
}
filter.newCursor( ).setTextValue( (String) xf.getExpression( ) );
}
SubscribeResponseDocument.SubscribeResponse sresres = null;
try
{
//now call
wsaSOAPConnection sconn = wsaSOAPConnection.newInstance( );
MessageFactory mf = MessageFactory.newInstance( );
SOAPMessage soapm = mf.createMessage( );
//put XMLbean into SOAPBody
soapm.getSOAPBody( ).addDocument( (org.w3c.dom.Document) sdom.newDomNode( ) );
SOAPMessage subscribeResponse = sconn.call( soapm,
epr.getAddress( ).toString( ) );
java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream( );
subscribeResponse.writeTo( os );
EnvelopeDocument sres =
EnvelopeDocument.Factory.parse( new java.io.ByteArrayInputStream( os.toByteArray( ) ) );
//TODO handle faults
SubscribeResponseDocument sresdom =
SubscribeResponseDocument.Factory.parse( sres.getEnvelope( ).getBody( ).xmlText( ) );
sresres = sresdom.getSubscribeResponse( );
}
catch ( Exception e )
{
e.printStackTrace( );
}
//set SubscriptionManager
ls.setSubscriptionManager( new XmlBeansEndpointReference( sresres.getSubscriptionManager( ) ) );
ls.setNotificationConsumer( notificationConsumer );
return ls;
}
}