use of java.io.InputStream in project camel by apache.
the class AtmosPropertyManager method loadProperties.
private static Properties loadProperties() throws Exception {
URL url = AtmosPropertyManager.class.getResource("/atmos.properties");
InputStream inStream;
try {
inStream = url.openStream();
} catch (IOException e) {
throw new AtmosException("atmos.properties could not be found");
}
properties = new Properties();
try {
properties.load(inStream);
} catch (IOException e) {
throw new AtmosException("atmos.properties can't be read");
}
return properties;
}
use of java.io.InputStream in project camel by apache.
the class BeanIOSplitter method createStreamFactory.
protected StreamFactory createStreamFactory(CamelContext camelContext) throws Exception {
ObjectHelper.notNull(getStreamName(), "Stream name not configured.");
// Create the stream factory that will be used to read/write objects.
StreamFactory answer = StreamFactory.newInstance();
// Load the mapping file using the resource helper to ensure it can be loaded in OSGi and other environments
InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, getMapping());
try {
if (getProperties() != null) {
answer.load(is, getProperties());
} else {
answer.load(is);
}
} finally {
IOHelper.close(is);
}
return answer;
}
use of java.io.InputStream in project camel by apache.
the class BeanIODataFormat method doStart.
@Override
protected void doStart() throws Exception {
ObjectHelper.notNull(getStreamName(), "Stream name not configured.");
if (factory == null) {
// Create the stream factory that will be used to read/write objects.
factory = StreamFactory.newInstance();
// Load the mapping file using the resource helper to ensure it can be loaded in OSGi and other environments
InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), getMapping());
try {
if (getProperties() != null) {
factory.load(is, getProperties());
} else {
factory.load(is);
}
} finally {
IOHelper.close(is);
}
}
}
use of java.io.InputStream in project camel by apache.
the class DefaultCamelContext method getLanguageParameterJsonSchema.
public String getLanguageParameterJsonSchema(String languageName) throws IOException {
// use the language factory finder to find the package name of the language class, which is the location
// where the documentation exists as well
FactoryFinder finder = getFactoryFinder(DefaultLanguageResolver.LANGUAGE_RESOURCE_PATH);
try {
Class<?> clazz = null;
try {
clazz = finder.findClass(languageName);
} catch (NoFactoryAvailableException e) {
// ignore, i.e. if a component is an auto-configured spring-boot
// languages
}
if (clazz == null) {
return null;
}
String packageName = clazz.getPackage().getName();
packageName = packageName.replace('.', '/');
String path = packageName + "/" + languageName + ".json";
ClassResolver resolver = getClassResolver();
InputStream inputStream = resolver.loadResourceAsStream(path);
log.debug("Loading language JSON Schema for: {} using class resolver: {} -> {}", new Object[] { languageName, resolver, inputStream });
if (inputStream != null) {
try {
return IOHelper.loadText(inputStream);
} finally {
IOHelper.close(inputStream);
}
}
return null;
} catch (ClassNotFoundException e) {
return null;
}
}
use of java.io.InputStream in project camel by apache.
the class RestProducerBindingProcessor method process.
@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
boolean isXml = false;
boolean isJson = false;
// skip before binding for empty/null body
Object body = exchange.getIn().getBody();
if (ObjectHelper.isEmpty(body)) {
if (outType != null) {
// wrap callback to add reverse operation if we know the output type from the REST service
callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, false);
}
// okay now we can continue routing to the producer
return getProcessor().process(exchange, callback);
}
// we only need to perform before binding if the message body is POJO based
if (body instanceof String || body instanceof byte[]) {
// the body is text based and thus not POJO so no binding needed
if (outType != null) {
// wrap callback to add reverse operation if we know the output type from the REST service
callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, false);
}
// okay now we can continue routing to the producer
return getProcessor().process(exchange, callback);
} else {
// if its convertable to stream based then its not POJO based
InputStream is = camelContext.getTypeConverter().tryConvertTo(InputStream.class, exchange, body);
if (is != null) {
exchange.getIn().setBody(is);
if (outType != null) {
// wrap callback to add reverse operation if we know the output type from the REST service
callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, false);
}
// okay now we can continue routing to the producer
return getProcessor().process(exchange, callback);
}
}
// assume body is POJO based and binding needed
String contentType = ExchangeHelper.getContentType(exchange);
if (contentType != null) {
isXml = contentType.toLowerCase(Locale.ENGLISH).contains("xml");
isJson = contentType.toLowerCase(Locale.ENGLISH).contains("json");
}
// only allow xml/json if the binding mode allows that
isXml &= bindingMode.equals("auto") || bindingMode.contains("xml");
isJson &= bindingMode.equals("auto") || bindingMode.contains("json");
// if we do not yet know if its xml or json, then use the binding mode to know the mode
if (!isJson && !isXml) {
isXml = bindingMode.equals("auto") || bindingMode.contains("xml");
isJson = bindingMode.equals("auto") || bindingMode.contains("json");
}
// favor json over xml
if (isJson && jsonMarshal != null) {
try {
jsonMarshal.process(exchange);
} catch (Exception e) {
// we failed so cannot call producer
exchange.setException(e);
callback.done(true);
return true;
}
// need to prepare exchange first
ExchangeHelper.prepareOutToIn(exchange);
if (outType != null) {
// wrap callback to add reverse operation if we know the output type from the REST service
callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, false);
}
// okay now we can continue routing to the producer
return getProcessor().process(exchange, callback);
} else if (isXml && xmlMarshal != null) {
try {
xmlMarshal.process(exchange);
} catch (Exception e) {
// we failed so cannot call producer
exchange.setException(e);
callback.done(true);
return true;
}
// need to prepare exchange first
ExchangeHelper.prepareOutToIn(exchange);
if (outType != null) {
// wrap callback to add reverse operation if we know the output type from the REST service
callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, true);
}
// okay now we can continue routing to the producer
return getProcessor().process(exchange, callback);
}
// we could not bind
if ("off".equals(bindingMode) || bindingMode.equals("auto")) {
if (outType != null) {
// wrap callback to add reverse operation if we know the output type from the REST service
callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, false);
}
// okay now we can continue routing to the producer
return getProcessor().process(exchange, callback);
} else {
if (bindingMode.contains("xml")) {
exchange.setException(new BindingException("Cannot bind to xml as message body is not xml compatible", exchange));
} else {
exchange.setException(new BindingException("Cannot bind to json as message body is not json compatible", exchange));
}
// we failed so cannot call producer
callback.done(true);
return true;
}
}
Aggregations