Search in sources :

Example 31 with MediaType

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

the class ResponseDataset method doResponseDataset.

public static void doResponseDataset(HttpAction action, Dataset dataset) {
    HttpServletRequest request = action.getRequest();
    // Header request type
    String mimeType = null;
    MediaType i = ConNeg.chooseContentType(request, DEF.constructOffer, DEF.acceptTurtle);
    if (i != null)
        mimeType = i.getContentTypeStr();
    String outputField = ResponseOps.paramOutput(request, shortNamesModel);
    if (outputField != null)
        mimeType = outputField;
    String writerMimeType = mimeType;
    if (mimeType == null) {
        Fuseki.actionLog.warn("Can't find MIME type for response");
        String x = FusekiNetLib.getAccept(request);
        String msg;
        if (x == null)
            msg = "No Accept: header";
        else
            msg = "Accept: " + x + " : Not understood";
        ServletOps.error(HttpSC.NOT_ACCEPTABLE_406, msg);
    }
    String contentType = mimeType;
    String charset = charsetUTF8;
    String forceAccept = ResponseOps.paramForceAccept(request);
    if (forceAccept != null) {
        contentType = forceAccept;
        charset = charsetUTF8;
    }
    Lang lang = RDFLanguages.contentTypeToLang(contentType);
    if (lang == null)
        ServletOps.errorBadRequest("Can't determine output content type: " + contentType);
    RDFFormat format = ActionLib.getNetworkFormatForLang(lang);
    try {
        ServletOps.success(action);
        ServletOutputStream out = action.getResponseOutputStream();
        try {
            // Use the Content-Type from the content negotiation.
            if (RDFLanguages.isQuads(lang))
                ActionLib.datasetResponse(action, dataset.asDatasetGraph(), format, contentType);
            else
                ActionLib.graphResponse(action, dataset.getDefaultModel().getGraph(), format, contentType);
            out.flush();
        } catch (JenaException ex) {
            ServletOps.errorOccurred("Failed to write output: " + ex.getMessage(), ex);
        }
    } catch (ActionErrorException ex) {
        throw ex;
    } catch (Exception ex) {
        action.log.info("Exception while writing the response model: " + ex.getMessage(), ex);
        ServletOps.errorOccurred("Exception while writing the response model: " + ex.getMessage(), ex);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JenaException(org.apache.jena.shared.JenaException) ServletOutputStream(javax.servlet.ServletOutputStream) MediaType(org.apache.jena.atlas.web.MediaType) Lang(org.apache.jena.riot.Lang) JenaException(org.apache.jena.shared.JenaException) RDFFormat(org.apache.jena.riot.RDFFormat)

Example 32 with MediaType

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

the class GSP_R method execHeadQuads.

protected void execHeadQuads(HttpAction action) {
    ActionLib.setCommonHeaders(action);
    MediaType mediaType = ActionLib.contentNegotationQuads(action);
    if (action.verbose)
        action.log.info(format("[%d]   Head: Content-Type=%s", action.id, mediaType.getContentTypeStr()));
    ServletOps.success(action);
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType)

Example 33 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.getContentTypeStr();
    if (contentTypeNTriples.equals(mt))
        return charsetUTF8;
    if (contentTypeNTriplesAlt.equals(mt))
        return charsetASCII;
    if (contentTypeNQuads.equals(mt))
        return charsetUTF8;
    if (contentTypeNQuadsAlt1.equals(mt))
        return charsetASCII;
    return charsetUTF8;
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType)

Aggregations

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