Search in sources :

Example 1 with InputSupplier

use of eu.esdihumboldt.util.io.InputSupplier in project hale by halestudio.

the class BundleResolver method resolve.

/**
 * @see ResourceResolver#resolve(URI)
 */
@Override
public InputSupplier<? extends InputStream> resolve(URI uri) throws ResourceNotFoundException {
    if (bundle != null) {
        // OSGi
        final URL entry = bundle.getEntry(uri.getPath());
        if (entry == null) {
            throw new ResourceNotFoundException("Resource with path " + uri.getPath() + " not contained in bundle " + bundle.getSymbolicName());
        }
        preventDirectoryMatch(uri, entry);
        return new InputSupplier<InputStream>() {

            @Override
            public InputStream getInput() throws IOException {
                return entry.openStream();
            }
        };
    } else {
        // no OSGi
        // ClassLoader.getSystemClassLoader();
        final ClassLoader loader = getClass().getClassLoader();
        String pathCandidate = uri.getPath();
        final String path = (pathCandidate != null && pathCandidate.startsWith("/")) ? (pathCandidate.substring(1)) : (pathCandidate);
        Enumeration<URL> resources;
        try {
            resources = loader.getResources(path);
        } catch (IOException e) {
            log.error("Error accessing classpath resource", e);
            throw new ResourceNotFoundException(e);
        }
        if (resources.hasMoreElements()) {
            URL entry = resources.nextElement();
            // XXX not sure if this is needed here
            preventDirectoryMatch(uri, entry);
            return new InputSupplier<InputStream>() {

                @Override
                public InputStream getInput() throws IOException {
                    return loader.getResourceAsStream(path);
                }
            };
        } else {
            throw new ResourceNotFoundException();
        }
    }
}
Also used : IOException(java.io.IOException) ResourceNotFoundException(eu.esdihumboldt.util.resource.ResourceNotFoundException) URL(java.net.URL) InputSupplier(eu.esdihumboldt.util.io.InputSupplier)

Example 2 with InputSupplier

use of eu.esdihumboldt.util.io.InputSupplier in project hale by halestudio.

the class INSPIRECodeListReader method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Loading code list.", ProgressIndicator.UNKNOWN);
    try {
        Document doc;
        URI loc = getSource().getLocation();
        if (loc != null && (loc.getScheme().equals("http") || loc.getScheme().equals("https"))) {
            // and provide headers to retrieve correct format and language
            try {
                doc = loadXmlDocument(loc);
            } catch (Exception e) {
                // try local resources as fall-back
                InputSupplier<? extends InputStream> localInput = Resources.tryResolve(loc, Resources.RESOURCE_TYPE_XML_CODELIST);
                if (localInput != null) {
                    try (InputStream is = localInput.getInput()) {
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                        DocumentBuilder db = dbf.newDocumentBuilder();
                        doc = db.parse(is);
                    }
                } else
                    throw e;
            }
        } else {
            // just access stream
            try (InputStream is = getSource().getInput()) {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                doc = db.parse(is);
            }
        }
        reporter.setSuccess(parse(doc, loc, reporter));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    progress.setCurrentTask("Code list loaded.");
    return reporter;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) Document(org.w3c.dom.Document) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) InputSupplier(eu.esdihumboldt.util.io.InputSupplier)

Aggregations

InputSupplier (eu.esdihumboldt.util.io.InputSupplier)2 IOException (java.io.IOException)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 ResourceNotFoundException (eu.esdihumboldt.util.resource.ResourceNotFoundException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URL (java.net.URL)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 HttpResponseException (org.apache.http.client.HttpResponseException)1 Document (org.w3c.dom.Document)1 SAXException (org.xml.sax.SAXException)1