DefaultTypeConverter implements TypeConverter { @Override public final <T> T convertIfNecessary(Object target, Class<T> type) { if (type.isInstance(target)) { return type.cast(target); } T result = convertBefore(target, type); if (result != null) { return result; } if (Source.class.isAssignableFrom(type)) { if (target.getClass().isAssignableFrom(String.class)) { return (T) new StringSource(String.valueOf(target)); } else if (target.getClass().isAssignableFrom(Node.class)) { return (T) new DOMSource((Node) target); } else if (target.getClass().isAssignableFrom(InputStreamSource.class)) { try { return (T) new StreamSource(((InputStreamSource)target).getInputStream()); } catch (IOException e) { log.warn("Failed to create stream source from object", e); } } } if (Map.class.isAssignableFrom(type)) { String mapString = String.valueOf(target); Properties props = new Properties(); try { props.load(new StringReader(mapString.substring(1, mapString.length() - 1).replaceAll(",\\s*", "\n"))); } catch (IOException e) { throw new CitrusRuntimeException("Failed to reconstruct object of type map", e); } Map<String, Object> map = new LinkedHashMap<>(); for (Map.Entry<Object, Object> entry : props.entrySet()) { map.put(entry.getKey().toString(), entry.getValue()); } return (T) map; } if (String[].class.isAssignableFrom(type)) { String arrayString = String.valueOf(target).replaceAll("^\\[", "").replaceAll("]$", "").replaceAll(",\\s", ","); return (T) StringUtils.commaDelimitedListToStringArray(arrayString); } if (List.class.isAssignableFrom(type)) { String listString = String.valueOf(target).replaceAll("^\\[", "").replaceAll("]$", "").replaceAll(",\\s", ","); return (T) Arrays.asList(StringUtils.commaDelimitedListToStringArray(listString)); } if (byte[].class.isAssignableFrom(type)) { if (target instanceof String) { try { return (T) String.valueOf(target).getBytes(CitrusSettings.CITRUS_FILE_ENCODING); } catch (UnsupportedEncodingException e) { return (T) String.valueOf(target).getBytes(); } } else if (target instanceof ByteBuffer) { return (T) ((ByteBuffer) target).array(); } else if (target instanceof ByteArrayInputStream) { try { return (T) StreamUtils.copyToByteArray((ByteArrayInputStream) target); } catch (IOException e) { throw new CitrusRuntimeException("Failed to convert input stream to byte[]"); } } } if (InputStream.class.isAssignableFrom(type)) { if (target instanceof InputStream) { return (T) target; } else if (target instanceof byte[]) { return (T) new ByteArrayInputStream((byte[]) target); } else if (target instanceof String) { try { return (T) new ByteArrayInputStream(String.valueOf(target).getBytes(CitrusSettings.CITRUS_FILE_ENCODING)); } catch (UnsupportedEncodingException e) { return (T) new ByteArrayInputStream(String.valueOf(target).getBytes()); } } else { try { return (T) new ByteArrayInputStream(target.toString().getBytes(CitrusSettings.CITRUS_FILE_ENCODING)); } catch (UnsupportedEncodingException e) { return (T) new ByteArrayInputStream(target.toString().getBytes()); } } } if (type.equals(String.class)) { if (ByteBuffer.class.isAssignableFrom(target.getClass())) { return (T) new String(((ByteBuffer) target).array()); } else if (short[].class.isAssignableFrom(target.getClass())) { return (T) Arrays.toString((short[]) target); } else if (int[].class.isAssignableFrom(target.getClass())) { return (T) Arrays.toString((int[]) target); } else if (long[].class.isAssignableFrom(target.getClass())) { return (T) Arrays.toString((long[]) target); } else if (float[].class.isAssignableFrom(target.getClass())) { return (T) Arrays.toString((float[]) target); } else if (double[].class.isAssignableFrom(target.getClass())) { return (T) Arrays.toString((double[]) target); } else if (char[].class.isAssignableFrom(target.getClass())) { return (T) Arrays.toString((char[]) target); } else if (boolean[].class.isAssignableFrom(target.getClass())) { return (T) Arrays.toString((boolean[]) target); } else if (Object[].class.isAssignableFrom(target.getClass())) { return (T) Arrays.toString((Object[]) target); } else if (String[].class.isAssignableFrom(target.getClass())) { return (T) Arrays.toString((String[]) target); } else if (List.class.isAssignableFrom(target.getClass()) || Map.class.isAssignableFrom(target.getClass())) { return (T) target.toString(); } else if (byte[].class.isAssignableFrom(target.getClass())) { return (T) Arrays.toString((byte[]) target); } } if (target instanceof String) { try { return TypeConversionUtils.convertStringToType(String.valueOf(target), type); } catch (CitrusRuntimeException e) { log.warn(String.format("Unable to convert String object to type '%s' - try fallback strategies", type), e); } } if (target instanceof Number) { if (type.isAssignableFrom(int.class) || type.isAssignableFrom(Integer.class)) { return (T) new Integer(((Number) target).intValue()); } else if (type.isAssignableFrom(short.class) || type.isAssignableFrom(Short.class)) { return (T) new Short(((Number) target).shortValue()); } else if (type.isAssignableFrom(byte.class) || type.isAssignableFrom(Byte.class)) { return (T) new Byte(((Number) target).byteValue()); } else if (type.isAssignableFrom(long.class) || type.isAssignableFrom(Long.class)) { return (T) new Long(((Number) target).longValue()); } else if (type.isAssignableFrom(float.class) || type.isAssignableFrom(Float.class)) { return (T) new Float(((Number) target).floatValue()); } else if (type.isAssignableFrom(double.class) || type.isAssignableFrom(Double.class)) { return (T) new Double(((Number) target).doubleValue()); } } try { return convertAfter(target, type); } catch (Exception e) { if (String.class.equals(type)) { log.warn(String.format("Using default toString representation because object type conversion failed with: %s", e.getMessage())); return (T) target.toString(); } throw e; } }  @Override final T convertIfNecessary(Object target, Class<T> type);  }
@Test public void testConvertIfNecessary() { String payload = "Hello Citrus!"; Assert.assertEquals(converter.convertIfNecessary("1", Byte.class), new Byte((byte) 1)); Assert.assertEquals(converter.convertIfNecessary((byte) 1, Byte.class), new Byte((byte) 1)); Assert.assertEquals(converter.convertIfNecessary("1", Short.class), new Short((short) 1)); Assert.assertEquals(converter.convertIfNecessary((short) 1, Short.class), new Short((short) 1)); Assert.assertEquals(converter.convertIfNecessary("1", Integer.class), new Integer(1)); Assert.assertEquals(converter.convertIfNecessary(1, Integer.class), new Integer(1)); Assert.assertEquals(converter.convertIfNecessary("1", Long.class), new Long(1)); Assert.assertEquals(converter.convertIfNecessary(1L, Long.class), new Long(1)); Assert.assertEquals(converter.convertIfNecessary("1", Float.class), new Float(1.0F)); Assert.assertEquals(converter.convertIfNecessary(1.0F, Float.class), new Float(1.0F)); Assert.assertEquals(converter.convertIfNecessary("1", Double.class), new Double(1.0D)); Assert.assertEquals(converter.convertIfNecessary(1.0D, Double.class), new Double(1.0D)); Assert.assertEquals(converter.convertIfNecessary("true", Boolean.class), Boolean.TRUE); Assert.assertEquals(converter.convertIfNecessary(Boolean.FALSE, Boolean.class), Boolean.FALSE); Assert.assertEquals(converter.convertIfNecessary("no", Boolean.class), Boolean.FALSE); Assert.assertEquals(converter.convertIfNecessary("[a,b,c]", String[].class).length, 3); Assert.assertEquals(converter.convertIfNecessary("[a, b, c]", String[].class).length, 3); Assert.assertEquals(converter.convertIfNecessary("[a,b,c]", List.class).size(), 3); Assert.assertEquals(converter.convertIfNecessary("[a, b, c]", List.class).size(), 3); Assert.assertEquals(converter.convertIfNecessary("{key=value}", Map.class).get("key"), "value"); Assert.assertEquals(converter.convertIfNecessary("{key1=value1, key2=value2}", Map.class).get("key2"), "value2"); Assert.assertEquals(converter.convertIfNecessary("{key1=value1,key2=value2}", Map.class).get("key2"), "value2"); Assert.assertEquals(converter.convertIfNecessary(Arrays.asList("foo", "bar"), String.class), "[foo, bar]"); Assert.assertEquals(converter.convertIfNecessary(new String[] {"foo", "bar"}, String.class), "[foo, bar]"); Assert.assertEquals(converter.convertIfNecessary(new Object[] {"foo", "bar"}, String.class), "[foo, bar]"); Assert.assertEquals(converter.convertIfNecessary(Collections.singletonMap("foo", "bar"), String.class), "{foo=bar}"); Assert.assertEquals(converter.convertIfNecessary(Arrays.asList(1, 2), String.class), "[1, 2]"); Assert.assertEquals(converter.convertIfNecessary(new int[] {1, 2}, String.class), "[1, 2]"); Assert.assertEquals(converter.convertIfNecessary(payload, String.class), payload); Assert.assertEquals(converter.convertIfNecessary(payload, InputStream.class).getClass(), ByteArrayInputStream.class); Assert.assertEquals(converter.convertIfNecessary(payload, Source.class).getClass(), StringSource.class); Assert.assertEquals(converter.convertIfNecessary(payload, byte[].class), payload.getBytes()); Assert.assertEquals(converter.convertIfNecessary(payload.getBytes(), String.class), Arrays.toString(payload.getBytes())); Assert.assertEquals(converter.convertIfNecessary(ByteBuffer.wrap(payload.getBytes()), String.class), payload); }