001package org.nuxeo.ecm.platform.api.ws;
002
003import static javax.xml.ws.Endpoint.WSDL_PORT;
004import static javax.xml.ws.Endpoint.WSDL_SERVICE;
005import static org.apache.commons.lang.StringUtils.isBlank;
006
007import java.io.FileNotFoundException;
008import java.io.IOException;
009import java.net.URL;
010import java.util.ArrayList;
011import java.util.HashMap;
012import java.util.List;
013import java.util.Map;
014
015import javax.xml.namespace.QName;
016import javax.xml.transform.Source;
017import javax.xml.transform.stream.StreamSource;
018import javax.xml.ws.Endpoint;
019import javax.xml.ws.handler.Handler;
020import javax.xml.ws.soap.SOAPBinding;
021
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XNodeList;
024import org.nuxeo.common.xmap.annotation.XObject;
025import org.nuxeo.ecm.platform.ws.WSEndpointManager;
026import org.nuxeo.ecm.platform.ws.WSEndpointManagerImpl;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
031 * @since 5.7.2
032 */
033@XObject("endpoint")
034public class WSEndpointDescriptor {
035
036    private static final String NUXEO_URL = "nuxeo.url";
037
038    @XNode("@name")
039    public String name;
040
041    @XNode("@address")
042    public String address;
043
044    @XNode("@implementor")
045    public Class<?> clazz;
046
047    @XNodeList(value = "handlers/handler", type = String[].class, componentType = Class.class)
048    public Class<? extends Handler>[] handlers;
049
050    @XNode("@namespace")
051    public String namespace;
052
053    @XNode("@wsdl")
054    public String wsdl;
055
056    @XNode("@port")
057    public String port;
058
059    @XNode("@service")
060    public String service;
061
062    @XNode("enable-mtom")
063    public boolean mtom;
064
065    @XNode("publishedEndpointUrl")
066    public String publishedEndpointUrl;
067
068    public Object getImplementorInstance() throws IllegalAccessException, InstantiationException {
069        return clazz != null ? clazz.newInstance() : null;
070    }
071
072    public Endpoint toEndpoint() throws IOException, IllegalAccessException, InstantiationException {
073        Endpoint ep = Endpoint.create(getImplementorInstance());
074        List<Source> metadata = new ArrayList<>();
075        Map<String, Object> properties = new HashMap<>();
076
077        if (!isBlank(port)) {
078            properties.put(WSDL_PORT, new QName(namespace, port));
079        }
080        if (!isBlank(port)) {
081            properties.put(WSDL_SERVICE, new QName(namespace, service));
082        }
083
084        if (!isBlank(wsdl)) {
085            URL wsdlURL = WSEndpointManagerImpl.class.getClassLoader().getResource(wsdl);
086            if (wsdlURL == null) {
087                throw new FileNotFoundException("WSDL: " + wsdl);
088            }
089            Source src = new StreamSource(wsdlURL.openStream());
090            src.setSystemId(wsdlURL.toExternalForm());
091            metadata.add(src);
092        }
093
094        if (isBlank(publishedEndpointUrl)) {
095            publishedEndpointUrl = String.format("%s%s%s", Framework.getProperty(NUXEO_URL),
096                    WSEndpointManager.WS_SERVLET, address);
097        }
098        properties.put("publishedEndpointUrl", publishedEndpointUrl);
099
100        ep.setMetadata(metadata);
101        ep.setProperties(properties);
102        return ep;
103    }
104
105    public void configurePostPublishing(Endpoint ep) throws IllegalAccessException, InstantiationException {
106        if (handlers != null) {
107            List<Handler> handlerChain = ep.getBinding().getHandlerChain();
108            for (Class<? extends Handler> handler : handlers) {
109                handlerChain.add(handler.newInstance());
110            }
111            ep.getBinding().setHandlerChain(handlerChain);
112        }
113
114        if (mtom && ep.getBinding() instanceof SOAPBinding) {
115            SOAPBinding binding = (SOAPBinding) ep.getBinding();
116            binding.setMTOMEnabled(mtom);
117        }
118    }
119}