Search in sources :

Example 6 with ContentType

use of org.apache.jena.atlas.web.ContentType 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 7 with ContentType

use of org.apache.jena.atlas.web.ContentType 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)

Example 8 with ContentType

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

the class RDFParser method parseNotUri.

/** Parse when there is no URI to guide the choice of syntax */
private void parseNotUri(StreamRDF destination) {
    // parse from bytes or chars, no indication of the syntax from the source.
    Lang lang = hintLang;
    if (forceLang != null)
        lang = forceLang;
    ContentType ct = WebContent.determineCT(null, lang, baseUri);
    if (ct == null)
        throw new RiotException("Failed to determine the RDF syntax (.lang or .base required)");
    ReaderRIOT readerRiot = createReader(ct);
    if (readerRiot == null)
        throw new RiotException("No parser registered for content type: " + ct.getContentType());
    Reader jr = javaReader;
    if (content != null)
        jr = new StringReader(content);
    read(readerRiot, inputStream, jr, baseUri, context, ct, destination);
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType) RDFLanguages.sameLang(org.apache.jena.riot.RDFLanguages.sameLang)

Example 9 with ContentType

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

the class TestMultipleEmbedded method graphToHttpEntity.

/** Create an HttpEntity for the graph */
protected static HttpEntity graphToHttpEntity(final Graph graph) {
    final RDFFormat syntax = RDFFormat.TURTLE_BLOCKS;
    ContentProducer producer = new ContentProducer() {

        @Override
        public void writeTo(OutputStream out) {
            RDFDataMgr.write(out, graph, syntax);
        }
    };
    EntityTemplate entity = new EntityTemplate(producer);
    ContentType ct = syntax.getLang().getContentType();
    entity.setContentType(ct.getContentType());
    return entity;
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType) OutputStream(java.io.OutputStream) EntityTemplate(org.apache.http.entity.EntityTemplate) RDFFormat(org.apache.jena.riot.RDFFormat) ContentProducer(org.apache.http.entity.ContentProducer)

Example 10 with ContentType

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

the class TestEmbeddedFuseki method graphToHttpEntity.

/** Create an HttpEntity for the graph */
protected static HttpEntity graphToHttpEntity(final Graph graph) {
    final RDFFormat syntax = RDFFormat.TURTLE_BLOCKS;
    ContentProducer producer = new ContentProducer() {

        @Override
        public void writeTo(OutputStream out) {
            RDFDataMgr.write(out, graph, syntax);
        }
    };
    EntityTemplate entity = new EntityTemplate(producer);
    ContentType ct = syntax.getLang().getContentType();
    entity.setContentType(ct.getContentType());
    return entity;
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType) OutputStream(java.io.OutputStream) EntityTemplate(org.apache.http.entity.EntityTemplate) RDFFormat(org.apache.jena.riot.RDFFormat) ContentProducer(org.apache.http.entity.ContentProducer)

Aggregations

ContentType (org.apache.jena.atlas.web.ContentType)28 InputStream (java.io.InputStream)9 IOException (java.io.IOException)8 WebContent.matchContentType (org.apache.jena.riot.WebContent.matchContentType)7 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)6 Lang (org.apache.jena.riot.Lang)4 OutputStream (java.io.OutputStream)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 StreamRDFCounting (org.apache.jena.riot.lang.StreamRDFCounting)3 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)2 FileItemStream (org.apache.commons.fileupload.FileItemStream)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 ContentProducer (org.apache.http.entity.ContentProducer)2 EntityTemplate (org.apache.http.entity.EntityTemplate)2 RDFFormat (org.apache.jena.riot.RDFFormat)2 RiotParseException (org.apache.jena.riot.RiotParseException)2 StreamRDF (org.apache.jena.riot.system.StreamRDF)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1