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();
}
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;
}
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;
}
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;
}
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;
}
}
Aggregations