src/share/jaxws_classes/com/sun/xml/internal/ws/transport/http/DeploymentDescriptorParser.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/transport/http/DeploymentDescriptorParser.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,608 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.transport.http;
    1.30 +
    1.31 +import com.oracle.webservices.internal.api.databinding.DatabindingModeFeature;
    1.32 +import com.oracle.webservices.internal.api.databinding.ExternalMetadataFeature;
    1.33 +import com.sun.istack.internal.NotNull;
    1.34 +import com.sun.xml.internal.ws.api.BindingID;
    1.35 +import com.sun.xml.internal.ws.api.WSBinding;
    1.36 +import com.sun.xml.internal.ws.api.databinding.MetadataReader;
    1.37 +import com.sun.xml.internal.ws.api.server.Container;
    1.38 +import com.sun.xml.internal.ws.api.server.SDDocumentSource;
    1.39 +import com.sun.xml.internal.ws.api.server.WSEndpoint;
    1.40 +import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory;
    1.41 +import com.sun.xml.internal.ws.binding.WebServiceFeatureList;
    1.42 +
    1.43 +import com.sun.xml.internal.ws.handler.HandlerChainsModel;
    1.44 +import com.sun.xml.internal.ws.resources.ServerMessages;
    1.45 +import com.sun.xml.internal.ws.resources.WsservletMessages;
    1.46 +import com.sun.xml.internal.ws.server.EndpointFactory;
    1.47 +import com.sun.xml.internal.ws.server.ServerRtException;
    1.48 +import com.sun.xml.internal.ws.streaming.Attributes;
    1.49 +import com.sun.xml.internal.ws.streaming.TidyXMLStreamReader;
    1.50 +import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
    1.51 +import com.sun.xml.internal.ws.util.HandlerAnnotationInfo;
    1.52 +import com.sun.xml.internal.ws.util.exception.LocatableWebServiceException;
    1.53 +import com.sun.xml.internal.ws.util.xml.XmlUtil;
    1.54 +import org.xml.sax.EntityResolver;
    1.55 +
    1.56 +import javax.xml.namespace.QName;
    1.57 +import javax.xml.stream.XMLStreamConstants;
    1.58 +import javax.xml.stream.XMLStreamException;
    1.59 +import javax.xml.stream.XMLStreamReader;
    1.60 +import javax.xml.ws.WebServiceException;
    1.61 +import javax.xml.ws.WebServiceFeature;
    1.62 +import javax.xml.ws.http.HTTPBinding;
    1.63 +import javax.xml.ws.soap.MTOMFeature;
    1.64 +import javax.xml.ws.soap.SOAPBinding;
    1.65 +import java.io.File;
    1.66 +import java.io.FileInputStream;
    1.67 +import java.io.IOException;
    1.68 +import java.io.InputStream;
    1.69 +import java.net.MalformedURLException;
    1.70 +import java.net.URL;
    1.71 +import java.util.ArrayList;
    1.72 +import java.util.Arrays;
    1.73 +import java.util.HashMap;
    1.74 +import java.util.HashSet;
    1.75 +import java.util.List;
    1.76 +import java.util.Map;
    1.77 +import java.util.Set;
    1.78 +import java.util.logging.Level;
    1.79 +import java.util.logging.Logger;
    1.80 +
    1.81 +/**
    1.82 + * Parses {@code sun-jaxws.xml} into {@link WSEndpoint}.
    1.83 + * <p/>
    1.84 + * <p/>
    1.85 + * Since {@code sun-jaxws.xml} captures more information than what {@link WSEndpoint}
    1.86 + * represents (in particular URL pattern and name), this class
    1.87 + * takes a parameterization 'A' so that the user of this parser can choose to
    1.88 + * create another type that wraps {@link WSEndpoint}.
    1.89 + * <p/>
    1.90 + * {@link HttpAdapter} and its derived type is used for this often,
    1.91 + * but it can be anything.
    1.92 + *
    1.93 + * @author WS Development Team
    1.94 + * @author Kohsuke Kawaguchi
    1.95 + */
    1.96 +public class DeploymentDescriptorParser<A> {
    1.97 +
    1.98 +    public static final String NS_RUNTIME = "http://java.sun.com/xml/ns/jax-ws/ri/runtime";
    1.99 +    public static final String JAXWS_WSDL_DD_DIR = "WEB-INF/wsdl";
   1.100 +
   1.101 +    public static final QName QNAME_ENDPOINTS = new QName(NS_RUNTIME, "endpoints");
   1.102 +    public static final QName QNAME_ENDPOINT = new QName(NS_RUNTIME, "endpoint");
   1.103 +    public static final QName QNAME_EXT_METADA = new QName(NS_RUNTIME, "external-metadata");
   1.104 +
   1.105 +    public static final String ATTR_FILE = "file";
   1.106 +    public static final String ATTR_RESOURCE = "resource";
   1.107 +
   1.108 +    public static final String ATTR_VERSION = "version";
   1.109 +    public static final String ATTR_NAME = "name";
   1.110 +    public static final String ATTR_IMPLEMENTATION = "implementation";
   1.111 +    public static final String ATTR_WSDL = "wsdl";
   1.112 +    public static final String ATTR_SERVICE = "service";
   1.113 +    public static final String ATTR_PORT = "port";
   1.114 +    public static final String ATTR_URL_PATTERN = "url-pattern";
   1.115 +    public static final String ATTR_ENABLE_MTOM = "enable-mtom";
   1.116 +    public static final String ATTR_MTOM_THRESHOLD_VALUE = "mtom-threshold-value";
   1.117 +    public static final String ATTR_BINDING = "binding";
   1.118 +    public static final String ATTR_DATABINDING = "databinding";
   1.119 +
   1.120 +    public static final List<String> ATTRVALUE_SUPPORTED_VERSIONS = Arrays.asList("2.0", "2.1");
   1.121 +
   1.122 +    private static final Logger logger = Logger.getLogger(com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.http");
   1.123 +
   1.124 +    private final Container container;
   1.125 +    private final ClassLoader classLoader;
   1.126 +    private final ResourceLoader loader;
   1.127 +    private final AdapterFactory<A> adapterFactory;
   1.128 +
   1.129 +    /**
   1.130 +     * Endpoint names that are declared.
   1.131 +     * Used to catch double definitions.
   1.132 +     */
   1.133 +    private final Set<String> names = new HashSet<String>();
   1.134 +
   1.135 +    /**
   1.136 +     * WSDL/schema documents collected from /WEB-INF/wsdl. Keyed by the system ID.
   1.137 +     */
   1.138 +    private final Map<String, SDDocumentSource> docs = new HashMap<String, SDDocumentSource>();
   1.139 +
   1.140 +    /**
   1.141 +     * @param cl             Used to load service implementations.
   1.142 +     * @param loader         Used to locate resources, in particular WSDL.
   1.143 +     * @param container      Optional {@link Container} that {@link WSEndpoint}s receive.
   1.144 +     * @param adapterFactory Creates {@link HttpAdapter} (or its derived class.)
   1.145 +     */
   1.146 +    public DeploymentDescriptorParser(ClassLoader cl, ResourceLoader loader, Container container,
   1.147 +                                      AdapterFactory<A> adapterFactory) throws MalformedURLException {
   1.148 +        classLoader = cl;
   1.149 +        this.loader = loader;
   1.150 +        this.container = container;
   1.151 +        this.adapterFactory = adapterFactory;
   1.152 +
   1.153 +        collectDocs("/WEB-INF/wsdl/");
   1.154 +        logger.log(Level.FINE, "war metadata={0}", docs);
   1.155 +    }
   1.156 +
   1.157 +    /**
   1.158 +     * Parses the {@code sun-jaxws.xml} file and configures
   1.159 +     * a set of {@link HttpAdapter}s.
   1.160 +     */
   1.161 +    public @NotNull List<A> parse(String systemId, InputStream is) {
   1.162 +        XMLStreamReader reader = null;
   1.163 +        try {
   1.164 +            reader = new TidyXMLStreamReader(
   1.165 +                    XMLStreamReaderFactory.create(systemId, is, true), is);
   1.166 +            XMLStreamReaderUtil.nextElementContent(reader);
   1.167 +            return parseAdapters(reader);
   1.168 +        } finally {
   1.169 +            if (reader != null) {
   1.170 +                try {
   1.171 +                    reader.close();
   1.172 +                } catch (XMLStreamException e) {
   1.173 +                    throw new ServerRtException("runtime.parser.xmlReader", e);
   1.174 +                }
   1.175 +            }
   1.176 +            try {
   1.177 +                is.close();
   1.178 +            } catch (IOException e) {
   1.179 +                // ignore
   1.180 +            }
   1.181 +        }
   1.182 +    }
   1.183 +
   1.184 +    /**
   1.185 +     * Parses the {@code sun-jaxws.xml} file and configures
   1.186 +     * a set of {@link HttpAdapter}s.
   1.187 +     */
   1.188 +    public @NotNull List<A> parse(File f) throws IOException {
   1.189 +        FileInputStream in = new FileInputStream(f);
   1.190 +        try {
   1.191 +            return parse(f.getPath(), in);
   1.192 +        } finally {
   1.193 +            in.close();
   1.194 +        }
   1.195 +    }
   1.196 +
   1.197 +    /**
   1.198 +     * Get all the WSDL & schema documents recursively.
   1.199 +     */
   1.200 +    private void collectDocs(String dirPath) throws MalformedURLException {
   1.201 +        Set<String> paths = loader.getResourcePaths(dirPath);
   1.202 +        if (paths != null) {
   1.203 +            for (String path : paths) {
   1.204 +                if (path.endsWith("/")) {
   1.205 +                    if (path.endsWith("/CVS/") || path.endsWith("/.svn/")) {
   1.206 +                        continue;
   1.207 +                    }
   1.208 +                    collectDocs(path);
   1.209 +                } else {
   1.210 +                    URL res = loader.getResource(path);
   1.211 +                    docs.put(res.toString(), SDDocumentSource.create(res));
   1.212 +                }
   1.213 +            }
   1.214 +        }
   1.215 +    }
   1.216 +
   1.217 +
   1.218 +    private List<A> parseAdapters(XMLStreamReader reader) {
   1.219 +        if (!reader.getName().equals(QNAME_ENDPOINTS)) {
   1.220 +            failWithFullName("runtime.parser.invalidElement", reader);
   1.221 +        }
   1.222 +
   1.223 +        List<A> adapters = new ArrayList<A>();
   1.224 +
   1.225 +        Attributes attrs = XMLStreamReaderUtil.getAttributes(reader);
   1.226 +        String version = getMandatoryNonEmptyAttribute(reader, attrs, ATTR_VERSION);
   1.227 +        if (!ATTRVALUE_SUPPORTED_VERSIONS.contains(version)) {
   1.228 +            failWithLocalName("runtime.parser.invalidVersionNumber", reader, version);
   1.229 +        }
   1.230 +
   1.231 +        while (XMLStreamReaderUtil.nextElementContent(reader) != XMLStreamConstants.END_ELEMENT) {
   1.232 +
   1.233 +            if (reader.getName().equals(QNAME_ENDPOINT)) {
   1.234 +                attrs = XMLStreamReaderUtil.getAttributes(reader);
   1.235 +
   1.236 +                String name = getMandatoryNonEmptyAttribute(reader, attrs, ATTR_NAME);
   1.237 +                if (!names.add(name)) {
   1.238 +                    logger.warning(
   1.239 +                            WsservletMessages.SERVLET_WARNING_DUPLICATE_ENDPOINT_NAME(/*name*/));
   1.240 +                }
   1.241 +
   1.242 +                String implementationName =
   1.243 +                        getMandatoryNonEmptyAttribute(reader, attrs, ATTR_IMPLEMENTATION);
   1.244 +                Class<?> implementorClass = getImplementorClass(implementationName, reader);
   1.245 +
   1.246 +                MetadataReader metadataReader = null;
   1.247 +                ExternalMetadataFeature externalMetadataFeature = null;
   1.248 +
   1.249 +                // parse subelements to instantiate externalMetadataReader, if necessary ...
   1.250 +                XMLStreamReaderUtil.nextElementContent(reader);
   1.251 +                if (reader.getEventType() != XMLStreamConstants.END_ELEMENT) {
   1.252 +                    externalMetadataFeature = configureExternalMetadataReader(reader);
   1.253 +                    if (externalMetadataFeature != null) {
   1.254 +                        metadataReader = externalMetadataFeature.getMetadataReader(implementorClass.getClassLoader(), false);
   1.255 +                    }
   1.256 +                }
   1.257 +
   1.258 +                QName serviceName = getQNameAttribute(attrs, ATTR_SERVICE);
   1.259 +                if (serviceName == null) {
   1.260 +                    serviceName = EndpointFactory.getDefaultServiceName(implementorClass, metadataReader);
   1.261 +                }
   1.262 +
   1.263 +                QName portName = getQNameAttribute(attrs, ATTR_PORT);
   1.264 +                if (portName == null) {
   1.265 +                    portName = EndpointFactory.getDefaultPortName(serviceName, implementorClass, metadataReader);
   1.266 +                }
   1.267 +
   1.268 +                //get enable-mtom attribute value
   1.269 +                String enable_mtom = getAttribute(attrs, ATTR_ENABLE_MTOM);
   1.270 +                String mtomThreshold = getAttribute(attrs, ATTR_MTOM_THRESHOLD_VALUE);
   1.271 +                String dbMode = getAttribute(attrs, ATTR_DATABINDING);
   1.272 +                String bindingId = getAttribute(attrs, ATTR_BINDING);
   1.273 +                if (bindingId != null) {
   1.274 +                    // Convert short-form tokens to API's binding ids
   1.275 +                    bindingId = getBindingIdForToken(bindingId);
   1.276 +                }
   1.277 +                WSBinding binding = createBinding(bindingId, implementorClass, enable_mtom, mtomThreshold, dbMode);
   1.278 +                if (externalMetadataFeature != null) {
   1.279 +                        binding.getFeatures().mergeFeatures(new WebServiceFeature[]{externalMetadataFeature},
   1.280 +                        true);
   1.281 +                }
   1.282 +
   1.283 +                String urlPattern = getMandatoryNonEmptyAttribute(reader, attrs, ATTR_URL_PATTERN);
   1.284 +
   1.285 +                // TODO use 'docs' as the metadata. If wsdl is non-null it's the primary.
   1.286 +                boolean handlersSetInDD = setHandlersAndRoles(binding, reader, serviceName, portName);
   1.287 +
   1.288 +                EndpointFactory.verifyImplementorClass(implementorClass, metadataReader);
   1.289 +                SDDocumentSource primaryWSDL = getPrimaryWSDL(reader, attrs, implementorClass, metadataReader);
   1.290 +
   1.291 +                WSEndpoint<?> endpoint = WSEndpoint.create(
   1.292 +                        implementorClass, !handlersSetInDD,
   1.293 +                        null,
   1.294 +                        serviceName, portName, container, binding,
   1.295 +                        primaryWSDL, docs.values(), createEntityResolver(), false
   1.296 +                );
   1.297 +                adapters.add(adapterFactory.createAdapter(name, urlPattern, endpoint));
   1.298 +            } else {
   1.299 +                failWithLocalName("runtime.parser.invalidElement", reader);
   1.300 +            }
   1.301 +        }
   1.302 +        return adapters;
   1.303 +    }
   1.304 +
   1.305 +    /**
   1.306 +     * @param ddBindingId   binding id explicitlyspecified in the DeploymentDescriptor or parameter
   1.307 +     * @param implClass     Endpoint Implementation class
   1.308 +     * @param mtomEnabled   represents mtom-enabled attribute in DD
   1.309 +     * @param mtomThreshold threshold value specified in DD
   1.310 +     * @return is returned with only MTOMFeature set resolving the various precendece rules
   1.311 +     */
   1.312 +    private static WSBinding createBinding(String ddBindingId, Class implClass,
   1.313 +                                           String mtomEnabled, String mtomThreshold, String dataBindingMode) {
   1.314 +        // Features specified through DD
   1.315 +        WebServiceFeatureList features;
   1.316 +
   1.317 +        MTOMFeature mtomfeature = null;
   1.318 +        if (mtomEnabled != null) {
   1.319 +            if (mtomThreshold != null) {
   1.320 +                mtomfeature = new MTOMFeature(Boolean.valueOf(mtomEnabled),
   1.321 +                        Integer.valueOf(mtomThreshold));
   1.322 +            } else {
   1.323 +                mtomfeature = new MTOMFeature(Boolean.valueOf(mtomEnabled));
   1.324 +            }
   1.325 +        }
   1.326 +
   1.327 +        BindingID bindingID;
   1.328 +        if (ddBindingId != null) {
   1.329 +            bindingID = BindingID.parse(ddBindingId);
   1.330 +            features = bindingID.createBuiltinFeatureList();
   1.331 +
   1.332 +            if (checkMtomConflict(features.get(MTOMFeature.class), mtomfeature)) {
   1.333 +                throw new ServerRtException(ServerMessages.DD_MTOM_CONFLICT(ddBindingId, mtomEnabled));
   1.334 +            }
   1.335 +        } else {
   1.336 +            bindingID = BindingID.parse(implClass);
   1.337 +            // Since bindingID is coming from implclass,
   1.338 +            // mtom through Feature annotation or DD takes precendece
   1.339 +
   1.340 +            features = new WebServiceFeatureList();
   1.341 +            if (mtomfeature != null) {  // this wins over MTOM setting in bindingID
   1.342 +                features.add(mtomfeature);
   1.343 +            }
   1.344 +            features.addAll(bindingID.createBuiltinFeatureList());
   1.345 +        }
   1.346 +
   1.347 +        if (dataBindingMode != null) {
   1.348 +            features.add(new DatabindingModeFeature(dataBindingMode));
   1.349 +        }
   1.350 +
   1.351 +        return bindingID.createBinding(features.toArray());
   1.352 +    }
   1.353 +
   1.354 +    private static boolean checkMtomConflict(MTOMFeature lhs, MTOMFeature rhs) {
   1.355 +        if (lhs == null || rhs == null) {
   1.356 +            return false;
   1.357 +        }
   1.358 +        return lhs.isEnabled() ^ rhs.isEnabled();
   1.359 +    }
   1.360 +
   1.361 +    /**
   1.362 +     * JSR-109 defines short-form tokens for standard binding Ids. These are
   1.363 +     * used only in DD. So stand alone deployment descirptor should also honor
   1.364 +     * these tokens. This method converts the tokens to API's standard
   1.365 +     * binding ids
   1.366 +     *
   1.367 +     * @param lexical binding attribute value from DD. Always not null
   1.368 +     * @return returns corresponding API's binding ID or the same lexical
   1.369 +     */
   1.370 +    public static @NotNull String getBindingIdForToken(@NotNull String lexical) {
   1.371 +        if (lexical.equals("##SOAP11_HTTP")) {
   1.372 +            return SOAPBinding.SOAP11HTTP_BINDING;
   1.373 +        } else if (lexical.equals("##SOAP11_HTTP_MTOM")) {
   1.374 +            return SOAPBinding.SOAP11HTTP_MTOM_BINDING;
   1.375 +        } else if (lexical.equals("##SOAP12_HTTP")) {
   1.376 +            return SOAPBinding.SOAP12HTTP_BINDING;
   1.377 +        } else if (lexical.equals("##SOAP12_HTTP_MTOM")) {
   1.378 +            return SOAPBinding.SOAP12HTTP_MTOM_BINDING;
   1.379 +        } else if (lexical.equals("##XML_HTTP")) {
   1.380 +            return HTTPBinding.HTTP_BINDING;
   1.381 +        }
   1.382 +        return lexical;
   1.383 +    }
   1.384 +
   1.385 +    /**
   1.386 +     * Creates a new "Adapter".
   1.387 +     * <p/>
   1.388 +     * Normally 'A' would be {@link HttpAdapter} or some derived class.
   1.389 +     * But the parser doesn't require that to be of any particular type.
   1.390 +     */
   1.391 +    public static interface AdapterFactory<A> {
   1.392 +        A createAdapter(String name, String urlPattern, WSEndpoint<?> endpoint);
   1.393 +    }
   1.394 +
   1.395 +    /**
   1.396 +     * Checks the deployment descriptor or {@link @WebServiceProvider} annotation
   1.397 +     * to see if it points to any WSDL. If so, returns the {@link SDDocumentSource}.
   1.398 +     *
   1.399 +     * @return The pointed WSDL, if any. Otherwise null.
   1.400 +     */
   1.401 +    private SDDocumentSource getPrimaryWSDL(XMLStreamReader xsr, Attributes attrs, Class<?> implementorClass, MetadataReader metadataReader) {
   1.402 +
   1.403 +        String wsdlFile = getAttribute(attrs, ATTR_WSDL);
   1.404 +        if (wsdlFile == null) {
   1.405 +            wsdlFile = EndpointFactory.getWsdlLocation(implementorClass, metadataReader);
   1.406 +        }
   1.407 +
   1.408 +        if (wsdlFile != null) {
   1.409 +            if (!wsdlFile.startsWith(JAXWS_WSDL_DD_DIR)) {
   1.410 +                logger.log(Level.WARNING, "Ignoring wrong wsdl={0}. It should start with {1}. Going to generate and publish a new WSDL.", new Object[]{wsdlFile, JAXWS_WSDL_DD_DIR});
   1.411 +                return null;
   1.412 +            }
   1.413 +
   1.414 +            URL wsdl;
   1.415 +            try {
   1.416 +                wsdl = loader.getResource('/' + wsdlFile);
   1.417 +            } catch (MalformedURLException e) {
   1.418 +                throw new LocatableWebServiceException(
   1.419 +                        ServerMessages.RUNTIME_PARSER_WSDL_NOT_FOUND(wsdlFile), e, xsr);
   1.420 +            }
   1.421 +            if (wsdl == null) {
   1.422 +                throw new LocatableWebServiceException(
   1.423 +                        ServerMessages.RUNTIME_PARSER_WSDL_NOT_FOUND(wsdlFile), xsr);
   1.424 +            }
   1.425 +            SDDocumentSource docInfo = docs.get(wsdl.toExternalForm());
   1.426 +            assert docInfo != null;
   1.427 +            return docInfo;
   1.428 +        }
   1.429 +
   1.430 +        return null;
   1.431 +    }
   1.432 +
   1.433 +    /**
   1.434 +     * Creates an {@link EntityResolver} that consults {@code /WEB-INF/jax-ws-catalog.xml}.
   1.435 +     */
   1.436 +    private EntityResolver createEntityResolver() {
   1.437 +        try {
   1.438 +            return XmlUtil.createEntityResolver(loader.getCatalogFile());
   1.439 +        } catch (MalformedURLException e) {
   1.440 +            throw new WebServiceException(e);
   1.441 +        }
   1.442 +    }
   1.443 +
   1.444 +    protected String getAttribute(Attributes attrs, String name) {
   1.445 +        String value = attrs.getValue(name);
   1.446 +        if (value != null) {
   1.447 +            value = value.trim();
   1.448 +        }
   1.449 +        return value;
   1.450 +    }
   1.451 +
   1.452 +    protected QName getQNameAttribute(Attributes attrs, String name) {
   1.453 +        String value = getAttribute(attrs, name);
   1.454 +        if (value == null || value.equals("")) {
   1.455 +            return null;
   1.456 +        } else {
   1.457 +            return QName.valueOf(value);
   1.458 +        }
   1.459 +    }
   1.460 +
   1.461 +    protected String getNonEmptyAttribute(XMLStreamReader reader, Attributes attrs, String name) {
   1.462 +        String value = getAttribute(attrs, name);
   1.463 +        if (value != null && value.equals("")) {
   1.464 +            failWithLocalName(
   1.465 +                    "runtime.parser.invalidAttributeValue",
   1.466 +                    reader,
   1.467 +                    name);
   1.468 +        }
   1.469 +        return value;
   1.470 +    }
   1.471 +
   1.472 +    protected String getMandatoryAttribute(XMLStreamReader reader, Attributes attrs, String name) {
   1.473 +        String value = getAttribute(attrs, name);
   1.474 +        if (value == null) {
   1.475 +            failWithLocalName("runtime.parser.missing.attribute", reader, name);
   1.476 +        }
   1.477 +        return value;
   1.478 +    }
   1.479 +
   1.480 +    protected String getMandatoryNonEmptyAttribute(XMLStreamReader reader, Attributes attributes,
   1.481 +                                                   String name) {
   1.482 +        String value = getAttribute(attributes, name);
   1.483 +        if (value == null) {
   1.484 +            failWithLocalName("runtime.parser.missing.attribute", reader, name);
   1.485 +        } else if (value.equals("")) {
   1.486 +            failWithLocalName(
   1.487 +                    "runtime.parser.invalidAttributeValue",
   1.488 +                    reader,
   1.489 +                    name);
   1.490 +        }
   1.491 +        return value;
   1.492 +    }
   1.493 +
   1.494 +    /**
   1.495 +     * Parses the handler and role information and sets it
   1.496 +     * on the {@link WSBinding}.
   1.497 +     *
   1.498 +     * @return true if <handler-chains> element present in DD
   1.499 +     *         false otherwise.
   1.500 +     */
   1.501 +    protected boolean setHandlersAndRoles(WSBinding binding, XMLStreamReader reader, QName serviceName, QName portName) {
   1.502 +
   1.503 +        if (reader.getEventType() == XMLStreamConstants.END_ELEMENT ||
   1.504 +                !reader.getName().equals(HandlerChainsModel.QNAME_HANDLER_CHAINS)) {
   1.505 +            return false;
   1.506 +        }
   1.507 +
   1.508 +        HandlerAnnotationInfo handlerInfo = HandlerChainsModel.parseHandlerFile(
   1.509 +                reader, classLoader, serviceName, portName, binding);
   1.510 +
   1.511 +        binding.setHandlerChain(handlerInfo.getHandlers());
   1.512 +        if (binding instanceof SOAPBinding) {
   1.513 +            ((SOAPBinding) binding).setRoles(handlerInfo.getRoles());
   1.514 +        }
   1.515 +
   1.516 +        // move past </handler-chains>
   1.517 +        XMLStreamReaderUtil.nextContent(reader);
   1.518 +        return true;
   1.519 +    }
   1.520 +
   1.521 +    protected ExternalMetadataFeature configureExternalMetadataReader(XMLStreamReader reader) {
   1.522 +
   1.523 +        ExternalMetadataFeature.Builder featureBuilder = null;
   1.524 +        while (QNAME_EXT_METADA.equals(reader.getName())) {
   1.525 +
   1.526 +            if (reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
   1.527 +                Attributes attrs = XMLStreamReaderUtil.getAttributes(reader);
   1.528 +                String file = getAttribute(attrs, ATTR_FILE);
   1.529 +                if (file != null) {
   1.530 +                    if (featureBuilder == null) {
   1.531 +                        featureBuilder = ExternalMetadataFeature.builder();
   1.532 +                    }
   1.533 +                    featureBuilder.addFiles(new File(file));
   1.534 +                }
   1.535 +
   1.536 +                String res = getAttribute(attrs, ATTR_RESOURCE);
   1.537 +                if (res != null) {
   1.538 +                    if (featureBuilder == null) {
   1.539 +                        featureBuilder = ExternalMetadataFeature.builder();
   1.540 +                    }
   1.541 +                    featureBuilder.addResources(res);
   1.542 +                }
   1.543 +            }
   1.544 +
   1.545 +            XMLStreamReaderUtil.nextElementContent(reader);
   1.546 +        }
   1.547 +
   1.548 +        return buildFeature(featureBuilder);
   1.549 +    }
   1.550 +
   1.551 +    private ExternalMetadataFeature buildFeature(ExternalMetadataFeature.Builder builder) {
   1.552 +        return builder != null ? builder.build() : null;
   1.553 +    }
   1.554 +
   1.555 +    protected static void fail(String key, XMLStreamReader reader) {
   1.556 +        logger.log(Level.SEVERE, "{0}{1}", new Object[]{key, reader.getLocation().getLineNumber()});
   1.557 +        throw new ServerRtException(
   1.558 +                key,
   1.559 +                Integer.toString(reader.getLocation().getLineNumber()));
   1.560 +    }
   1.561 +
   1.562 +    protected static void failWithFullName(String key, XMLStreamReader reader) {
   1.563 +        throw new ServerRtException(
   1.564 +                key,
   1.565 +                reader.getLocation().getLineNumber(),
   1.566 +                reader.getName());
   1.567 +    }
   1.568 +
   1.569 +    protected static void failWithLocalName(String key, XMLStreamReader reader) {
   1.570 +        throw new ServerRtException(
   1.571 +                key,
   1.572 +                reader.getLocation().getLineNumber(),
   1.573 +                reader.getLocalName());
   1.574 +    }
   1.575 +
   1.576 +    protected static void failWithLocalName(String key, XMLStreamReader reader, String arg) {
   1.577 +        throw new ServerRtException(
   1.578 +                key,
   1.579 +                reader.getLocation().getLineNumber(),
   1.580 +                reader.getLocalName(),
   1.581 +                arg);
   1.582 +    }
   1.583 +
   1.584 +    protected Class loadClass(String name) {
   1.585 +        try {
   1.586 +            return Class.forName(name, true, classLoader);
   1.587 +        } catch (ClassNotFoundException e) {
   1.588 +            logger.log(Level.SEVERE, e.getMessage(), e);
   1.589 +            throw new ServerRtException(
   1.590 +                    "runtime.parser.classNotFound",
   1.591 +                    name);
   1.592 +        }
   1.593 +    }
   1.594 +
   1.595 +
   1.596 +    /**
   1.597 +     * Loads the class of the given name.
   1.598 +     *
   1.599 +     * @param xsr Used to report the source location information if there's any error.
   1.600 +     */
   1.601 +    private Class getImplementorClass(String name, XMLStreamReader xsr) {
   1.602 +        try {
   1.603 +            return Class.forName(name, true, classLoader);
   1.604 +        } catch (ClassNotFoundException e) {
   1.605 +            logger.log(Level.SEVERE, e.getMessage(), e);
   1.606 +            throw new LocatableWebServiceException(
   1.607 +                    ServerMessages.RUNTIME_PARSER_CLASS_NOT_FOUND(name), e, xsr);
   1.608 +        }
   1.609 +    }
   1.610 +
   1.611 +}

mercurial