Search in sources :

Example 11 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 12 with MediaType

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

the class ConNeg method choose.

/**
     * <p>This method receives a HTTP header string, an {@link AcceptList} and a
     * default {@link MediaType}.</p>
     *
     * <p>If the header string is null, it returns the given default MediaType.</p>
     *
     * <p>Otherwise it builds an {@link AcceptList} object with the header string
     * and uses it to match against the given MediaType.</p>
     *
     * @param headerString HTTP header string
     * @param myPrefs accept list
     * @param defaultMediaType default media type
     * @return a media type or <code>null</code> if none matched or if the
     *          HTTP header string and the default media type are <code>null</code>.
     */
private static MediaType choose(String headerString, AcceptList myPrefs, MediaType defaultMediaType) {
    if (headerString == null)
        return defaultMediaType;
    AcceptList headerList = new AcceptList(headerString);
    if (myPrefs == null) {
        MediaType i = headerList.first();
        if (i == null)
            return defaultMediaType;
        return i;
    }
    MediaType i = AcceptList.match(headerList, myPrefs);
    if (i == null)
        return defaultMediaType;
    return i;
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType) AcceptList(org.apache.jena.atlas.web.AcceptList)

Example 13 with MediaType

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

the class ConNeg method match.

/**
     * Match a single media type against a header string.
     *
     * @param headerString HTTP header string
     * @param mediaRangeStr Semi-colon separated list of media types
     * @return the matched media type or <code>null</code> if there was no match
     */
public static String match(String headerString, String mediaRangeStr) {
    AcceptList l = new AcceptList(headerString);
    // MediaType
    MediaRange aItem = new MediaRange(mediaRangeStr);
    MediaType m = l.match(aItem);
    if (m == null)
        return null;
    return m.toHeaderString();
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType) MediaRange(org.apache.jena.atlas.web.MediaRange) AcceptList(org.apache.jena.atlas.web.AcceptList)

Example 14 with MediaType

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

the class REST_Quads_R method doGet.

@Override
protected void doGet(HttpAction action) {
    MediaType mediaType = ActionLib.contentNegotationQuads(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 (lang == null)
        lang = RDFLanguages.TRIG;
    if (action.verbose)
        action.log.info(format("[%d]   Get: Content-Type=%s, Charset=%s => %s", action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));
    if (!RDFLanguages.isQuads(lang))
        ServletOps.errorBadRequest("Not a quads format: " + mediaType);
    action.beginRead();
    try {
        DatasetGraph dsg = action.getActiveDSG();
        action.response.setHeader("Content-type", lang.getContentType().toHeaderString());
        // ActionLib.contentNegotationQuads above
        // RDF/XML is not a choice but this code is general.
        RDFFormat fmt = // Choose streaming.
        (lang == Lang.RDFXML) ? RDFFormat.RDFXML_PLAIN : RDFWriterRegistry.defaultSerialization(lang);
        try {
            RDFDataMgr.write(out, dsg, fmt);
        } catch (JenaException ex) {
            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) ServletOutputStream(javax.servlet.ServletOutputStream) MediaType(org.apache.jena.atlas.web.MediaType) TypedOutputStream(org.apache.jena.atlas.web.TypedOutputStream) IOException(java.io.IOException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 15 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.request;
    HttpServletResponse response = action.response;
    // Header request type
    String mimeType = null;
    MediaType i = ConNeg.chooseContentType(request, DEF.constructOffer, DEF.acceptTurtle);
    if (i != null)
        mimeType = i.getContentType();
    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 = WebLib.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);
    try {
        ResponseResultSet.setHttpResponse(action, contentType, charset);
        response.setStatus(HttpSC.OK_200);
        ServletOutputStream out = response.getOutputStream();
        try {
            if (RDFLanguages.isQuads(lang))
                RDFDataMgr.write(out, dataset, lang);
            else
                RDFDataMgr.write(out, dataset.getDefaultModel(), lang);
            out.flush();
        } catch (JenaException ex) {
            // request (inappropriate content type).
            if (lang.equals(Lang.RDFXML))
                ServletOps.errorBadRequest("Failed to write output in RDF/XML: " + ex.getMessage());
            else
                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) HttpServletResponse(javax.servlet.http.HttpServletResponse) MediaType(org.apache.jena.atlas.web.MediaType) Lang(org.apache.jena.riot.Lang) JenaException(org.apache.jena.shared.JenaException)

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