@Override public <T> T convert(Class<T> cls, Object value) { try { if (ClassUtils.isAssignable(value.getClass(), String.class)) { return stringToDate(cls, (String) value); } if (ClassUtils.isAssignable(value.getClass(), Number.class, true)) { return numberToDate(cls, (Number) value); } throw new UnsupportedOperationException(value.getClass().getSimpleName() + " is not a valid epoch"); } catch (IndexOutOfBoundsException | ReflectiveOperationException | UnsupportedOperationException | IllegalArgumentException e) { throw new InvalidAttributeException("Unknown " + cls.getSimpleName() + " value " + value, e); } }
@Override public <T> T convert(Class<T> cls, Object value) { try { if (ClassUtils.isAssignable(value.getClass(), String.class)) { return stringToDate(cls, (String) value); } if (ClassUtils.isAssignable(value.getClass(), Number.class, true)) { return numberToDate(cls, (Number) value); } throw new UnsupportedOperationException(value.getClass().getSimpleName() + " is not a valid epoch"); } catch (IndexOutOfBoundsException | ReflectiveOperationException | UnsupportedOperationException | IllegalArgumentException e) { throw new InvalidAttributeException("Unknown " + cls.getSimpleName() + " value " + value, e); } }
/** * Convert value to enum. * * @param cls enum to convert to * @param value value to convert * @param <T> enum type * @return enum */ @Override public <T> T convert(Class<T> cls, Object value) { try { if (ClassUtils.isAssignable(value.getClass(), String.class)) { return stringToEnum(cls, (String) value); } else if (ClassUtils.isAssignable(value.getClass(), Integer.class, true)) { return intToEnum(cls, (Integer) value); } else { throw new UnsupportedOperationException(value.getClass().getSimpleName() + " to Enum no supported"); } } catch (IndexOutOfBoundsException | ReflectiveOperationException | UnsupportedOperationException | IllegalArgumentException e) { throw new InvalidAttributeException("Unknown " + cls.getSimpleName() + " value " + value, e); } }
/** * Invoke the get[fieldName] method on the target object OR get the field with the corresponding name. * @param target the object to get * @param fieldName the field name to get or invoke equivalent get method * @param requestScope the request scope * @return the value */ public static Object getValue(Object target, String fieldName, RequestScope requestScope) { EntityDictionary dictionary = requestScope.getDictionary(); AccessibleObject accessor = dictionary.getAccessibleObject(target, fieldName); try { if (accessor instanceof Method) { // Pass RequestScope into @Computed fields if requested if (dictionary.isMethodRequestScopeable(target, (Method) accessor)) { return ((Method) accessor).invoke(target, requestScope); } return ((Method) accessor).invoke(target); } if (accessor instanceof Field) { return ((Field) accessor).get(target); } } catch (IllegalAccessException e) { throw new InvalidAttributeException(fieldName, dictionary.getJsonAliasFor(target.getClass()), e); } catch (InvocationTargetException e) { throw handleInvocationTargetException(e); } throw new InvalidAttributeException(fieldName, dictionary.getJsonAliasFor(target.getClass())); }
/** * Convert value to enum. * * @param cls enum to convert to * @param value value to convert * @param <T> enum type * @return enum */ @Override public <T> T convert(Class<T> cls, Object value) { try { if (ClassUtils.isAssignable(value.getClass(), String.class)) { return stringToEnum(cls, (String) value); } else if (ClassUtils.isAssignable(value.getClass(), Integer.class, true)) { return intToEnum(cls, (Integer) value); } else { throw new UnsupportedOperationException(value.getClass().getSimpleName() + " to Enum no supported"); } } catch (IndexOutOfBoundsException | ReflectiveOperationException | UnsupportedOperationException | IllegalArgumentException e) { throw new InvalidAttributeException("Unknown " + cls.getSimpleName() + " value " + value, e); } }
/** * Invoke the get[fieldName] method on the target object OR get the field with the corresponding name. * @param target the object to get * @param fieldName the field name to get or invoke equivalent get method * @param requestScope the request scope * @return the value */ public static Object getValue(Object target, String fieldName, RequestScope requestScope) { EntityDictionary dictionary = requestScope.getDictionary(); AccessibleObject accessor = dictionary.getAccessibleObject(target, fieldName); try { if (accessor instanceof Method) { // Pass RequestScope into @Computed fields if requested if (dictionary.isMethodRequestScopeable(target, (Method) accessor)) { return ((Method) accessor).invoke(target, requestScope); } return ((Method) accessor).invoke(target); } if (accessor instanceof Field) { return ((Field) accessor).get(target); } } catch (IllegalAccessException e) { throw new InvalidAttributeException(fieldName, dictionary.getJsonAliasFor(target.getClass()), e); } catch (InvocationTargetException e) { throw handleInvocationTargetException(e); } throw new InvalidAttributeException(fieldName, dictionary.getJsonAliasFor(target.getClass())); }
method.invoke(obj, coerce(value, fieldName, fieldClass)); } catch (IllegalAccessException e) { throw new InvalidAttributeException(fieldName, type, e); } catch (InvocationTargetException e) { throw handleInvocationTargetException(e); field.set(obj, coerce(value, fieldName, field.getType())); } catch (IllegalAccessException noField) { throw new InvalidAttributeException(fieldName, type, noField); throw new InvalidAttributeException(fieldName, type);
method.invoke(obj, coerce(value, fieldName, fieldClass)); } catch (IllegalAccessException e) { throw new InvalidAttributeException(fieldName, type, e); } catch (InvocationTargetException e) { throw handleInvocationTargetException(e); field.set(obj, coerce(value, fieldName, field.getType())); } catch (IllegalAccessException noField) { throw new InvalidAttributeException(fieldName, type, noField); throw new InvalidAttributeException(fieldName, type);
throw new InvalidAttributeException(relation, type); } else { if (!ids.isEmpty()) {
throw new InvalidAttributeException(relation, type); } else { if (!ids.isEmpty()) {
/** * Check the permissions of the relationship, and return true or false. * @param relationName The relationship to the entity * @return True if the relationship to the entity has valid permissions for the user */ protected boolean checkRelation(String relationName) { List<String> relations = dictionary.getRelationships(obj); String realName = dictionary.getNameFromAlias(obj, relationName); relationName = (realName == null) ? relationName : realName; if (relationName == null || relations == null || !relations.contains(relationName)) { throw new InvalidAttributeException(relationName, type); } checkFieldAwareDeferPermissions(ReadPermission.class, relationName, null, null); return !shouldSkipCollection( dictionary.getParameterizedType(obj, relationName), ReadPermission.class, requestScope); }
/** * Check the permissions of the relationship, and return true or false. * @param relationName The relationship to the entity * @return True if the relationship to the entity has valid permissions for the user */ protected boolean checkRelation(String relationName) { List<String> relations = dictionary.getRelationships(obj); String realName = dictionary.getNameFromAlias(obj, relationName); relationName = (realName == null) ? relationName : realName; if (relationName == null || relations == null || !relations.contains(relationName)) { throw new InvalidAttributeException(relationName, type); } checkFieldAwareDeferPermissions(ReadPermission.class, relationName, null, null); return !shouldSkipCollection( dictionary.getParameterizedType(obj, relationName), ReadPermission.class, requestScope); }
final Class<?> relationClass = dictionary.getParameterizedType(obj, relationName); if (relationClass == null) { throw new InvalidAttributeException(relationName, this.getType());
final Class<?> relationClass = dictionary.getParameterizedType(obj, relationName); if (relationClass == null) { throw new InvalidAttributeException(relationName, this.getType());
/** * Get the filter expression for a particular relationship * @param parent The object which has the relationship * @param relationName The relationship name * @return A type specific filter expression for the given relationship */ public Optional<FilterExpression> getExpressionForRelation(PersistentResource parent, String relationName) { final Class<?> entityClass = dictionary.getParameterizedType(parent.getObject(), relationName); if (entityClass == null) { throw new InvalidAttributeException(relationName, parent.getType()); } if (dictionary.isMappedInterface(entityClass) && interfaceHasFilterExpression(entityClass)) { throw new InvalidOperationException( "Cannot apply filters to polymorphic relations mapped with MappedInterface"); } final String valType = dictionary.getJsonAliasFor(entityClass); return getFilterExpressionByType(valType); }
/** * Get the filter expression for a particular relationship * @param parent The object which has the relationship * @param relationName The relationship name * @return A type specific filter expression for the given relationship */ public Optional<FilterExpression> getExpressionForRelation(PersistentResource parent, String relationName) { final Class<?> entityClass = dictionary.getParameterizedType(parent.getObject(), relationName); if (entityClass == null) { throw new InvalidAttributeException(relationName, parent.getType()); } if (dictionary.isMappedInterface(entityClass) && interfaceHasFilterExpression(entityClass)) { throw new InvalidOperationException( "Cannot apply filters to polymorphic relations mapped with MappedInterface"); } final String valType = dictionary.getJsonAliasFor(entityClass); return getFilterExpressionByType(valType); }