Search in sources :

Example 1 with TypedInputStream

use of org.apache.jena.atlas.web.TypedInputStream in project jena by apache.

the class LocatorClassLoader method open.

@Override
public TypedInputStream open(String resourceName) {
    if (classLoader == null)
        return null;
    InputStream in = classLoader.getResourceAsStream(resourceName);
    if (in == null) {
        if (StreamManager.logAllLookups && log.isTraceEnabled())
            log.trace("Failed to open: " + resourceName);
        return null;
    }
    if (StreamManager.logAllLookups && log.isTraceEnabled())
        log.trace("Found: " + resourceName);
    ContentType ct = RDFLanguages.guessContentType(resourceName);
    // No sensible base URI.
    return new TypedInputStream(in, ct, null);
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) InputStream(java.io.InputStream) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream)

Example 2 with TypedInputStream

use of org.apache.jena.atlas.web.TypedInputStream in project jena by apache.

the class LocatorFile method open.

/** Open anything that looks a bit like a file name */
@Override
public TypedInputStream open(String filenameIRI) {
    String fn = toFileName(filenameIRI);
    if (fn == null)
        return null;
    try {
        if (!exists$(fn)) {
            if (StreamManager.logAllLookups && log.isTraceEnabled())
                log.trace("Not found: " + filenameIRI + thisDirLogStr);
            return null;
        }
    } catch (AccessControlException e) {
        log.warn("Security problem testing for file", e);
        return null;
    }
    try {
        InputStream in = IO.openFileEx(fn);
        if (StreamManager.logAllLookups && log.isTraceEnabled())
            log.trace("Found: " + filenameIRI + thisDirLogStr);
        ContentType ct = RDFLanguages.guessContentType(filenameIRI);
        return new TypedInputStream(in, ct, filenameIRI);
    } catch (IOException ioEx) {
        // Includes FileNotFoundException
        // We already tested whether the file exists or not.
        log.warn("File unreadable (but exists): " + fn + " Exception: " + ioEx.getMessage());
        return null;
    }
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) InputStream(java.io.InputStream) AccessControlException(java.security.AccessControlException) IOException(java.io.IOException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream)

Example 3 with TypedInputStream

use of org.apache.jena.atlas.web.TypedInputStream in project jena by apache.

the class AdapterFileManager method readModelWorker.

// -------- Cache operations (end)
@Override
protected Model readModelWorker(Model model, String filenameOrURI, String baseURI, String syntax) {
    // Doesn't call open() - we want to make the syntax guess
    // based on the mapped URI.
    String mappedURI = mapURI(filenameOrURI);
    if (log.isDebugEnabled() && !mappedURI.equals(filenameOrURI))
        log.debug("Map: " + filenameOrURI + " => " + mappedURI);
    Lang lang = (syntax != null) ? RDFLanguages.nameToLang(syntax) : RDFLanguages.resourceNameToLang(mappedURI, Lang.RDFXML);
    // Allow model.read to be overridden e.g. by OntModel which does import processing.   
    if (baseURI == null)
        baseURI = SysRIOT.chooseBaseIRI(filenameOrURI);
    try (TypedInputStream in = streamManager.openNoMapOrNull(mappedURI)) {
        Lang lang2 = RDFDataMgr.determineLang(mappedURI, in.getContentType(), lang);
        // May be overridden by model implementation.
        model.read(in, baseURI, lang2.getName());
    }
    return model;
}
Also used : Lang(org.apache.jena.riot.Lang) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream)

Example 4 with TypedInputStream

use of org.apache.jena.atlas.web.TypedInputStream in project jena by apache.

the class RDFParser method parseURI.

/** Parse when there is a URI to guide the choice of syntax */
private void parseURI(StreamRDF destination) {
    // Source by uri or path.
    try (TypedInputStream input = openTypedInputStream(uri, path)) {
        ReaderRIOT reader;
        ContentType ct;
        if (forceLang != null) {
            @SuppressWarnings("deprecation") ReaderRIOTFactory r = RDFParserRegistry.getFactory(forceLang);
            if (r == null)
                throw new RiotException("No parser registered for language: " + forceLang);
            ct = forceLang.getContentType();
            reader = createReader(r, forceLang);
        } else {
            // No forced language.
            // Conneg and hint, ignoring text/plain.
            ct = WebContent.determineCT(input.getContentType(), hintLang, baseUri);
            if (ct == null)
                throw new RiotException("Failed to determine the content type: (URI=" + baseUri + " : stream=" + input.getContentType() + ")");
            reader = createReader(ct);
            if (reader == null)
                throw new RiotException("No parser registered for content type: " + ct.getContentType());
        }
        read(reader, input, null, baseUri, context, ct, destination);
    }
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream)

Example 5 with TypedInputStream

use of org.apache.jena.atlas.web.TypedInputStream in project jena by apache.

the class RDFParser method openTypedInputStream.

@SuppressWarnings("resource")
private TypedInputStream openTypedInputStream(String urlStr, Path path) {
    // If path, use that.
    if (path != null) {
        try {
            InputStream in = Files.newInputStream(path);
            ContentType ct = RDFLanguages.guessContentType(urlStr);
            return new TypedInputStream(in, ct);
        } catch (NoSuchFileException | FileNotFoundException ex) {
            throw new RiotNotFoundException();
        } catch (IOException ex) {
            IO.exception(ex);
        }
    }
    TypedInputStream in;
    if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) {
        // For complete compatibility, we have to let null pass through.
        // Pair with RDFParserBuilder.buildHttpClient
        //   Objects.requireNonNull(httpClient);
        // Remap.
        urlStr = StreamManager.get(context).mapURI(urlStr);
        in = HttpOp.execHttpGet(urlStr, null, httpClient, null);
    } else {
        in = streamManager.open(urlStr);
    }
    if (in == null)
        throw new RiotNotFoundException("Not found: " + urlStr);
    return in;
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) NoSuchFileException(java.nio.file.NoSuchFileException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream)

Aggregations

TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)32 Test (org.junit.Test)11 BaseTest (org.apache.jena.atlas.junit.BaseTest)8 HttpException (org.apache.jena.atlas.web.HttpException)7 ContentType (org.apache.jena.atlas.web.ContentType)6 InputStream (java.io.InputStream)4 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)4 HttpClient (org.apache.http.client.HttpClient)4 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)4 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 URL (java.net.URL)3 JsonValue (org.apache.jena.atlas.json.JsonValue)3 HttpEntity (org.apache.http.HttpEntity)2 Credentials (org.apache.http.auth.Credentials)2 FusekiTestAuth.assertAuthHttpException (org.apache.jena.fuseki.embedded.FusekiTestAuth.assertAuthHttpException)2 Model (org.apache.jena.rdf.model.Model)2 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)2 NoSuchFileException (java.nio.file.NoSuchFileException)1 AccessControlException (java.security.AccessControlException)1