/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.servicemix.components.jaxws;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.servicemix.MessageExchangeListener;
import org.servicemix.components.util.TransformComponentSupport;
import org.springframework.core.io.Resource;
import org.w3c.dom.Node;
import javax.jbi.JBIException;
import javax.jbi.messaging.InOnly;
import javax.jbi.messaging.InOut;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.NormalizedMessage;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.Service.Mode;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Iterator;
/**
* Converts an inbound JBI message into a <a
* href="https://jax-rpc.dev.java.net/">JAX-WS</a> one way or a
* request-response where the output is replied back to the Normalized Message
* Router.
*
* @version $Revision: 560 $
*/
public class JAXWSOutBinding extends TransformComponentSupport implements MessageExchangeListener {
private static final Log log = LogFactory.getLog(JAXWSOutBinding.class);
private Dispatch<Source> dispatch;
private Service jaxService;
private URL wsdl;
private QName interfaceName;
private Mode mode = Mode.PAYLOAD;
private boolean defaultInOut = true;
private Resource wsdlResource;
public Dispatch<Source> getDispatch() throws IOException, URISyntaxException, MessagingException {
if (dispatch == null) {
dispatch = createDispatch();
}
return dispatch;
}
public void setDispatch(Dispatch<Source> dispatch) {
this.dispatch = dispatch;
}
public Service getJaxService() throws IOException {
if (jaxService == null) {
jaxService = createJaxService();
}
return jaxService;
}
public void setJaxService(Service jaxService) {
this.jaxService = jaxService;
}
public URL getWsdl() throws IOException {
if (wsdl == null) {
wsdl = createWsdl();
}
return wsdl;
}
public void setWsdl(URL wsdl) {
this.wsdl = wsdl;
}
public Resource getWsdlResource() {
return wsdlResource;
}
public void setWsdlResource(Resource wsdlResource) {
this.wsdlResource = wsdlResource;
}
public QName getInterfaceName() {
return interfaceName;
}
public void setInterfaceName(QName interfaceName) {
this.interfaceName = interfaceName;
}
public Mode getMode() {
return mode;
}
public void setMode(Mode mode) {
this.mode = mode;
}
public boolean isDefaultInOut() {
return defaultInOut;
}
/**
* Sets whether an InOut (the default) or an InOnly message exchange will be
* used by default.
*/
public void setDefaultInOut(boolean defaultInOut) {
this.defaultInOut = defaultInOut;
}
// Implementation methods
// -------------------------------------------------------------------------
protected void init() throws JBIException {
super.init();
// lets force the lazy initialization
try {
getDispatch();
}
catch (IOException e) {
throw new JBIException("Failed to create Dispatch: " + e, e);
}
catch (URISyntaxException e) {
throw new JBIException("Failed to create Dispatch: " + e, e);
}
}
protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
Source content = getInMessage(exchange).getContent();
try {
if (isInOutRequest(exchange, in, out)) {
copyPropertiesAndAttachments(exchange, in, out);
Object answer = dispatch.invoke(content);
Source source = null;
if (answer instanceof Source) {
source = (Source) answer;
}
else if (answer instanceof Node) {
source = new DOMSource((Node) answer);
}
else if (answer instanceof Throwable) {
throw new MessagingException((Throwable) answer);
}
else {
throw new MessagingException("Unknown source type: " + answer);
}
out.setContent(source);
}
else {
dispatch.invokeOneWay(content);
}
}
catch (WebServiceException e) {
throw new MessagingException(e);
}
catch (Throwable t) {
throw new MessagingException(t);
}
return true;
}
/**
* Return true if this request is an {@link InOut} request otherwise it will
* be assumed to be an {@link InOnly}
*/
boolean isInOutRequest(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) {
return isDefaultInOut();
}
protected Dispatch<Source> createDispatch() throws IOException, URISyntaxException, MessagingException {
Service webService = getJaxService();
QName interfaceName = getInterfaceName();
if (interfaceName == null) {
interfaceName = findInterfaceName(webService);
}
return webService.createDispatch(interfaceName, Source.class, getMode());
}
protected QName findInterfaceName(Service webService) throws MessagingException {
Iterator<QName> iter = webService.getPorts();
while (iter.hasNext()) {
return iter.next();
}
throw new MessagingException("No interfaceName property is specified and the Service implementation has no ports available!");
}
protected Service createJaxService() throws IOException {
QName serviceName = getService();
URL url = getWsdl();
if (url != null) {
return Service.create(url, serviceName);
}
else {
return Service.create(serviceName);
}
}
protected URL createWsdl() throws IOException {
if (wsdlResource != null) {
return wsdlResource.getURL();
}
return null;
}
}