@FallbackConverter public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) { try { if (value != null && value.getClass().equals(String.class)) { if (value.toString().startsWith("{") || value.toString().startsWith("[")) { T result = new UnirestJacksonObjectMapper().readValue(value.toString(), type); return result; } } } catch(Exception ex){ return null; } return null; }
/** * 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 HttpRequest request = msg.getHttpRequest(); request.getContent().resetReaderIndex(); return request; } } return null; }
/** * 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 convertToHttpResponse(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) { // if we want to covert to convertToHttpResponse if (value != null && HttpResponse.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) { return msg.getHttpResponse(); } } return null; }
/** * 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; }
@FallbackConverter public static Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) { // 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(); return tc.convertTo(type, exchange, body); } } return null; }
@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; }
/** * A fallback converter that allows us to easily call Java beans and use the raw Spark {@link Request} as parameter types. */ @FallbackConverter public static Object convertToRequest(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) { // if we want to covert to Request if (value != null && Request.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 SparkMessage msg; if (exchange.hasOut()) { msg = exchange.getOut(SparkMessage.class); } else { msg = exchange.getIn(SparkMessage.class); } if (msg != null) { return msg.getRequest(); } } return null; }
/** * A fallback converter that allows us to easily call Java beans and use the raw Spark {@link Response} as parameter types. */ @FallbackConverter public static Object convertToResponse(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) { // if we want to covert to Response if (value != null && Response.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 SparkMessage msg; if (exchange.hasOut()) { msg = exchange.getOut(SparkMessage.class); } else { msg = exchange.getIn(SparkMessage.class); } if (msg != null) { return msg.getResponse(); } } return null; }
@FallbackConverter @SuppressWarnings("unchecked") public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) { if (value != null) { // should not try to convert Request as its not possible if (Request.class.isAssignableFrom(value.getClass())) { return (T) Void.TYPE; } } return null; }
protected void registerFallbackTypeConverter(TypeConverterRegistry registry, TypeConverter typeConverter, Method method) { boolean canPromote = false; // check whether the annotation may indicate it can promote if (method.getAnnotation(FallbackConverter.class) != null) { canPromote = method.getAnnotation(FallbackConverter.class).canPromote(); } registry.addFallbackTypeConverter(typeConverter, canPromote); }
@FallbackConverter public static Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) { // use a fallback type converter so we can convert the embedded body if the value is BeanInvocation if (BeanInvocation.class.isAssignableFrom(value.getClass())) { BeanInvocation bi = (BeanInvocation) value; if (bi.getArgs() == null || bi.getArgs().length != 1) { // not possible to convert at this time as we try to convert the data passed in at first argument return Void.TYPE; } Class<?> from = bi.getArgs()[0].getClass(); Object body = bi.getArgs()[0]; // maybe from is already the type we want if (type.isAssignableFrom(from)) { return body; } // no then try to lookup a type converter TypeConverter tc = registry.lookup(type, from); if (tc != null) { return tc.convertTo(type, exchange, body); } } return null; }
@FallbackConverter public Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) { if (Currency.class.isAssignableFrom(value.getClass())) { return "Money talks says " + camelContext.getName(); } return null; }
@FallbackConverter(canPromote = true) public Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) { if (MyCoolBean.class.isAssignableFrom(value.getClass())) { return "This is cool: " + value.toString(); } return null; }
@FallbackConverter public static Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) { if (TimeZone.class.isAssignableFrom(value.getClass())) { return "Time talks"; } return null; }
@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; }