@SuppressWarnings("unchecked") @Override public <T> Optional<T> getParameter(String name, Class<T> type) { ObjectUtils.argumentNotNull(name, "Parameter name must be not null"); ObjectUtils.argumentNotNull(type, "Parameter type must be not null"); Object value = getParameterValue(name); if (value != null && !TypeUtils.isAssignable(value.getClass(), type)) { throw new TypeMismatchException("Value type " + value.getClass().getName() + " is not compatible with required type " + type.getName()); } return Optional.ofNullable((T) value); }
/** * Check the given <code>value</code> type is consistent with Property type * @param property Property to check * @param value Value to check * @param <T> Property type * @return Property value * @throws TypeMismatchException If property type and value type are not consistent */ protected <T> T checkValueTypeConsistency(Property<T> property, T value) throws TypeMismatchException { if (value != null && !TypeUtils.isAssignable(value.getClass(), property.getType())) { throw new TypeMismatchException("Property " + property + " expected a value of type " + property.getType().getName() + ", got a value of type: " + value.getClass().getName()); } return value; }
@SuppressWarnings("unchecked") @Override public <T> T getProperty(String key, Class<T> targetType) throws IllegalArgumentException { ObjectUtils.argumentNotNull(key, "Property name must be not null"); ObjectUtils.argumentNotNull(targetType, "Property type must be not null"); Object value = properties.get(key); if (value != null) { if (TypeUtils.isAssignable(value.getClass(), targetType)) { return (T) value; } else if (TypeUtils.isString(value.getClass())) { return ConversionUtils.convertStringValue(value.toString(), targetType); } else { throw new IllegalArgumentException( "Property " + key + " type is not consistent " + "with required type: " + targetType.getName() + " (got type: " + value.getClass().getName() + ")"); } } return null; }
return false; return TypeUtils.isAssignable(element.getClass(), elementType); return TypeUtils.isAssignable(value.getClass(), converter.getModelType());
@SuppressWarnings("unchecked") @Override public <T> Optional<T> getParameterIf(String name, Class<T> type, Predicate<T> condition) { ObjectUtils.argumentNotNull(name, "Parameter name must be not null"); ObjectUtils.argumentNotNull(type, "Parameter type must be not null"); ObjectUtils.argumentNotNull(condition, "Condition must be not null"); Object value = getParameterValue(name); if (value != null && !TypeUtils.isAssignable(value.getClass(), type)) { throw new TypeMismatchException("Value type " + value.getClass().getName() + " is not compatible with required type " + type.getName()); } final T typedValue = (T) value; if (value != null && condition.test(typedValue)) { return Optional.of(typedValue); } return Optional.empty(); }
/** * Check the given property is of given value type. * @param property Property to check * @param type Required type * @return Typed property * @throws TypeMismatchException If the given type is not consistent with actual property type */ @SuppressWarnings("unchecked") private static <PT> PathProperty<PT> checkPropertyType(PathProperty<?> property, Class<PT> type) { if (!TypeUtils.isAssignable(type, property.getType())) { throw new TypeMismatchException("Requested property type " + type.getName() + " doesn't match property type " + property.getType().getName()); } return (PathProperty<PT>) property; }
@SuppressWarnings("unchecked") @Override public <T> Optional<T> get(String resourceKey, Class<T> resourceType) throws TypeMismatchException { ObjectUtils.argumentNotNull(resourceKey, "Resource key must be not null"); ObjectUtils.argumentNotNull(resourceType, "Resource type must be not null"); final VaadinSession session = VaadinSession.getCurrent(); if (session != null) { Object value = session.getAttribute(resourceKey); if (value != null) { // check type if (!TypeUtils.isAssignable(value.getClass(), resourceType)) { throw new TypeMismatchException("<" + NAME + "> Actual resource type [" + value.getClass().getName() + "] and required resource type [" + resourceType.getName() + "] mismatch"); } return Optional.of((T) value); } } return Optional.empty(); }
@SuppressWarnings("unchecked") @Override public <T> Optional<T> get(String resourceKey, Class<T> resourceType) throws TypeMismatchException { ObjectUtils.argumentNotNull(resourceKey, "Resource key must be not null"); ObjectUtils.argumentNotNull(resourceType, "Resource type must be not null"); final VaadinSession session = VaadinSession.getCurrent(); if (session != null) { Object value = session.getAttribute(resourceKey); if (value != null) { // check type if (!TypeUtils.isAssignable(value.getClass(), resourceType)) { throw new TypeMismatchException("<" + NAME + "> Actual resource type [" + value.getClass().getName() + "] and required resource type [" + resourceType.getName() + "] mismatch"); } return Optional.of((T) value); } } return Optional.empty(); }
@SuppressWarnings("unchecked") @Override public <T> Optional<T> get(String resourceKey, Class<T> resourceType) throws TypeMismatchException { ObjectUtils.argumentNotNull(resourceKey, "Resource key must be not null"); ObjectUtils.argumentNotNull(resourceType, "Resource type must be not null"); final VaadinSession session = VaadinSession.getCurrent(); if (session != null) { Object value = session.getAttribute(resourceKey); if (value != null) { // check type if (!TypeUtils.isAssignable(value.getClass(), resourceType)) { throw new TypeMismatchException("<" + NAME + "> Actual resource type [" + value.getClass().getName() + "] and required resource type [" + resourceType.getName() + "] mismatch"); } return Optional.of((T) value); } } return Optional.empty(); }
@SuppressWarnings("unchecked") @Override public <T> Optional<Property<T>> getProperty(String name, Class<T> type) { ObjectUtils.argumentNotNull(name, "Property name must be not null"); ObjectUtils.argumentNotNull(type, "Property type must be not null"); Optional<Property<?>> property = getProperty(name); property.ifPresent(p -> { if (!TypeUtils.isAssignable(p.getType(), type)) { throw new TypeMismatchException("Property type " + p.getType().getName() + " is not compatible with required type " + type.getName()); } }); return property.map(p -> (Property<T>) p); }
/** * Get a value of given <code>type</code> identified by given <code>key</code>. * @param key Resource key * @param type Resource type * @return Resource value, or <code>null</code> if not found * @param <T> Resource type * @throws TypeMismatchException Expected and actual resource type mismatch */ @SuppressWarnings("unchecked") public <T> T get(String key, Class<T> type) throws TypeMismatchException { ObjectUtils.argumentNotNull(key, "Resource key must be not null"); ObjectUtils.argumentNotNull(type, "Resource type must be not null"); LOGGER.debug(() -> "<" + scopeName + "> get resource with key [" + key + "]"); Object resource = resources.get(key); if (resource != null) { // check type if (!TypeUtils.isAssignable(resource.getClass(), type)) { throw new TypeMismatchException( "<" + scopeName + "> Actual resource type [" + resource.getClass().getName() + "] and required resource type [" + type.getName() + "] mismatch"); } LOGGER.debug(() -> "<" + scopeName + "> Retrieved resource value of type [" + resource.getClass().getName() + "] for key [" + key + "]"); return (T) resource; } return null; }
@SuppressWarnings({ "unchecked", "rawtypes" }) private <V> V read(BeanProperty<?> property, T instance, Class<V> expectedType) { ObjectUtils.argumentNotNull(property, "Property must be not null"); ObjectUtils.argumentNotNull(instance, "Bean instance must be not null"); Object value = null; Object currentInstance = instance; for (BeanProperty<?> p : getPropertyHierarchy(property)) { currentInstance = value = readValue(p, currentInstance); } final Object readValue = value; final V propertyValue = (expectedType == null) ? (V) readValue : (V) property.getConverter() .filter(c -> (readValue == null || (!TypeUtils.isAssignable(readValue.getClass(), expectedType) && TypeUtils.isAssignable(readValue.getClass(), c.getPropertyType())))) .map(cv -> ((PropertyValueConverter) cv).toModel(readValue, property)).orElse(readValue); LOGGER.debug(() -> "BeanPropertySet: read property [" + property + "] value [" + propertyValue + "] from instance [" + instance + "]"); return propertyValue; }
@SuppressWarnings("unchecked") @Override public <V> V read(String propertyName, T instance) { ObjectUtils.argumentNotNull(propertyName, "Property name must be not null"); ObjectUtils.argumentNotNull(instance, "Bean instance must be not null"); final BeanProperty<?> property = (BeanProperty<?>) property(propertyName); final Object value = read(property, instance, null); // check type if (value != null && !TypeUtils.isAssignable(value.getClass(), property.getType())) { throw new TypeMismatchException("Read value type " + value.getClass().getName() + " doesn't match property type " + property.getType().getName()); } return (V) value; }
@SuppressWarnings("unchecked") @Override public T convert(MongoResolutionContext<?> context, Document document) { if (document != null) { EnumCodecStrategy strategy = (Property.class.isAssignableFrom(expression.getClass())) ? ((Property<?>) expression).getConfiguration().getParameter(EnumCodecStrategy.CONFIG_PROPERTY) .orElse(null) : null; final Object fieldValue = document.get(selection); Object value = context.resolveOrFail(FieldValue.create(fieldValue, expression, strategy), Value.class) .getValue(); if (value != null && !TypeUtils.isAssignable(value.getClass(), getConversionType())) { throw new DataAccessException("Failed to convert document value [" + fieldValue + "] to required type [" + getConversionType() + "]: value type [" + value.getClass().getName() + "] is not compatible"); } return (T) value; } return null; }
/** * Get value for given property, checking type consistency and apply any {@link PropertyValueConverter} bound to * property. * <p> * Actual property value must be provided by subclasses through {@link #getPropertyValue(Property)} method. * </p> * @param <T> Property type * @param property Property for which obtain the value * @return Property value * @throws PropertyAccessException Error handling property value */ @SuppressWarnings("unchecked") protected <T> T getAndCheckPropertyValue(Property<T> property) throws PropertyAccessException { // check value provider if (property instanceof VirtualProperty) { return getValueProviderPropertyValue((VirtualProperty<T>) property); } // use delegate method to obtain value Object value = getPropertyValue(property); // check type if (value != null) { if (!TypeUtils.isAssignable(value.getClass(), property.getType())) { throw new TypeMismatchException("Value type " + value.getClass().getName() + " doesn't match property type " + property.getType().getName()); } } return (T) value; }
@Override public BeanBulkUpdate<B> set(String propertyName, Object value) { ObjectUtils.argumentNotNull(propertyName, "Property name must be not null"); if (value == null) { return setNull(propertyName); } final PathProperty<Object> beanProperty = getBeanPropertySet(getBeanClass()).property(propertyName); if (!TypeUtils.isAssignable(value.getClass(), beanProperty.getType())) { throw new TypeMismatchException( "Value type [" + value.getClass().getName() + "] doesn't match the bean property [" + propertyName + "] type [" + beanProperty.getType().getName() + "]"); } return set(beanProperty, value); }
@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; }
@SuppressWarnings("unchecked") @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (authentication == null) { throw new AuthenticationServiceException("Null Authentication"); } if (!TypeUtils.isAssignable(authenticationType, authentication.getClass())) { return null; } com.holonplatform.auth.Authentication authc; try { authc = authenticator.authenticate(converter.apply((A) authentication)); } catch (com.holonplatform.auth.exceptions.UnknownAccountException e) { throw new UsernameNotFoundException("Unknown account", e); } catch (com.holonplatform.auth.exceptions.InvalidCredentialsException e) { throw new BadCredentialsException("Invalid credentials", e); } catch (com.holonplatform.auth.exceptions.ExpiredCredentialsException e) { throw new CredentialsExpiredException("Expired credentials", e); } catch (com.holonplatform.auth.exceptions.DisabledAccountException e) { throw new DisabledException("Disabled account", e); } catch (com.holonplatform.auth.exceptions.LockedAccountException e) { throw new LockedException("Locked account", e); } catch (com.holonplatform.auth.exceptions.AuthenticationException e) { throw new InternalAuthenticationServiceException("Internal authentication error", e); } return new SpringSecurityAuthenticationAdapter(authc); }
/** * Get the {@link DocumentConverter} to use to process given query results, checking the conversion type and * expected query results type consistency. * @param <R> Query result type * @param query Query * @param resultType Query result type * @return DocumentConverter * @throws DataAccessException If the conversion type and expected query results type are not consistent */ public static <R> DocumentConverter<R> getAndCheckConverter(BsonQuery query, Class<? extends R> resultType) throws DataAccessException { final DocumentConverter<R> documentConverter = MongoOperations.getConverter(query); if (!TypeUtils.isAssignable(documentConverter.getConversionType(), resultType)) { throw new DataAccessException("The query results converter type [" + documentConverter.getConversionType() + "] is not compatible with the query projection type [" + resultType + "]"); } return documentConverter; }
@SuppressWarnings("unchecked") @Override public T convert(MongoResolutionContext<?> context, Document document) { if (document != null) { final Object fieldValue = document.get(selection); Object value = context .resolveOrFail(FieldValue.create(fieldValue, ConstantConverterExpression.create(fieldValue, getConversionType())), Value.class) .getValue(); if (value != null && !TypeUtils.isAssignable(value.getClass(), getConversionType())) { throw new DataAccessException("Failed to convert document value [" + value + "] to required type [" + getConversionType() + "]: value type [" + value.getClass().getName() + "] is not compatible"); } return (T) value; } return null; }