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.JAXBContext;
024 import javax.xml.bind.JAXBElement;
025 import javax.xml.bind.JAXBException;
026 import javax.xml.namespace.QName;
027 import javax.xml.ws.wsaddressing.W3CEndpointReference;
028
029 import org.apache.servicemix.client.ServiceMixClient;
030 import org.apache.servicemix.client.ServiceMixClientFacade;
031 import org.apache.servicemix.jbi.container.JBIContainer;
032 import org.apache.servicemix.jbi.resolver.ServiceNameEndpointResolver;
033 import org.apache.servicemix.wsn.AbstractSubscription;
034 import org.oasis_open.docs.wsn.b_2.FilterType;
035 import org.oasis_open.docs.wsn.b_2.GetCurrentMessage;
036 import org.oasis_open.docs.wsn.b_2.GetCurrentMessageResponse;
037 import org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType;
038 import org.oasis_open.docs.wsn.b_2.Notify;
039 import org.oasis_open.docs.wsn.b_2.QueryExpressionType;
040 import org.oasis_open.docs.wsn.b_2.Subscribe;
041 import org.oasis_open.docs.wsn.b_2.SubscribeResponse;
042 import org.oasis_open.docs.wsn.b_2.TopicExpressionType;
043 import org.oasis_open.docs.wsn.b_2.UseRaw;
044 import org.oasis_open.docs.wsn.br_2.RegisterPublisher;
045 import org.oasis_open.docs.wsn.br_2.RegisterPublisherResponse;
046
047 public class NotificationBroker extends AbstractWSAClient {
048
049 public static final String WSN_URI = "http://servicemix.org/wsnotification";
050
051 public static final String WSN_SERVICE = "NotificationBroker";
052
053 public static final QName NOTIFICATION_BROKER = new QName(WSN_URI, WSN_SERVICE);
054
055 public NotificationBroker(ComponentContext context) throws JAXBException {
056 ServiceMixClientFacade client = new ServiceMixClientFacade(context);
057 client.setMarshaler(new JAXBMarshaler(JAXBContext.newInstance(Subscribe.class, RegisterPublisher.class)));
058 setClient(client);
059 setResolver(new ServiceNameEndpointResolver(NOTIFICATION_BROKER));
060 }
061
062 public NotificationBroker(ComponentContext context, String brokerName) throws JAXBException {
063 setClient(createJaxbClient(context));
064 setEndpoint(createWSA(WSN_URI + "/" + WSN_SERVICE + "/" + brokerName));
065 setResolver(resolveWSA(getEndpoint()));
066 }
067
068 public NotificationBroker(JBIContainer container) throws JBIException, JAXBException {
069 setClient(createJaxbClient(container));
070 setResolver(new ServiceNameEndpointResolver(NOTIFICATION_BROKER));
071 }
072
073 public NotificationBroker(JBIContainer container, String brokerName) throws JBIException, JAXBException {
074 setClient(createJaxbClient(container));
075 setEndpoint(createWSA(WSN_URI + "/" + WSN_SERVICE + "/" + brokerName));
076 setResolver(resolveWSA(getEndpoint()));
077 }
078
079 public NotificationBroker(ServiceMixClient client) {
080 setClient(client);
081 setResolver(new ServiceNameEndpointResolver(NOTIFICATION_BROKER));
082 }
083
084 public NotificationBroker(ServiceMixClient client, String brokerName) {
085 setClient(client);
086 setEndpoint(createWSA(WSN_URI + "/" + WSN_SERVICE + "/" + brokerName));
087 setResolver(resolveWSA(getEndpoint()));
088 }
089
090 public void notify(String topic, Object msg) throws JBIException {
091 Notify notify = new Notify();
092 NotificationMessageHolderType holder = new NotificationMessageHolderType();
093 if (topic != null) {
094 TopicExpressionType topicExp = new TopicExpressionType();
095 topicExp.getContent().add(topic);
096 holder.setTopic(topicExp);
097 }
098 holder.setMessage(new NotificationMessageHolderType.Message());
099 holder.getMessage().setAny(msg);
100 notify.getNotificationMessage().add(holder);
101 send(notify);
102 }
103
104 public Subscription subscribe(W3CEndpointReference consumer, String topic) throws JBIException {
105 return subscribe(consumer, topic, null, false);
106 }
107
108 public Subscription subscribe(W3CEndpointReference consumer, String topic, String xpath) throws JBIException {
109 return subscribe(consumer, topic, xpath, false);
110 }
111
112 public Subscription subscribe(W3CEndpointReference consumer, String topic,
113 String xpath, boolean raw) throws JBIException {
114
115 Subscribe subscribeRequest = new Subscribe();
116 subscribeRequest.setConsumerReference(consumer);
117 subscribeRequest.setFilter(new FilterType());
118 if (topic != null) {
119 TopicExpressionType topicExp = new TopicExpressionType();
120 topicExp.getContent().add(topic);
121 subscribeRequest.getFilter().getAny().add(
122 new JAXBElement<TopicExpressionType>(AbstractSubscription.QNAME_TOPIC_EXPRESSION,
123 TopicExpressionType.class, topicExp));
124 }
125 if (xpath != null) {
126 QueryExpressionType xpathExp = new QueryExpressionType();
127 xpathExp.setDialect(AbstractSubscription.XPATH1_URI);
128 xpathExp.getContent().add(xpath);
129 subscribeRequest.getFilter().getAny().add(
130 new JAXBElement<QueryExpressionType>(AbstractSubscription.QNAME_MESSAGE_CONTENT,
131 QueryExpressionType.class, xpathExp));
132 }
133 if (raw) {
134 subscribeRequest.setSubscriptionPolicy(new Subscribe.SubscriptionPolicy());
135 subscribeRequest.getSubscriptionPolicy().getAny().add(new UseRaw());
136 }
137 SubscribeResponse response = (SubscribeResponse) request(subscribeRequest);
138 return new Subscription(response.getSubscriptionReference(), getClient());
139 }
140
141 public List<Object> getCurrentMessage(String topic) throws JBIException {
142 GetCurrentMessage getCurrentMessageRequest = new GetCurrentMessage();
143 if (topic != null) {
144 TopicExpressionType topicExp = new TopicExpressionType();
145 topicExp.getContent().add(topic);
146 getCurrentMessageRequest.setTopic(topicExp);
147 }
148 GetCurrentMessageResponse response = (GetCurrentMessageResponse) request(getCurrentMessageRequest);
149 return response.getAny();
150 }
151
152 public Publisher registerPublisher(W3CEndpointReference publisherReference,
153 String topic, boolean demand) throws JBIException {
154
155 RegisterPublisher registerPublisherRequest = new RegisterPublisher();
156 registerPublisherRequest.setPublisherReference(publisherReference);
157 if (topic != null) {
158 TopicExpressionType topicExp = new TopicExpressionType();
159 topicExp.getContent().add(topic);
160 registerPublisherRequest.getTopic().add(topicExp);
161 }
162 registerPublisherRequest.setDemand(Boolean.valueOf(demand));
163 RegisterPublisherResponse response = (RegisterPublisherResponse) request(registerPublisherRequest);
164 return new Publisher(response.getPublisherRegistrationReference(), getClient());
165 }
166
167 }