001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.wsn.client;
018
019 import java.util.List;
020
021 import javax.jbi.JBIException;
022 import javax.jbi.component.ComponentContext;
023 import javax.xml.bind.JAXBElement;
024 import javax.xml.namespace.QName;
025 import javax.xml.ws.wsaddressing.W3CEndpointReference;
026
027 import org.apache.servicemix.wsn.AbstractSubscription;
028 import org.oasis_open.docs.wsn.b_2.FilterType;
029 import org.oasis_open.docs.wsn.b_2.GetCurrentMessage;
030 import org.oasis_open.docs.wsn.b_2.GetCurrentMessageResponse;
031 import org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType;
032 import org.oasis_open.docs.wsn.b_2.Notify;
033 import org.oasis_open.docs.wsn.b_2.QueryExpressionType;
034 import org.oasis_open.docs.wsn.b_2.Subscribe;
035 import org.oasis_open.docs.wsn.b_2.SubscribeResponse;
036 import org.oasis_open.docs.wsn.b_2.TopicExpressionType;
037 import org.oasis_open.docs.wsn.b_2.UseRaw;
038 import org.oasis_open.docs.wsn.br_2.RegisterPublisher;
039 import org.oasis_open.docs.wsn.br_2.RegisterPublisherResponse;
040 import org.oasis_open.docs.wsrf.rpw_2.GetResourceProperty;
041 import org.oasis_open.docs.wsrf.rp_2.GetResourcePropertyResponse;
042 import org.oasis_open.docs.wsrf.rp_2.ObjectFactory;
043
044 public class NotificationBroker extends AbstractWSAClient {
045
046 public static final String WSN_URI = "http://servicemix.org/wsnotification";
047
048 public static final String WSN_SERVICE = "NotificationBroker";
049
050 public static final QName NOTIFICATION_BROKER = new QName(WSN_URI, WSN_SERVICE);
051
052 public NotificationBroker(ComponentContext context) {
053 this(context, "Broker");
054 }
055
056 public NotificationBroker(ComponentContext context, String brokerName) {
057 this(context, createWSA(WSN_URI + "/" + WSN_SERVICE + "/" + brokerName));
058 }
059
060 public NotificationBroker(ComponentContext context, W3CEndpointReference endpoint) {
061 super(context, endpoint);
062 }
063
064 public void notify(String topic, Object msg) throws JBIException {
065 Notify notify = new Notify();
066 NotificationMessageHolderType holder = new NotificationMessageHolderType();
067 if (topic != null) {
068 TopicExpressionType topicExp = new TopicExpressionType();
069 topicExp.getContent().add(topic);
070 holder.setTopic(topicExp);
071 }
072 holder.setMessage(new NotificationMessageHolderType.Message());
073 holder.getMessage().setAny(msg);
074 notify.getNotificationMessage().add(holder);
075 send(notify);
076 }
077
078 public Subscription subscribe(W3CEndpointReference consumer, String topic) throws JBIException {
079 return subscribe(consumer, topic, null, false);
080 }
081
082 public Subscription subscribe(W3CEndpointReference consumer, String topic, String xpath) throws JBIException {
083 return subscribe(consumer, topic, xpath, false);
084 }
085
086 public Subscription subscribe(W3CEndpointReference consumer, String topic,
087 String xpath, boolean raw) throws JBIException {
088
089 Subscribe subscribeRequest = new Subscribe();
090 subscribeRequest.setConsumerReference(consumer);
091 subscribeRequest.setFilter(new FilterType());
092 if (topic != null) {
093 TopicExpressionType topicExp = new TopicExpressionType();
094 topicExp.getContent().add(topic);
095 subscribeRequest.getFilter().getAny().add(
096 new JAXBElement<TopicExpressionType>(AbstractSubscription.QNAME_TOPIC_EXPRESSION,
097 TopicExpressionType.class, topicExp));
098 }
099 if (xpath != null) {
100 QueryExpressionType xpathExp = new QueryExpressionType();
101 xpathExp.setDialect(AbstractSubscription.XPATH1_URI);
102 xpathExp.getContent().add(xpath);
103 subscribeRequest.getFilter().getAny().add(
104 new JAXBElement<QueryExpressionType>(AbstractSubscription.QNAME_MESSAGE_CONTENT,
105 QueryExpressionType.class, xpathExp));
106 }
107 if (raw) {
108 subscribeRequest.setSubscriptionPolicy(new Subscribe.SubscriptionPolicy());
109 subscribeRequest.getSubscriptionPolicy().getAny().add(new UseRaw());
110 }
111 SubscribeResponse response = (SubscribeResponse) request(subscribeRequest);
112 return new Subscription(getContext(), response.getSubscriptionReference());
113 }
114
115 public List<Object> getCurrentMessage(String topic) throws JBIException {
116 GetCurrentMessage getCurrentMessageRequest = new GetCurrentMessage();
117 if (topic != null) {
118 TopicExpressionType topicExp = new TopicExpressionType();
119 topicExp.getContent().add(topic);
120 getCurrentMessageRequest.setTopic(topicExp);
121 }
122 GetCurrentMessageResponse response = (GetCurrentMessageResponse) request(getCurrentMessageRequest);
123 return response.getAny();
124 }
125
126 public Publisher registerPublisher(W3CEndpointReference publisherReference,
127 String topic, boolean demand) throws JBIException {
128
129 RegisterPublisher registerPublisherRequest = new RegisterPublisher();
130 registerPublisherRequest.setPublisherReference(publisherReference);
131 if (topic != null) {
132 TopicExpressionType topicExp = new TopicExpressionType();
133 topicExp.getContent().add(topic);
134 registerPublisherRequest.getTopic().add(topicExp);
135 }
136 registerPublisherRequest.setDemand(Boolean.valueOf(demand));
137 RegisterPublisherResponse response = (RegisterPublisherResponse) request(registerPublisherRequest);
138 return new Publisher(getContext(), response.getPublisherRegistrationReference());
139 }
140
141 public List<Object> getResourceProperty(QName property) throws JBIException {
142 GetResourcePropertyResponse response = (GetResourcePropertyResponse) request(
143 new ObjectFactory().createGetResourceProperty(property));
144 return response.getAny();
145 }
146
147 }