Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.appslandia.plum.base.ModelBinder Maven / Gradle / Ivy
// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package com.appslandia.plum.base;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import javax.validation.ConstraintViolation;
import javax.validation.Path;
import javax.validation.Validator;
import javax.validation.groups.Default;
import javax.validation.metadata.ConstraintDescriptor;
import com.appslandia.common.base.FormatProvider;
import com.appslandia.common.base.Out;
import com.appslandia.common.base.Params;
import com.appslandia.common.formatters.Fmt;
import com.appslandia.common.formatters.Formatter;
import com.appslandia.common.formatters.FormatterException;
import com.appslandia.common.formatters.FormatterProvider;
import com.appslandia.common.json.JsonProcessor;
import com.appslandia.common.utils.AssertUtils;
import com.appslandia.common.utils.CharsetUtils;
import com.appslandia.common.utils.MathUtils;
import com.appslandia.common.utils.MimeTypes;
import com.appslandia.common.utils.ObjectUtils;
import com.appslandia.common.utils.ReflectionUtils;
import com.appslandia.common.utils.StringUtils;
import com.appslandia.common.utils.TypeDefaults;
import com.appslandia.common.validators.BitMask;
import com.appslandia.common.validators.G1;
import com.appslandia.plum.utils.ServletUtils;
/**
*
* @author Loc Ha
*
*/
@ApplicationScoped
public class ModelBinder {
@Inject
protected FormatterProvider formatterProvider;
@Inject
protected Validator validator;
@Inject
protected JsonProcessor jsonProcessor;
public void bindModel(HttpServletRequest request, Object model) throws Exception {
bindModel(request, model, null);
}
public void bindModel(HttpServletRequest request, Object model, Function excludePaths) throws Exception {
Queue queue = new LinkedList<>();
queue.add(new BindingNode(model, null));
while (!queue.isEmpty()) {
BindingNode bindNode = queue.poll();
for (PropertyDescriptor property : Introspector.getBeanInfo(bindNode.model.getClass()).getPropertyDescriptors()) {
if (property.getWriteMethod() == null) {
continue;
}
// Field
Field field = ReflectionUtils.findField(bindNode.model.getClass(), property.getName());
if ((field == null) || (field.getDeclaredAnnotation(NotBind.class) != null)) {
continue;
}
String propertyPath = StringUtils.isNullOrEmpty(bindNode.path) ? field.getName() : (bindNode.path + "." + field.getName());
if ((excludePaths != null) && (excludePaths.apply(propertyPath))) {
continue;
}
// Parameter?
if (request.getParameterMap().keySet().stream().anyMatch(p -> p.equalsIgnoreCase(propertyPath))) {
// Array || @BitMask
if (field.getType().isArray() || field.getDeclaredAnnotation(BitMask.class) != null) {
boolean bitMaskParam = field.getDeclaredAnnotation(BitMask.class) != null;
Class> elementType = null;
if (!bitMaskParam) {
elementType = field.getType().getComponentType();
} else {
elementType = field.getType();
AssertUtils.assertTrue((elementType == long.class) || (elementType == int.class));
}
// Formatter
Formatter formatter = this.formatterProvider.findFormatter(field.getDeclaredAnnotation(Fmt.class), elementType);
if (formatter == null) {
continue;
}
Out msgKey = new Out<>();
Object parsedValue = parseArray(request.getParameterValues(propertyPath), elementType, msgKey, formatter, ServletUtils.getFormatProvider(request));
Out bitMaskResult = new Out<>(Boolean.TRUE);
if (!bitMaskParam) {
property.getWriteMethod().invoke(bindNode.model, parsedValue);
} else if (elementType == long.class) {
property.getWriteMethod().invoke(bindNode.model, toBitMask(parsedValue, bitMaskResult));
} else {
property.getWriteMethod().invoke(bindNode.model, (int) toBitMask(parsedValue, bitMaskResult));
}
if (msgKey.value != null) {
ServletUtils.addError(request, propertyPath, msgKey.value, getMsgParams(field, bindNode.model.getClass(), ServletUtils.getResources(request)));
} else if (Boolean.FALSE.equals(bitMaskResult.value)) {
ServletUtils.addError(request, propertyPath, Resources.ERROR_FIELD_INVALID,
getMsgParams(field, bindNode.model.getClass(), ServletUtils.getResources(request)));
}
continue;
}
// Formatter
Class> valueType = getValueType(field);
Formatter formatter = this.formatterProvider.findFormatter(field.getDeclaredAnnotation(Fmt.class), valueType);
if (formatter != null) {
Out msgKey = new Out<>();
Object parsedValue = parseValue(request.getParameter(propertyPath), valueType, msgKey, formatter, ServletUtils.getFormatProvider(request));
if (field.getType() != Out.class) {
property.getWriteMethod().invoke(bindNode.model, parsedValue);
} else {
property.getWriteMethod().invoke(bindNode.model, new Out(parsedValue));
}
if (msgKey.value != null) {
ServletUtils.addError(request, propertyPath, msgKey.value, getMsgParams(field, bindNode.model.getClass(), ServletUtils.getResources(request)));
}
continue;
}
}
// List
if (List.class.isAssignableFrom(field.getType())) {
Class> elementType = getArgumentType(field.getGenericType());
if (elementType == null) {
continue;
}
int subIndexProps = getSubIndexProps(request, propertyPath);
if (subIndexProps == 0) {
continue;
}
// Declare List Or Concretes
List subModel = (field.getType() == List.class) ? new ArrayList<>(subIndexProps) : ObjectUtils.cast(ReflectionUtils.newInstance(field.getType()));
int idx = 0;
int count = 0;
while (true) {
String subIndexProp = propertyPath + "[" + idx + "]";
if (hasSubProperties(request, subIndexProp)) {
Object elementModel = ReflectionUtils.newInstance(elementType);
subModel.add(elementModel);
queue.add(new BindingNode(elementModel, subIndexProp));
if (++count == subIndexProps) {
break;
}
}
idx++;
}
property.getWriteMethod().invoke(bindNode.model, subModel);
continue;
}
// ITERABLE/Map
if (Iterable.class.isAssignableFrom(field.getType()) || Map.class.isAssignableFrom(field.getType())) {
continue;
}
// Sub-Model
if (hasSubProperties(request, propertyPath)) {
Object subModel = AssertUtils.assertNotNull(property.getReadMethod()).invoke(bindNode.model);
if (subModel == null) {
subModel = ReflectionUtils.newInstance(field.getType());
property.getWriteMethod().invoke(bindNode.model, subModel);
}
queue.add(new BindingNode(subModel, propertyPath));
}
} // Iteration of properties
}
// Validate Model
validateModel(model, ServletUtils.getModelState(request), ServletUtils.getResources(request));
}
public T bindModel(HttpServletRequest request, String partName, Class modelType, ModelState modelState) throws Exception {
Part part = request.getPart(partName);
AssertUtils.assertNotNull(part);
AssertUtils.assertTrue(ServletUtils.allowContentType(part.getContentType(), MimeTypes.APP_JSON));
T model = null;
try (BufferedReader br = new BufferedReader(new InputStreamReader(part.getInputStream(), CharsetUtils.parse(part.getContentType())))) {
model = this.jsonProcessor.read(br, modelType);
}
if (model != null) {
validateModel(model, modelState, ServletUtils.getResources(request));
}
return model;
}
public void validateModel(Object model, ModelState modelState, Resources resources) {
StringBuilder propertyPath = null;
for (Object error : this.validator.validate(model, Default.class, G1.class)) {
ConstraintViolation> violation = (ConstraintViolation>) error;
if (propertyPath == null) {
propertyPath = new StringBuilder();
} else {
propertyPath.setLength(0);
}
String fieldName = getLeafProp(violation.getPropertyPath(), propertyPath);
AssertUtils.assertNotNull(fieldName);
modelState.addError(fieldName, resources.get(getMsgKey(violation), getMsgParams(violation, fieldName, resources)));
}
}
// propertyPath.subProp
public static boolean hasSubProperties(HttpServletRequest request, String propertyPath) {
String subProp = propertyPath + '.';
for (String parameter : request.getParameterMap().keySet()) {
if (StringUtils.startsWithIgnoreCase(parameter, subProp) && (parameter.length() > subProp.length())) {
return true;
}
}
return false;
}
// [index].subProp
private static final Pattern SUB_INDEX_PROP_PATTERN = Pattern.compile("\\[\\d+]\\.[^\\s]+");
public static int getSubIndexProps(HttpServletRequest request, String propertyPath) {
Set indexes = null;
for (String parameter : request.getParameterMap().keySet()) {
if (StringUtils.startsWithIgnoreCase(parameter, propertyPath)) {
String subIndexProp = parameter.substring(propertyPath.length());
if (SUB_INDEX_PROP_PATTERN.matcher(subIndexProp).matches()) {
if (indexes == null) {
indexes = new HashSet<>();
}
indexes.add(subIndexProp.substring(1, subIndexProp.indexOf(']')));
}
}
}
return indexes != null ? indexes.size() : 0;
}
private static String getLeafProp(Path path, StringBuilder propertyPath) {
Iterator iter = path.iterator();
String lastProp = null;
while (iter.hasNext()) {
Path.Node node = (Path.Node) iter.next();
if (node.getIndex() != null) {
propertyPath.append('[').append(node.getIndex()).append(']');
}
if (node.getName() != null) {
lastProp = node.getName();
if (propertyPath.length() != 0) {
propertyPath.append('.');
}
propertyPath.append(lastProp);
}
}
return lastProp;
}
protected String getFieldDisplayName(Field field, Class> modelType, Resources resources) {
return resources.get(StringUtils.firstLowerCase(modelType.getSimpleName(), Locale.ENGLISH) + '.' + field.getName());
}
private Map getMsgParams(Field field, Class> modelType, Resources resources) {
return new Params(1).put(Resources.PARAM_FIELD_NAME, getFieldDisplayName(field, modelType, resources));
}
private Map getMsgParams(ConstraintViolation> violation, String fieldName, Resources resources) {
Field field = ReflectionUtils.findField(violation.getLeafBean().getClass(), fieldName);
Map map = buildMsgParams(violation.getConstraintDescriptor());
map.put(Resources.PARAM_FIELD_NAME, getFieldDisplayName(field, violation.getLeafBean().getClass(), resources));
return map;
}
public static String getMsgKey(ConstraintViolation> violation) {
String msgKey = violation.getMessageTemplate();
AssertUtils.assertTrue(msgKey.startsWith("{") && msgKey.endsWith("}"), "messageTemplate is invalid.");
return msgKey.substring(1, msgKey.length() - 1);
}
public static Map buildMsgParams(ConstraintDescriptor> desc) {
Map map = new HashMap<>(8);
for (Entry attribute : desc.getAttributes().entrySet()) {
if (attribute.getKey().equals("message") || attribute.getKey().equals("groups") || attribute.getKey().equals("payload")) {
continue;
}
map.put(attribute.getKey(), attribute.getValue());
}
return map;
}
public static Object parseArray(String[] paramValues, Class> elementType, Out msgKey, Formatter formatter, FormatProvider formatProvider) {
if (paramValues == null) {
return null;
}
Object parsedArray = Array.newInstance(elementType, paramValues.length);
for (int idx = 0; idx < paramValues.length; idx++) {
Object element = null;
try {
element = formatter.parse(paramValues[idx], formatProvider);
if ((element == null) && (elementType.isPrimitive())) {
element = TypeDefaults.defaultValue(elementType);
msgKey.value = Resources.ERROR_FIELD_INVALID;
}
} catch (FormatterException ex) {
element = (elementType != String.class) ? TypeDefaults.defaultValue(elementType) : paramValues[idx];
msgKey.value = Resources.ERROR_FIELD_INVALID;
}
Array.set(parsedArray, idx, element);
}
return parsedArray;
}
public static Object parseValue(String paramValue, Class> targetType, Out msgKey, Formatter formatter, FormatProvider formatProvider) {
Object result = null;
try {
result = formatter.parse(paramValue, formatProvider);
if ((result == null) && (targetType.isPrimitive())) {
result = TypeDefaults.defaultValue(targetType);
msgKey.value = Resources.ERROR_FIELD_REQUIRED;
}
} catch (FormatterException ex) {
result = (targetType != String.class) ? TypeDefaults.defaultValue(targetType) : paramValue;
msgKey.value = ex.getMsgKey();
}
return result;
}
public static long toBitMask(Object numberArray, Out result) {
if (numberArray == null) {
return 0l;
}
int len = Array.getLength(numberArray);
if (len == 0) {
return 0l;
}
Set numbers = new HashSet<>();
for (int i = 0; i < len; i++) {
Number value = (Number) Array.get(numberArray, i);
if (value == null) {
continue;
}
if (!MathUtils.isPow2(value.longValue())) {
result.value = false;
} else {
numbers.add(value.longValue());
}
}
return numbers.stream().mapToLong(e -> e.longValue()).sum();
}
public static Class> getArgumentType(Type genericType) {
if (!(genericType instanceof ParameterizedType)) {
return null;
}
Type[] types = ((ParameterizedType) genericType).getActualTypeArguments();
if (types.length != 1) {
return null;
}
Type type = types[0];
if (!(type instanceof Class)) {
return null;
}
return (Class>) type;
}
public static Class> getValueType(Field field) {
if (field.getType() == Out.class) {
Class> type = getArgumentType(field.getGenericType());
if (type != null) {
return type;
}
}
return field.getType();
}
public static Class> getValueType(Parameter parameter) {
if (parameter.getType() == Out.class) {
Class> type = getArgumentType(parameter.getParameterizedType());
if (type != null) {
return type;
}
}
return parameter.getType();
}
private static class BindingNode {
final Object model;
final String path;
public BindingNode(Object model, String path) {
this.model = model;
this.path = path;
}
}
}