Search in sources :

Example 26 with Lang

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

the class FusekiConfig method readConfigurationDirectory.

// ---- Directory of assemblers
/** Read service descriptions in the given directory */
public static List<DataAccessPoint> readConfigurationDirectory(String dir) {
    Path pDir = Paths.get(dir).normalize();
    File dirFile = pDir.toFile();
    if (!dirFile.exists()) {
        log.warn("Not found: directory for assembler files for services: '" + dir + "'");
        return Collections.emptyList();
    }
    if (!dirFile.isDirectory()) {
        log.warn("Not a directory: '" + dir + "'");
        return Collections.emptyList();
    }
    // Files that are not hidden.
    DirectoryStream.Filter<Path> filter = (entry) -> {
        File f = entry.toFile();
        final Lang lang = filenameToLang(f.getName());
        return !f.isHidden() && f.isFile() && lang != null && isRegistered(lang);
    };
    List<DataAccessPoint> dataServiceRef = new ArrayList<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(pDir, filter)) {
        for (Path p : stream) {
            DatasetDescriptionRegistry dsDescMap = FusekiServer.registryForBuild();
            String fn = IRILib.filenameToIRI(p.toString());
            log.info("Load configuration: " + fn);
            Model m = readAssemblerFile(fn);
            readConfiguration(m, dsDescMap, dataServiceRef);
        }
    } catch (IOException ex) {
        log.warn("IOException:" + ex.getMessage(), ex);
    }
    return dataServiceRef;
}
Also used : Path(java.nio.file.Path) RDF(org.apache.jena.vocabulary.RDF) StrUtils(org.apache.jena.atlas.lib.StrUtils) RDFParserRegistry.isRegistered(org.apache.jena.riot.RDFParserRegistry.isRegistered) ArrayList(java.util.ArrayList) JA(org.apache.jena.assembler.JA) DirectoryStream(java.nio.file.DirectoryStream) QuerySolution(org.apache.jena.query.QuerySolution) Fuseki(org.apache.jena.fuseki.Fuseki) Iter(org.apache.jena.atlas.iterator.Iter) FusekiLib(org.apache.jena.fuseki.FusekiLib) Method(java.lang.reflect.Method) Path(java.nio.file.Path) Dataset(org.apache.jena.query.Dataset) Lang(org.apache.jena.riot.Lang) Logger(org.slf4j.Logger) Files(java.nio.file.Files) IOException(java.io.IOException) org.apache.jena.fuseki.server(org.apache.jena.fuseki.server) RDFLanguages.filenameToLang(org.apache.jena.riot.RDFLanguages.filenameToLang) File(java.io.File) org.apache.jena.rdf.model(org.apache.jena.rdf.model) IRILib(org.apache.jena.atlas.lib.IRILib) FusekiConfigException(org.apache.jena.fuseki.FusekiConfigException) List(java.util.List) Paths(java.nio.file.Paths) ReadWrite(org.apache.jena.query.ReadWrite) AssemblerUtils(org.apache.jena.sparql.core.assembler.AssemblerUtils) Collections(java.util.Collections) ResultSet(org.apache.jena.query.ResultSet) DirectoryStream(java.nio.file.DirectoryStream) ArrayList(java.util.ArrayList) Lang(org.apache.jena.riot.Lang) RDFLanguages.filenameToLang(org.apache.jena.riot.RDFLanguages.filenameToLang) IOException(java.io.IOException) File(java.io.File)

Example 27 with Lang

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

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

the class QueryEngineHTTP method execDataset.

private Dataset execDataset(Dataset dataset) {
    Pair<InputStream, Lang> p = execConstructWorker(datasetContentType);
    InputStream in = p.getLeft();
    Lang lang = p.getRight();
    try {
        RDFDataMgr.read(dataset, in, lang);
    } finally {
        this.close();
    }
    return dataset;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Lang(org.apache.jena.riot.Lang)

Example 29 with Lang

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

the class QueryEngineHTTP method setModelContentType.

/**
     * Sets the Content Type for CONSTRUCT/DESCRIBE queries provided that the
     * format is supported
     * 
     * @param contentType
     */
public void setModelContentType(String contentType) {
    // Check that this is a valid setting
    Lang lang = RDFLanguages.contentTypeToLang(contentType);
    if (lang == null)
        throw new IllegalArgumentException("Given Content Type '" + contentType + "' is not supported by RIOT");
    if (!RDFLanguages.isTriples(lang))
        throw new IllegalArgumentException("Given Content Type '" + contentType + "' is not a RDF Graph format");
    modelContentType = contentType;
}
Also used : Lang(org.apache.jena.riot.Lang)

Example 30 with Lang

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

the class QueryEngineHTTP method execTriples.

private Iterator<Triple> execTriples() {
    Pair<InputStream, Lang> p = execConstructWorker(modelContentType);
    InputStream in = p.getLeft();
    Lang lang = p.getRight();
    // Base URI?
    return RDFDataMgr.createIteratorTriples(in, lang, null);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) 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