Search in sources :

Example 16 with Lang

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

the class FusekiCmd method processModulesAndArgs.

@Override
protected void processModulesAndArgs() {
    int x = 0;
    Logger log = Fuseki.serverLog;
    if (contains(argFusekiConfig))
        fusekiConfigFile = getValue(argFusekiConfig);
    ArgDecl assemblerDescDecl = new ArgDecl(ArgDecl.HasValue, "desc", "dataset");
    if (contains(argMem))
        x++;
    if (contains(argFile))
        x++;
    if (contains(assemblerDescDecl))
        x++;
    if (contains(argTDB))
        x++;
    if (contains(argMemTDB))
        x++;
    if (fusekiConfigFile != null) {
        if (x >= 1)
            throw new CmdException("Dataset specified on the command line but a configuration file also given.");
    } else {
        if (x != 1)
            throw new CmdException("Required: either --config=FILE or one of --mem, --file, --loc or --desc");
    }
    if (contains(argMem)) {
        log.info("Dataset: in-memory");
        dsg = DatasetGraphFactory.create();
    }
    if (contains(argFile)) {
        dsg = DatasetGraphFactory.create();
        // replace by RiotLoader after ARQ refresh.
        String filename = getValue(argFile);
        log.info("Dataset: in-memory: load file: " + filename);
        if (!FileOps.exists(filename))
            throw new CmdException("File not found: " + filename);
        Lang language = RDFLanguages.filenameToLang(filename);
        if (language == null)
            throw new CmdException("Can't guess language for file: " + filename);
        InputStream input = IO.openFile(filename);
        if (RDFLanguages.isQuads(language))
            RDFDataMgr.read(dsg, filename);
        else
            RDFDataMgr.read(dsg.getDefaultGraph(), filename);
    }
    if (contains(argMemTDB)) {
        log.info("TDB dataset: in-memory");
        dsg = TDBFactory.createDatasetGraph();
    }
    if (contains(argTDB)) {
        String dir = getValue(argTDB);
        if (Objects.equals(dir, Names.memName)) {
            log.info("TDB dataset: in-memory");
        } else {
            if (!FileOps.exists(dir))
                throw new CmdException("Directory not found: " + dir);
            log.info("TDB dataset: directory=" + dir);
        }
        dsg = TDBFactory.createDatasetGraph(dir);
    }
    // Otherwise
    if (contains(assemblerDescDecl)) {
        log.info("Dataset from assembler");
        Dataset ds = modDataset.createDataset();
        if (ds != null)
            dsg = ds.asDatasetGraph();
    }
    if (contains(argFusekiConfig)) {
        if (dsg != null)
            throw new CmdException("(internal error) Dataset specificed on the command line but a a configuration file also given.");
        fusekiConfigFile = getValue(argFusekiConfig);
    }
    if (contains(argPort)) {
        String portStr = getValue(argPort);
        try {
            port = Integer.parseInt(portStr);
        } catch (NumberFormatException ex) {
            throw new CmdException(argPort.getKeyName() + " : bad port number: " + portStr);
        }
    }
    if (contains(argMgtPort)) {
        String mgtPortStr = getValue(argMgtPort);
        try {
            mgtPort = Integer.parseInt(mgtPortStr);
        } catch (NumberFormatException ex) {
            throw new CmdException(argMgtPort.getKeyName() + " : bad port number: " + mgtPortStr);
        }
    }
    if (contains(argLocalhost))
        listenLocal = true;
    if (fusekiConfigFile == null && dsg == null)
        throw new CmdException("No dataset defined and no configuration file: " + argUsage);
    if (dsg != null) {
        if (getPositional().size() == 0)
            throw new CmdException("No dataset path name given");
        if (getPositional().size() > 1)
            throw new CmdException("Multiple dataset path names given");
        datasetPath = getPositionalArg(0);
        if (datasetPath.length() > 0 && !datasetPath.startsWith("/"))
            throw new CmdException("Dataset path name must begin with a /: " + datasetPath);
        allowUpdate = contains(argAllowUpdate);
    }
    if (contains(argTimeout)) {
        String str = getValue(argTimeout);
        ARQ.getContext().set(ARQ.queryTimeout, str);
    }
    if (contains(argJettyConfig)) {
        jettyConfigFile = getValue(argJettyConfig);
        if (!FileOps.exists(jettyConfigFile))
            throw new CmdException("No such file: " + jettyConfigFile);
    }
    if (contains(argBasicAuth)) {
        authConfigFile = getValue(argBasicAuth);
        if (!FileOps.exists(authConfigFile))
            throw new CmdException("No such file: " + authConfigFile);
    }
    if (contains(argHome)) {
        List<String> args = super.getValues(argHome);
        homeDir = args.get(args.size() - 1);
    }
    if (contains(argPages)) {
        List<String> args = super.getValues(argPages);
        pagesDir = args.get(args.size() - 1);
    }
    if (contains(argGZip)) {
        if (!hasValueOfTrue(argGZip) && !hasValueOfFalse(argGZip))
            throw new CmdException(argGZip.getNames().get(0) + ": Not understood: " + getValue(argGZip));
        enableCompression = super.hasValueOfTrue(argGZip);
    }
    if (contains(argUber))
        SPARQLServer.überServlet = true;
    if (contains(argGSP)) {
        SPARQLServer.überServlet = true;
        Fuseki.graphStoreProtocolPostCreate = true;
    }
}
Also used : CmdException(jena.cmd.CmdException) InputStream(java.io.InputStream) Dataset(org.apache.jena.query.Dataset) ArgDecl(jena.cmd.ArgDecl) Lang(org.apache.jena.riot.Lang) Logger(org.slf4j.Logger)

Example 17 with Lang

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

the class SPARQL_Upload method uploadWorker.

/** Process an HTTP file upload of RDF with additiona name field for the graph name.
     *  We can't stream straight into a dataset because the graph name can be after the data.
     *  @return graph name and count
     */
// ?? Combine with Upload.fileUploadWorker
// Difference is the handling of names for graphs.
private static UploadDetails uploadWorker(HttpAction action, String base) {
    DatasetGraph dsgTmp = DatasetGraphFactory.create();
    ServletFileUpload upload = new ServletFileUpload();
    String graphName = null;
    boolean isQuads = false;
    long count = -1;
    String name = null;
    ContentType ct = null;
    Lang lang = null;
    try {
        FileItemIterator iter = upload.getItemIterator(action.request);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            String fieldName = item.getFieldName();
            InputStream stream = item.openStream();
            if (item.isFormField()) {
                // Graph name.
                String value = Streams.asString(stream, "UTF-8");
                if (fieldName.equals(HttpNames.paramGraph)) {
                    graphName = value;
                    if (graphName != null && !graphName.equals("") && !graphName.equals(HttpNames.valueDefault)) {
                        IRI iri = IRIResolver.parseIRI(value);
                        if (iri.hasViolation(false))
                            ServletOps.errorBadRequest("Bad IRI: " + graphName);
                        if (iri.getScheme() == null)
                            ServletOps.errorBadRequest("Bad IRI: no IRI scheme name: " + graphName);
                        if (iri.getScheme().equalsIgnoreCase("http") || iri.getScheme().equalsIgnoreCase("https")) {
                            // Redundant??
                            if (iri.getRawHost() == null)
                                ServletOps.errorBadRequest("Bad IRI: no host name: " + graphName);
                            if (iri.getRawPath() == null || iri.getRawPath().length() == 0)
                                ServletOps.errorBadRequest("Bad IRI: no path: " + graphName);
                            if (iri.getRawPath().charAt(0) != '/')
                                ServletOps.errorBadRequest("Bad IRI: Path does not start '/': " + graphName);
                        }
                    }
                } else if (fieldName.equals(HttpNames.paramDefaultGraphURI))
                    graphName = null;
                else
                    // Add file type?
                    action.log.info(format("[%d] Upload: Field=%s ignored", action.id, fieldName));
            } else {
                // Process the input stream
                name = item.getName();
                if (name == null || name.equals("") || name.equals("UNSET FILE NAME"))
                    ServletOps.errorBadRequest("No name for content - can't determine RDF syntax");
                String contentTypeHeader = item.getContentType();
                ct = ContentType.create(contentTypeHeader);
                lang = RDFLanguages.contentTypeToLang(ct.getContentType());
                if (lang == null) {
                    lang = RDFLanguages.filenameToLang(name);
                    // present we wrap the stream accordingly
                    if (name.endsWith(".gz"))
                        stream = new GZIPInputStream(stream);
                }
                if (lang == null)
                    // Desperate.
                    lang = RDFLanguages.RDFXML;
                isQuads = RDFLanguages.isQuads(lang);
                action.log.info(format("[%d] Upload: Filename: %s, Content-Type=%s, Charset=%s => %s", action.id, name, ct.getContentType(), ct.getCharset(), lang.getName()));
                StreamRDF x = StreamRDFLib.dataset(dsgTmp);
                StreamRDFCounting dest = StreamRDFLib.count(x);
                ActionSPARQL.parse(action, dest, stream, lang, base);
                count = dest.count();
            }
        }
        if (graphName == null || graphName.equals(""))
            graphName = HttpNames.valueDefault;
        if (isQuads)
            graphName = null;
        return new UploadDetails(graphName, dsgTmp, count);
    } catch (ActionErrorException ex) {
        throw ex;
    } catch (Exception ex) {
        ServletOps.errorOccurred(ex);
        return null;
    }
}
Also used : IRI(org.apache.jena.iri.IRI) ContentType(org.apache.jena.atlas.web.ContentType) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) Lang(org.apache.jena.riot.Lang) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) GZIPInputStream(java.util.zip.GZIPInputStream) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) StreamRDF(org.apache.jena.riot.system.StreamRDF) StreamRDFCounting(org.apache.jena.riot.lang.StreamRDFCounting) FileItemIterator(org.apache.commons.fileupload.FileItemIterator)

Example 18 with Lang

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

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

the class FusekiServer method configFromTemplate.

private static DataAccessPoint configFromTemplate(String templateFile, String datasetPath, boolean allowUpdate, Map<String, String> params) {
    DatasetDescriptionRegistry registry = FusekiServer.registryForBuild();
    // ---- Setup
    if (params == null) {
        params = new HashMap<>();
        params.put(Template.NAME, datasetPath);
    } else {
        if (!params.containsKey(Template.NAME)) {
            Fuseki.configLog.warn("No NAME found in template parameters (added)");
            params.put(Template.NAME, datasetPath);
        }
    }
    //-- Logging
    Fuseki.configLog.info("Template file: " + templateFile);
    String dir = params.get(Template.DIR);
    if (dir != null) {
        if (Objects.equals(dir, Names.memName)) {
            Fuseki.configLog.info("TDB dataset: in-memory");
        } else {
            if (!FileOps.exists(dir))
                throw new CmdException("Directory not found: " + dir);
            Fuseki.configLog.info("TDB dataset: directory=" + dir);
        }
    }
    //-- Logging
    datasetPath = DataAccessPoint.canonical(datasetPath);
    // DRY -- ActionDatasets (and others?)
    addGlobals(params);
    String str = TemplateFunctions.templateFile(templateFile, params, Lang.TTL);
    Lang lang = RDFLanguages.filenameToLang(str, Lang.TTL);
    StringReader sr = new StringReader(str);
    Model model = ModelFactory.createDefaultModel();
    RDFDataMgr.read(model, sr, datasetPath, lang);
    // ---- DataAccessPoint
    Statement stmt = getOne(model, null, FusekiVocab.pServiceName, null);
    if (stmt == null) {
        StmtIterator sIter = model.listStatements(null, FusekiVocab.pServiceName, (RDFNode) null);
        if (!sIter.hasNext())
            ServletOps.errorBadRequest("No name given in description of Fuseki service");
        sIter.next();
        if (sIter.hasNext())
            ServletOps.errorBadRequest("Multiple names given in description of Fuseki service");
        throw new InternalErrorException("Inconsistent: getOne didn't fail the second time");
    }
    Resource subject = stmt.getSubject();
    if (!allowUpdate) {
    // Opportunity for more sophisticated "read-only" mode.
    //  1 - clean model, remove "fu:serviceUpdate", "fu:serviceUpload", "fu:serviceReadGraphStore", "fu:serviceReadWriteGraphStore"
    //  2 - set a flag on DataAccessPoint
    }
    DataAccessPoint dap = FusekiBuilder.buildDataAccessPoint(subject, registry);
    return dap;
}
Also used : CmdException(jena.cmd.CmdException) StringReader(java.io.StringReader) Lang(org.apache.jena.riot.Lang) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException)

Example 20 with Lang

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

the class RdfResourceImporter method importResource.

@Override
public ResourceState importResource(InputStream is, String resourceName) throws IOException {
    String name = FilenameUtils.getName(resourceName);
    if ("gz".equalsIgnoreCase(FilenameUtils.getExtension(name))) {
        is = new GZIPInputStream(is);
        name = FilenameUtils.removeExtension(name);
        log.debug("   - from GZIP Archive");
    } else if ("bz2".equalsIgnoreCase(FilenameUtils.getExtension(name))) {
        is = new BZip2CompressorInputStream(is, //use true as 2nd param (see http://s.apache.org/QbK) 
        true);
        name = FilenameUtils.removeExtension(name);
        log.debug("   - from BZip2 Archive");
    }
    // TODO: No Zip Files inside Zip Files supported :o( ^^
    Lang format = RDFLanguages.filenameToLang(name);
    if (format == null) {
        log.warn("ignore File {} because of unknown extension ");
        return ResourceState.IGNORED;
    } else {
        log.info("    - bulk loading File {} using Format {}", resourceName, format);
        try {
            destination.startBulk();
            RiotReader.parse(is, format, null, destination);
        } catch (RuntimeException e) {
            return ResourceState.ERROR;
        } finally {
            destination.finishBulk();
        }
    }
    //        }
    return ResourceState.LOADED;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) Lang(org.apache.jena.riot.Lang)

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