Search in sources :

Example 6 with StreamRDF

use of org.apache.jena.riot.system.StreamRDF in project jena by apache.

the class ActionDatasets method execPostContainer.

// ---- POST 
@Override
protected JsonValue execPostContainer(HttpAction action) {
    JenaUUID uuid = JenaUUID.generate();
    DatasetDescriptionRegistry registry = FusekiServer.registryForBuild();
    ContentType ct = FusekiLib.getContentType(action);
    boolean committed = false;
    // Also acts as a concurrency lock
    system.begin(ReadWrite.WRITE);
    String systemFileCopy = null;
    String configFile = null;
    try {
        // Where to build the templated service/database. 
        Model model = ModelFactory.createDefaultModel();
        StreamRDF dest = StreamRDFLib.graph(model.getGraph());
        if (WebContent.isHtmlForm(ct))
            assemblerFromForm(action, dest);
        else if (WebContent.isMultiPartForm(ct))
            assemblerFromUpload(action, dest);
        else
            assemblerFromBody(action, dest);
        // ----
        // Keep a persistent copy immediately.  This is not used for
        // anything other than being "for the record".
        systemFileCopy = FusekiServer.dirFileArea.resolve(uuid.asString()).toString();
        try (OutputStream outCopy = IO.openOutputFile(systemFileCopy)) {
            RDFDataMgr.write(outCopy, model, Lang.TURTLE);
        }
        // ----
        // Process configuration.
        Statement stmt = getOne(model, null, pServiceName, null);
        if (stmt == null) {
            StmtIterator sIter = model.listStatements(null, 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");
        }
        if (!stmt.getObject().isLiteral())
            ServletOps.errorBadRequest("Found " + FmtUtils.stringForRDFNode(stmt.getObject()) + " : Service names are strings, then used to build the external URI");
        Resource subject = stmt.getSubject();
        Literal object = stmt.getObject().asLiteral();
        if (object.getDatatype() != null && !object.getDatatype().equals(XSDDatatype.XSDstring))
            action.log.warn(format("[%d] Service name '%s' is not a string", action.id, FmtUtils.stringForRDFNode(object)));
        String datasetPath;
        {
            // Check the name provided.
            String datasetName = object.getLexicalForm();
            // ---- Check and canonicalize name.
            if (datasetName.isEmpty())
                ServletOps.error(HttpSC.BAD_REQUEST_400, "Empty dataset name");
            if (StringUtils.isBlank(datasetName))
                ServletOps.error(HttpSC.BAD_REQUEST_400, format("Whitespace dataset name: '%s'", datasetName));
            if (datasetName.contains(" "))
                ServletOps.error(HttpSC.BAD_REQUEST_400, format("Bad dataset name (contains spaces) '%s'", datasetName));
            if (datasetName.equals("/"))
                ServletOps.error(HttpSC.BAD_REQUEST_400, format("Bad dataset name '%s'", datasetName));
            datasetPath = DataAccessPoint.canonical(datasetName);
        }
        action.log.info(format("[%d] Create database : name = %s", action.id, datasetPath));
        // ---- Check whether it already exists 
        if (action.getDataAccessPointRegistry().isRegistered(datasetPath))
            // And abort.
            ServletOps.error(HttpSC.CONFLICT_409, "Name already registered " + datasetPath);
        configFile = FusekiEnv.generateConfigurationFilename(datasetPath);
        List<String> existing = FusekiEnv.existingConfigurationFile(datasetPath);
        if (!existing.isEmpty())
            ServletOps.error(HttpSC.CONFLICT_409, "Configuration file for '" + datasetPath + "' already exists");
        // Write to configuration directory.
        try (OutputStream outCopy = IO.openOutputFile(configFile)) {
            RDFDataMgr.write(outCopy, model, Lang.TURTLE);
        }
        // Currently do nothing with the system database.
        // In the future ... maybe ...
        //            Model modelSys = system.getNamedModel(gn.getURI()) ;
        //            modelSys.removeAll(null, pStatus, null) ;
        //            modelSys.add(subject, pStatus, FusekiVocab.stateActive) ;
        // Need to be in Resource space at this point.
        DataAccessPoint ref = FusekiBuilder.buildDataAccessPoint(subject, registry);
        action.getDataAccessPointRegistry().register(datasetPath, ref);
        action.getResponse().setContentType(WebContent.contentTypeTextPlain);
        ServletOps.success(action);
        system.commit();
        committed = true;
    } catch (IOException ex) {
        IO.exception(ex);
    } finally {
        if (!committed) {
            if (systemFileCopy != null)
                FileOps.deleteSilent(systemFileCopy);
            if (configFile != null)
                FileOps.deleteSilent(configFile);
            system.abort();
        }
        system.end();
    }
    return null;
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType) OutputStream(java.io.OutputStream) JenaUUID(org.apache.jena.shared.uuid.JenaUUID) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException) IOException(java.io.IOException) DatasetDescriptionRegistry(org.apache.jena.fuseki.build.DatasetDescriptionRegistry) StreamRDF(org.apache.jena.riot.system.StreamRDF)

Example 7 with StreamRDF

use of org.apache.jena.riot.system.StreamRDF in project jena by apache.

the class TriGWriterFlat method output.

@Override
protected void output(IndentedWriter iOut, DatasetGraph datasetGraph, PrefixMap prefixMap, String baseURI, Context context) {
    StreamRDF dest = new WriterStreamRDFFlat(iOut);
    dest.start();
    dest.base(baseURI);
    StreamOps.sendDatasetToStream(datasetGraph, dest, prefixMap);
    dest.finish();
}
Also used : StreamRDF(org.apache.jena.riot.system.StreamRDF)

Example 8 with StreamRDF

use of org.apache.jena.riot.system.StreamRDF in project jena by apache.

the class NQuadsWriter method write.

public static void write(OutputStream out, Iterator<Quad> iter, CharSpace charSpace) {
    StreamRDF s = StreamRDFLib.writer(out, charSpace);
    write$(s, iter);
}
Also used : StreamRDF(org.apache.jena.riot.system.StreamRDF)

Example 9 with StreamRDF

use of org.apache.jena.riot.system.StreamRDF in project jena by apache.

the class NQuadsWriter method write.

public static void write(Writer out, Iterator<Quad> iter, CharSpace charSpace) {
    StreamRDF s = StreamRDFLib.writer(out, charSpace);
    write$(s, iter);
}
Also used : StreamRDF(org.apache.jena.riot.system.StreamRDF)

Example 10 with StreamRDF

use of org.apache.jena.riot.system.StreamRDF in project jena by apache.

the class NTriplesWriter method write.

public static void write(Writer out, Iterator<Triple> iter, CharSpace charSpace) {
    StreamRDF s = StreamRDFLib.writer(out, charSpace);
    write$(s, iter);
}
Also used : StreamRDF(org.apache.jena.riot.system.StreamRDF)

Aggregations

StreamRDF (org.apache.jena.riot.system.StreamRDF)34 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)9 Graph (org.apache.jena.graph.Graph)7 RiotException (org.apache.jena.riot.RiotException)7 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 BaseTest (org.apache.jena.atlas.junit.BaseTest)3 Node (org.apache.jena.graph.Node)3 Triple (org.apache.jena.graph.Triple)3 Lang (org.apache.jena.riot.Lang)3 ContentType (org.apache.jena.atlas.web.ContentType)2 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 StringReader (java.io.StringReader)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 CmdException (jena.cmd.CmdException)1