Search in sources :

Example 31 with Lang

use of org.apache.jena.riot.Lang 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 32 with Lang

use of org.apache.jena.riot.Lang in project jena by apache.

the class DataValidatorHTML method executeHTML.

//static final String paramSyntaxExtended   = "extendedSyntax" ;
public static void executeHTML(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        //            if ( log.isInfoEnabled() )
        //                log.info("data validation request") ;
        String syntax = FusekiLib.safeParameter(httpRequest, paramSyntax);
        if (syntax == null || syntax.equals(""))
            syntax = RDFLanguages.NQUADS.getName();
        Lang language = RDFLanguages.shortnameToLang(syntax);
        if (language == null) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown syntax: " + syntax);
            return;
        }
        Reader input = createInput(httpRequest, httpResponse);
        ServletOutputStream outStream = httpResponse.getOutputStream();
        ErrorHandlerMsg errorHandler = new ErrorHandlerMsg(outStream);
        // Capture logging errors.
        PrintStream stderr = System.err;
        System.setErr(new PrintStream(outStream));
        // Headers
        setHeaders(httpResponse);
        outStream.println("<html>");
        printHead(outStream, "Jena Data Validator Report");
        outStream.println("<body>");
        outStream.println("<h1>RIOT Parser Report</h1>");
        outStream.println("<p>Line and column numbers refer to original input</p>");
        outStream.println("<p>&nbsp;</p>");
        // Need to escape HTML. 
        OutputStream output1 = new OutputStreamNoHTML(new BufferedOutputStream(outStream));
        StreamRDF output = StreamRDFWriter.getWriterStream(output1, Lang.NQUADS);
        try {
            startFixed(outStream);
            RDFParser parser = RDFParser.create().lang(language).errorHandler(errorHandler).resolveURIs(false).build();
            RiotException exception = null;
            startFixed(outStream);
            try {
                output.start();
                parser.parse(output);
                output.finish();
                output1.flush();
                outStream.flush();
                System.err.flush();
            } catch (RiotException ex) {
                ex.printStackTrace(stderr);
                exception = ex;
            }
        } finally {
            finishFixed(outStream);
            System.err.flush();
            System.setErr(stderr);
        }
        outStream.println("</body>");
        outStream.println("</html>");
    } catch (Exception ex) {
        serviceLog.warn("Exception in validationRequest", ex);
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) Lang(org.apache.jena.riot.Lang) RDFParser(org.apache.jena.riot.RDFParser) RiotException(org.apache.jena.riot.RiotException) RiotException(org.apache.jena.riot.RiotException) StreamRDF(org.apache.jena.riot.system.StreamRDF)

Example 33 with Lang

use of org.apache.jena.riot.Lang 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 34 with Lang

use of org.apache.jena.riot.Lang 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 35 with Lang

use of org.apache.jena.riot.Lang in project jena by apache.

the class ModLangOutput method printRegistered.

private static void printRegistered(PrintStream out) {
    out.println("Streaming languages:");
    Set<Lang> seen = new HashSet<>();
    for (RDFFormat fmt : StreamRDFWriter.registered()) {
        Lang lang = fmt.getLang();
        if (hiddenLanguages.contains(lang))
            continue;
        if (seen.contains(lang))
            continue;
        seen.add(lang);
        out.println("   " + lang.getLabel());
    }
    System.err.println("Non-streaming languages:");
    for (RDFFormat fmt : RDFWriterRegistry.registered()) {
        Lang lang = fmt.getLang();
        if (hiddenLanguages.contains(lang))
            continue;
        if (seen.contains(lang))
            continue;
        seen.add(lang);
        out.println("   " + lang.getLabel());
    }
}
Also used : Lang(org.apache.jena.riot.Lang) HashSet(java.util.HashSet) RDFFormat(org.apache.jena.riot.RDFFormat)

Aggregations

Lang (org.apache.jena.riot.Lang)41 InputStream (java.io.InputStream)10 CmdException (jena.cmd.CmdException)8 IOException (java.io.IOException)6 Model (org.apache.jena.rdf.model.Model)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 GZIPInputStream (java.util.zip.GZIPInputStream)4 ServletOutputStream (javax.servlet.ServletOutputStream)4 ContentType (org.apache.jena.atlas.web.ContentType)4 MediaType (org.apache.jena.atlas.web.MediaType)4 RiotException (org.apache.jena.riot.RiotException)4 Dataset (org.apache.jena.query.Dataset)3 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)3 ArrayList (java.util.ArrayList)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)2 FileItemStream (org.apache.commons.fileupload.FileItemStream)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 BaseTest (org.apache.jena.atlas.junit.BaseTest)2