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 javax.jbi.JBIException;
020 import javax.jbi.servicedesc.ServiceEndpoint;
021 import javax.jbi.messaging.InOut;
022 import javax.jbi.messaging.NormalizedMessage;
023 import javax.jbi.messaging.InOnly;
024 import javax.jbi.messaging.ExchangeStatus;
025 import javax.jbi.component.ComponentContext;
026 import javax.xml.bind.JAXBContext;
027 import javax.xml.bind.JAXBException;
028 import javax.xml.bind.util.JAXBSource;
029 import javax.xml.namespace.QName;
030 import javax.xml.parsers.ParserConfigurationException;
031 import javax.xml.transform.Source;
032 import javax.xml.transform.dom.DOMResult;
033 import javax.xml.transform.dom.DOMSource;
034 import javax.xml.ws.wsaddressing.W3CEndpointReference;
035
036 import org.w3c.dom.Element;
037 import org.w3c.dom.NodeList;
038 import org.w3c.dom.Document;
039
040 import org.apache.servicemix.common.util.DOMUtil;
041 import org.apache.servicemix.common.util.URIResolver;
042 import org.apache.servicemix.jbi.jaxp.StringSource;
043 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
044 import org.apache.servicemix.wsn.jbi.JbiWrapperHelper;
045 import org.oasis_open.docs.wsn.b_2.Subscribe;
046 import org.oasis_open.docs.wsn.br_2.RegisterPublisher;
047 import org.oasis_open.docs.wsrf.rp_2.GetResourcePropertyResponse;
048
049 public abstract class AbstractWSAClient {
050
051 private static JAXBContext jaxbContext;
052
053 private W3CEndpointReference endpoint;
054
055 private ComponentContext context;
056
057 private ServiceEndpoint serviceEndpoint;
058
059 private boolean jbiWrapped;
060
061 private SourceTransformer transformer;
062
063 public AbstractWSAClient() {
064 }
065
066 public AbstractWSAClient(ComponentContext context, W3CEndpointReference endpoint) {
067 this.context = context;
068 this.endpoint = endpoint;
069 }
070
071 public boolean isJbiWrapped() {
072 return jbiWrapped;
073 }
074
075 public void setJbiWrapped(boolean jbiWrapped) {
076 this.jbiWrapped = jbiWrapped;
077 }
078
079 public static W3CEndpointReference createWSA(String address) {
080 Source src = new StringSource("<EndpointReference xmlns='http://www.w3.org/2005/08/addressing'><Address>"
081 + address + "</Address></EndpointReference>");
082 return new W3CEndpointReference(src);
083 }
084
085 public static String getWSAAddress(W3CEndpointReference ref) {
086 try {
087 Element element = JbiWrapperHelper.createDocument().createElement("elem");
088 ref.writeTo(new DOMResult(element));
089 NodeList nl = element.getElementsByTagNameNS("http://www.w3.org/2005/08/addressing", "Address");
090 if (nl != null && nl.getLength() > 0) {
091 Element e = (Element) nl.item(0);
092 return DOMUtil.getElementText(e).trim();
093 }
094 } catch (ParserConfigurationException e) {
095 // Ignore
096 }
097 return null;
098 }
099
100 public static ServiceEndpoint resolveWSA(W3CEndpointReference ref, ComponentContext context) {
101 String[] parts = URIResolver.split3(getWSAAddress(ref));
102 return context.getEndpoint(new QName(parts[0], parts[1]), parts[2]);
103 }
104
105 public W3CEndpointReference getEndpoint() {
106 return endpoint;
107 }
108
109 public void setEndpoint(W3CEndpointReference endpoint) {
110 this.endpoint = endpoint;
111 }
112
113 public ServiceEndpoint getServiceEndpoint() {
114 if (serviceEndpoint == null && endpoint != null) {
115 serviceEndpoint = resolveWSA(endpoint, context);
116 }
117 return serviceEndpoint;
118 }
119
120 public void setServiceEndpoint(ServiceEndpoint serviceEndpoint) {
121 this.serviceEndpoint = serviceEndpoint;
122 }
123
124 public ComponentContext getContext() {
125 return context;
126 }
127
128 public void setContext(ComponentContext context) {
129 this.context = context;
130 }
131
132 protected Object request(Object request) throws JBIException {
133 return request(null, request);
134 }
135
136 protected Object request(QName operation, Object request) throws JBIException {
137 try {
138 InOut exchange = getContext().getDeliveryChannel().createExchangeFactory().createInOutExchange();
139 exchange.setEndpoint(getServiceEndpoint());
140 exchange.setOperation(operation);
141 NormalizedMessage in = exchange.createMessage();
142 exchange.setInMessage(in);
143 if (isJbiWrapped()) {
144 Document doc = JbiWrapperHelper.createDocument();
145 getJAXBContext().createMarshaller().marshal(request, doc);
146 JbiWrapperHelper.wrap(doc);
147 in.setContent(new DOMSource(doc));
148 } else {
149 in.setContent(new JAXBSource(getJAXBContext(), request));
150 }
151 getContext().getDeliveryChannel().sendSync(exchange);
152 if (exchange.getStatus() == ExchangeStatus.ERROR) {
153 throw new JBIException(exchange.getError());
154 } else if (exchange.getFault() != null) {
155 if (transformer == null) {
156 transformer = new SourceTransformer();
157 }
158 String fault = transformer.contentToString(exchange.getFault());
159 exchange.setStatus(ExchangeStatus.DONE);
160 getContext().getDeliveryChannel().send(exchange);
161 throw new JBIException(fault);
162 } else {
163 NormalizedMessage out = exchange.getOutMessage();
164 Source source = out.getContent();
165 if (isJbiWrapped()) {
166 source = JbiWrapperHelper.unwrap(source);
167 }
168 Object result = getJAXBContext().createUnmarshaller().unmarshal(source);
169 exchange.setStatus(ExchangeStatus.DONE);
170 getContext().getDeliveryChannel().send(exchange);
171 return result;
172 }
173 } catch (Exception e) {
174 throw new JBIException(e);
175 }
176 }
177
178 protected void send(Object request) throws JBIException {
179 send(null, request);
180 }
181
182 protected void send(QName operation, Object request) throws JBIException {
183 try {
184 InOnly exchange = getContext().getDeliveryChannel().createExchangeFactory().createInOnlyExchange();
185 exchange.setEndpoint(getServiceEndpoint());
186 exchange.setOperation(operation);
187 NormalizedMessage in = exchange.createMessage();
188 exchange.setInMessage(in);
189 in.setContent(new JAXBSource(getJAXBContext(), request));
190 getContext().getDeliveryChannel().sendSync(exchange);
191 if (exchange.getStatus() == ExchangeStatus.ERROR) {
192 throw new JBIException(exchange.getError());
193 }
194 } catch (JAXBException e) {
195 throw new JBIException(e);
196 }
197 }
198
199 private synchronized JAXBContext getJAXBContext() throws JAXBException {
200 if (jaxbContext == null) {
201 jaxbContext = JAXBContext.newInstance(Subscribe.class, RegisterPublisher.class, GetResourcePropertyResponse.class);
202 }
203 return jaxbContext;
204 }
205
206 }