1
0

JSonDeserializerWithAccessFilter

This commit is contained in:
Michael Hoennig
2019-04-23 06:17:55 +02:00
parent 998a5a8aa1
commit 1dae396d99
7 changed files with 130 additions and 62 deletions

View File

@ -3,6 +3,13 @@ package org.hostsharing.hsadminng.service.accessfilter;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.LongNode;
import com.fasterxml.jackson.databind.node.TextNode;
import org.apache.commons.lang3.NotImplementedException;
import org.hostsharing.hsadminng.service.util.ReflectionUtil;
import java.lang.reflect.Field;
import static org.hostsharing.hsadminng.service.util.ReflectionUtil.unchecked;
@ -17,18 +24,40 @@ public class JSonDeserializerWithAccessFilter<T> {
}
public T deserialize() {
//
// CustomerDTO dto = new CustomerDTO();
// dto.setId(((IntNode) treeNode.get("id")).asLong());
// dto.setReference(((IntNode) treeNode.get("reference")).asInt());
// dto.setPrefix(((TextNode) treeNode.get("prefix")).asText());
// dto.setName(((TextNode) treeNode.get("name")).asText());
// dto.setContractualAddress(((TextNode) treeNode.get("contractualAddress")).asText());
// dto.setContractualSalutation(((TextNode) treeNode.get("contractualSalutation")).asText());
// dto.setBillingAddress(((TextNode) treeNode.get("billingAddress")).asText());
// dto.setBillingSalutation(((TextNode) treeNode.get("billingSalutation")).asText());
// dto.setRemark(((TextNode) treeNode.get("remark")).asText());
treeNode.fieldNames().forEachRemaining(fieldName -> {
try {
final Field field = dto.getClass().getDeclaredField(fieldName);
final Object value = readValue(treeNode, field);
writeValue(dto, field, value);
} catch (NoSuchFieldException e) {
throw new RuntimeException("setting field " + fieldName + " failed", e);
}
});
return dto;
}
private Object readValue(final TreeNode treeNode, final Field field) {
final TreeNode fieldNode = treeNode.get(field.getName());
if (fieldNode instanceof TextNode) {
return ((TextNode)fieldNode).asText();
} else if (fieldNode instanceof IntNode) {
return ((IntNode)fieldNode).asInt();
} else if (fieldNode instanceof LongNode) {
return ((LongNode)fieldNode).asLong();
} else {
throw new NotImplementedException("property type not yet implemented: " + field);
}
}
private void writeValue(final T dto, final Field field, final Object value) {
if ( field.getType().isAssignableFrom(value.getClass()) ) {
ReflectionUtil.setValue(dto, field, value);
} else if (Integer.class.isAssignableFrom(field.getType()) || int.class.isAssignableFrom(field.getType())) {
ReflectionUtil.setValue(dto, field, ((Number)value).intValue());
} else if (Long.class.isAssignableFrom(field.getType()) || long.class.isAssignableFrom(field.getType())) {
ReflectionUtil.setValue(dto, field, ((Number)value).longValue());
} else {
throw new NotImplementedException("property type not yet implemented: " + field);
}
}
}

View File

@ -40,7 +40,7 @@ public class JSonSerializerWithAccessFilter extends JsonSerializer<Object> {
} else if (String.class.isAssignableFrom(prop.getType())) {
jsonGenerator.writeStringField(fieldName, (String) get(dto, prop));
} else {
throw new NotImplementedException("property type not yet implemented" + prop);
throw new NotImplementedException("property type not yet implemented: " + prop);
}
}
}

View File

@ -175,9 +175,9 @@ public class CustomerDTO implements Serializable {
@Override
public CustomerDTO deserialize(final JsonParser jsonParser,
final DeserializationContext deserializationContext) throws IOException {
final DeserializationContext deserializationContext) {
return new JSonDeserializerWithAccessFilter<CustomerDTO>(jsonParser, deserializationContext, CustomerDTO.class).deserialize();
return new JSonDeserializerWithAccessFilter<>(jsonParser, deserializationContext, CustomerDTO.class).deserialize();
}
}
}

View File

@ -1,9 +1,31 @@
package org.hostsharing.hsadminng.service.util;
import com.fasterxml.jackson.core.TreeNode;
import java.lang.reflect.Field;
import java.util.function.Supplier;
public class ReflectionUtil {
public static <T> void setValue(final T dto, final String fieldName, final Object value) {
try {
final Field field = dto.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(dto, value);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public static <T> void setValue(final T dto, final Field field, final Object value) {
try {
field.setAccessible(true);
field.set(dto, value);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@FunctionalInterface
public interface ThrowingSupplier<T> {
T get() throws Exception;