Search in sources :

Example 36 with TypeConverter

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
    }
}
Also used : TypeConverter(org.apache.camel.TypeConverter) TypeConversionException(org.apache.camel.TypeConversionException) Test(org.junit.Test)

Example 37 with TypeConverter

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
    }
}
Also used : TypeConverter(org.apache.camel.TypeConverter) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) ByteArrayInputStream(java.io.ByteArrayInputStream) TypeConversionException(org.apache.camel.TypeConversionException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PersonType(org.apache.camel.foo.bar.PersonType) Test(org.junit.Test)

Example 38 with TypeConverter

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;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) ArrayList(java.util.ArrayList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) InvalidPayloadException(org.apache.camel.InvalidPayloadException)

Example 39 with TypeConverter

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;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) DOMNodeList(net.sf.saxon.dom.DOMNodeList) NodeOverNodeInfo(net.sf.saxon.dom.NodeOverNodeInfo) NodeInfo(net.sf.saxon.om.NodeInfo) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) DOMNodeList(net.sf.saxon.dom.DOMNodeList) NodeList(org.w3c.dom.NodeList) DOMNodeList(net.sf.saxon.dom.DOMNodeList) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) FallbackConverter(org.apache.camel.FallbackConverter)

Example 40 with TypeConverter

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;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) InputStream(java.io.InputStream) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) File(java.io.File) FallbackConverter(org.apache.camel.FallbackConverter)

Aggregations

TypeConverter (org.apache.camel.TypeConverter)48 Map (java.util.Map)13 FallbackConverter (org.apache.camel.FallbackConverter)9 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 Exchange (org.apache.camel.Exchange)6 RuntimeCamelException (org.apache.camel.RuntimeCamelException)6 ArrayList (java.util.ArrayList)5 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)5 HttpString (io.undertow.util.HttpString)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 List (java.util.List)4 HeaderMap (io.undertow.util.HeaderMap)3 IOException (java.io.IOException)3 URI (java.net.URI)3 Message (org.apache.camel.Message)3