Search in sources :

Example 1 with UploadDetails

use of org.apache.jena.fuseki.system.UploadDetails in project jena by apache.

the class GSP_RW method triplesPutPostTxn.

/**
 * Directly add data in a transaction.
 * Assumes recovery from parse errors by transaction abort.
 * Return whether the target existed before.
 */
private UploadDetails triplesPutPostTxn(HttpAction action, boolean replaceOperation) {
    action.beginWrite();
    try {
        DatasetGraph dsg = decideDataset(action);
        GraphTarget target = determineTargetGSP(dsg, action);
        if (action.log.isDebugEnabled())
            action.log.debug(action.getRequestMethod().toUpperCase() + "->" + target);
        if (target.isUnion())
            ServletOps.errorBadRequest("Can't load into the union graph");
        // Check URI.
        if (!target.isDefault() && target.graphName() != null && !target.graphName().isBlank()) {
            String uri = target.graphName().getURI();
            try {
                Validators.graphName(uri);
            } catch (FusekiConfigException ex) {
                ServletOps.errorBadRequest("Bad URI: " + uri);
                return null;
            }
        }
        boolean existedBefore = target.exists();
        Graph g = target.graph();
        if (replaceOperation && existedBefore)
            clearGraph(target);
        StreamRDF sink = StreamRDFLib.graph(g);
        UploadDetails upload = DataUploader.incomingData(action, sink);
        upload.setExistedBefore(existedBefore);
        action.commit();
        return upload;
    } catch (RiotParseException ex) {
        action.abortSilent();
        ServletOps.errorParseError(ex);
        return null;
    } catch (RiotException ex) {
        // Parse error
        action.abortSilent();
        ServletOps.errorBadRequest(ex.getMessage());
        return null;
    } catch (OperationDeniedException ex) {
        action.abortSilent();
        throw ex;
    } catch (ActionErrorException ex) {
        // Any ServletOps.error from calls in the try{} block.
        action.abortSilent();
        throw ex;
    } catch (Exception ex) {
        // Something unexpected.
        action.abortSilent();
        ServletOps.errorOccurred(ex.getMessage());
        return null;
    } finally {
        action.end();
    }
}
Also used : RiotParseException(org.apache.jena.riot.RiotParseException) UploadDetails(org.apache.jena.fuseki.system.UploadDetails) RiotException(org.apache.jena.riot.RiotException) FusekiConfigException(org.apache.jena.fuseki.FusekiConfigException) OperationDeniedException(org.apache.jena.shared.OperationDeniedException) RiotParseException(org.apache.jena.riot.RiotParseException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) OperationDeniedException(org.apache.jena.shared.OperationDeniedException) FusekiConfigException(org.apache.jena.fuseki.FusekiConfigException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) RiotException(org.apache.jena.riot.RiotException) StreamRDF(org.apache.jena.riot.system.StreamRDF)

Example 2 with UploadDetails

use of org.apache.jena.fuseki.system.UploadDetails in project jena by apache.

the class UploadRDF method quadsPutPostNonTxn.

/**
 * Load data, without assuming the dataset of an action is transactional -
 * specifically, whether it supports "abort". This requires loading the data into
 * a temporary dataset, which means we check the data is legal RDF, then copying
 * it into the finally destination.
 * <p>
 * Delayed choice of dataset via a function so that the decision is made inside the transaction updating the data.
 */
public static UploadDetails quadsPutPostNonTxn(HttpAction action, Function<HttpAction, DatasetGraph> decideDataset, boolean replaceOperation) {
    DatasetGraph dsgTmp = DatasetGraphFactory.create();
    StreamRDF dest = StreamRDFLib.dataset(dsgTmp);
    UploadDetails details;
    try {
        details = DataUploader.incomingData(action, dest);
    } catch (RiotException ex) {
        ServletOps.errorBadRequest(ex.getMessage());
        return null;
    }
    // Now insert into dataset
    action.beginWrite();
    try {
        DatasetGraph dsg = decideDataset.apply(action);
        if (replaceOperation)
            dsg.clear();
        FusekiNetLib.addDataInto(dsgTmp, dsg);
        action.commit();
    } catch (OperationDeniedException ex) {
        action.abortSilent();
        throw ex;
    } catch (Exception ex) {
        // We're in a non-transactional upload so this probably will not
        // work but there still may be transaction state tracking.
        // There is no harm safely trying.
        action.abortSilent();
        ServletOps.errorOccurred(ex.getMessage());
    } finally {
        action.end();
    }
    return details;
}
Also used : OperationDeniedException(org.apache.jena.shared.OperationDeniedException) UploadDetails(org.apache.jena.fuseki.system.UploadDetails) RiotException(org.apache.jena.riot.RiotException) StreamRDF(org.apache.jena.riot.system.StreamRDF) RiotException(org.apache.jena.riot.RiotException) OperationDeniedException(org.apache.jena.shared.OperationDeniedException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 3 with UploadDetails

use of org.apache.jena.fuseki.system.UploadDetails in project jena by apache.

the class UploadRDF method execPutPost.

private void execPutPost(HttpAction action, boolean replaceOperation) {
    ContentType ct = ActionLib.getContentType(action);
    if (ct == null)
        ServletOps.errorBadRequest("No Content-Type:");
    if (!action.getDataService().allowUpdate())
        ServletOps.errorMethodNotAllowed(action.getMethod());
    UploadDetails details;
    if (action.isTransactional())
        details = quadsPutPostTxn(action, replaceOperation);
    else
        details = quadsPutPostNonTxn(action, replaceOperation);
    ServletOps.uploadResponse(action, details);
}
Also used : UploadDetails(org.apache.jena.fuseki.system.UploadDetails) ContentType(org.apache.jena.atlas.web.ContentType)

Example 4 with UploadDetails

use of org.apache.jena.fuseki.system.UploadDetails in project jena by apache.

the class UploadRDF method quadsPutPostTxn.

/**
 * Load data using a transaction into the dataset of an action. if the data is bad,
 * abort the transaction.
 * <p>
 * Delayed choice of dataset via a function so that the decision is made inside the transaction.
 */
public static UploadDetails quadsPutPostTxn(HttpAction action, Function<HttpAction, DatasetGraph> decideDataset, boolean replaceOperation) {
    UploadDetails details = null;
    action.beginWrite();
    try {
        DatasetGraph dsg = decideDataset.apply(action);
        if (replaceOperation)
            dsg.clear();
        StreamRDF dest = StreamRDFLib.dataset(dsg);
        details = DataUploader.incomingData(action, dest);
        action.commit();
    } catch (RiotException ex) {
        // Parse error
        action.abortSilent();
        if (ex.getMessage() != null)
            action.log.info(format("[%d] Data error: %s", action.id, ex.getMessage()));
        else
            action.log.info(format("[%d] Data error", action.id), ex);
        ServletOps.errorBadRequest(ex.getMessage());
    } catch (OperationDeniedException ex) {
        action.abortSilent();
        throw ex;
    } catch (ActionErrorException ex) {
        action.abortSilent();
        if (ex.getMessage() != null)
            action.log.info(format("[%d] Upload error: %s", action.id, ex.getMessage()));
        else
            action.log.info(format("[%d] Upload error", action.id), ex);
        throw ex;
    } catch (Exception ex) {
        // Something else went wrong. Backout.
        action.abortSilent();
        ServletOps.errorOccurred(ex.getMessage());
    } finally {
        action.end();
    }
    return details;
}
Also used : OperationDeniedException(org.apache.jena.shared.OperationDeniedException) UploadDetails(org.apache.jena.fuseki.system.UploadDetails) RiotException(org.apache.jena.riot.RiotException) StreamRDF(org.apache.jena.riot.system.StreamRDF) RiotException(org.apache.jena.riot.RiotException) OperationDeniedException(org.apache.jena.shared.OperationDeniedException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 5 with UploadDetails

use of org.apache.jena.fuseki.system.UploadDetails in project jena by apache.

the class GSP_RW method triplesPutPostNonTxn.

/**
 * Add data where the destination does not support full transactions.
 *  In particular, with no abort, and actions probably going to the real storage
 *  parse errors can lead to partial updates.  Instead, parse to a temporary
 *  graph, then insert that data.
 */
private UploadDetails triplesPutPostNonTxn(HttpAction action, boolean replaceOperation) {
    Graph graphTmp = GraphFactory.createGraphMem();
    StreamRDF dest = StreamRDFLib.graph(graphTmp);
    UploadDetails details;
    try {
        details = DataUploader.incomingData(action, dest);
    } catch (RiotParseException ex) {
        ServletOps.errorParseError(ex);
        return null;
    }
    // Now insert into dataset
    action.beginWrite();
    try {
        DatasetGraph dsg = decideDataset(action);
        GraphTarget target = determineTargetGSP(dsg, action);
        if (action.log.isDebugEnabled())
            action.log.debug("  ->" + target);
        if (target.isUnion())
            ServletOps.errorBadRequest("Can't load into the union graph");
        boolean existedBefore = target.exists();
        if (replaceOperation && existedBefore)
            clearGraph(target);
        FusekiNetLib.addDataInto(graphTmp, target.dataset(), target.graphName());
        details.setExistedBefore(existedBefore);
        action.commit();
        return details;
    } catch (OperationDeniedException ex) {
        action.abortSilent();
        throw ex;
    } catch (Exception ex) {
        // We parsed into a temporary graph so an exception at this point
        // is not because of a parse error.
        // We're in the non-transactional branch, this probably will not work
        // but it might and there is no harm safely trying.
        action.abortSilent();
        ServletOps.errorOccurred(ex.getMessage());
        return null;
    } finally {
        action.end();
    }
}
Also used : OperationDeniedException(org.apache.jena.shared.OperationDeniedException) RiotParseException(org.apache.jena.riot.RiotParseException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) UploadDetails(org.apache.jena.fuseki.system.UploadDetails) StreamRDF(org.apache.jena.riot.system.StreamRDF) RiotException(org.apache.jena.riot.RiotException) FusekiConfigException(org.apache.jena.fuseki.FusekiConfigException) OperationDeniedException(org.apache.jena.shared.OperationDeniedException) RiotParseException(org.apache.jena.riot.RiotParseException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Aggregations

UploadDetails (org.apache.jena.fuseki.system.UploadDetails)7 RiotException (org.apache.jena.riot.RiotException)4 StreamRDF (org.apache.jena.riot.system.StreamRDF)4 OperationDeniedException (org.apache.jena.shared.OperationDeniedException)4 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)4 ContentType (org.apache.jena.atlas.web.ContentType)3 FusekiConfigException (org.apache.jena.fuseki.FusekiConfigException)2 Graph (org.apache.jena.graph.Graph)2 RiotParseException (org.apache.jena.riot.RiotParseException)2