@Test public void testThatInstancesCanBeCreatedFromFormParameters() { FakeContext context = new FakeContext() .setFormField("name", "wisdom") .setFormField("id", "0") .setFormField("time", "1000") .setFormField("flavors", "a", "b") .setFormField("not_bound", "not_bound"); context.route(route); context.getFakeRequest().uri("/post/2"); Body body = parser.invoke(context, Body.class); assertThat(body.name).isEqualToIgnoringCase("wisdom"); assertThat(body.id).isEqualTo(0); assertThat(body.time).isEqualTo(1000l); assertThat(body.flavors).containsExactly("a", "b"); assertThat(body.path).isEqualTo(2); }
@Override public <T> T invoke(Context context, Class<T> classOfT, Type genericType) { T t; try { t = classOfT.newInstance(); } catch (Exception e) { LOGGER.error("Failed to create a new instance of {}", classOfT, e); return null; } Map<String, ReflectionHelper.Property> properties = ReflectionHelper.getProperties(classOfT, genericType); for (Entry<String, List<String>> ent : context.parameters().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } final Map<String, String> fromPath = context.route().getPathParametersEncoded(context.request().uri()); for (Entry<String, String> ent : fromPath .entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValue(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.debug(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.form() == null || context.form().isEmpty()) { return t; } for (Entry<String, List<String>> ent : context.form().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.files() == null || context.files().isEmpty()) { return t; } for (FileItem item : context.files()) { try { ReflectionHelper.Property property = properties.get(item.field()); if (property != null) { if (InputStream.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item.stream()); } else if (FileItem.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item); } else if (property.getClassOfProperty().isArray() && property.getClassOfProperty().getComponentType().equals(Byte.TYPE)) { property.set(t, item.bytes()); } } } catch (Exception e) { LOGGER.warn(ERROR_KEY + item.field() + ERROR_AND + item, e); } } return t; }
BodyParserForm implements BodyParser { @Override public <T> T invoke(Context context, Class<T> classOfT, Type genericType) { T t; try { t = classOfT.newInstance(); } catch (Exception e) { LOGGER.error("Failed to create a new instance of {}", classOfT, e); return null; } Map<String, ReflectionHelper.Property> properties = ReflectionHelper.getProperties(classOfT, genericType); for (Entry<String, List<String>> ent : context.parameters().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } final Map<String, String> fromPath = context.route().getPathParametersEncoded(context.request().uri()); for (Entry<String, String> ent : fromPath .entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValue(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.debug(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.form() == null || context.form().isEmpty()) { return t; } for (Entry<String, List<String>> ent : context.form().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.files() == null || context.files().isEmpty()) { return t; } for (FileItem item : context.files()) { try { ReflectionHelper.Property property = properties.get(item.field()); if (property != null) { if (InputStream.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item.stream()); } else if (FileItem.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item); } else if (property.getClassOfProperty().isArray() && property.getClassOfProperty().getComponentType().equals(Byte.TYPE)) { property.set(t, item.bytes()); } } } catch (Exception e) { LOGGER.warn(ERROR_KEY + item.field() + ERROR_AND + item, e); } } return t; } }
BodyParserForm implements BodyParser { @Override public <T> T invoke(Context context, Class<T> classOfT, Type genericType) { T t; try { t = classOfT.newInstance(); } catch (Exception e) { LOGGER.error("Failed to create a new instance of {}", classOfT, e); return null; } Map<String, ReflectionHelper.Property> properties = ReflectionHelper.getProperties(classOfT, genericType); for (Entry<String, List<String>> ent : context.parameters().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } final Map<String, String> fromPath = context.route().getPathParametersEncoded(context.request().uri()); for (Entry<String, String> ent : fromPath .entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValue(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.debug(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.form() == null || context.form().isEmpty()) { return t; } for (Entry<String, List<String>> ent : context.form().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.files() == null || context.files().isEmpty()) { return t; } for (FileItem item : context.files()) { try { ReflectionHelper.Property property = properties.get(item.field()); if (property != null) { if (InputStream.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item.stream()); } else if (FileItem.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item); } else if (property.getClassOfProperty().isArray() && property.getClassOfProperty().getComponentType().equals(Byte.TYPE)) { property.set(t, item.bytes()); } } } catch (Exception e) { LOGGER.warn(ERROR_KEY + item.field() + ERROR_AND + item, e); } } return t; }  }
BodyParserForm implements BodyParser { @Override public <T> T invoke(Context context, Class<T> classOfT, Type genericType) { T t; try { t = classOfT.newInstance(); } catch (Exception e) { LOGGER.error("Failed to create a new instance of {}", classOfT, e); return null; } Map<String, ReflectionHelper.Property> properties = ReflectionHelper.getProperties(classOfT, genericType); for (Entry<String, List<String>> ent : context.parameters().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } final Map<String, String> fromPath = context.route().getPathParametersEncoded(context.request().uri()); for (Entry<String, String> ent : fromPath .entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValue(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.debug(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.form() == null || context.form().isEmpty()) { return t; } for (Entry<String, List<String>> ent : context.form().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.files() == null || context.files().isEmpty()) { return t; } for (FileItem item : context.files()) { try { ReflectionHelper.Property property = properties.get(item.field()); if (property != null) { if (InputStream.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item.stream()); } else if (FileItem.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item); } else if (property.getClassOfProperty().isArray() && property.getClassOfProperty().getComponentType().equals(Byte.TYPE)) { property.set(t, item.bytes()); } } } catch (Exception e) { LOGGER.warn(ERROR_KEY + item.field() + ERROR_AND + item, e); } } return t; }  @Override T invoke(Context context, Class<T> classOfT, Type genericType); @Override T invoke(Context context, Class<T> classOfT); @Override T invoke(byte[] bytes, Class<T> classOfT); List<String> getContentTypes(); }
BodyParserForm implements BodyParser { @Override public <T> T invoke(Context context, Class<T> classOfT, Type genericType) { T t; try { t = classOfT.newInstance(); } catch (Exception e) { LOGGER.error("Failed to create a new instance of {}", classOfT, e); return null; } Map<String, ReflectionHelper.Property> properties = ReflectionHelper.getProperties(classOfT, genericType); for (Entry<String, List<String>> ent : context.parameters().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } final Map<String, String> fromPath = context.route().getPathParametersEncoded(context.request().uri()); for (Entry<String, String> ent : fromPath .entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValue(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.debug(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.form() == null || context.form().isEmpty()) { return t; } for (Entry<String, List<String>> ent : context.form().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.files() == null || context.files().isEmpty()) { return t; } for (FileItem item : context.files()) { try { ReflectionHelper.Property property = properties.get(item.field()); if (property != null) { if (InputStream.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item.stream()); } else if (FileItem.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item); } else if (property.getClassOfProperty().isArray() && property.getClassOfProperty().getComponentType().equals(Byte.TYPE)) { property.set(t, item.bytes()); } } } catch (Exception e) { LOGGER.warn(ERROR_KEY + item.field() + ERROR_AND + item, e); } } return t; }  @Override T invoke(Context context, Class<T> classOfT, Type genericType); @Override T invoke(Context context, Class<T> classOfT); @Override T invoke(byte[] bytes, Class<T> classOfT); List<String> getContentTypes();  }