Search in sources :

Example 6 with RiotException

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

the class SPARQL_GSP_RW method addDataIntoTxn.

/** Directly add data in a transaction.
     * Assumes recovery from parse errors by transaction abort.
     * Return whether the target existed before.
     * @param action
     * @param cleanDest Whether to remove data first (true = PUT, false = POST)
     * @return whether the target existed beforehand
     */
protected static UploadDetails addDataIntoTxn(HttpAction action, boolean overwrite) {
    action.beginWrite();
    try {
        Target target = determineTarget(action);
        if (action.log.isDebugEnabled())
            action.log.debug(action.request.getMethod().toUpperCase() + "->" + target);
        boolean existedBefore = target.exists();
        Graph g = target.graph();
        if (overwrite && existedBefore)
            clearGraph(target);
        StreamRDF sink = StreamRDFLib.graph(g);
        UploadDetails upload = Upload.incomingData(action, sink);
        upload.setExistedBefore(existedBefore);
        action.commit();
        return upload;
    } catch (RiotException ex) {
        // Parse error
        action.abort();
        ServletOps.errorBadRequest(ex.getMessage());
        return null;
    } catch (Exception ex) {
        // Something else went wrong.  Backout.
        action.abort();
        ServletOps.errorOccurred(ex.getMessage());
        return null;
    } finally {
        action.endWrite();
    }
}
Also used : Graph(org.apache.jena.graph.Graph) RiotException(org.apache.jena.riot.RiotException) StreamRDF(org.apache.jena.riot.system.StreamRDF) RiotException(org.apache.jena.riot.RiotException)

Example 7 with RiotException

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

the class SPARQL_GSP method determineTarget.

protected static final Target determineTarget(HttpAction action) {
    // Delayed until inside a transaction.
    if (action.getActiveDSG() == null)
        ServletOps.errorOccurred("Internal error : No action graph (not in a transaction?)");
    boolean dftGraph = getOneOnly(action.request, HttpNames.paramGraphDefault) != null;
    String uri = getOneOnly(action.request, HttpNames.paramGraph);
    if (!dftGraph && uri == null) {
        // No params - direct naming.
        if (!Fuseki.GSP_DIRECT_NAMING)
            ServletOps.errorBadRequest("Neither default graph nor named graph specified");
        // Direct naming.
        String directName = action.request.getRequestURL().toString();
        if (action.request.getRequestURI().equals(action.getDatasetName()))
            // No name (should have been a quads operations).
            ServletOps.errorBadRequest("Neither default graph nor named graph specified and no direct name");
        Node gn = NodeFactory.createURI(directName);
        return namedTarget(action, directName);
    }
    if (dftGraph)
        return Target.createDefault(action.getActiveDSG());
    // Named graph
    if (uri.equals(HttpNames.valueDefault))
        // But "named" default
        return Target.createDefault(action.getActiveDSG());
    // Strictly, a bit naughty on the URI resolution.  But more sensible. 
    // Base is dataset.
    // XXX Remove any service.
    //wholeRequestURL(request) ;
    String base = action.request.getRequestURL().toString();
    // Make sure it ends in "/", ie. dataset as container.
    if (action.request.getQueryString() != null && !base.endsWith("/"))
        base = base + "/";
    String absUri = null;
    try {
        absUri = IRIResolver.resolveString(uri, base);
    } catch (RiotException ex) {
        // Bad IRI
        ServletOps.errorBadRequest("Bad IRI: " + ex.getMessage());
    }
    return namedTarget(action, absUri);
}
Also used : RiotException(org.apache.jena.riot.RiotException) Node(org.apache.jena.graph.Node)

Example 8 with RiotException

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

the class StreamRDFLimited method quad.

@Override
public void quad(Quad quad) {
    count++;
    if (count > limit)
        throw new RiotException("Limit (" + limit + ") reached");
    super.quad(quad);
}
Also used : RiotException(org.apache.jena.riot.RiotException)

Example 9 with RiotException

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

the class SPARQL_QueryGeneral method datasetFromDescriptionWeb.

/**
     * Construct a Dataset based on a dataset description. Loads graph from the
     * web.
     */
protected Dataset datasetFromDescriptionWeb(HttpAction action, DatasetDescription datasetDesc) {
    try {
        if (datasetDesc == null)
            return null;
        if (datasetDesc.isEmpty())
            return null;
        List<String> graphURLs = datasetDesc.getDefaultGraphURIs();
        List<String> namedGraphs = datasetDesc.getNamedGraphURIs();
        if (graphURLs.size() == 0 && namedGraphs.size() == 0)
            return null;
        Dataset dataset = DatasetFactory.create();
        // Look in cache for loaded graphs!!
        // ---- Default graph
        {
            Model model = ModelFactory.createDefaultModel();
            for (String uri : graphURLs) {
                if (uri == null || uri.equals(""))
                    throw new InternalErrorException("Default graph URI is null or the empty string");
                try {
                    GraphLoadUtils.loadModel(model, uri, MaxTriples);
                    action.log.info(format("[%d] Load (default graph) %s", action.id, uri));
                } catch (RiotException ex) {
                    action.log.info(format("[%d] Parsing error loading %s: %s", action.id, uri, ex.getMessage()));
                    ServletOps.errorBadRequest("Failed to load URL (parse error) " + uri + " : " + ex.getMessage());
                } catch (Exception ex) {
                    action.log.info(format("[%d] Failed to load (default) %s: %s", action.id, uri, ex.getMessage()));
                    ServletOps.errorBadRequest("Failed to load URL " + uri);
                }
            }
            dataset.setDefaultModel(model);
        }
        // ---- Named graphs
        if (namedGraphs != null) {
            for (String uri : namedGraphs) {
                if (uri == null || uri.equals(""))
                    throw new InternalErrorException("Named graph URI is null or the empty string");
                try {
                    Model model = ModelFactory.createDefaultModel();
                    GraphLoadUtils.loadModel(model, uri, MaxTriples);
                    action.log.info(format("[%d] Load (named graph) %s", action.id, uri));
                    dataset.addNamedModel(uri, model);
                } catch (RiotException ex) {
                    action.log.info(format("[%d] Parsing error loading %s: %s", action.id, uri, ex.getMessage()));
                    ServletOps.errorBadRequest("Failed to load URL (parse error) " + uri + " : " + ex.getMessage());
                } catch (Exception ex) {
                    action.log.info(format("[%d] Failed to load (named graph) %s: %s", action.id, uri, ex.getMessage()));
                    ServletOps.errorBadRequest("Failed to load URL " + uri);
                }
            }
        }
        return dataset;
    } catch (ActionErrorException ex) {
        throw ex;
    } catch (Exception ex) {
        action.log.info(format("[%d] SPARQL parameter error: " + ex.getMessage(), action.id, ex));
        ServletOps.errorBadRequest("Parameter error: " + ex.getMessage());
        return null;
    }
}
Also used : RiotException(org.apache.jena.riot.RiotException) Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException) RiotException(org.apache.jena.riot.RiotException) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException)

Example 10 with RiotException

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

the class ReaderTriX method read.

@Override
public void read(InputStream in, String baseURI, ContentType ct, StreamRDF output, Context context) {
    XMLInputFactory xf = XMLInputFactory.newInstance();
    XMLStreamReader xReader;
    try {
        xReader = xf.createXMLStreamReader(in);
    } catch (XMLStreamException e) {
        throw new RiotException("Can't initialize StAX parsing engine", e);
    }
    read(xReader, baseURI, output);
}
Also used : RiotException(org.apache.jena.riot.RiotException)

Aggregations

RiotException (org.apache.jena.riot.RiotException)33 Node (org.apache.jena.graph.Node)10 IOException (java.io.IOException)7 StreamRDF (org.apache.jena.riot.system.StreamRDF)7 Graph (org.apache.jena.graph.Graph)4 Model (org.apache.jena.rdf.model.Model)4 Lang (org.apache.jena.riot.Lang)4 Token (org.apache.jena.riot.tokens.Token)4 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)4 Dataset (org.apache.jena.query.Dataset)3 InternalErrorException (org.apache.jena.atlas.lib.InternalErrorException)2 IRI (org.apache.jena.iri.IRI)2 Tokenizer (org.apache.jena.riot.tokens.Tokenizer)2 Var (org.apache.jena.sparql.core.Var)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 JsonLdError (com.github.jsonldjava.core.JsonLdError)1 RDFDataset (com.github.jsonldjava.core.RDFDataset)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1