Search in sources :

Example 56 with JenaException

use of org.apache.jena.shared.JenaException in project jena by apache.

the class REST_Quads_R method doGet.

@Override
protected void doGet(HttpAction action) {
    MediaType mediaType = ActionLib.contentNegotationQuads(action);
    ServletOutputStream output;
    try {
        output = action.response.getOutputStream();
    } catch (IOException ex) {
        ServletOps.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)
        action.log.info(format("[%d]   Get: Content-Type=%s, Charset=%s => %s", action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));
    if (!RDFLanguages.isQuads(lang))
        ServletOps.errorBadRequest("Not a quads format: " + mediaType);
    action.beginRead();
    try {
        DatasetGraph dsg = action.getActiveDSG();
        action.response.setHeader("Content-type", lang.getContentType().toHeaderString());
        // ActionLib.contentNegotationQuads above
        // RDF/XML is not a choice but this code is general.
        RDFFormat fmt = // Choose streaming.
        (lang == Lang.RDFXML) ? RDFFormat.RDFXML_PLAIN : RDFWriterRegistry.defaultSerialization(lang);
        try {
            RDFDataMgr.write(out, dsg, fmt);
        } catch (JenaException ex) {
            if (fmt.getLang().equals(Lang.RDFXML))
                ServletOps.errorBadRequest("Failed to write output in RDF/XML: " + ex.getMessage());
            else
                ServletOps.errorOccurred("Failed to write output: " + ex.getMessage(), ex);
        }
        ServletOps.success(action);
    } finally {
        action.endRead();
    }
}
Also used : JenaException(org.apache.jena.shared.JenaException) ServletOutputStream(javax.servlet.ServletOutputStream) MediaType(org.apache.jena.atlas.web.MediaType) TypedOutputStream(org.apache.jena.atlas.web.TypedOutputStream) IOException(java.io.IOException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 57 with JenaException

use of org.apache.jena.shared.JenaException in project jena by apache.

the class ResponseDataset method doResponseDataset.

public static void doResponseDataset(HttpAction action, Dataset dataset) {
    HttpServletRequest request = action.request;
    HttpServletResponse response = action.response;
    // Header request type
    String mimeType = null;
    MediaType i = ConNeg.chooseContentType(request, DEF.constructOffer, DEF.acceptTurtle);
    if (i != null)
        mimeType = i.getContentType();
    String outputField = ResponseOps.paramOutput(request, shortNamesModel);
    if (outputField != null)
        mimeType = outputField;
    String writerMimeType = mimeType;
    if (mimeType == null) {
        Fuseki.actionLog.warn("Can't find MIME type for response");
        String x = WebLib.getAccept(request);
        String msg;
        if (x == null)
            msg = "No Accept: header";
        else
            msg = "Accept: " + x + " : Not understood";
        ServletOps.error(HttpSC.NOT_ACCEPTABLE_406, msg);
    }
    String contentType = mimeType;
    String charset = charsetUTF8;
    String forceAccept = ResponseOps.paramForceAccept(request);
    if (forceAccept != null) {
        contentType = forceAccept;
        charset = charsetUTF8;
    }
    Lang lang = RDFLanguages.contentTypeToLang(contentType);
    if (lang == null)
        ServletOps.errorBadRequest("Can't determine output content type: " + contentType);
    try {
        ResponseResultSet.setHttpResponse(action, contentType, charset);
        response.setStatus(HttpSC.OK_200);
        ServletOutputStream out = response.getOutputStream();
        try {
            if (RDFLanguages.isQuads(lang))
                RDFDataMgr.write(out, dataset, lang);
            else
                RDFDataMgr.write(out, dataset.getDefaultModel(), lang);
            out.flush();
        } catch (JenaException ex) {
            // request (inappropriate content type).
            if (lang.equals(Lang.RDFXML))
                ServletOps.errorBadRequest("Failed to write output in RDF/XML: " + ex.getMessage());
            else
                ServletOps.errorOccurred("Failed to write output: " + ex.getMessage(), ex);
        }
    } catch (ActionErrorException ex) {
        throw ex;
    } catch (Exception ex) {
        action.log.info("Exception while writing the response model: " + ex.getMessage(), ex);
        ServletOps.errorOccurred("Exception while writing the response model: " + ex.getMessage(), ex);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JenaException(org.apache.jena.shared.JenaException) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) MediaType(org.apache.jena.atlas.web.MediaType) Lang(org.apache.jena.riot.Lang) JenaException(org.apache.jena.shared.JenaException)

Example 58 with JenaException

use of org.apache.jena.shared.JenaException in project jena by apache.

the class ParserARQUpdate method _parse.

private void _parse(UpdateSink sink, Reader r) {
    ARQParser parser = null;
    try {
        parser = new ARQParser(r);
        parser.setUpdateSink(sink);
        parser.UpdateUnit();
    } catch (org.apache.jena.sparql.lang.arq.ParseException ex) {
        throw new QueryParseException(ex.getMessage(), ex.currentToken.beginLine, ex.currentToken.beginColumn);
    } catch (org.apache.jena.sparql.lang.arq.TokenMgrError tErr) {
        // Last valid token : not the same as token error message - but this should not happen
        int col = parser.token.endColumn;
        int line = parser.token.endLine;
        throw new QueryParseException(tErr.getMessage(), line, col);
    } catch (QueryException ex) {
        throw ex;
    } catch (JenaException ex) {
        throw new QueryException(ex.getMessage(), ex);
    } catch (Error err) {
        // The token stream can throw errors.
        throw new QueryParseException(err.getMessage(), err, -1, -1);
    } catch (Throwable th) {
        Log.error(this, "Unexpected throwable: ", th);
        throw new QueryException(th.getMessage(), th);
    }
}
Also used : JenaException(org.apache.jena.shared.JenaException) QueryException(org.apache.jena.query.QueryException) ARQParser(org.apache.jena.sparql.lang.arq.ARQParser) QueryParseException(org.apache.jena.query.QueryParseException)

Example 59 with JenaException

use of org.apache.jena.shared.JenaException in project jena by apache.

the class ParserSPARQL11 method perform.

// All throwable handling.
private static void perform(Query query, String string, Action action) {
    Reader in = new StringReader(string);
    SPARQLParser11 parser = new SPARQLParser11(in);
    try {
        query.setStrict(true);
        parser.setQuery(query);
        action.exec(parser);
    } catch (org.apache.jena.sparql.lang.sparql_11.ParseException ex) {
        throw new QueryParseException(ex.getMessage(), ex.currentToken.beginLine, ex.currentToken.beginColumn);
    } catch (org.apache.jena.sparql.lang.sparql_11.TokenMgrError tErr) {
        // Last valid token : not the same as token error message - but this should not happen
        int col = parser.token.endColumn;
        int line = parser.token.endLine;
        throw new QueryParseException(tErr.getMessage(), line, col);
    } catch (QueryException ex) {
        throw ex;
    } catch (JenaException ex) {
        throw new QueryException(ex.getMessage(), ex);
    } catch (Error err) {
        System.err.println(err.getMessage());
        // The token stream can throw errors.
        throw new QueryParseException(err.getMessage(), err, -1, -1);
    } catch (Throwable th) {
        Log.warn(ParserSPARQL11.class, "Unexpected throwable: ", th);
        throw new QueryException(th.getMessage(), th);
    }
}
Also used : SPARQLParser11(org.apache.jena.sparql.lang.sparql_11.SPARQLParser11) StringReader(java.io.StringReader) Reader(java.io.Reader) QueryParseException(org.apache.jena.query.QueryParseException) JenaException(org.apache.jena.shared.JenaException) QueryException(org.apache.jena.query.QueryException) StringReader(java.io.StringReader)

Example 60 with JenaException

use of org.apache.jena.shared.JenaException in project jena by apache.

the class ParserSPARQL11Update method _parse.

private void _parse(UpdateSink sink, Reader r) {
    SPARQLParser11 parser = null;
    try {
        parser = new SPARQLParser11(r);
        parser.setUpdateSink(sink);
        parser.UpdateUnit();
    } catch (org.apache.jena.sparql.lang.sparql_11.ParseException ex) {
        throw new QueryParseException(ex.getMessage(), ex.currentToken.beginLine, ex.currentToken.beginColumn);
    } catch (org.apache.jena.sparql.lang.sparql_11.TokenMgrError tErr) {
        // Last valid token : not the same as token error message - but this should not happen
        int col = parser.token.endColumn;
        int line = parser.token.endLine;
        throw new QueryParseException(tErr.getMessage(), line, col);
    } catch (UpdateException ex) {
        throw ex;
    } catch (JenaException ex) {
        throw new QueryException(ex.getMessage(), ex);
    } catch (Error err) {
        // The token stream can throw errors.
        throw new QueryParseException(err.getMessage(), err, -1, -1);
    } catch (Throwable th) {
        Log.error(this, "Unexpected throwable: ", th);
        throw new QueryException(th.getMessage(), th);
    }
}
Also used : JenaException(org.apache.jena.shared.JenaException) QueryException(org.apache.jena.query.QueryException) SPARQLParser11(org.apache.jena.sparql.lang.sparql_11.SPARQLParser11) UpdateException(org.apache.jena.update.UpdateException) QueryParseException(org.apache.jena.query.QueryParseException)

Aggregations

JenaException (org.apache.jena.shared.JenaException)70 Model (org.apache.jena.rdf.model.Model)12 RDFReader (org.apache.jena.rdf.model.RDFReader)8 IOException (java.io.IOException)7 QueryException (org.apache.jena.query.QueryException)5 QueryParseException (org.apache.jena.query.QueryParseException)5 java.io (java.io)4 InputStream (java.io.InputStream)4 Reader (java.io.Reader)4 StringReader (java.io.StringReader)4 Graph (org.apache.jena.graph.Graph)4 ServletOutputStream (javax.servlet.ServletOutputStream)3 CmdException (jena.cmd.CmdException)3 AmbiguousSpecificTypeException (org.apache.jena.assembler.exceptions.AmbiguousSpecificTypeException)3 MediaType (org.apache.jena.atlas.web.MediaType)3 Triple (org.apache.jena.graph.Triple)3 BadDescriptionMultipleRootsException (org.apache.jena.shared.BadDescriptionMultipleRootsException)3 BadDescriptionNoRootException (org.apache.jena.shared.BadDescriptionNoRootException)3 ARQParser (org.apache.jena.sparql.lang.arq.ARQParser)3 FileNotFoundException (java.io.FileNotFoundException)2