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();
}
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;
}
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);
}
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);
}
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);
}
Aggregations