Search in sources :

Example 6 with FileNotFoundException

use of java.io.FileNotFoundException in project camel by apache.

the class RouteScopedOnExceptionSameTypeTest method testOnExceptionRouteBestMatchAndGlobalSameType.

public void testOnExceptionRouteBestMatchAndGlobalSameType() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            onException(IOException.class).handled(true).to("mock:foo");
            from("direct:start").onException(IOException.class).handled(true).to("mock:damn").end().throwException(new FileNotFoundException("unknown.txt"));
        }
    });
    context.start();
    // route scope is preferred over context scoped
    getMockEndpoint("mock:damn").expectedMessageCount(1);
    getMockEndpoint("mock:foo").expectedMessageCount(0);
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 7 with FileNotFoundException

use of java.io.FileNotFoundException in project camel by apache.

the class ResourceUtils method getInputStream.

public static InputStream getInputStream(String path) {
    InputStream is = null;
    if (isClasspathResource(path)) {
        String classpathResourcePath = ResourceUtils.getClasspathResourcePath(path);
        is = ResourceUtils.class.getResourceAsStream(classpathResourcePath);
        if (is == null) {
            throw new RuntimeIOException("Certificate stream is null: '" + classpathResourcePath + "'");
        }
    } else {
        try {
            is = IOHelper.buffered(new FileInputStream(path));
        } catch (FileNotFoundException e) {
            throw new RuntimeIOException(e);
        }
    }
    return is;
}
Also used : RuntimeIOException(com.notnoop.exceptions.RuntimeIOException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream)

Example 8 with FileNotFoundException

use of java.io.FileNotFoundException in project camel by apache.

the class AmazonS3ClientMock method putObject.

@SuppressWarnings("resource")
@Override
public PutObjectResult putObject(PutObjectRequest putObjectRequest) throws AmazonClientException, AmazonServiceException {
    putObjectRequests.add(putObjectRequest);
    S3Object s3Object = new S3Object();
    s3Object.setBucketName(putObjectRequest.getBucketName());
    s3Object.setKey(putObjectRequest.getKey());
    if (putObjectRequest.getFile() != null) {
        try {
            s3Object.setObjectContent(new FileInputStream(putObjectRequest.getFile()));
        } catch (FileNotFoundException e) {
            throw new AmazonServiceException("Cannot store the file object.", e);
        }
    } else {
        s3Object.setObjectContent(putObjectRequest.getInputStream());
    }
    objects.add(s3Object);
    PutObjectResult putObjectResult = new PutObjectResult();
    putObjectResult.setETag("3a5c8b1ad448bca04584ecb55b836264");
    return putObjectResult;
}
Also used : PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) FileNotFoundException(java.io.FileNotFoundException) AmazonServiceException(com.amazonaws.AmazonServiceException) S3Object(com.amazonaws.services.s3.model.S3Object) FileInputStream(java.io.FileInputStream)

Example 9 with FileNotFoundException

use of java.io.FileNotFoundException in project camel by apache.

the class ObjectHelper method getScanner.

/**
     * Creates a {@link Scanner} for scanning the given value.
     *
     * @param exchange  the current exchange
     * @param value     the value, typically the message IN body
     * @return the scanner, is newer <tt>null</tt>
     */
public static Scanner getScanner(Exchange exchange, Object value) {
    if (value instanceof WrappedFile) {
        WrappedFile<?> gf = (WrappedFile<?>) value;
        Object body = gf.getBody();
        if (body != null) {
            // we have loaded the file content into the body so use that
            value = body;
        } else {
            // generic file is just a wrapper for the real file so call again with the real file
            return getScanner(exchange, gf.getFile());
        }
    }
    String charset = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
    Scanner scanner = null;
    if (value instanceof Readable) {
        scanner = new Scanner((Readable) value);
    } else if (value instanceof InputStream) {
        scanner = charset == null ? new Scanner((InputStream) value) : new Scanner((InputStream) value, charset);
    } else if (value instanceof File) {
        try {
            scanner = charset == null ? new Scanner((File) value) : new Scanner((File) value, charset);
        } catch (FileNotFoundException e) {
            throw new RuntimeCamelException(e);
        }
    } else if (value instanceof String) {
        scanner = new Scanner((String) value);
    } else if (value instanceof ReadableByteChannel) {
        scanner = charset == null ? new Scanner((ReadableByteChannel) value) : new Scanner((ReadableByteChannel) value, charset);
    }
    if (scanner == null) {
        // value is not a suitable type, try to convert value to a string
        String text = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, value);
        if (text != null) {
            scanner = new Scanner(text);
        }
    }
    if (scanner == null) {
        scanner = new Scanner("");
    }
    return scanner;
}
Also used : Scanner(java.util.Scanner) ReadableByteChannel(java.nio.channels.ReadableByteChannel) WrappedFile(org.apache.camel.WrappedFile) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile)

Example 10 with FileNotFoundException

use of java.io.FileNotFoundException in project camel by apache.

the class ResourceHelper method resolveMandatoryResourceAsInputStream.

/**
     * Resolves the mandatory resource.
     * <p/>
     * The resource uri can refer to the following systems to be loaded from
     * <ul>
     *     <il>file:nameOfFile - to refer to the file system</il>
     *     <il>classpath:nameOfFile - to refer to the classpath (default)</il>
     *     <il>http:uri - to load the resource using HTTP</il>
     *     <il>ref:nameOfBean - to lookup the resource in the {@link org.apache.camel.spi.Registry}</il>
     *     <il>bean:nameOfBean.methodName - to lookup a bean in the {@link org.apache.camel.spi.Registry} and call the method</il>
     * </ul>
     * If no prefix has been given, then the resource is loaded from the classpath
     * <p/>
     * If possible recommended to use {@link #resolveMandatoryResourceAsUrl(org.apache.camel.spi.ClassResolver, String)}
     *
     * @param camelContext the Camel Context
     * @param uri URI of the resource
     * @return the resource as an {@link InputStream}.  Remember to close this stream after usage.
     * @throws java.io.IOException is thrown if the resource file could not be found or loaded as {@link InputStream}
     */
public static InputStream resolveMandatoryResourceAsInputStream(CamelContext camelContext, String uri) throws IOException {
    if (uri.startsWith("ref:")) {
        String ref = uri.substring(4);
        String value = CamelContextHelper.mandatoryLookup(camelContext, ref, String.class);
        return new ByteArrayInputStream(value.getBytes());
    } else if (uri.startsWith("bean:")) {
        String bean = uri.substring(5);
        if (bean.contains(".")) {
            String method = StringHelper.after(bean, ".");
            bean = StringHelper.before(bean, ".") + "?method=" + method;
        }
        Exchange dummy = new DefaultExchange(camelContext);
        Object out = camelContext.resolveLanguage("bean").createExpression(bean).evaluate(dummy, Object.class);
        if (dummy.getException() != null) {
            IOException io = new IOException("Cannot find resource: " + uri + " from calling the bean");
            io.initCause(dummy.getException());
            throw io;
        }
        if (out != null) {
            InputStream is = camelContext.getTypeConverter().tryConvertTo(InputStream.class, dummy, out);
            if (is == null) {
                String text = camelContext.getTypeConverter().tryConvertTo(String.class, dummy, out);
                if (text != null) {
                    return new ByteArrayInputStream(text.getBytes());
                }
            } else {
                return is;
            }
        } else {
            throw new IOException("Cannot find resource: " + uri + " from calling the bean");
        }
    }
    InputStream is = resolveResourceAsInputStream(camelContext.getClassResolver(), uri);
    if (is == null) {
        String resolvedName = resolveUriPath(uri);
        throw new FileNotFoundException("Cannot find resource: " + resolvedName + " in classpath for URI: " + uri);
    } else {
        return is;
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)3218 IOException (java.io.IOException)1836 File (java.io.File)1277 FileInputStream (java.io.FileInputStream)814 FileOutputStream (java.io.FileOutputStream)492 InputStream (java.io.InputStream)466 BufferedReader (java.io.BufferedReader)262 FileReader (java.io.FileReader)230 ArrayList (java.util.ArrayList)205 Path (org.apache.hadoop.fs.Path)202 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)189 InputStreamReader (java.io.InputStreamReader)171 Test (org.junit.Test)171 XmlPullParser (org.xmlpull.v1.XmlPullParser)166 BufferedInputStream (java.io.BufferedInputStream)138 ParcelFileDescriptor (android.os.ParcelFileDescriptor)131 Properties (java.util.Properties)120 URL (java.net.URL)119 FileStatus (org.apache.hadoop.fs.FileStatus)119 RandomAccessFile (java.io.RandomAccessFile)101