use of org.apache.camel.FallbackConverter in project camel by apache.
the class NettyHttpConverter method convertToHttpRequest.
/**
* A fallback converter that allows us to easily call Java beans and use the raw Netty {@link HttpRequest} as parameter types.
*/
@FallbackConverter
public static Object convertToHttpRequest(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
// if we want to covert to HttpRequest
if (value != null && HttpRequest.class.isAssignableFrom(type)) {
// okay we may need to cheat a bit when we want to grab the HttpRequest as its stored on the NettyHttpMessage
// so if the message instance is a NettyHttpMessage and its body is the value, then we can grab the
// HttpRequest from the NettyHttpMessage
NettyHttpMessage msg;
if (exchange.hasOut()) {
msg = exchange.getOut(NettyHttpMessage.class);
} else {
msg = exchange.getIn(NettyHttpMessage.class);
}
if (msg != null && msg.getBody() == value) {
// ensure the http request content is reset so we can read all the content out-of-the-box
FullHttpRequest request = msg.getHttpRequest();
request.content().resetReaderIndex();
return request;
}
}
return null;
}
use of org.apache.camel.FallbackConverter in project camel by apache.
the class JacksonTypeConverters method convertTo.
@FallbackConverter
public <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) throws Exception {
// only do this if enabled (disabled by default)
if (!init && exchange != null) {
// init to see if this is enabled
String text = exchange.getContext().getProperties().get(JacksonConstants.ENABLE_TYPE_CONVERTER);
if (text != null) {
text = exchange.getContext().resolvePropertyPlaceholders(text);
enabled = "true".equalsIgnoreCase(text);
}
// pojoOnly is enabled by default
text = exchange.getContext().getProperties().get(JacksonConstants.TYPE_CONVERTER_TO_POJO);
if (text != null) {
text = exchange.getContext().resolvePropertyPlaceholders(text);
toPojo = "true".equalsIgnoreCase(text);
}
init = true;
}
if (!enabled) {
return null;
}
if (!toPojo && isNotPojoType(type)) {
return null;
}
if (exchange != null) {
ObjectMapper mapper = resolveObjectMapper(exchange.getContext().getRegistry());
// if we want to convert to a String or byte[] then use write operation
if (String.class.isAssignableFrom(type)) {
String out = mapper.writeValueAsString(value);
return type.cast(out);
} else if (byte[].class.isAssignableFrom(type)) {
byte[] out = mapper.writeValueAsBytes(value);
return type.cast(out);
} else if (mapper.canSerialize(type)) {
// if the source value type is readable by the mapper then use its read operation
if (String.class.isAssignableFrom(value.getClass())) {
return mapper.readValue((String) value, type);
} else if (byte[].class.isAssignableFrom(value.getClass())) {
return mapper.readValue((byte[]) value, type);
} else if (File.class.isAssignableFrom(value.getClass())) {
return mapper.readValue((File) value, type);
} else if (InputStream.class.isAssignableFrom(value.getClass())) {
return mapper.readValue((InputStream) value, type);
} else if (Reader.class.isAssignableFrom(value.getClass())) {
return mapper.readValue((Reader) value, type);
} else {
// fallback to generic convert value
return mapper.convertValue(value, type);
}
}
}
// Just return null to let other fallback converter to do the job
return null;
}
use of org.apache.camel.FallbackConverter in project camel by apache.
the class JcloudsPayloadConverter method convertTo.
@FallbackConverter
@SuppressWarnings("unchecked")
public static <T extends Payload> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) throws IOException {
Class<?> sourceType = value.getClass();
if (GenericFile.class.isAssignableFrom(sourceType)) {
GenericFile<?> genericFile = (GenericFile<?>) value;
if (genericFile.getFile() != null) {
Class<?> genericFileType = genericFile.getFile().getClass();
TypeConverter converter = registry.lookup(Payload.class, genericFileType);
if (converter != null) {
return (T) converter.convertTo(Payload.class, genericFile.getFile());
}
}
}
return null;
}
use of org.apache.camel.FallbackConverter in project camel by apache.
the class JacksonXMLTypeConverters method convertTo.
@FallbackConverter
public <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
// only do this if enabled
if (!init && exchange != null) {
// init to see if this is enabled
String text = exchange.getContext().getProperties().get(JacksonXMLConstants.ENABLE_XML_TYPE_CONVERTER);
enabled = "true".equalsIgnoreCase(text);
init = true;
}
if (enabled == null || !enabled) {
return null;
}
if (isNotPojoType(type)) {
return null;
}
if (exchange != null && value instanceof Map) {
ObjectMapper mapper = resolveObjectMapper(exchange.getContext().getRegistry());
if (mapper.canSerialize(type)) {
return mapper.convertValue(value, type);
}
}
// Just return null to let other fallback converter to do the job
return null;
}
use of org.apache.camel.FallbackConverter 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;
}
Aggregations