@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public PropertyBox read(final PropertyBox propertyBox, final T instance, final boolean ignoreMissing) { ObjectUtils.argumentNotNull(propertyBox, "PropertyBox must be not null"); ObjectUtils.argumentNotNull(instance, "Bean instance must be not null"); propertyBox.stream().filter(p -> !p.isReadOnly()).filter(p -> Path.class.isAssignableFrom(p.getClass())) .map(p -> (Path<?>) p).forEach(p -> { getProperty(p, ignoreMissing).ifPresent( bp -> propertyBox.setValue((Property) p, read(bp, instance, (Class<Object>) p.getType()))); }); return propertyBox; }
@Override public void writeTo(final PropertyBox t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { try (final Writer writer = new OutputStreamWriter(entityStream, CHARSET)) { try { if (t != null) { // encode String value = t.stream().filter(p -> p instanceof Path) .filter(p -> t.getValue((Property<?>) p) != null) .map(p -> encode(p, ((Path<?>) p).getName()) + "=" + encode(p, serialize(p, t.getValue((Property<?>) p)))) .collect(Collectors.joining("&")); if (value != null) { writer.write(value); } } } catch (PropertyAccessException e) { throw new WebApplicationException(e.getMessage(), e, Status.BAD_REQUEST); } catch (Exception e) { throw new IOException("Failed to encode given PropertyBox as " + MediaType.APPLICATION_FORM_URLENCODED + ": [" + ((t != null) ? t.toString() : "NULL") + "]", e); } } }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public T write(PropertyBox propertyBox, T instance, boolean ignoreMissing) { ObjectUtils.argumentNotNull(propertyBox, "PropertyBox must be not null"); ObjectUtils.argumentNotNull(instance, "Bean instance must be not null"); propertyBox.stream().filter(p -> !p.isReadOnly()).filter(p -> Path.class.isAssignableFrom(p.getClass())) .map(p -> (Path<?>) p).forEach(p -> { getProperty(p, ignoreMissing).ifPresent(bp -> { final Property<Object> property = ((Property) p); final Object boxValue = propertyBox.getValue(property); Object value = boxValue; // check conversion if (!TypeUtils.isAssignable(bp.getType(), property.getType())) { value = property.getConverter() .filter(c -> TypeUtils.isAssignable(bp.getType(), c.getModelType())) .map(c -> ((PropertyValueConverter) c).toModel(boxValue, property)) .orElse(boxValue); } write(bp, p.getType(), value, instance); }); }); return instance; }