use of java.io.InputStream in project camel by apache.
the class BlobServiceUtil method doGetBlob.
private static void doGetBlob(CloudBlob client, Exchange exchange, BlobServiceConfiguration cfg) throws Exception {
BlobServiceUtil.configureCloudBlobForRead(client, cfg);
BlobServiceRequestOptions opts = getRequestOptions(exchange);
LOG.trace("Getting a blob [{}] from exchange [{}]...", cfg.getBlobName(), exchange);
OutputStream os = exchange.getIn().getBody(OutputStream.class);
if (os == null) {
String fileDir = cfg.getFileDir();
if (fileDir != null) {
File file = new File(fileDir, getBlobFileName(cfg));
ExchangeUtil.getMessageForResponse(exchange).setBody(file);
os = new FileOutputStream(file);
}
}
try {
if (os == null) {
// Let the producers like file: deal with it
InputStream blobStream = client.openInputStream(opts.getAccessCond(), opts.getRequestOpts(), opts.getOpContext());
exchange.getIn().setBody(blobStream);
exchange.getIn().setHeader(Exchange.FILE_NAME, getBlobFileName(cfg));
} else {
Long blobOffset = cfg.getBlobOffset();
Long blobDataLength = cfg.getDataLength();
if (client instanceof CloudPageBlob) {
PageRange range = exchange.getIn().getHeader(BlobServiceConstants.PAGE_BLOB_RANGE, PageRange.class);
if (range != null) {
blobOffset = range.getStartOffset();
blobDataLength = range.getEndOffset() - range.getStartOffset();
}
}
client.downloadRange(blobOffset, blobDataLength, os, opts.getAccessCond(), opts.getRequestOpts(), opts.getOpContext());
}
} finally {
if (os != null && cfg.isCloseStreamAfterRead()) {
os.close();
}
}
}
use of java.io.InputStream in project camel by apache.
the class SftpSimpleConsumeStreamingTest method testSftpSimpleConsume.
@Test
public void testSftpSimpleConsume() throws Exception {
if (!canTest()) {
return;
}
String expected = "Hello World";
// create file using regular file
template.sendBodyAndHeader("file://" + FTP_ROOT_DIR, expected, Exchange.FILE_NAME, "hello.txt");
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.expectedHeaderReceived(Exchange.FILE_NAME, "hello.txt");
mock.expectedBodiesReceived(expected);
context.startRoute("foo");
assertMockEndpointsSatisfied();
GenericFile<?> remoteFile = mock.getExchanges().get(0).getIn().getBody(GenericFile.class);
assertTrue(remoteFile.getBody() instanceof InputStream);
}
use of java.io.InputStream in project camel by apache.
the class SftpKeyPairDSAConsumeTest method getBytesFromFile.
private byte[] getBytesFromFile(String filename) throws IOException {
InputStream input;
input = new FileInputStream(new File(filename));
ByteArrayOutputStream output = new ByteArrayOutputStream();
IOHelper.copyAndCloseInput(input, output);
return output.toByteArray();
}
use of java.io.InputStream in project camel by apache.
the class DefaultPackageScanClassResolver method find.
protected void find(PackageScanFilter test, String packageName, ClassLoader loader, Set<Class<?>> classes) {
if (log.isTraceEnabled()) {
log.trace("Searching for: {} in package: {} using classloader: {}", new Object[] { test, packageName, loader.getClass().getName() });
}
Enumeration<URL> urls;
try {
urls = getResources(loader, packageName);
if (!urls.hasMoreElements()) {
log.trace("No URLs returned by classloader");
}
} catch (IOException ioe) {
log.warn("Cannot read package: " + packageName, ioe);
return;
}
while (urls.hasMoreElements()) {
URL url = null;
try {
url = urls.nextElement();
log.trace("URL from classloader: {}", url);
url = customResourceLocator(url);
String urlPath = url.getFile();
urlPath = URLDecoder.decode(urlPath, "UTF-8");
if (log.isTraceEnabled()) {
log.trace("Decoded urlPath: {} with protocol: {}", urlPath, url.getProtocol());
}
// If it's a file in a directory, trim the stupid file: spec
if (urlPath.startsWith("file:")) {
// to remedy this then create new path without using the URLDecoder
try {
urlPath = new URI(url.getFile()).getPath();
} catch (URISyntaxException e) {
// fallback to use as it was given from the URLDecoder
// this allows us to work on Windows if users have spaces in paths
}
if (urlPath.startsWith("file:")) {
urlPath = urlPath.substring(5);
}
}
// osgi bundles should be skipped
if (url.toString().startsWith("bundle:") || urlPath.startsWith("bundle:")) {
log.trace("Skipping OSGi bundle: {}", url);
continue;
}
// bundle resource should be skipped
if (url.toString().startsWith("bundleresource:") || urlPath.startsWith("bundleresource:")) {
log.trace("Skipping bundleresource: {}", url);
continue;
}
// Else it's in a JAR, grab the path to the jar
if (urlPath.indexOf('!') > 0) {
urlPath = urlPath.substring(0, urlPath.indexOf('!'));
}
log.trace("Scanning for classes in: {} matching criteria: {}", urlPath, test);
File file = new File(urlPath);
if (file.isDirectory()) {
log.trace("Loading from directory using file: {}", file);
loadImplementationsInDirectory(test, packageName, file, classes);
} else {
InputStream stream;
if (urlPath.startsWith("http:") || urlPath.startsWith("https:") || urlPath.startsWith("sonicfs:") || isAcceptableScheme(urlPath)) {
// load resources using http/https, sonicfs and other acceptable scheme
// sonic ESB requires to be loaded using a regular URLConnection
log.trace("Loading from jar using url: {}", urlPath);
URL urlStream = new URL(urlPath);
URLConnection con = urlStream.openConnection();
// disable cache mainly to avoid jar file locking on Windows
con.setUseCaches(false);
stream = con.getInputStream();
} else {
log.trace("Loading from jar using file: {}", file);
stream = new FileInputStream(file);
}
loadImplementationsInJar(test, packageName, stream, urlPath, classes, jarCache);
}
} catch (IOException e) {
// use debug logging to avoid being to noisy in logs
log.debug("Cannot read entries in url: " + url, e);
}
}
}
use of java.io.InputStream in project camel by apache.
the class DefaultCamelContext method getComponentParameterJsonSchema.
public String getComponentParameterJsonSchema(String componentName) throws IOException {
// use the component factory finder to find the package name of the component class, which is the location
// where the documentation exists as well
FactoryFinder finder = getFactoryFinder(DefaultComponentResolver.RESOURCE_PATH);
try {
Class<?> clazz = null;
try {
clazz = finder.findClass(componentName);
} catch (NoFactoryAvailableException e) {
// ignore, i.e. if a component is an auto-configured spring-boot
// component
}
if (clazz == null) {
// fallback and find existing component
Component existing = hasComponent(componentName);
if (existing != null) {
clazz = existing.getClass();
} else {
return null;
}
}
String packageName = clazz.getPackage().getName();
packageName = packageName.replace('.', '/');
String path = packageName + "/" + componentName + ".json";
ClassResolver resolver = getClassResolver();
InputStream inputStream = resolver.loadResourceAsStream(path);
log.debug("Loading component JSON Schema for: {} using class resolver: {} -> {}", new Object[] { componentName, resolver, inputStream });
if (inputStream != null) {
try {
return IOHelper.loadText(inputStream);
} finally {
IOHelper.close(inputStream);
}
}
// special for ActiveMQ as it is really just JMS
if ("ActiveMQComponent".equals(clazz.getSimpleName())) {
return getComponentParameterJsonSchema("jms");
} else {
return null;
}
} catch (ClassNotFoundException e) {
return null;
}
}
Aggregations