001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Arnaud Kervern
018 */
019package org.nuxeo.ecm.platform.api.ws;
020
021import static javax.xml.ws.Endpoint.WSDL_PORT;
022import static javax.xml.ws.Endpoint.WSDL_SERVICE;
023import static org.apache.commons.lang.StringUtils.isBlank;
024
025import java.io.FileNotFoundException;
026import java.io.IOException;
027import java.net.URL;
028import java.util.ArrayList;
029import java.util.HashMap;
030import java.util.List;
031import java.util.Map;
032
033import javax.xml.namespace.QName;
034import javax.xml.transform.Source;
035import javax.xml.transform.stream.StreamSource;
036import javax.xml.ws.Endpoint;
037import javax.xml.ws.handler.Handler;
038import javax.xml.ws.soap.SOAPBinding;
039
040import org.nuxeo.common.xmap.annotation.XNode;
041import org.nuxeo.common.xmap.annotation.XNodeList;
042import org.nuxeo.common.xmap.annotation.XObject;
043import org.nuxeo.ecm.platform.ws.WSEndpointManager;
044import org.nuxeo.ecm.platform.ws.WSEndpointManagerImpl;
045import org.nuxeo.runtime.api.Framework;
046
047/**
048 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
049 * @since 5.7.2
050 */
051@XObject("endpoint")
052public class WSEndpointDescriptor {
053
054    private static final String NUXEO_URL = "nuxeo.url";
055
056    @XNode("@name")
057    public String name;
058
059    @XNode("@address")
060    public String address;
061
062    @XNode("@implementor")
063    public Class<?> clazz;
064
065    @XNodeList(value = "handlers/handler", type = String[].class, componentType = Class.class)
066    public Class<? extends Handler>[] handlers;
067
068    @XNode("@namespace")
069    public String namespace;
070
071    @XNode("@wsdl")
072    public String wsdl;
073
074    @XNode("@port")
075    public String port;
076
077    @XNode("@service")
078    public String service;
079
080    @XNode("enable-mtom")
081    public boolean mtom;
082
083    @XNode("publishedEndpointUrl")
084    public String publishedEndpointUrl;
085
086    public Object getImplementorInstance() throws IllegalAccessException, InstantiationException {
087        return clazz != null ? clazz.newInstance() : null;
088    }
089
090    public Endpoint toEndpoint() throws IOException, IllegalAccessException, InstantiationException {
091        Endpoint ep = Endpoint.create(getImplementorInstance());
092        List<Source> metadata = new ArrayList<>();
093        Map<String, Object> properties = new HashMap<>();
094
095        if (!isBlank(port)) {
096            properties.put(WSDL_PORT, new QName(namespace, port));
097        }
098        if (!isBlank(port)) {
099            properties.put(WSDL_SERVICE, new QName(namespace, service));
100        }
101
102        if (!isBlank(wsdl)) {
103            URL wsdlURL = WSEndpointManagerImpl.class.getClassLoader().getResource(wsdl);
104            if (wsdlURL == null) {
105                throw new FileNotFoundException("WSDL: " + wsdl);
106            }
107            Source src = new StreamSource(wsdlURL.openStream());
108            src.setSystemId(wsdlURL.toExternalForm());
109            metadata.add(src);
110        }
111
112        if (isBlank(publishedEndpointUrl)) {
113            publishedEndpointUrl = String.format("%s%s%s", Framework.getProperty(NUXEO_URL),
114                    WSEndpointManager.WS_SERVLET, address);
115        }
116        properties.put("publishedEndpointUrl", publishedEndpointUrl);
117
118        ep.setMetadata(metadata);
119        ep.setProperties(properties);
120        return ep;
121    }
122
123    public void configurePostPublishing(Endpoint ep) throws IllegalAccessException, InstantiationException {
124        if (handlers != null) {
125            List<Handler> handlerChain = ep.getBinding().getHandlerChain();
126            for (Class<? extends Handler> handler : handlers) {
127                handlerChain.add(handler.newInstance());
128            }
129            ep.getBinding().setHandlerChain(handlerChain);
130        }
131
132        if (mtom && ep.getBinding() instanceof SOAPBinding) {
133            SOAPBinding binding = (SOAPBinding) ep.getBinding();
134            binding.setMTOMEnabled(mtom);
135        }
136    }
137}