/*=============================================================================*
* 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.notification.pubsub;
//import org.xmlsoap.schemas.ws.x2003.x03.addressing.EEndpointReference;
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.jndi.XmlBeanJndiUtils;
import org.apache.xmlbeans.XmlCursor;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.SubscribeDocument;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.SubscribeResponseDocument;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.TopicExpressionType;
import org.xmlsoap.schemas.soap.envelope.EnvelopeDocument;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import java.util.Calendar;
/**
*
* @author Stefan Lischke
*/
public class Subscriber
implements NotificationProducer
{
private static final Log LOG = LogFactory.getLog( Subscriber.class.getName( ) );
private EndpointReference epr;
private SubscriptionHome sH;
/** Creates a new instance of Subscriber */
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
*
* @param filters DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public Object getCurrentMessage( org.apache.ws.pubsub.Filter[] filters )
{
return null;
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public org.apache.ws.addressing.EndpointReference getEPR( )
{
return this.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 )
{
SubscribeDocument sdom = SubscribeDocument.Factory.newInstance( );
SubscribeDocument.Subscribe s = sdom.addNewSubscribe( );
//subscription ends are send to:
//TODO
//notifications are send to:
s.setConsumerReference( (org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceType) ( (XmlObjectWrapper) notificationConsumer
.getEPR( ) )
.getXmlObject( ) );
//TODO check Calendar serializing
s.setInitialTerminationTime( initialTerminationTime );
//TODO multiple filters
if ( tf != null )
{
TopicExpressionType te = s.addNewTopicExpression( );
XmlCursor xc = te.newCursor( );
xc.toNextToken( );
xc.insertNamespace( "tns",
tf.getNameSpace( ) );
te.newCursor( ).setTextValue( (String) tf.getExpression( ) );
te.setDialect( tf.getURI( ).toString( ) );
}
//create Subscription
//add to local SubscriptionHome
org.apache.ws.notification.pubsub.Subscription ls = this.sH.create( );
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( ) ) );
//extract SubscribeResponse from SOAPBody
/*SubscribeResponseDocument sresdom = SubscribeResponseDocument.Factory.newInstance();
sresres= sresdom.addNewSubscribeResponse();
sresres.set(sres.getEnvelope().getBody());
*/
//TODO handle faults
SubscribeResponseDocument sresdom =
SubscribeResponseDocument.Factory.parse( sres.getEnvelope( ).getBody( ).xmlText( ) );
sresres = sresdom.getSubscribeResponse( );
}
catch ( Exception e )
{
e.printStackTrace( );
}
//set SubscritpionManager
ls.setSubscriptionManager( sresres );
ls.setNotificationConsumer( notificationConsumer );
return ls;
}
}