Search in sources :

Example 1 with MediaType

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

the class REST_Quads method doHead.

@Override
protected void doHead(HttpAction action) {
    action.beginRead();
    try {
        MediaType mediaType = HttpAction.contentNegotationQuads(action);
        success(action);
    } finally {
        action.endRead();
    }
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType)

Example 2 with MediaType

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

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

the class ResponseModel method doResponseModel.

public static void doResponseModel(HttpAction action, Model model) {
    HttpServletRequest request = action.request;
    HttpServletResponse response = action.response;
    // Header request type 
    String mimeType = null;
    // TODO Use MediaType throughout.
    MediaType i = ConNeg.chooseContentType(request, DEF.rdfOffer, DEF.acceptRDFXML);
    if (i != null)
        mimeType = i.getContentType();
    String outputField = ResponseOps.paramOutput(request, shortNamesModel);
    if (outputField != null)
        mimeType = outputField;
    String writerMimeType = mimeType;
    if (mimeType == null) {
        Fuseki.requestLog.warn("Can't find MIME type for response");
        String x = WebLib.getAccept(request);
        String msg;
        if (x == null)
            msg = "No Accept: header";
        else
            msg = "Accept: " + x + " : Not understood";
        error(HttpSC.NOT_ACCEPTABLE_406, msg);
    }
    String contentType = mimeType;
    String charset = WebContent.charsetUTF8;
    String forceAccept = ResponseOps.paramForceAccept(request);
    if (forceAccept != null) {
        contentType = forceAccept;
        charset = WebContent.charsetUTF8;
    }
    Lang lang = RDFLanguages.contentTypeToLang(contentType);
    if (lang == null)
        errorBadRequest("Can't determine output content type: " + contentType);
    try {
        ResponseResultSet.setHttpResponse(request, response, contentType, charset);
        response.setStatus(HttpSC.OK_200);
        ServletOutputStream out = response.getOutputStream();
        RDFDataMgr.write(out, model, lang);
        out.flush();
    } catch (Exception ex) {
        slog.info("Exception while writing the response model: " + ex.getMessage(), ex);
        errorOccurred("Exception while writing the response model: " + ex.getMessage(), ex);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) MediaType(org.apache.jena.atlas.web.MediaType) Lang(org.apache.jena.riot.Lang)

Example 4 with MediaType

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

the class TestQuery method query_describe_conneg.

@Test
public void query_describe_conneg() throws IOException {
    try (CloseableHttpClient client = HttpOp.createPoolingHttpClient()) {
        String query = "DESCRIBE ?s WHERE {?s ?p ?o}";
        for (MediaType type : rdfOfferTest.entries()) {
            String contentType = type.toHeaderString();
            try (QueryEngineHTTP qExec = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(serviceQuery(), query)) {
                qExec.setModelContentType(contentType);
                qExec.setClient(client);
                Model m = qExec.execDescribe();
                String x = qExec.getHttpResponseContentType();
                assertEquals(contentType, x);
                assertFalse(m.isEmpty());
            }
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) QueryEngineHTTP(org.apache.jena.sparql.engine.http.QueryEngineHTTP) Model(org.apache.jena.rdf.model.Model) MediaType(org.apache.jena.atlas.web.MediaType) ServerTest(org.apache.jena.fuseki.ServerTest) Test(org.junit.Test)

Example 5 with MediaType

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

the class ConNeg method chooseContentType.

/**
     * <p>Choose the content media type by extracting the Accept HTTP header from
     * the HTTP request and choosing
     * (see {@link ConNeg#choose(String, AcceptList, MediaType)}) a content media
     * type that matches the header.</p>
     *
     * @param httpRequest HTTP request
     * @param myPrefs accept list
     * @param defaultMediaType default media type
     * @return media type chosen
     */
public static MediaType chooseContentType(HttpServletRequest httpRequest, AcceptList myPrefs, MediaType defaultMediaType) {
    String a = WebLib.getAccept(httpRequest);
    if (log.isDebugEnabled())
        log.debug("Accept request: " + a);
    MediaType item = choose(a, myPrefs, defaultMediaType);
    if (log.isDebugEnabled())
        log.debug("Content type chosen: " + item);
    return item;
}
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