Search in sources :

Example 6 with MediaType

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

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

the class SPARQL_GSP_RW method doPutPost.

private void doPutPost(HttpAction action, boolean overwrite) {
    ContentType ct = FusekiLib.getContentType(action);
    if (ct == null)
        ServletOps.errorBadRequest("No Content-Type:");
    if (matchContentType(ctMultipartMixed, ct)) {
        ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "multipart/mixed not supported");
    }
    UploadDetails details;
    if (action.isTransactional())
        details = addDataIntoTxn(action, overwrite);
    else
        details = addDataIntoNonTxn(action, overwrite);
    MediaType mt = ConNeg.chooseCharset(action.request, DEF.jsonOffer, DEF.acceptJSON);
    if (mt == null) {
        // No return body.
        if (details.getExistedBefore().equals(PreState.ABSENT))
            ServletOps.successCreated(action);
        else
            ServletOps.successNoContent(action);
        return;
    }
    ServletOps.uploadResponse(action, details);
}
Also used : WebContent.matchContentType(org.apache.jena.riot.WebContent.matchContentType) ContentType(org.apache.jena.atlas.web.ContentType) MediaType(org.apache.jena.atlas.web.MediaType)

Example 8 with MediaType

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

the class ResponseResultSet method doResponseResultSet$.

// If we refactor the conneg into a single function, we can split boolean and result set handling.
// One or the other argument must be null
private static void doResponseResultSet$(HttpAction action, ResultSet resultSet, Boolean booleanResult, Prologue qPrologue, AcceptList contentTypeOffer) {
    HttpServletRequest request = action.request;
    HttpServletResponse response = action.response;
    long id = action.id;
    if (resultSet == null && booleanResult == null) {
        xlog.warn("doResponseResult: Both result set and boolean result are null");
        throw new FusekiException("Both result set and boolean result are null");
    }
    if (resultSet != null && booleanResult != null) {
        xlog.warn("doResponseResult: Both result set and boolean result are set");
        throw new FusekiException("Both result set and boolean result are set");
    }
    String mimeType = null;
    MediaType i = ConNeg.chooseContentType(request, contentTypeOffer, DEF.acceptRSXML);
    if (i != null)
        mimeType = i.getContentType();
    // Override content type
    // Does &output= override?
    // Requested output type by the web form or &output= in the request.
    // Expands short names
    String outputField = ResponseOps.paramOutput(request, shortNamesResultSet);
    if (outputField != null)
        mimeType = outputField;
    // Choose the serializer based on this.
    String serializationType = mimeType;
    // Set the HTTP respose header to this.
    String contentType = mimeType;
    // Stylesheet - change to application/xml.
    final String stylesheetURL = ResponseOps.paramStylesheet(request);
    if (stylesheetURL != null && Objects.equals(serializationType, contentTypeResultsXML))
        contentType = contentTypeXML;
    // Force to text/plain?
    String forceAccept = ResponseOps.paramForceAccept(request);
    if (forceAccept != null)
        contentType = contentTypeTextPlain;
    // Better : dispatch on MediaType
    if (Objects.equals(serializationType, contentTypeResultsXML))
        sparqlXMLOutput(action, contentType, resultSet, stylesheetURL, booleanResult);
    else if (Objects.equals(serializationType, contentTypeResultsJSON))
        jsonOutput(action, contentType, resultSet, booleanResult);
    else if (Objects.equals(serializationType, contentTypeTextPlain))
        textOutput(action, contentType, resultSet, qPrologue, booleanResult);
    else if (Objects.equals(serializationType, contentTypeTextCSV))
        csvOutput(action, contentType, resultSet, booleanResult);
    else if (Objects.equals(serializationType, contentTypeTextTSV))
        tsvOutput(action, contentType, resultSet, booleanResult);
    else if (Objects.equals(serializationType, WebContent.contentTypeResultsThrift))
        thriftOutput(action, contentType, resultSet, booleanResult);
    else
        ServletOps.errorBadRequest("Can't determine output serialization: " + serializationType);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FusekiException(org.apache.jena.fuseki.FusekiException) HttpServletResponse(javax.servlet.http.HttpServletResponse) MediaType(org.apache.jena.atlas.web.MediaType)

Example 9 with MediaType

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

the class SPARQL_GSP_R method doHead.

@Override
protected void doHead(HttpAction action) {
    action.beginRead();
    setCommonHeaders(action.response);
    try {
        Target target = determineTarget(action);
        if (action.log.isDebugEnabled())
            action.log.debug("HEAD->" + target);
        if (!target.exists()) {
            ServletOps.successNotFound(action);
            return;
        }
        MediaType mediaType = ActionLib.contentNegotationRDF(action);
        ServletOps.success(action);
    } finally {
        action.endRead();
    }
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType)

Example 10 with MediaType

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

the class RDFLanguages method getCharsetForContentType.

public static String getCharsetForContentType(String contentType) {
    MediaType ct = MediaType.create(contentType);
    if (ct.getCharset() != null)
        return ct.getCharset();
    String mt = ct.getContentType();
    if (contentTypeNTriples.equals(mt))
        return charsetUTF8;
    if (contentTypeNTriplesAlt.equals(mt))
        return charsetASCII;
    if (contentTypeNQuads.equals(mt))
        return charsetUTF8;
    if (contentTypeNQuadsAlt1.equals(mt))
        return charsetASCII;
    if (contentTypeNQuadsAlt2.equals(mt))
        return charsetASCII;
    return charsetUTF8;
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType)

Aggregations

MediaType (org.apache.jena.atlas.web.MediaType)23 ServletOutputStream (javax.servlet.ServletOutputStream)6 IOException (java.io.IOException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 TypedOutputStream (org.apache.jena.atlas.web.TypedOutputStream)4 Lang (org.apache.jena.riot.Lang)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)3 AcceptList (org.apache.jena.atlas.web.AcceptList)3 ServerTest (org.apache.jena.fuseki.ServerTest)3 JenaException (org.apache.jena.shared.JenaException)3 QueryEngineHTTP (org.apache.jena.sparql.engine.http.QueryEngineHTTP)3 Test (org.junit.Test)3 Graph (org.apache.jena.graph.Graph)2 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)2 ContentType (org.apache.jena.atlas.web.ContentType)1 MediaRange (org.apache.jena.atlas.web.MediaRange)1 FusekiException (org.apache.jena.fuseki.FusekiException)1 ServerCtl.serviceQuery (org.apache.jena.fuseki.ServerCtl.serviceQuery)1 Triple (org.apache.jena.graph.Triple)1