Package org.apache.camel

Examples of org.apache.camel.TypeConverter


        Source source = null;
        if (body instanceof InputStream) {
            return new StreamSource((InputStream)body);
        }
        if (body != null) {
            TypeConverter tc = exchange.getContext().getTypeConverterRegistry().lookup(Source.class, body.getClass());
            if (tc != null) {
                source = tc.convertTo(Source.class, body);
            }
        }

        if (source == null) {
            // then try SAX
View Full Code Here


        String schemeSpecificPart = null;
        String authority = null;
        String path = null;
        String fragment = null;

        TypeConverter converter = getCamelContext().getTypeConverter();

        // Separate URI values from query parameters
        for (Map.Entry<String, Object> entry : entries) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (key.equals(EndpointConfiguration.URI_SCHEME)) {
                scheme = converter.convertTo(String.class, value);
            } else if (key.equals(EndpointConfiguration.URI_SCHEME_SPECIFIC_PART)) {
                schemeSpecificPart = converter.convertTo(String.class, value);
            } else if (key.equals(EndpointConfiguration.URI_AUTHORITY)) {
                authority = converter.convertTo(String.class, value);
            } else if (key.equals(EndpointConfiguration.URI_USER_INFO)) {
                // ignore, part of authority
            } else if (key.equals(EndpointConfiguration.URI_HOST)) {
                // ignore, part of authority
            } else if (key.equals(EndpointConfiguration.URI_PORT)) {
                // ignore, part of authority
            } else if (key.equals(EndpointConfiguration.URI_PATH)) {
                path = converter.convertTo(String.class, value);
            } else if (key.equals(EndpointConfiguration.URI_QUERY)) {
                // ignore, but this should not be the case, may be a good idea to log...
            } else if (key.equals(EndpointConfiguration.URI_FRAGMENT)) {
                fragment = converter.convertTo(String.class, value);
            } else {
                // convert to "param=value" format here, order will be preserved
                if (value instanceof List) {
                    for (Object item : (List<?>)value) {
                        queryParams.add(key + "=" + UnsafeUriCharactersEncoder.encode(item.toString()));
View Full Code Here

            // short circuit the lookup
            return (InputStream)obj;
        }
       
        TypeConverterRegistry registry = exchange.getContext().getTypeConverterRegistry();
        TypeConverter tc = registry.lookup(InputStream.class, obj.getClass());
       
        if (tc != null) {
            return tc.convertTo(InputStream.class, exchange, obj);
        }
       
        return null;
    }
View Full Code Here

            for (Object embedded : list) {
                if (embedded != null) {
                    if (type.isInstance(embedded)) {
                        return type.cast(embedded);
                    } else {
                        TypeConverter tc = registry.lookup(type, embedded.getClass());
                        if (tc != null) {
                            Object result = tc.convertTo(type, exchange, embedded);
                            if (result != null) {
                                return (T)result;
                            }
                            // there is no suitable result will be return
                            break;
                        }
                    }
                }
            }
            // return void to indicate its not possible to convert at this time
            return (T) Void.TYPE;
        }

        // CXF-RS Response class
        if (Response.class.isAssignableFrom(value.getClass())) {
            Response response = (Response) value;
            Object entity = response.getEntity();

            TypeConverter tc = registry.lookup(type, entity.getClass());
            if (tc != null) {
                return tc.convertTo(type, exchange, entity);
            }

            // return void to indicate its not possible to convert at this time
            return (T) Void.TYPE;
        }
View Full Code Here

    public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
        // use fallback type converter, so we can probably convert into
        // CxfPayloads from other types
        if (type.isAssignableFrom(CxfPayload.class)) {
            if (!value.getClass().isArray()) {
                TypeConverter tc = registry.lookup(Source.class, value.getClass());
                if (tc != null) {
                    Source src = tc.convertTo(Source.class, exchange, value);
                    return (T) sourceToCxfPayload(src, exchange);
                }               
            }
            TypeConverter tc = registry.lookup(NodeList.class, value.getClass());
            if (tc != null) {
                NodeList nodeList = tc.convertTo(NodeList.class, exchange, value);
                return (T) nodeListToCxfPayload(nodeList, exchange);
            }
            tc = registry.lookup(Document.class, value.getClass());
            if (tc != null) {
                Document document = tc.convertTo(Document.class, exchange, value);
                return (T) documentToCxfPayload(document, exchange);
            }
            // maybe we can convert via an InputStream
            CxfPayload<?> p;
            p = convertVia(InputStream.class, exchange, value, registry);
            if (p != null) {
                return (T) p;
            }
            // String is the converter of last resort
            p = convertVia(String.class, exchange, value, registry);
            if (p != null) {
                return (T) p;
            }
            // no we could not do it currently
            return (T) Void.TYPE;
        }
        // Convert a CxfPayload into something else
        if (CxfPayload.class.isAssignableFrom(value.getClass())) {
            CxfPayload<?> payload = (CxfPayload<?>) value;
           
            if (payload.getBodySources().size() == 1) {
                if (type.isAssignableFrom(Document.class)) {
                    Source s = payload.getBodySources().get(0);
                    Document d;
                    try {
                        d = StaxUtils.read(s);
                    } catch (XMLStreamException e) {
                        throw new RuntimeException(e);
                    }
                    payload.getBodySources().set(0, new DOMSource(d.getDocumentElement()));
                    return type.cast(d);
                }
                TypeConverter tc = registry.lookup(type, Source.class);
                if (tc != null) {
                    Source s = payload.getBodySources().get(0);
                    if (type.isInstance(s)) {
                        return type.cast(s);
                    }
                    if ((s instanceof StreamSource
                        || s instanceof SAXSource)
                        && !type.isAssignableFrom(Document.class)
                        && !type.isAssignableFrom(Source.class)) {
                        //non-reproducible sources, we need to convert to DOMSource first
                        //or the payload will get wiped out
                        Document d;
                        try {
                            d = StaxUtils.read(s);
                        } catch (XMLStreamException e) {
                            throw new RuntimeException(e);
                        }
                        s = new DOMSource(d.getDocumentElement());
                        payload.getBodySources().set(0, s);
                    }
                   
                    T t = tc.convertTo(type, s);
                    if (t instanceof Document) {
                        payload.getBodySources().set(0, new DOMSource(((Document)t).getDocumentElement()));
                    } else if (t instanceof Source) {
                        payload.getBodySources().set(0, (Source)t);
                    }
                    return t;
                }               
            }
            TypeConverter tc = registry.lookup(type, NodeList.class);
            if (tc != null) {
                Object result = tc.convertTo(type, cxfPayloadToNodeList((CxfPayload<?>) value, exchange));
                if (result == null) {
                    // no we could not do it currently, and we just abort the convert here
                    return (T) Void.TYPE;
                } else {
                    return (T) result;
                }
               
            }
            // we cannot convert a node list, so we try the first item from the
            // node list
            tc = registry.lookup(type, Node.class);
            if (tc != null) {
                NodeList nodeList = cxfPayloadToNodeList((CxfPayload<?>) value, exchange);
                if (nodeList.getLength() > 0) {
                    return tc.convertTo(type, nodeList.item(0));
                } else {
                    // no we could not do it currently
                    return (T) Void.TYPE;
                }
            }
View Full Code Here

        }
        return null;
    }

    private static <T, V> CxfPayload<T> convertVia(Class<V> via, Exchange exchange, Object value, TypeConverterRegistry registry) {
        TypeConverter tc = registry.lookup(via, value.getClass());
        if (tc != null) {
            TypeConverter tc1 = registry.lookup(Document.class, via);
            if (tc1 != null) {
                V is = tc.convertTo(via, exchange, value);
                Document document = tc1.convertTo(Document.class, exchange, is);
                return documentToCxfPayload(document, exchange);
            }
        }
        return null;
    }
View Full Code Here

        Source source = null;
        if (body instanceof InputStream) {
            return new StreamSource((InputStream)body);
        }
        if (body != null) {
            TypeConverter tc = exchange.getContext().getTypeConverterRegistry().lookup(Source.class, body.getClass());
            if (tc != null) {
                source = tc.convertTo(Source.class, body);
            }
        }

        if (source == null && isAllowStAX()) {
            source = exchange.getContext().getTypeConverter().tryConvertTo(StAXSource.class, exchange, body);
View Full Code Here

                return Void.TYPE;
            }
        }

        // try to find a suitable type converter
        TypeConverter converter = getOrFindTypeConverter(type, value);
        if (converter != null) {
            log.trace("Using converter: {} to convert {}", converter, key);
            Object rc;
            if (tryConvert) {
                rc = converter.tryConvertTo(type, exchange, value);
            } else {
                rc = converter.convertTo(type, exchange, value);
            }
            if (rc != null) {
                return rc;
            }
        }

        // not found with that type then if it was a primitive type then try again with the wrapper type
        if (type.isPrimitive()) {
            Class<?> primitiveType = ObjectHelper.convertPrimitiveTypeToWrapperType(type);
            if (primitiveType != type) {
                Class<?> fromType = value.getClass();
                TypeConverter tc = getOrFindTypeConverter(primitiveType, value);
                if (tc != null) {
                    // add the type as a known type converter as we can convert from primitive to object converter
                    addTypeConverter(type, fromType, tc);
                    Object rc;
                    if (tryConvert) {
                        rc = tc.tryConvertTo(primitiveType, exchange, value);
                    } else {
                        rc = tc.convertTo(primitiveType, exchange, value);
                    }
                    if (rc != null) {
                        return rc;
                    }
                }
            }
        }

        // fallback converters
        for (FallbackTypeConverter fallback : fallbackConverters) {
            TypeConverter tc = fallback.getFallbackTypeConverter();
            Object rc;
            if (tryConvert) {
                rc = tc.tryConvertTo(type, exchange, value);
            } else {
                rc = tc.convertTo(type, exchange, value);
            }

            if (Void.TYPE.equals(rc)) {
                // it cannot be converted so give up
                return Void.TYPE;
View Full Code Here

    @Override
    public void addTypeConverter(Class<?> toType, Class<?> fromType, TypeConverter typeConverter) {
        log.trace("Adding type converter: {}", typeConverter);
        TypeMapping key = new TypeMapping(toType, fromType);
        TypeConverter converter = typeMappings.get(key);
        // only override it if its different
        // as race conditions can lead to many threads trying to promote the same fallback converter
        if (typeConverter != converter) {
            if (converter != null) {
                log.warn("Overriding type converter from: " + converter + " to: " + typeConverter);
View Full Code Here

        Class<?> fromType = null;
        if (value != null) {
            fromType = value.getClass();
        }
        TypeMapping key = new TypeMapping(toType, fromType);
        TypeConverter converter = typeMappings.get(key);
        if (converter == null) {
            // converter not found, try to lookup then
            converter = lookup(toType, fromType);
            if (converter != null) {
                typeMappings.putIfAbsent(key, converter);
View Full Code Here

TOP

Related Classes of org.apache.camel.TypeConverter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.