use of org.apache.camel.TypeConverter in project camel by apache.
the class CamelJaxbFallbackConverterTest method testFallbackConverterUnmarshalWithNonJAXBComplaintValue.
@Test
public void testFallbackConverterUnmarshalWithNonJAXBComplaintValue() throws Exception {
TypeConverter converter = context.getTypeConverter();
try {
converter.convertTo(Foo.class, "Not every String is XML");
fail("Should have thrown exception");
} catch (TypeConversionException e) {
// expected
}
try {
converter.convertTo(Bar.class, "<bar></bar");
fail("Should have thrown exception");
} catch (TypeConversionException e) {
// expected
}
}
use of org.apache.camel.TypeConverter in project camel by apache.
the class CamelJaxbFallbackConverterTest method testConverter.
@Test
public void testConverter() throws Exception {
TypeConverter converter = context.getTypeConverter();
PersonType person = converter.convertTo(PersonType.class, "<Person><firstName>FOO</firstName><lastName>BAR</lastName></Person>");
assertNotNull("Person should not be null ", person);
assertEquals("Get the wrong first name ", "FOO", person.getFirstName());
assertEquals("Get the wrong second name ", "BAR", person.getLastName());
Exchange exchange = new DefaultExchange(context);
exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
String value = converter.convertTo(String.class, exchange, person);
assertTrue("Should get a right marshalled string", value.indexOf("<lastName>BAR</lastName>") > 0);
byte[] buffers = "<Person><firstName>FOO</firstName><lastName>BAR</lastName></Person>".getBytes("UTF-8");
InputStream is = new ByteArrayInputStream(buffers);
try {
converter.convertTo(PersonType.class, exchange, is);
fail("Should have thrown exception");
} catch (TypeConversionException e) {
// expected
}
}
use of org.apache.camel.TypeConverter in project camel by apache.
the class MongoDbProducer method attemptConvertToList.
@SuppressWarnings("rawtypes")
private List<DBObject> attemptConvertToList(List insertList, Exchange exchange) throws CamelMongoDbException {
List<DBObject> dbObjectList = new ArrayList<DBObject>(insertList.size());
TypeConverter converter = exchange.getContext().getTypeConverter();
for (Object item : insertList) {
try {
DBObject dbObject = converter.mandatoryConvertTo(DBObject.class, item);
dbObjectList.add(dbObject);
} catch (Exception e) {
throw new CamelMongoDbException("MongoDB operation = insert, Assuming List variant of MongoDB insert operation, but List contains non-DBObject items", e);
}
}
return dbObjectList;
}
use of org.apache.camel.TypeConverter in project camel by apache.
the class SaxonConverter method convertTo.
@FallbackConverter
public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
if (NodeInfo.class.isAssignableFrom(value.getClass())) {
// use a fallback type converter so we can convert the embedded body if the value is NodeInfo
NodeInfo ni = (NodeInfo) value;
// first try to find a Converter for Node
TypeConverter tc = registry.lookup(type, Node.class);
if (tc != null) {
Node node = NodeOverNodeInfo.wrap(ni);
return tc.convertTo(type, exchange, node);
}
// if this does not exist we can also try NodeList (there are some type converters for that) as
// the default Xerces Node implementation also implements NodeList.
tc = registry.lookup(type, NodeList.class);
if (tc != null) {
List<NodeInfo> nil = new LinkedList<NodeInfo>();
nil.add((NodeInfo) value);
return tc.convertTo(type, exchange, toDOMNodeList(nil));
}
} else if (List.class.isAssignableFrom(value.getClass())) {
TypeConverter tc = registry.lookup(type, NodeList.class);
if (tc != null) {
List<NodeInfo> lion = new LinkedList<NodeInfo>();
for (Object o : (List<?>) value) {
if (o instanceof NodeInfo) {
lion.add((NodeInfo) o);
}
}
if (lion.size() > 0) {
NodeList nl = toDOMNodeList(lion);
return tc.convertTo(type, exchange, nl);
}
}
} else if (NodeOverNodeInfo.class.isAssignableFrom(value.getClass())) {
// NodeOverNode info is a read-only Node implementation from Saxon. In contrast to the JDK
// com.sun.org.apache.xerces.internal.dom.NodeImpl class it does not implement NodeList, but
// many Camel type converters are based on that interface. Therefore we convert to NodeList and
// try type conversion in the fallback type converter.
TypeConverter tc = registry.lookup(type, NodeList.class);
if (tc != null) {
List<Node> domNodeList = new LinkedList<Node>();
domNodeList.add((NodeOverNodeInfo) value);
return tc.convertTo(type, exchange, new DOMNodeList(domNodeList));
}
}
return null;
}
use of org.apache.camel.TypeConverter in project camel by apache.
the class GenericFileConverter method convertTo.
@FallbackConverter
public static Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) throws IOException, NoTypeConversionAvailableException {
// use a fallback type converter so we can convert the embedded body if the value is GenericFile
if (GenericFile.class.isAssignableFrom(value.getClass())) {
GenericFile<?> file = (GenericFile<?>) value;
Class<?> from = file.getBody().getClass();
// maybe from is already the type we want
if (from.isAssignableFrom(type)) {
return file.getBody();
}
// no then try to lookup a type converter
TypeConverter tc = registry.lookup(type, from);
if (tc != null) {
Object body = file.getBody();
// if the desired type is InputStream or Reader we can use the optimized methods
if (Reader.class.isAssignableFrom(type)) {
Reader reader = genericFileToReader(file, exchange);
if (reader != null) {
return reader;
}
}
if (InputStream.class.isAssignableFrom(type)) {
InputStream is = genericFileToInputStream(file, exchange);
if (is != null) {
return is;
}
}
// which mean we have to use the Reader first, and then convert from there
if (body instanceof File && file.getCharset() != null) {
Reader reader = genericFileToReader(file, exchange);
// we dont want a reader back, so use the type converter registry to find a suitable converter
TypeConverter readerTc = registry.lookup(type, Reader.class);
if (readerTc != null) {
// use the reader based type converter
return readerTc.convertTo(type, exchange, reader);
}
}
// fallback and use the type suitable type converter
return tc.convertTo(type, exchange, body);
}
}
return null;
}
Aggregations