Search in sources :

Example 51 with InputStream

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();
        }
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) CloudPageBlob(com.microsoft.azure.storage.blob.CloudPageBlob) PageRange(com.microsoft.azure.storage.blob.PageRange)

Example 52 with InputStream

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);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 53 with 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();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 54 with InputStream

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);
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File) URL(java.net.URL) URLConnection(java.net.URLConnection) FileInputStream(java.io.FileInputStream)

Example 55 with InputStream

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;
    }
}
Also used : InputStream(java.io.InputStream) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) Component(org.apache.camel.Component) PackageScanClassResolver(org.apache.camel.spi.PackageScanClassResolver) ClassResolver(org.apache.camel.spi.ClassResolver) FactoryFinder(org.apache.camel.spi.FactoryFinder)

Aggregations

InputStream (java.io.InputStream)12635 IOException (java.io.IOException)4626 ByteArrayInputStream (java.io.ByteArrayInputStream)2818 Test (org.junit.Test)2722 FileInputStream (java.io.FileInputStream)2443 File (java.io.File)1750 OutputStream (java.io.OutputStream)1173 InputStreamReader (java.io.InputStreamReader)1091 BufferedInputStream (java.io.BufferedInputStream)1075 URL (java.net.URL)1032 FileOutputStream (java.io.FileOutputStream)821 ByteArrayOutputStream (java.io.ByteArrayOutputStream)782 BufferedReader (java.io.BufferedReader)742 FileNotFoundException (java.io.FileNotFoundException)587 Properties (java.util.Properties)580 ArrayList (java.util.ArrayList)562 HttpURLConnection (java.net.HttpURLConnection)424 HashMap (java.util.HashMap)397 GZIPInputStream (java.util.zip.GZIPInputStream)342 Metadata (org.apache.tika.metadata.Metadata)319