use of java.io.InputStream in project camel by apache.
the class ManagedCamelContext method dumpRestsAsXml.
@Override
public String dumpRestsAsXml(boolean resolvePlaceholders) throws Exception {
List<RestDefinition> rests = context.getRestDefinitions();
if (rests.isEmpty()) {
return null;
}
// use a routes definition to dump the rests
RestsDefinition def = new RestsDefinition();
def.setRests(rests);
String xml = ModelHelper.dumpModelAsXml(context, def);
// if resolving placeholders we parse the xml, and resolve the property placeholders during parsing
if (resolvePlaceholders) {
final AtomicBoolean changed = new AtomicBoolean();
InputStream is = new ByteArrayInputStream(xml.getBytes());
Document dom = XmlLineNumberParser.parseXml(is, new XmlLineNumberParser.XmlTextTransformer() {
@Override
public String transform(String text) {
try {
String after = getContext().resolvePropertyPlaceholders(text);
if (!changed.get()) {
changed.set(!text.equals(after));
}
return after;
} catch (Exception e) {
// ignore
return text;
}
}
});
// okay there were some property placeholder replaced so re-create the model
if (changed.get()) {
xml = context.getTypeConverter().mandatoryConvertTo(String.class, dom);
RestsDefinition copy = ModelHelper.createModelFromXml(context, xml, RestsDefinition.class);
xml = ModelHelper.dumpModelAsXml(context, copy);
}
}
return xml;
}
use of java.io.InputStream in project camel by apache.
the class ObjectHelper method loadResourceAsStream.
/**
* Attempts to load the given resource as a stream using the thread context
* class loader or the class loader used to load this class
*
* @param name the name of the resource to load
* @param loader optional classloader to attempt first
* @return the stream or null if it could not be loaded
*/
public static InputStream loadResourceAsStream(String name, ClassLoader loader) {
InputStream in = null;
String resolvedName = resolveUriPath(name);
if (loader != null) {
in = loader.getResourceAsStream(resolvedName);
}
if (in == null) {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (contextClassLoader != null) {
in = contextClassLoader.getResourceAsStream(resolvedName);
}
}
if (in == null) {
in = ObjectHelper.class.getClassLoader().getResourceAsStream(resolvedName);
}
if (in == null) {
in = ObjectHelper.class.getResourceAsStream(resolvedName);
}
return in;
}
use of java.io.InputStream 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.InputStream 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;
}
}
use of java.io.InputStream in project camel by apache.
the class BeanWithInputStreamBodyTest method testBeanWithInputStreamBodyMethod.
public void testBeanWithInputStreamBodyMethod() throws Exception {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").bean(MyCoolBean.class, "doSomething").to("mock:result");
}
});
context.start();
getMockEndpoint("mock:result").expectedBodiesReceived("There is 11 bytes");
InputStream bais = new ByteArrayInputStream("Hello World".getBytes());
template.sendBody("direct:start", bais);
assertMockEndpointsSatisfied();
}
Aggregations