Search in sources :

Example 41 with IOException

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

the class FailOverLoadBalanceMultipleExceptionTest method createRouteBuilder.

protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {

        public void configure() {
            from("direct:start").loadBalance().failover(IllegalArgumentException.class, IOException.class, CamelException.class).to("direct:x", "direct:y", "direct:z");
            from("direct:x").to("mock:x").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    throw new CamelExchangeException("Forced", exchange);
                }
            });
            from("direct:y").to("mock:y").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    throw new IOException("Forced");
                }
            });
            from("direct:z").to("mock:z");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) CamelExchangeException(org.apache.camel.CamelExchangeException) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) CamelException(org.apache.camel.CamelException) IOException(java.io.IOException) IOException(java.io.IOException) CamelExchangeException(org.apache.camel.CamelExchangeException) CamelException(org.apache.camel.CamelException)

Example 42 with IOException

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

the class XMLTokenExpressionIteratorInvalidXMLTest method invokeAndVerify.

private void invokeAndVerify(Iterator<?> tokenizer, boolean error) throws IOException, XMLStreamException {
    Exception exp = null;
    try {
        tokenizer.next();
        tokenizer.next();
    } catch (Exception e) {
        exp = e;
    } finally {
        ((Closeable) tokenizer).close();
    }
    if (error) {
        assertNotNull("the error expected", exp);
    } else {
        assertNull("no error expected", exp);
    }
}
Also used : Closeable(java.io.Closeable) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Example 43 with IOException

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

the class OnExceptionRouteTest method testErrorWhileHandlingException.

public void testErrorWhileHandlingException() throws Exception {
    // DLC does not handle the exception as we failed during processing in onException
    MockEndpoint error = getMockEndpoint("mock:error");
    error.expectedMessageCount(0);
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(0);
    try {
        template.sendBody("direct:start", "<order><type>myType</type><user>FuncError</user></order>");
        fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
        // the myOwnHandlerBean throw exception while handling an exception
        IOException cause = assertIsInstanceOf(IOException.class, e.getCause());
        assertEquals("Damn something did not work", cause.getMessage());
    }
    assertMockEndpointsSatisfied();
    // should not handle it
    assertNull(myOwnHandlerBean.getPayload());
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) IOException(java.io.IOException)

Example 44 with IOException

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

the class WebsocketRouteTest method readAll.

private static byte[] readAll(InputStream is) {
    ByteArrayOutputStream bytebuf = new ByteArrayOutputStream();
    try {
        byte[] buf = new byte[4024];
        int n;
        while ((n = is.read(buf, 0, buf.length)) > 0) {
            bytebuf.write(buf, 0, n);
        }
    } catch (IOException e) {
    // ignore
    } finally {
        try {
            is.close();
        } catch (IOException e) {
        // ignore
        }
    }
    return bytebuf.toByteArray();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 45 with IOException

use of java.io.IOException 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)

Aggregations

IOException (java.io.IOException)41104 File (java.io.File)7663 InputStream (java.io.InputStream)4105 Test (org.junit.Test)3557 ArrayList (java.util.ArrayList)3023 FileInputStream (java.io.FileInputStream)2674 FileOutputStream (java.io.FileOutputStream)2358 FileNotFoundException (java.io.FileNotFoundException)2146 BufferedReader (java.io.BufferedReader)2028 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1761 InputStreamReader (java.io.InputStreamReader)1677 URL (java.net.URL)1608 HashMap (java.util.HashMap)1552 ByteArrayInputStream (java.io.ByteArrayInputStream)1534 OutputStream (java.io.OutputStream)1349 Path (org.apache.hadoop.fs.Path)1316 Map (java.util.Map)1212 List (java.util.List)1042 Properties (java.util.Properties)994 ServletException (javax.servlet.ServletException)916