public final T expand(ExpandContext<T> context, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler) { final T expandedEntity = expandInternal(context.getEntity()); if (!context.getEntityExpandParameter().isEmpty()) { entityCrawler.crawl(expandedEntity, context.getEntityExpandParameter().getExpandParameter(context.getExpandable()), expanderResolver); } return expandedEntity; }
protected SelfExpanding expandInternal(SelfExpanding selfExpandingObject) { selfExpandingObject.expand(); return selfExpandingObject; }
public <T> EntityExpander<T> getExpander(Class<? extends T> aClass) { return hasExpander(aClass) ? (EntityExpander<T>) expander : null; } }
public Object expand(ExpandContext<Object> context, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler) { final Object entity = context.getEntity(); try { method.invoke(entity, context.getEntityExpandParameter().getIndexes(context.getExpandable())); } catch (IllegalAccessException e) { throw new ExpandException(e); } catch (InvocationTargetException e) { throw new ExpandException(e); } entityCrawler.crawl(entity, context.getEntityExpandParameter().getExpandParameter(context.getExpandable()), expanderResolver); return entity; } }
public ListWrapper<T> expand(ExpandContext<ListWrapper<T>> context, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler) { final ListWrapper<T> entity = context.getEntity(); final Set<Field> collectionFields = Sets.newHashSet(); for (Class cls = entity.getClass(); cls != null; cls = cls.getSuperclass()) { final Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { if (Collection.class.isAssignableFrom(field.getType())) { collectionFields.add(field); } } } if (collectionFields.isEmpty()) { throw new RuntimeException("Entity " + entity.getClass() + " has no collection field, cannot expand."); } if (collectionFields.size() > 1) { throw new RuntimeException("Entity " + entity.getClass() + " has more than one collection field, cannot determine which collection to expand."); } setFieldValue(collectionFields.iterator().next(), entity, entity.getCallback().getItems(context.getEntityExpandParameter().getIndexes(context.getExpandable()))); entityCrawler.crawl(entity, context.getEntityExpandParameter().getExpandParameter(context.getExpandable()), expanderResolver); return entity; }
public void intercept(MethodInvocation invocation) throws IllegalAccessException, InvocationTargetException { invocation.invoke(); HttpRequestContext request = invocation.getHttpContext().getRequest(); HttpResponseContext response = invocation.getHttpContext().getResponse(); final ExpandParameter expandParameter = new DefaultExpandParameter(request.getQueryParameters().get(expandParameterName)); new EntityCrawler(additionalExpandsProviders).crawl(response.getEntity(), expandParameter, expanderResolver); } }
public List expand(ExpandContext<List> context, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler) { final List list = new LinkedList(); for (Object item : context.getEntity()) { final ExpandContext<Object> itemContext = new DefaultExpandContext<Object>(item, context.getExpandable(), context.getEntityExpandParameter()); final EntityExpander<Object> entityExpander = item != null ? expanderResolver.getExpander(item.getClass()) : null; list.add(entityExpander != null ? entityExpander.expand(itemContext, expanderResolver, entityCrawler) : item); } return list; } }
private void expandFields(Collection<Field> expandableFields, Object entity, ExpandParameter expandParameter, EntityExpanderResolver expanderResolver) { for (Field field : expandableFields) { final Expandable expandable = getExpandable(field); if (expandParameter.shouldExpand(expandable) && expanderResolver.hasExpander(field.getType())) { // we know the expander is not null, as per ExpanderResolver contract final EntityExpander<Object> entityExpander = expanderResolver.getExpander(field.getType()); final ExpandContext<Object> context = new DefaultExpandContext<Object>(getFieldValue(field, entity), expandable, expandParameter); setFieldValue(field, entity, entityExpander.expand(context, expanderResolver, this)); } } }
/** * Crawls an entity for fields that should be expanded and expands them. * * @param entity the object to crawl, can be {@code null}. * @param expandParameter the parameters to match for expansion * @param expanderResolver the resolver to lookup {@link EntityExpander} for fields to be expanded. */ public void crawl(Object entity, ExpandParameter expandParameter, EntityExpanderResolver expanderResolver) { if (entity == null) { return; } final Collection<Field> expandableFields = getExpandableFields(entity); setExpandParameter(expandableFields, entity); expandFields(expandableFields, entity, expandParameter, expanderResolver); }
public ExpandParameter getExpandParameter(Expandable expandable) { final ExpandInformation wildcardExpandInformation = parameters.get(WILDCARD); final ExpandInformation valueExpandInformation = parameters.get(Preconditions.checkNotNull(expandable).value()); return new ChainingExpandParameter( wildcardExpandInformation != null ? wildcardExpandInformation.getExpandParameter() : EMPTY_EXPAND_PARAMETER, valueExpandInformation != null ? valueExpandInformation.getExpandParameter() : EMPTY_EXPAND_PARAMETER); }
@SuppressWarnings("unchecked") private List<String> getAdditionalExpandFromProvider(Object entity, AdditionalExpandsProvider filteredProvider) { return filteredProvider.getAdditionalExpands(filteredProvider.getSupportedType().cast(entity)); }
public Indexes getIndexes(Expandable expandable) { final ExpandInformation expandInformation = parameters.get(Preconditions.checkNotNull(expandable).value()); return expandInformation != null ? expandInformation.getIndexes() : IndexParser.EMPTY; }
private Collection<String> getAdditionalExpands(Object entity) { return additionalExpandsProviders.stream() .filter(provider -> provider.getSupportedType().isInstance(entity)) .flatMap(filteredProvider -> getAdditionalExpandFromProvider(entity, filteredProvider).stream()) .collect(Collectors.toList()); }
/** * Returns the expandable annotation with the properly set value. The value is defined as the first valid point in the following list: * <ol> * <li>the value of the {@link Expandable} annotation if it is set</li> * <li>the name of an {@link XmlElement} if the annotation is present on the field and its name is not {@code ##default}</li> * <li>the name of the field</li> * <ol> * * @param field the field to look up the Expandable for * @return {@code null} if the field is null, {@code null} if the field doesn't have an expandable annotation, * an expandable annotation with a properly set value. */ Expandable getExpandable(final Field field) { if (field == null) { return null; } final Expandable expandable = field.getAnnotation(Expandable.class); if (expandable == null) { return null; } if (StringUtils.isNotEmpty(expandable.value())) { return expandable; } final XmlElement xmlElement = field.getAnnotation(XmlElement.class); if (xmlElement != null && StringUtils.isNotEmpty(xmlElement.name()) && !StringUtils.equals("##default", xmlElement.name())) { return new ExpandableWithValue(xmlElement.name()); } return new ExpandableWithValue(field.getName()); }
private void setExpandParameter(Collection<Field> expandableFields, Object entity) { final Optional<Field> expand = expandFields.getUnchecked(entity.getClass()); if (expand != null && expand.isPresent() && !expandableFields.isEmpty()) { String expandValue = createExpandString(expandableFields, getAdditionalExpands(entity)); setFieldValue(expand.get(), entity, expandValue); } }
@SuppressWarnings("unchecked") public <T> EntityExpander<T> getExpander(T instance) { return (EntityExpander<T>) getExpander(instance.getClass()); }
public boolean shouldExpand(Expandable expandable) { return parameters.containsKey(WILDCARD) || parameters.containsKey(Preconditions.checkNotNull(expandable).value()); }
public boolean apply(Field field) { return getExpandable(field) != null && ReflectionUtils.getFieldValue(field, entity) != null; } }));
public Collection expand(ExpandContext<Collection> context, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler) { final List list = new LinkedList(); for (Object item : context.getEntity()) { final ExpandContext<Object> itemContext = new DefaultExpandContext<Object>(item, context.getExpandable(), context.getEntityExpandParameter()); final EntityExpander<Object> entityExpander = item != null ? expanderResolver.getExpander(item.getClass()) : null; list.add(entityExpander != null ? entityExpander.expand(itemContext, expanderResolver, entityCrawler) : item); } return list; } }
public <T> boolean hasExpander(T instance) { return hasExpander(instance.getClass()); }