Search in sources :

Example 26 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 27 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 28 with MediaType

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

the class ServletOps method successPage.

public static void successPage(HttpAction action, String message) {
    String x = action.getRequestHeader(HttpNames.hAccept);
    MediaType mt = null;
    if (x != null && x.equals("*/*")) {
        if (action.getRequestContentType().equals(WebContent.contentTypeHTMLForm))
            mt = mtTextHTML;
    }
    if (mt == null && x != null)
        mt = ConNeg.chooseContentType(action.getRequest(), successReponseAccept, MediaType.create("text/plain"));
    if (mt == null)
        mt = mtTextHTML;
    if (WebContent.ctTextPlain.agreesWith(mt)) {
        successPageText(action, message);
        return;
    }
    if (WebContent.ctJSON.agreesWith(mt)) {
        successPageJson(action, message);
        return;
    }
    // Default.
    successPageHtml(action, message);
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType)

Example 29 with MediaType

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

the class ConNeg method chooseCharset.

/**
 * <p>
 * Chooses the charset by using the Accept-Charset HTTP header.
 * </p>
 * <p>
 * See {@link ConNeg#choose(String, AcceptList, MediaType)}.
 * </p>
 *
 * @param httpRequest HTTP request
 * @param myPrefs accept list
 * @param defaultMediaType default media type
 * @return media type chosen
 */
public static MediaType chooseCharset(HttpServletRequest httpRequest, AcceptList myPrefs, MediaType defaultMediaType) {
    String a = httpRequest.getHeader(hAcceptCharset);
    if (log.isDebugEnabled())
        log.debug("Accept-Charset request: " + a);
    MediaType item = choose(a, myPrefs, defaultMediaType);
    if (log.isDebugEnabled())
        log.debug("Charset chosen: " + item);
    return item;
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType)

Example 30 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 = FusekiNetLib.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