use of org.apache.camel.Converter in project camel by apache.
the class RecordStringConverter method toString.
@Converter
public static String toString(Record record) {
Charset charset = Charset.forName("UTF-8");
ByteBuffer buffer = record.getData();
if (buffer.hasArray()) {
byte[] bytes = record.getData().array();
return new String(bytes, charset);
} else {
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
return new String(bytes, charset);
}
}
use of org.apache.camel.Converter in project camel by apache.
the class GoogleDriveFilesConverter method genericFileToGoogleDriveFile.
@Converter
public static com.google.api.services.drive.model.File genericFileToGoogleDriveFile(GenericFile<?> file, Exchange exchange) throws Exception {
if (file.getFile() instanceof File) {
File f = (File) file.getFile();
com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
fileMetadata.setTitle(f.getName());
FileContent mediaContent = new FileContent(null, f);
if (exchange != null) {
exchange.getIn().setHeader("CamelGoogleDrive.content", fileMetadata);
exchange.getIn().setHeader("CamelGoogleDrive.mediaContent", mediaContent);
}
return fileMetadata;
}
if (exchange != null) {
// body wasn't a java.io.File so let's try to convert it
file.getBinding().loadContent(exchange, file);
InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, exchange, file.getBody());
com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
fileMetadata.setTitle(file.getFileName());
InputStreamContent mediaContent = new InputStreamContent(null, is);
if (exchange != null) {
exchange.getIn().setHeader("CamelGoogleDrive.content", fileMetadata);
exchange.getIn().setHeader("CamelGoogleDrive.mediaContent", mediaContent);
}
return fileMetadata;
}
return null;
}
use of org.apache.camel.Converter in project camel by apache.
the class DomConverter method toString.
@Converter
public String toString(NodeList nodeList, Exchange exchange) throws TransformerException {
// converting NodeList to String is more tricky
// sometimes the NodeList is a Node which we can then leverage
// the XML converter to turn into XML incl. tags
StringBuilder buffer = new StringBuilder();
// use XML converter at first since it preserves tag names
boolean found = false;
if (nodeList instanceof Node) {
Node node = (Node) nodeList;
String s = toString(node, exchange);
if (ObjectHelper.isNotEmpty(s)) {
found = true;
buffer.append(s);
}
} else {
// use XML converter at first since it preserves tag names
int size = nodeList.getLength();
for (int i = 0; i < size; i++) {
Node node = nodeList.item(i);
String s = toString(node, exchange);
if (ObjectHelper.isNotEmpty(s)) {
found = true;
buffer.append(s);
}
}
}
// used an xpath to select an attribute or text() or something
if (!found) {
append(buffer, nodeList);
}
return buffer.toString();
}
use of org.apache.camel.Converter in project camel by apache.
the class XmlConverter method toString.
/**
* Converts the given input Source into text
*/
@Converter
public String toString(Source source, Exchange exchange) throws TransformerException {
if (source == null) {
return null;
} else if (source instanceof StringSource) {
return ((StringSource) source).getText();
} else if (source instanceof BytesSource) {
return new String(((BytesSource) source).getData());
} else {
StringWriter buffer = new StringWriter();
if (exchange != null) {
// check the camelContext properties first
Properties properties = ObjectHelper.getCamelPropertiesWithPrefix(OUTPUT_PROPERTIES_PREFIX, exchange.getContext());
if (properties.size() > 0) {
toResult(source, new StreamResult(buffer), properties);
return buffer.toString();
}
}
// using the old way to deal with it
toResult(source, new StreamResult(buffer));
return buffer.toString();
}
}
use of org.apache.camel.Converter in project camel by apache.
the class XmlConverter method toDOMNodeFromStAX.
@Converter
public Node toDOMNodeFromStAX(StAXSource source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
DOMResult result = new DOMResult();
toResult(source, result);
return result.getNode();
}
Aggregations