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; }
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); } }
/** * 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 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 boolean apply(Field field) { return getExpandable(field) != null && ReflectionUtils.getFieldValue(field, entity) != null; } }));
private Collection<String> getAdditionalExpands(Object entity) { return additionalExpandsProviders.stream() .filter(provider -> provider.getSupportedType().isInstance(entity)) .flatMap(filteredProvider -> getAdditionalExpandFromProvider(entity, filteredProvider).stream()) .collect(Collectors.toList()); }
private static String marshallBean(Object object, Collection<String> expand, List<Module> modules) throws JAXBException, IOException { // expand stuff... if (!expand.isEmpty()) { new EntityCrawler().crawl(object, new DefaultExpandParameter(expand), getExpanders()); } JaxbJsonMarshaller m = DefaultJaxbJsonMarshaller.builder().modules(modules).build(); final String result = m.marshal(object); LOG.log(Level.FINE, "Got marshalled output:\n" + result); return result; }
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)); } } }
public Option<String> marshallBeanToJson(Object object, Collection<String> expands) { checkNotNull(object); // expand stuff... if (!expands.isEmpty()) { new EntityCrawler().crawl(object, new DefaultExpandParameter(expands), getExpanders()); } JaxbJsonMarshaller m = DefaultJaxbJsonMarshaller.builder().modules(getModules()).build(); try { return option(m.marshal(object)); } catch (JsonMarshallingException e) { log.error(String.format("Unable to Atlassian marshall the object of class %s", object.getClass()), e); return none(); } }
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; } }
private ProjectBean createAndExpandProjectBean(Project project, ProjectBeanBuilder builder, String expand) { StringList expandList = StringList.fromQueryParam(expand); List<String> expandItems = (expandList == null ? ImmutableList.<String>of() : expandList.asList()); //Could be expensive call so only do when needed (vs SelfExpandingExpander / bean doing its own expansion) if (expandItems.contains(EXPAND_PROJECT_KEYS) || expandItems.contains(EXPAND_WILDCARD)) { builder.projectKeys(projectManager.getAllProjectKeys(project.getId())); } ProjectBean projectBean = builder.build(); EntityExpanderResolver expandResolver = new SelfExpandingExpander.Resolver(); EntityCrawler entityCrawler = new EntityCrawler(); ExpandParameter expandParameter = new DefaultExpandParameter(expandItems); entityCrawler.crawl(projectBean, expandParameter, expandResolver); return projectBean; }
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; }
public IssueBean apply(@Nullable Issue issue) { IssueBean bean = issueBeanBuilder.build(issue); // explicitly crawl all over this motha, since atlassian-rest didn't take care of business. entityCrawler.crawl(bean, expand, expandResolver); return bean; } }
@Override public FilterBean apply(SearchRequest filter) { final ApplicationUser user = authenticationContext.getUser(); final String canonicalBaseUrl = velocityRequestContextFactory.getJiraVelocityRequestContext().getCanonicalBaseUrl(); boolean isFavourite = this.isFavourite == null ? user != null && favouritesService.isFavourite(user, filter) : this.isFavourite; final FilterBean bean = beanBuilderFactory.newFilterBeanBuilder() .filter(filter) .context(uriInfo, canonicalBaseUrl) .owner(filter.getOwner()) .favourite(isFavourite).build(); entityCrawler.crawl(bean, expand, expandResolver); return bean; } }