use of java.io.InputStream in project camel by apache.
the class NIOConverter method toByteBuffer.
@Converter
public static ByteBuffer toByteBuffer(File file) throws IOException {
InputStream in = null;
try {
byte[] buf = new byte[(int) file.length()];
in = IOHelper.buffered(new FileInputStream(file));
int sizeLeft = (int) file.length();
int offset = 0;
while (sizeLeft > 0) {
int readSize = in.read(buf, offset, sizeLeft);
sizeLeft -= readSize;
offset += readSize;
}
return ByteBuffer.wrap(buf);
} finally {
IOHelper.close(in, "Failed to close file stream: " + file.getPath(), LOG);
}
}
use of java.io.InputStream in project camel by apache.
the class XmlConverter method toStAXSource.
/**
* Converts the source instance to a {@link StAXSource} or returns null if the conversion is not
* supported (making it easy to derive from this class to add new kinds of conversion).
* @throws FileNotFoundException
* @throws XMLStreamException
*/
@Converter
public StAXSource toStAXSource(File file, Exchange exchange) throws FileNotFoundException, XMLStreamException {
InputStream is = IOHelper.buffered(new FileInputStream(file));
XMLStreamReader r = new StaxConverter().createXMLStreamReader(is, exchange);
return new StAXSource(r);
}
use of java.io.InputStream in project camel by apache.
the class XmlConverter method toDOMSourceFromStream.
@Converter
public DOMSource toDOMSourceFromStream(StreamSource source, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
Document document;
String systemId = source.getSystemId();
DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
Reader reader = source.getReader();
if (reader != null) {
document = builder.parse(new InputSource(reader));
} else {
InputStream inputStream = source.getInputStream();
if (inputStream != null) {
InputSource inputsource = new InputSource(inputStream);
inputsource.setSystemId(systemId);
document = builder.parse(inputsource);
} else {
throw new IOException("No input stream or reader available on StreamSource: " + source);
}
}
return new DOMSource(document, systemId);
}
use of java.io.InputStream in project camel by apache.
the class ManagedCamelContext method dumpRoutesAsXml.
@Override
public String dumpRoutesAsXml(boolean resolvePlaceholders) throws Exception {
List<RouteDefinition> routes = context.getRouteDefinitions();
if (routes.isEmpty()) {
return null;
}
// use a routes definition to dump the routes
RoutesDefinition def = new RoutesDefinition();
def.setRoutes(routes);
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);
RoutesDefinition copy = ModelHelper.createModelFromXml(context, xml, RoutesDefinition.class);
xml = ModelHelper.dumpModelAsXml(context, copy);
}
}
return xml;
}
use of java.io.InputStream in project camel by apache.
the class ManagedCamelContext method addOrUpdateRoutesFromXml.
public void addOrUpdateRoutesFromXml(String xml, boolean urlDecode) throws Exception {
// decode String as it may have been encoded, from its xml source
if (urlDecode) {
xml = URLDecoder.decode(xml, "UTF-8");
}
InputStream is = context.getTypeConverter().mandatoryConvertTo(InputStream.class, xml);
RoutesDefinition def = context.loadRoutesDefinition(is);
if (def == null) {
return;
}
try {
// add will remove existing route first
context.addRouteDefinitions(def.getRoutes());
} catch (Exception e) {
// log the error as warn as the management api may be invoked remotely over JMX which does not propagate such exception
String msg = "Error updating routes from xml: " + xml + " due: " + e.getMessage();
LOG.warn(msg, e);
throw e;
}
}
Aggregations