Search in sources :

Example 11 with TypeConverter

use of org.apache.camel.TypeConverter in project camel by apache.

the class MappedEndpointConfiguration method toUriString.

@Override
public String toUriString(UriFormat format) {
    Set<Map.Entry<String, Object>> entries = params.entrySet();
    List<String> queryParams = new ArrayList<String>();
    String scheme = null;
    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()));
                }
            } else {
                queryParams.add(key + "=" + UnsafeUriCharactersEncoder.encode(value.toString()));
            }
        }
    }
    queryParams.sort(null);
    String q = "";
    for (String entry : queryParams) {
        q += q.length() == 0 ? "" : "&";
        q += entry;
    }
    StringBuilder u = new StringBuilder(64);
    if (scheme != null) {
        // SHOULD NOT be null
        u.append(scheme);
        u.append(":");
    }
    if (authority != null) {
        u.append("//");
        u.append(authority);
        u.append(path);
        if (q.length() > 0) {
            u.append("?");
            u.append(q);
        }
        if (fragment != null) {
            u.append("#");
            u.append(fragment);
        }
    } else {
        // add leading // if not provided
        if (!schemeSpecificPart.startsWith("//")) {
            u.append("//");
        }
        u.append(schemeSpecificPart);
    }
    return u.toString();
}
Also used : TypeConverter(org.apache.camel.TypeConverter) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 12 with TypeConverter

use of org.apache.camel.TypeConverter in project camel by apache.

the class MessageSupport method getBody.

protected <T> T getBody(Class<T> type, Object body) {
    // if already same type
    if (type.isInstance(body)) {
        return type.cast(body);
    }
    Exchange e = getExchange();
    if (e != null) {
        TypeConverter converter = e.getContext().getTypeConverter();
        // lets first try converting the body itself first
        // as for some types like InputStream v Reader its more efficient to do the transformation
        // from the body itself as its got efficient implementations of them, before trying the message
        T answer = converter.convertTo(type, e, body);
        if (answer != null) {
            return answer;
        }
        // fallback and try the message itself (e.g. used in camel-http)
        answer = converter.tryConvertTo(type, e, this);
        if (answer != null) {
            return answer;
        }
    }
    // not possible to convert
    return null;
}
Also used : Exchange(org.apache.camel.Exchange) TypeConverter(org.apache.camel.TypeConverter)

Example 13 with TypeConverter

use of org.apache.camel.TypeConverter in project camel by apache.

the class DefaultCamelContextLazyLoadTypeConvertersTest method convert.

private void convert() throws Exception {
    TypeConverter converter = context.getTypeConverter();
    Integer value = converter.convertTo(Integer.class, "1000");
    assertNotNull(value);
    assertEquals("Converted to Integer", new Integer(1000), value);
    String text = converter.convertTo(String.class, value);
    assertEquals("Converted to String", "1000", text);
}
Also used : TypeConverter(org.apache.camel.TypeConverter)

Example 14 with TypeConverter

use of org.apache.camel.TypeConverter in project camel by apache.

the class ExchangeHelper method convertToMandatoryType.

/**
     * Converts the value to the given expected type or throws an exception
     *
     * @return the converted value
     * @throws TypeConversionException is thrown if error during type conversion
     * @throws NoTypeConversionAvailableException} if no type converters exists to convert to the given type
     */
public static <T> T convertToMandatoryType(Exchange exchange, Class<T> type, Object value) throws TypeConversionException, NoTypeConversionAvailableException {
    CamelContext camelContext = exchange.getContext();
    ObjectHelper.notNull(camelContext, "CamelContext of Exchange");
    TypeConverter converter = camelContext.getTypeConverter();
    if (converter != null) {
        return converter.mandatoryConvertTo(type, exchange, value);
    }
    throw new NoTypeConversionAvailableException(value, type);
}
Also used : CamelContext(org.apache.camel.CamelContext) TypeConverter(org.apache.camel.TypeConverter) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException)

Example 15 with TypeConverter

use of org.apache.camel.TypeConverter in project camel by apache.

the class FallbackPromoteTest method testFallbackPromote.

public void testFallbackPromote() throws Exception {
    MyCoolBean cool = new MyCoolBean();
    cool.setCool("Camel rocks");
    TypeConverter tc = context.getTypeConverterRegistry().lookup(String.class, MyCoolBean.class);
    assertNull("No regular type converters", tc);
    String s = context.getTypeConverter().convertTo(String.class, cool);
    assertEquals("This is cool: Camel rocks", s);
    cool.setCool("It works");
    s = context.getTypeConverter().convertTo(String.class, cool);
    assertEquals("This is cool: It works", s);
    tc = context.getTypeConverterRegistry().lookup(String.class, MyCoolBean.class);
    assertNotNull("Should have been promoted", tc);
}
Also used : TypeConverter(org.apache.camel.TypeConverter)

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