Search in sources :

Example 16 with ContentType

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

the class Upload method incomingData.

public static UploadDetails incomingData(HttpAction action, StreamRDF dest) {
    ContentType ct = FusekiLib.getContentType(action);
    if (ct == null) {
        ServletOps.errorBadRequest("No content type");
        return null;
    }
    if (matchContentType(ctMultipartFormData, ct)) {
        return fileUploadWorker(action, dest);
    }
    // Single graph (or quads) in body.
    String base = ActionLib.wholeRequestURL(action.request);
    Lang lang = RDFLanguages.contentTypeToLang(ct.getContentType());
    if (lang == null) {
        ServletOps.errorBadRequest("Unknown content type for triples: " + ct);
        return null;
    }
    InputStream input = null;
    try {
        input = action.request.getInputStream();
    } catch (IOException ex) {
        IO.exception(ex);
    }
    int len = action.request.getContentLength();
    StreamRDFCounting countingDest = StreamRDFLib.count(dest);
    try {
        ActionSPARQL.parse(action, countingDest, input, lang, base);
        UploadDetails details = new UploadDetails(countingDest.count(), countingDest.countTriples(), countingDest.countQuads());
        action.log.info(format("[%d] Body: Content-Length=%d, Content-Type=%s, Charset=%s => %s : %s", action.id, len, ct.getContentType(), ct.getCharset(), lang.getName(), details.detailsStr()));
        return details;
    } catch (RiotParseException ex) {
        action.log.info(format("[%d] Body: Content-Length=%d, Content-Type=%s, Charset=%s => %s : %s", action.id, len, ct.getContentType(), ct.getCharset(), lang.getName(), ex.getMessage()));
        throw ex;
    }
}
Also used : RiotParseException(org.apache.jena.riot.RiotParseException) WebContent.matchContentType(org.apache.jena.riot.WebContent.matchContentType) ContentType(org.apache.jena.atlas.web.ContentType) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) StreamRDFCounting(org.apache.jena.riot.lang.StreamRDFCounting) Lang(org.apache.jena.riot.Lang) IOException(java.io.IOException)

Example 17 with ContentType

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

the class SPARQL_GSP_RW method doPutPost.

private void doPutPost(HttpAction action, boolean overwrite) {
    ContentType ct = FusekiLib.getContentType(action);
    if (ct == null)
        ServletOps.errorBadRequest("No Content-Type:");
    if (matchContentType(ctMultipartMixed, ct)) {
        ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "multipart/mixed not supported");
    }
    UploadDetails details;
    if (action.isTransactional())
        details = addDataIntoTxn(action, overwrite);
    else
        details = addDataIntoNonTxn(action, overwrite);
    MediaType mt = ConNeg.chooseCharset(action.request, DEF.jsonOffer, DEF.acceptJSON);
    if (mt == null) {
        // No return body.
        if (details.getExistedBefore().equals(PreState.ABSENT))
            ServletOps.successCreated(action);
        else
            ServletOps.successNoContent(action);
        return;
    }
    ServletOps.uploadResponse(action, details);
}
Also used : WebContent.matchContentType(org.apache.jena.riot.WebContent.matchContentType) ContentType(org.apache.jena.atlas.web.ContentType) MediaType(org.apache.jena.atlas.web.MediaType)

Example 18 with ContentType

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

the class SPARQL_Query method validateParams.

/**
     * Helper method for validating request.
     * @param request HTTP request
     * @param params parameters in a collection of Strings
     */
protected void validateParams(HttpAction action, Collection<String> params) {
    HttpServletRequest request = action.request;
    ContentType ct = FusekiLib.getContentType(request);
    boolean mustHaveQueryParam = true;
    if (ct != null) {
        String incoming = ct.getContentType();
        if (matchContentType(ctSPARQLQuery, ct)) {
            mustHaveQueryParam = false;
        // Drop through.
        } else if (matchContentType(ctHTMLForm, ct)) {
        // Nothing specific to do
        } else
            ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Unsupported: " + incoming);
    }
    if (mustHaveQueryParam) {
        int N = countParamOccurences(request, paramQuery);
        if (N == 0)
            ServletOps.errorBadRequest("SPARQL Query: No 'query=' parameter");
        if (N > 1)
            ServletOps.errorBadRequest("SPARQL Query: Multiple 'query=' parameters");
        // application/sparql-query does not use a query param.
        String queryStr = request.getParameter(HttpNames.paramQuery);
        if (queryStr == null)
            ServletOps.errorBadRequest("SPARQL Query: No query specified (no 'query=' found)");
        if (queryStr.isEmpty())
            ServletOps.errorBadRequest("SPARQL Query: Empty query string");
    }
    if (params != null) {
        Enumeration<String> en = request.getParameterNames();
        for (; en.hasMoreElements(); ) {
            String name = en.nextElement();
            if (!params.contains(name))
                ServletOps.warning(action, "SPARQL Query: Unrecognize request parameter (ignored): " + name);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebContent.matchContentType(org.apache.jena.riot.WebContent.matchContentType) ContentType(org.apache.jena.atlas.web.ContentType)

Example 19 with ContentType

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

the class SPARQL_Query method perform.

@Override
protected final void perform(HttpAction action) {
    // OPTIONS
    if (action.request.getMethod().equals(HttpNames.METHOD_OPTIONS)) {
        // Share with update via SPARQL_Protocol.
        doOptions(action);
        return;
    }
    // GET
    if (action.request.getMethod().equals(HttpNames.METHOD_GET)) {
        executeWithParameter(action);
        return;
    }
    ContentType ct = FusekiLib.getContentType(action);
    // POST ?query= and no Content-Type
    if (ct == null || isHtmlForm(ct)) {
        // validation checked that if no Content-type, then its a POST with ?query=
        executeWithParameter(action);
        return;
    }
    // POST application/sparql-query
    if (matchContentType(ct, ctSPARQLQuery)) {
        executeBody(action);
        return;
    }
    ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Bad content type: " + ct.getContentType());
}
Also used : WebContent.matchContentType(org.apache.jena.riot.WebContent.matchContentType) ContentType(org.apache.jena.atlas.web.ContentType)

Example 20 with ContentType

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

the class SPARQL_Update method perform.

@Override
protected void perform(HttpAction action) {
    ContentType ct = FusekiLib.getContentType(action);
    if (ct == null)
        ct = ctSPARQLUpdate;
    if (matchContentType(ctSPARQLUpdate, ct)) {
        executeBody(action);
        return;
    }
    if (isHtmlForm(ct)) {
        executeForm(action);
        return;
    }
    ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Bad content type: " + action.request.getContentType());
}
Also used : WebContent.matchContentType(org.apache.jena.riot.WebContent.matchContentType) ContentType(org.apache.jena.atlas.web.ContentType)

Aggregations

ContentType (org.apache.jena.atlas.web.ContentType)28 InputStream (java.io.InputStream)9 IOException (java.io.IOException)8 WebContent.matchContentType (org.apache.jena.riot.WebContent.matchContentType)7 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)6 Lang (org.apache.jena.riot.Lang)4 OutputStream (java.io.OutputStream)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 StreamRDFCounting (org.apache.jena.riot.lang.StreamRDFCounting)3 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)2 FileItemStream (org.apache.commons.fileupload.FileItemStream)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 ContentProducer (org.apache.http.entity.ContentProducer)2 EntityTemplate (org.apache.http.entity.EntityTemplate)2 RDFFormat (org.apache.jena.riot.RDFFormat)2 RiotParseException (org.apache.jena.riot.RiotParseException)2 StreamRDF (org.apache.jena.riot.system.StreamRDF)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1