Search in sources :

Example 1 with TypedOutputStream

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

the class SPARQL_REST_R method doGet.

@Override
protected void doGet(HttpAction action) {
    // Assume success - do the set up before grabbing the lock.
    // Sets content type.
    MediaType mediaType = HttpAction.contentNegotationRDF(action);
    ServletOutputStream output;
    try {
        output = action.response.getOutputStream();
    } catch (IOException ex) {
        errorOccurred(ex);
        output = null;
    }
    TypedOutputStream out = new TypedOutputStream(output, mediaType);
    Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentType());
    if (action.verbose)
        log.info(format("[%d]   Get: Content-Type=%s, Charset=%s => %s", action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));
    action.beginRead();
    setCommonHeaders(action.response);
    try {
        Target target = determineTarget(action);
        if (log.isDebugEnabled())
            log.debug("GET->" + target);
        boolean exists = target.exists();
        if (!exists)
            errorNotFound("No such graph: <" + target.name + ">");
        // If we want to set the Content-Length, we need to buffer.
        //response.setContentLength(??) ;
        String ct = lang.getContentType().toHeaderString();
        action.response.setContentType(ct);
        Graph g = target.graph();
        //Special case RDF/XML to be the plain (faster, less readable) form
        RDFFormat fmt = (lang == Lang.RDFXML) ? RDFFormat.RDFXML_PLAIN : RDFWriterRegistry.defaultSerialization(lang);
        RDFDataMgr.write(out, g, fmt);
        success(action);
    } finally {
        action.endRead();
    }
}
Also used : Graph(org.apache.jena.graph.Graph) ServletOutputStream(javax.servlet.ServletOutputStream) MediaType(org.apache.jena.atlas.web.MediaType) TypedOutputStream(org.apache.jena.atlas.web.TypedOutputStream) IOException(java.io.IOException)

Example 2 with TypedOutputStream

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

the class SPARQL_GSP_R method doGet.

@Override
protected void doGet(HttpAction action) {
    // Assume success - do the set up before grabbing the lock.
    // Sets content type.
    MediaType mediaType = ActionLib.contentNegotationRDF(action);
    ServletOutputStream output;
    try {
        output = action.response.getOutputStream();
    } catch (IOException ex) {
        ServletOps.errorOccurred(ex);
        output = null;
    }
    TypedOutputStream out = new TypedOutputStream(output, mediaType);
    Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentType());
    if (action.verbose)
        action.log.info(format("[%d]   Get: Content-Type=%s, Charset=%s => %s", action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));
    action.beginRead();
    setCommonHeaders(action.response);
    try {
        Target target = determineTarget(action);
        if (action.log.isDebugEnabled())
            action.log.debug("GET->" + target);
        boolean exists = target.exists();
        if (!exists)
            ServletOps.errorNotFound("No such graph: <" + target.name + ">");
        // If we want to set the Content-Length, we need to buffer.
        //response.setContentLength(??) ;
        String ct = lang.getContentType().toHeaderString();
        action.response.setContentType(ct);
        Graph g = target.graph();
        //Special case RDF/XML to be the plain (faster, less readable) form
        RDFFormat fmt = (lang == Lang.RDFXML) ? RDFFormat.RDFXML_PLAIN : RDFWriterRegistry.defaultSerialization(lang);
        try {
            RDFDataMgr.write(out, g, fmt);
        } catch (JenaException ex) {
            // Good news - this happens before any output for RDF/XML-ABBREV. 
            if (fmt.getLang().equals(Lang.RDFXML))
                ServletOps.errorBadRequest("Failed to write output in RDF/XML: " + ex.getMessage());
            else
                ServletOps.errorOccurred("Failed to write output: " + ex.getMessage(), ex);
        }
        ServletOps.success(action);
    } finally {
        action.endRead();
    }
}
Also used : JenaException(org.apache.jena.shared.JenaException) Graph(org.apache.jena.graph.Graph) ServletOutputStream(javax.servlet.ServletOutputStream) MediaType(org.apache.jena.atlas.web.MediaType) TypedOutputStream(org.apache.jena.atlas.web.TypedOutputStream) IOException(java.io.IOException)

Example 3 with TypedOutputStream

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

the class REST_Quads_R method doGet.

@Override
protected void doGet(HttpAction action) {
    MediaType mediaType = ActionLib.contentNegotationQuads(action);
    ServletOutputStream output;
    try {
        output = action.response.getOutputStream();
    } catch (IOException ex) {
        ServletOps.errorOccurred(ex);
        output = null;
    }
    TypedOutputStream out = new TypedOutputStream(output, mediaType);
    Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentType());
    if (lang == null)
        lang = RDFLanguages.TRIG;
    if (action.verbose)
        action.log.info(format("[%d]   Get: Content-Type=%s, Charset=%s => %s", action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));
    if (!RDFLanguages.isQuads(lang))
        ServletOps.errorBadRequest("Not a quads format: " + mediaType);
    action.beginRead();
    try {
        DatasetGraph dsg = action.getActiveDSG();
        action.response.setHeader("Content-type", lang.getContentType().toHeaderString());
        // ActionLib.contentNegotationQuads above
        // RDF/XML is not a choice but this code is general.
        RDFFormat fmt = // Choose streaming.
        (lang == Lang.RDFXML) ? RDFFormat.RDFXML_PLAIN : RDFWriterRegistry.defaultSerialization(lang);
        try {
            RDFDataMgr.write(out, dsg, fmt);
        } catch (JenaException ex) {
            if (fmt.getLang().equals(Lang.RDFXML))
                ServletOps.errorBadRequest("Failed to write output in RDF/XML: " + ex.getMessage());
            else
                ServletOps.errorOccurred("Failed to write output: " + ex.getMessage(), ex);
        }
        ServletOps.success(action);
    } finally {
        action.endRead();
    }
}
Also used : JenaException(org.apache.jena.shared.JenaException) ServletOutputStream(javax.servlet.ServletOutputStream) MediaType(org.apache.jena.atlas.web.MediaType) TypedOutputStream(org.apache.jena.atlas.web.TypedOutputStream) IOException(java.io.IOException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 4 with TypedOutputStream

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

the class REST_Quads method doGet.

@Override
protected void doGet(HttpAction action) {
    MediaType mediaType = HttpAction.contentNegotationQuads(action);
    ServletOutputStream output;
    try {
        output = action.response.getOutputStream();
    } catch (IOException ex) {
        errorOccurred(ex);
        output = null;
    }
    TypedOutputStream out = new TypedOutputStream(output, mediaType);
    Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentType());
    if (lang == null)
        lang = RDFLanguages.TRIG;
    if (action.verbose)
        log.info(format("[%d]   Get: Content-Type=%s, Charset=%s => %s", action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));
    if (!RDFLanguages.isQuads(lang))
        errorBadRequest("Not a quads format: " + mediaType);
    action.beginRead();
    try {
        DatasetGraph dsg = action.getActiveDSG();
        RDFDataMgr.write(out, dsg, lang);
        success(action);
    } finally {
        action.endRead();
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) MediaType(org.apache.jena.atlas.web.MediaType) TypedOutputStream(org.apache.jena.atlas.web.TypedOutputStream) Lang(org.apache.jena.riot.Lang) IOException(java.io.IOException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Aggregations

IOException (java.io.IOException)4 ServletOutputStream (javax.servlet.ServletOutputStream)4 MediaType (org.apache.jena.atlas.web.MediaType)4 TypedOutputStream (org.apache.jena.atlas.web.TypedOutputStream)4 Graph (org.apache.jena.graph.Graph)2 JenaException (org.apache.jena.shared.JenaException)2 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)2 Lang (org.apache.jena.riot.Lang)1