Search in sources :

Example 21 with MediaType

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

the class REST_Quads method doPost.

@Override
protected void doPost(HttpAction action) {
    if (!action.getDatasetRef().allowDatasetUpdate)
        errorMethodNotAllowed("POST");
    // Graph Store Protocol mode - POST triples to dataset causes
    // a new graph to be created and the new URI returned via Location.
    // Normally off.  
    // When off, POST of triples goes to default graph.
    boolean gspMode = Fuseki.graphStoreProtocolPostCreate;
    // Code to pass the GSP test suite.
    // Not necessarily good code.
    String x = action.request.getContentType();
    if (x == null)
        errorBadRequest("Content-type required for data format");
    MediaType mediaType = MediaType.create(x);
    Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentType());
    if (lang == null)
        lang = RDFLanguages.TRIG;
    if (action.verbose)
        log.info(format("[%d]   Post: Content-Type=%s, Charset=%s => %s", action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));
    if (RDFLanguages.isQuads(lang))
        doPostQuads(action, lang);
    else if (gspMode && RDFLanguages.isTriples(lang))
        doPostTriplesGSP(action, lang);
    else if (RDFLanguages.isTriples(lang))
        doPostTriples(action, lang);
    else
        errorBadRequest("Not a triples or quads format: " + mediaType);
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType) Lang(org.apache.jena.riot.Lang)

Example 22 with MediaType

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

Example 23 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