Search in sources :

Example 26 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)

Example 27 with ContentType

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

the class Upload method fileUploadWorker.

/**  Process an HTTP upload of RDF files (triples or quads)
     *   Stream straight into a graph or dataset -- unlike SPARQL_Upload the destination
     *   is known at the start of the multipart file body
     */
public static UploadDetails fileUploadWorker(HttpAction action, StreamRDF dest) {
    String base = ActionLib.wholeRequestURL(action.request);
    ServletFileUpload upload = new ServletFileUpload();
    //log.info(format("[%d] Upload: Field=%s ignored", action.id, fieldName)) ;
    // Overall counting.
    StreamRDFCounting countingDest = StreamRDFLib.count(dest);
    try {
        FileItemIterator iter = upload.getItemIterator(action.request);
        while (iter.hasNext()) {
            FileItemStream fileStream = iter.next();
            if (fileStream.isFormField()) {
                // Ignore?
                String fieldName = fileStream.getFieldName();
                InputStream stream = fileStream.openStream();
                String value = Streams.asString(stream, "UTF-8");
                ServletOps.errorBadRequest(format("Only files accepted in multipart file upload (got %s=%s)", fieldName, value));
            }
            //Ignore the field name.
            //String fieldName = fileStream.getFieldName();
            InputStream stream = fileStream.openStream();
            // Process the input stream
            String contentTypeHeader = fileStream.getContentType();
            ContentType ct = ContentType.create(contentTypeHeader);
            Lang lang = null;
            if (!matchContentType(ctTextPlain, ct))
                lang = RDFLanguages.contentTypeToLang(ct.getContentType());
            if (lang == null) {
                String name = fileStream.getName();
                if (name == null || name.equals(""))
                    ServletOps.errorBadRequest("No name for content - can't determine RDF syntax");
                lang = RDFLanguages.filenameToLang(name);
                if (name.endsWith(".gz"))
                    stream = new GZIPInputStream(stream);
            }
            if (lang == null)
                // Desperate.
                lang = RDFLanguages.RDFXML;
            String printfilename = fileStream.getName();
            if (printfilename == null || printfilename.equals(""))
                printfilename = "<none>";
            // Before
            // action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s", 
            //                        action.id, printfilename,  ct.getContentType(), ct.getCharset(), lang.getName())) ;
            // count just this step
            StreamRDFCounting countingDest2 = StreamRDFLib.count(countingDest);
            try {
                ActionSPARQL.parse(action, countingDest2, stream, lang, base);
                UploadDetails details1 = new UploadDetails(countingDest2.count(), countingDest2.countTriples(), countingDest2.countQuads());
                action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s : %s", action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName(), details1.detailsStr()));
            } catch (RiotParseException ex) {
                action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s : %s", action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName(), ex.getMessage()));
                throw ex;
            }
        }
    } catch (ActionErrorException ex) {
        throw ex;
    } catch (Exception ex) {
        ServletOps.errorOccurred(ex.getMessage());
    }
    // Overall results.
    UploadDetails details = new UploadDetails(countingDest.count(), countingDest.countTriples(), countingDest.countQuads());
    return details;
}
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) Lang(org.apache.jena.riot.Lang) IOException(java.io.IOException) RiotParseException(org.apache.jena.riot.RiotParseException) GZIPInputStream(java.util.zip.GZIPInputStream) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) StreamRDFCounting(org.apache.jena.riot.lang.StreamRDFCounting) FileItemIterator(org.apache.commons.fileupload.FileItemIterator)

Example 28 with ContentType

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

the class SPARQL_REST_RW method doPutPost.

private void doPutPost(HttpAction action, boolean overwrite) {
    ContentType ct = FusekiLib.getContentType(action);
    if (ct == null)
        errorBadRequest("No Content-Type:");
    // Helper case - if it's a possible HTTP file upload, pretend that's the action.
    if (WebContent.contentTypeMultipartFormData.equalsIgnoreCase(ct.getContentType())) {
        String base = wholeRequestURL(action.request);
        SPARQL_Upload.upload(action, base);
        return;
    }
    if (WebContent.matchContentType(WebContent.ctMultipartMixed, ct))
        error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "multipart/mixed not supported");
    boolean existedBefore = false;
    if (action.isTransactional())
        existedBefore = addDataIntoTxn(action, overwrite);
    else
        existedBefore = addDataIntoNonTxn(action, overwrite);
    if (existedBefore)
        ServletBase.successNoContent(action);
    else
        ServletBase.successCreated(action);
}
Also used : 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