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.jbi;
018
019 import javax.jbi.JBIException;
020 import javax.jbi.component.ComponentContext;
021 import javax.jbi.messaging.InOnly;
022 import javax.jbi.messaging.MessageExchangeFactory;
023 import javax.jbi.messaging.NormalizedMessage;
024 import javax.jbi.servicedesc.ServiceEndpoint;
025 import javax.xml.bind.JAXBContext;
026 import javax.xml.namespace.QName;
027 import javax.xml.parsers.DocumentBuilder;
028 import javax.xml.parsers.DocumentBuilderFactory;
029 import javax.xml.transform.dom.DOMSource;
030
031 import org.w3c.dom.Document;
032 import org.w3c.dom.DocumentFragment;
033 import org.w3c.dom.Element;
034 import org.w3c.dom.NodeList;
035
036 import org.apache.commons.logging.Log;
037 import org.apache.commons.logging.LogFactory;
038 import org.apache.servicemix.wsn.ComponentContextAware;
039 import org.apache.servicemix.wsn.client.AbstractWSAClient;
040 import org.apache.servicemix.wsn.jms.JmsSubscription;
041 import org.oasis_open.docs.wsn.b_2.Subscribe;
042 import org.oasis_open.docs.wsn.b_2.SubscribeCreationFailedFaultType;
043 import org.oasis_open.docs.wsn.bw_2.InvalidFilterFault;
044 import org.oasis_open.docs.wsn.bw_2.InvalidMessageContentExpressionFault;
045 import org.oasis_open.docs.wsn.bw_2.InvalidProducerPropertiesExpressionFault;
046 import org.oasis_open.docs.wsn.bw_2.InvalidTopicExpressionFault;
047 import org.oasis_open.docs.wsn.bw_2.SubscribeCreationFailedFault;
048 import org.oasis_open.docs.wsn.bw_2.TopicExpressionDialectUnknownFault;
049 import org.oasis_open.docs.wsn.bw_2.TopicNotSupportedFault;
050 import org.oasis_open.docs.wsn.bw_2.UnacceptableInitialTerminationTimeFault;
051 import org.oasis_open.docs.wsn.bw_2.UnsupportedPolicyRequestFault;
052 import org.oasis_open.docs.wsn.bw_2.UnrecognizedPolicyRequestFault;
053
054 public class JbiSubscription extends JmsSubscription implements ComponentContextAware {
055
056 private static Log log = LogFactory.getLog(JbiSubscription.class);
057
058 private ComponentContext context;
059
060 private ServiceEndpoint endpoint;
061
062 public JbiSubscription(String name) {
063 super(name);
064 }
065
066 @Override
067 protected void start() throws SubscribeCreationFailedFault {
068 super.start();
069 }
070
071 @Override
072 protected void validateSubscription(Subscribe subscribeRequest) throws InvalidFilterFault,
073 InvalidMessageContentExpressionFault, InvalidProducerPropertiesExpressionFault,
074 InvalidTopicExpressionFault, SubscribeCreationFailedFault, TopicExpressionDialectUnknownFault,
075 TopicNotSupportedFault, UnacceptableInitialTerminationTimeFault,
076 UnsupportedPolicyRequestFault, UnrecognizedPolicyRequestFault {
077 super.validateSubscription(subscribeRequest);
078 try {
079 endpoint = resolveConsumer(subscribeRequest);
080 } catch (Exception e) {
081 SubscribeCreationFailedFaultType fault = new SubscribeCreationFailedFaultType();
082 throw new SubscribeCreationFailedFault("Unable to resolve consumer reference endpoint", fault, e);
083 }
084 if (endpoint == null) {
085 SubscribeCreationFailedFaultType fault = new SubscribeCreationFailedFaultType();
086 throw new SubscribeCreationFailedFault("Unable to resolve consumer reference endpoint", fault);
087 }
088 }
089
090 protected ServiceEndpoint resolveConsumer(Subscribe subscribeRequest) throws Exception {
091 // Try to resolve the WSA endpoint
092 JAXBContext ctx = JAXBContext.newInstance(Subscribe.class);
093 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
094 dbf.setNamespaceAware(true);
095 DocumentBuilder db = dbf.newDocumentBuilder();
096 Document doc = db.newDocument();
097 ctx.createMarshaller().marshal(subscribeRequest, doc);
098 NodeList nl = doc.getDocumentElement().getElementsByTagNameNS("http://docs.oasis-open.org/wsn/b-2",
099 "ConsumerReference");
100 if (nl.getLength() != 1) {
101 throw new Exception("Subscribe request must have exactly one ConsumerReference node");
102 }
103 Element el = (Element) nl.item(0);
104 DocumentFragment epr = doc.createDocumentFragment();
105 epr.appendChild(el);
106 ServiceEndpoint ep = getContext().resolveEndpointReference(epr);
107 if (ep == null) {
108 String[] parts = split(AbstractWSAClient.getWSAAddress(subscribeRequest.getConsumerReference()));
109 ep = getContext().getEndpoint(new QName(parts[0], parts[1]), parts[2]);
110 }
111 return ep;
112 }
113
114 protected String[] split(String uri) {
115 char sep;
116 if (uri.indexOf('/') > 0) {
117 sep = '/';
118 } else {
119 sep = ':';
120 }
121 int idx1 = uri.lastIndexOf(sep);
122 int idx2 = uri.lastIndexOf(sep, idx1 - 1);
123 String epName = uri.substring(idx1 + 1);
124 String svcName = uri.substring(idx2 + 1, idx1);
125 String nsUri = uri.substring(0, idx2);
126 return new String[] {nsUri, svcName, epName };
127 }
128
129 @Override
130 protected void doNotify(final Element content) {
131 try {
132 MessageExchangeFactory factory = context.getDeliveryChannel().createExchangeFactory(endpoint);
133 InOnly inonly = factory.createInOnlyExchange();
134 NormalizedMessage msg = inonly.createMessage();
135 inonly.setInMessage(msg);
136 msg.setContent(new DOMSource(content));
137 context.getDeliveryChannel().send(inonly);
138 } catch (JBIException e) {
139 log.warn("Could not deliver notification", e);
140 }
141 }
142
143 public ComponentContext getContext() {
144 return context;
145 }
146
147 public void setContext(ComponentContext context) {
148 this.context = context;
149 }
150 }