org.apache.jena.atlas.web.TypedInputStream Java Examples

The following examples show how to use org.apache.jena.atlas.web.TypedInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JenaIOService.java    From trellis with Apache License 2.0 6 votes vote down vote up
private void writeJsonLd(final OutputStream output, final DatasetGraph graph, final IRI... profiles) {
    final String profile = getCustomJsonLdProfile(profiles);
    final RDFFormat format = canUseCustomJsonLdProfile(profile) ? JSONLD_COMPACT_FLAT : getJsonLdProfile(profiles);
    final JsonLDWriteContext ctx = new JsonLDWriteContext();
    if (canUseCustomJsonLdProfile(profile)) {
        LOGGER.debug("Setting JSON-LD context with profile: {}", profile);
        final String c = cache.get(profile, p -> {
            try (final TypedInputStream res = HttpOp.execHttpGet(profile)) {
                return IOUtils.toString(res.getInputStream(), UTF_8);
            } catch (final IOException | HttpException ex) {
                LOGGER.warn("Error fetching profile {}: {}", p, ex.getMessage());
                return null;
            }
        });
        if (c != null) {
            ctx.setJsonLDContext(c);
            ctx.setJsonLDContextSubstitution("\"" + profile + "\"");
        }
    }
    RDFWriter.create().format(format).context(ctx).source(graph).output(output);
}
 
Example #2
Source File: LocatorClassLoaderAccept.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public TypedInputStream open(LookUpRequest request) {
    if (classLoader == null) {
        return null;
    }
    String resourceName = request.getFilenameOrURI();
    InputStream in = classLoader.getResourceAsStream(resourceName);
    if (in == null) {
        return null;
    }
    
    try {
        String ct = Files.probeContentType(Paths.get(new File(resourceName).getName()));
        return new TypedInputStream(in, ContentType.create(ct), resourceName);
    } catch (IOException ex) {
            log.trace("Error while trying to probe content type for " + resourceName + ": " + ex.getMessage());
        return new TypedInputStream(in, (String) null);
    } 
}
 
Example #3
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
public static void loadGraph(Context context, String sourceURI, String baseURI, StreamRDF dest) {
	if(getDataset(context).containsNamedModel(sourceURI)) {
		final Model model = getDataset(context).getNamedModel(sourceURI);
		StreamRDFOps.sendGraphToStream(model.getGraph(), dest);
		return;
	}
	if(!isRootContext(context)) {
		Context parentContext = (Context) context.get(PARENT_CONTEXT);
		loadGraph(parentContext, sourceURI, baseURI, dest);
		return;
	}
	final SPARQLExtStreamManager sm = (SPARQLExtStreamManager) context.get(SysRIOT.sysStreamManager);
	final String acceptHeader = "text/turtle;q=1.0,application/rdf+xml;q=0.9,*/*;q=0.1";
	final LookUpRequest request = new LookUpRequest(sourceURI, acceptHeader);
	try (TypedInputStream tin = sm.open(request);) {
		if(tin == null) {
			LOG.warn("Could not locate graph " + request);
			return;
		}
		Lang lang = RDFLanguages.contentTypeToLang(tin.getMediaType());
		RDFParser.create().source(tin).base(baseURI).context(context).lang(lang).parse(dest);
	} catch (RiotException ex) {
		LOG.warn("Error while loading graph " + sourceURI, ex);
	}
}
 
Example #4
Source File: LocatorFileAccept.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * Open anything that looks a bit like a file name
 */
@Override
public TypedInputStream open(LookUpRequest request) {
    String filenameIRI = request.getFilenameOrURI();
    if(filenameIRI.startsWith("http") || filenameIRI.startsWith("coap")) {
        return null;
    }
    String fn = toFileName(filenameIRI);
    if (fn == null) {
        log.debug("Cannot find a proper filename: " + filenameIRI);
        return null;
    }

    try {
        if (!exists$(fn)) {
            return null;
        }
    } catch (AccessControlException e) {
        log.debug("Security problem testing for file", e);
        return null;
    }

    try {
        InputStream in = IO.openFileEx(fn);
        try {
            String ct = Files.probeContentType(Paths.get(new File(filenameIRI).getName()));
            return new TypedInputStream(in, ContentType.create(ct), filenameIRI);
        } catch (Exception ex) {
            return new TypedInputStream(in, (ContentType) null, filenameIRI);
        } 
    } catch (IOException ioEx) {
        // Includes FileNotFoundException
        // We already tested whether the file exists or not.
        log.debug("File unreadable (but exists): " + fn + " Exception: " + ioEx.getMessage());
        return null;
    }
}
 
Example #5
Source File: SPARQLExtStreamManager.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public TypedInputStream openNoMap(LookUpRequest request) {
    TypedInputStream in = openNoMapOrNull(request);
    if (in == null) {
        throw new RiotNotFoundException(request.toString());
    }
    return in;
}
 
Example #6
Source File: SPARQLExtStreamManager.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public TypedInputStream openNoMapOrNull(LookUpRequest request) {
    for (Locator loc : locators()) {
        LocatorAccept loca = (LocatorAccept) loc;
        TypedInputStream in = loca.open(request);
        if (in != null) {
            LOG.debug("Locator " + loc.getName() + " found: " + request.getFilenameOrURI() + " with accept: " + request.getAccept());
            return in;
        }
    };
    return null;
}
 
Example #7
Source File: SourcePlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
final protected Binding exec(
        final Binding binding,
        final Context context) {

    LOG.debug("Start " + this);
    Objects.nonNull(binding);
    // generate the source URI.
    final String sourceUri = getActualSource(binding);
    final String acceptHeader = getAcceptHeader(binding);
    LOG.trace("... resolved to SOURCE <" + sourceUri + "> ACCEPT " + acceptHeader + " AS " + var);
    final LookUpRequest request = new LookUpRequest(sourceUri, acceptHeader);
    final SPARQLExtStreamManager sm = (SPARQLExtStreamManager) context.get(SysRIOT.sysStreamManager);
    Objects.requireNonNull(sm);
    final TypedInputStream stream = sm.open(request);
    if (stream == null) {
        LOG.info("Exec SOURCE <" + sourceUri + "> ACCEPT " + acceptHeader + " AS " + var + " returned nothing.");
        return BindingFactory.binding(binding);
    }
    try (InputStream in = stream.getInputStream()) {
        final String literal = IOUtils.toString(in, "UTF-8");
        final RDFDatatype dt;
        if (stream.getMediaType() != null && stream.getMediaType().getContentType() != null) {
            dt = tm.getSafeTypeByName("http://www.iana.org/assignments/media-types/" + stream.getMediaType().getContentType());
        } else {
            dt = tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#string");
        }
        final Node n = NodeFactory.createLiteral(literal, dt);
        LOG.debug("Exec " + this + " returned. "
                + "Enable TRACE level for more.");
        if (LOG.isTraceEnabled()) {
            LOG.trace("Exec " + this + " returned\n" + LogUtils.compress(n));
        }
        return BindingFactory.binding(binding, var, n);
    } catch (IOException | DatatypeFormatException ex) {
        LOG.warn("Exception while looking up " + sourceUri + ":", ex);
        return BindingFactory.binding(binding);
    }
}
 
Example #8
Source File: LocatorAcceptBase.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
@Deprecated
public final TypedInputStream open(String uri) {
    return open(new LookUpRequest(uri));
}
 
Example #9
Source File: SPARQLExtStreamManager.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
/**
 * Open a file using the locators of this StreamManager. Returns null if not
 * found.
 */
@Override
@Deprecated
public TypedInputStream open(String filenameOrURI) {
    return open(new LookUpRequest(filenameOrURI, LookUpRequest.ACCEPT_ALL));
}
 
Example #10
Source File: SPARQLExtStreamManager.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
/**
 * Open a file using the locators of this StreamManager. Returns null if not
 * found.
 */
public TypedInputStream open(LookUpRequest _request) {
    LookUpRequest request = mapRequest(_request);
    return openNoMapOrNull(request);
}
 
Example #11
Source File: SPARQLExtStreamManager.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
/**
 * Open a file using the locators of this FileManager but without location
 * mapping. Throws RiotNotFoundException if not found.
 */
@Override
@Deprecated
public TypedInputStream openNoMap(String filenameOrURI) {
    return openNoMap(new LookUpRequest(filenameOrURI, LookUpRequest.ACCEPT_ALL));
}
 
Example #12
Source File: SPARQLExtStreamManager.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
/**
 * Open a file using the locators of this FileManager without location
 * mapping. Return null if not found
 */
@Override
@Deprecated
public TypedInputStream openNoMapOrNull(String filenameOrURI) {
    return openNoMapOrNull(new LookUpRequest(filenameOrURI, LookUpRequest.ACCEPT_ALL));
}
 
Example #13
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static TypedInputStream openStream(Context context, String sourceUri, String acceptHeader) {
	final LookUpRequest request = new LookUpRequest(sourceUri, acceptHeader);
	final SPARQLExtStreamManager sm = (SPARQLExtStreamManager) context.get(SysRIOT.sysStreamManager);
	Objects.requireNonNull(sm);
	return sm.open(request);
}
 
Example #14
Source File: LocatorAccept.java    From sparql-generate with Apache License 2.0 votes vote down vote up
public TypedInputStream open(LookUpRequest request);