Search in sources :

Example 6 with URIResolver

use of org.apache.cxf.resource.URIResolver in project cxf by apache.

the class AbstractServiceFactoryBean method fillDataBindingSchemas.

private void fillDataBindingSchemas() {
    ResourceManager rr = getBus().getExtension(ResourceManager.class);
    List<DOMSource> schemas = new ArrayList<>();
    for (String l : schemaLocations) {
        URL url = rr.resolveResource(l, URL.class);
        if (url == null) {
            try (URIResolver res = new URIResolver(l)) {
                if (!res.isResolved()) {
                    throw new ServiceConstructionException(new Message("INVALID_SCHEMA_URL", LOG, l));
                }
                url = res.getURL();
            } catch (IOException e) {
                throw new ServiceConstructionException(new Message("INVALID_SCHEMA_URL", LOG, l), e);
            }
        }
        Document d;
        try (InputStream in = url.openStream()) {
            d = StaxUtils.read(in);
        } catch (Exception e) {
            throw new ServiceConstructionException(new Message("ERROR_READING_SCHEMA", LOG, l), e);
        }
        schemas.add(new DOMSource(d, url.toString()));
    }
    ((AbstractDataBinding) getDataBinding()).setSchemas(schemas);
}
Also used : AbstractDataBinding(org.apache.cxf.databinding.AbstractDataBinding) DOMSource(javax.xml.transform.dom.DOMSource) Message(org.apache.cxf.common.i18n.Message) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ModCountCopyOnWriteArrayList(org.apache.cxf.common.util.ModCountCopyOnWriteArrayList) URIResolver(org.apache.cxf.resource.URIResolver) ResourceManager(org.apache.cxf.resource.ResourceManager) IOException(java.io.IOException) Document(org.w3c.dom.Document) URL(java.net.URL) IOException(java.io.IOException)

Example 7 with URIResolver

use of org.apache.cxf.resource.URIResolver in project cxf by apache.

the class WSDL11Validator method addDefaultSchemas.

private void addDefaultSchemas(String location, List<InputSource> xsdList) throws IOException {
    ClassLoader clzLoader = Thread.currentThread().getContextClassLoader();
    Enumeration<URL> urls = clzLoader.getResources(location);
    while (urls.hasMoreElements()) {
        URL url = urls.nextElement();
        // from jar files
        if (url.toString().startsWith("jar")) {
            JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
            JarFile jarFile = jarConnection.getJarFile();
            Enumeration<JarEntry> entry = jarFile.entries();
            while (entry.hasMoreElements()) {
                JarEntry ele = entry.nextElement();
                if (ele.getName().endsWith(".xsd") && ele.getName().indexOf(ToolConstants.CXF_SCHEMAS_DIR_INJAR) > -1) {
                    URIResolver resolver = new URIResolver(ele.getName());
                    if (resolver.isResolved()) {
                        InputSource is = new InputSource(resolver.getInputStream());
                        // the id to the relative path.
                        if (resolver.getURI() != null) {
                            is.setSystemId(resolver.getURI().toString());
                        } else {
                            is.setSystemId(ele.getName());
                        }
                        xsdList.add(is);
                    }
                }
            }
        // from class path direcotry
        } else if (url.toString().startsWith("file")) {
            URI loc = null;
            try {
                loc = url.toURI();
            } catch (URISyntaxException e) {
            // 
            }
            if (loc != null) {
                java.io.File file = new java.io.File(loc);
                if (file.exists()) {
                    File[] files = file.listFiles(new FileFilter() {

                        public boolean accept(File pathname) {
                            if (pathname.getAbsolutePath().endsWith(".xsd")) {
                                return true;
                            }
                            return false;
                        }
                    });
                    if (files != null) {
                        for (int i = 0; i < files.length; i++) {
                            InputSource is = new InputSource(files[i].toURI().toURL().openStream());
                            is.setSystemId(files[i].toURI().toURL().toString());
                            xsdList.add(is);
                        }
                    }
                }
            }
        }
    }
    sort(xsdList);
}
Also used : InputSource(org.xml.sax.InputSource) JarURLConnection(java.net.JarURLConnection) URIResolver(org.apache.cxf.resource.URIResolver) URISyntaxException(java.net.URISyntaxException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URI(java.net.URI) URL(java.net.URL) File(java.io.File) FileFilter(java.io.FileFilter) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 8 with URIResolver

use of org.apache.cxf.resource.URIResolver in project cxf by apache.

the class JAXWSContainer method validate.

public void validate(ToolContext env) throws ToolException {
    env.put("service.target", getServiceTarget());
    env.put("service.superclass", getServiceSuperclass());
    super.validate(env);
    if (env.containsKey(ToolConstants.CFG_BINDING)) {
        String[] bindings = (String[]) env.get(ToolConstants.CFG_BINDING);
        for (int i = 0; i < bindings.length; i++) {
            try (URIResolver resolver = new URIResolver(bindings[i])) {
                if (!resolver.isResolved()) {
                    Message msg = new Message("FILE_NOT_EXIST", LOG, bindings[i]);
                    throw new ToolException(msg);
                }
            } catch (IOException ioe) {
                throw new ToolException(ioe);
            }
        }
        env.put(ToolConstants.CFG_BINDING, bindings);
    }
    cleanArrays(env, ToolConstants.CFG_ASYNCMETHODS);
    cleanArrays(env, ToolConstants.CFG_BAREMETHODS);
    cleanArrays(env, ToolConstants.CFG_MIMEMETHODS);
}
Also used : Message(org.apache.cxf.common.i18n.Message) URIResolver(org.apache.cxf.resource.URIResolver) ToolException(org.apache.cxf.tools.common.ToolException) IOException(java.io.IOException)

Example 9 with URIResolver

use of org.apache.cxf.resource.URIResolver in project cxf by apache.

the class CustomizationParser method addBinding.

private void addBinding(String bindingFile) throws XMLStreamException {
    final Element root;
    try (URIResolver resolver = new URIResolver(bindingFile)) {
        root = StaxUtils.read(resolver.getInputStream()).getDocumentElement();
    } catch (Exception e1) {
        Message msg = new Message("CAN_NOT_READ_AS_ELEMENT", LOG, new Object[] { bindingFile });
        throw new ToolException(msg, e1);
    }
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(root);
    StaxUtils.toNextTag(reader);
    if (isValidJaxbBindingFile(reader)) {
        String schemaLocation = root.getAttribute("schemaLocation");
        String resolvedSchemaLocation = resolveByCatalog(schemaLocation);
        if (resolvedSchemaLocation == null) {
            resolvedSchemaLocation = schemaLocation.isEmpty() ? wadlPath : getBaseWadlPath() + schemaLocation;
        }
        try {
            jaxbBindings.add(convertToTmpInputSource(root, resolvedSchemaLocation));
        } catch (Exception e1) {
            Message msg = new Message("FAILED_TO_ADD_SCHEMALOCATION", LOG, bindingFile);
            throw new ToolException(msg, e1);
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) Message(org.apache.cxf.common.i18n.Message) Element(org.w3c.dom.Element) URIResolver(org.apache.cxf.resource.URIResolver) ToolException(org.apache.cxf.tools.common.ToolException) XMLStreamException(javax.xml.stream.XMLStreamException) ToolException(org.apache.cxf.tools.common.ToolException)

Example 10 with URIResolver

use of org.apache.cxf.resource.URIResolver in project cxf by apache.

the class CustomizationParser method addBinding.

private void addBinding(String bindingFile) throws XMLStreamException {
    final Element root;
    XMLStreamReader xmlReader = null;
    try (URIResolver resolver = new URIResolver(bindingFile)) {
        xmlReader = StaxUtils.createXMLStreamReader(resolver.getURI().toString(), resolver.getInputStream());
        root = StaxUtils.read(xmlReader, true).getDocumentElement();
    } catch (Exception e1) {
        Message msg = new Message("CAN_NOT_READ_AS_ELEMENT", LOG, new Object[] { bindingFile });
        throw new ToolException(msg, e1);
    } finally {
        StaxUtils.close(xmlReader);
    }
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(root);
    StaxUtils.toNextTag(reader);
    if (isValidJaxwsBindingFile(bindingFile, reader)) {
        String wsdlLocation = root.getAttribute("wsdlLocation");
        Element targetNode;
        if (!StringUtils.isEmpty(wsdlLocation)) {
            String wsdlURI = getAbsoluteURI(wsdlLocation, bindingFile);
            targetNode = getTargetNode(wsdlURI);
            String resolvedLoc = wsdlURI;
            if (targetNode == null && env.get(ToolConstants.CFG_CATALOG) != null) {
                resolvedLoc = resolveByCatalog(wsdlURI);
                targetNode = getTargetNode(resolvedLoc);
            }
            if (targetNode == null) {
                Message msg = new Message("POINT_TO_WSDL_DOES_NOT_EXIST", LOG, new Object[] { bindingFile, resolvedLoc });
                throw new ToolException(msg);
            }
            root.setAttributeNS(null, "wsdlLocation", wsdlURI);
        } else {
            targetNode = wsdlNode;
            root.setAttributeNS(null, "wsdlLocation", wsdlURL);
        }
        jaxwsBindingsMap.put(root, targetNode);
    } else if (isValidJaxbBindingFile(reader)) {
        String schemaLocation = root.getAttribute("schemaLocation");
        String resolvedSchemaLocation = resolveByCatalog(schemaLocation);
        if (resolvedSchemaLocation != null) {
            final InputSource tmpIns;
            try {
                tmpIns = convertToTmpInputSource(root, resolvedSchemaLocation);
            } catch (Exception e1) {
                Message msg = new Message("FAILED_TO_ADD_SCHEMALOCATION", LOG, bindingFile);
                throw new ToolException(msg, e1);
            }
            jaxbBindings.add(tmpIns);
        } else {
            jaxbBindings.add(new InputSource(bindingFile));
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) Message(org.apache.cxf.common.i18n.Message) Element(org.w3c.dom.Element) URIResolver(org.apache.cxf.resource.URIResolver) ToolException(org.apache.cxf.tools.common.ToolException) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException)

Aggregations

URIResolver (org.apache.cxf.resource.URIResolver)10 IOException (java.io.IOException)8 Message (org.apache.cxf.common.i18n.Message)6 URISyntaxException (java.net.URISyntaxException)4 ToolException (org.apache.cxf.tools.common.ToolException)4 URL (java.net.URL)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 InputSource (org.xml.sax.InputSource)2 File (java.io.File)1 FileFilter (java.io.FileFilter)1 InputStream (java.io.InputStream)1 JarURLConnection (java.net.JarURLConnection)1 URI (java.net.URI)1 PrivilegedAction (java.security.PrivilegedAction)1 ArrayList (java.util.ArrayList)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1