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