@Override public String toString() { return String.format("Resource { content: %s, %s }", getContent(), super.toString()); }
public static Map<String, Object> findProperties(Object object) { if (object.getClass().equals(Resource.class)) { return findProperties(((Resource<?>) object).getContent()); } return getPropertyDescriptors(object.getClass()) .collect(HashMap::new, (hashMap, descriptor) -> { try { Method readMethod = descriptor.getReadMethod(); ReflectionUtils.makeAccessible(readMethod); hashMap.put(descriptor.getName(), readMethod.invoke(object)); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } }, HashMap::putAll); }
/** * Convert a {@link Resource} into a list of {@link UberData}s, containing links and content. * * @param resource * @return */ static List<UberData> extractLinksAndContent(Resource<?> resource) { List<UberData> data = extractLinks(resource); extractContent(resource.getContent()).ifPresent(data::add); return data; }
@Override @SuppressWarnings("unchecked") public Class<?> getRelTargetType() { Object peek = peek(); if (peek == null) { return null; } peek = peek instanceof Resource ? ((Resource<Object>) peek).getContent() : peek; return AopUtils.getTargetClass(peek); }
/** * Returns whether the given {@link Resource} matches the given target {@link ResolvableType}. We inspect the * {@link Resource}'s value to determine the match. * * @param resource * @param target must not be {@literal null}. * @return whether the given {@link Resource} can be assigned to the given target {@link ResolvableType} */ private static boolean isValueTypeMatch(Resource<?> resource, ResolvableType target) { if (resource == null || !isRawTypeAssignable(target, resource.getClass())) { return false; } Object content = resource.getContent(); if (content == null) { return false; } ResolvableType type = findGenericType(target, Resource.class); return type != null && type.getGeneric(0).isAssignableFrom(ResolvableType.forClass(content.getClass())); }
/** * Returns whether the given {@link Resource} matches the given target {@link ResolvableType}. We inspect the * {@link Resource}'s value to determine the match. * * @param resource * @param target must not be {@literal null}. * @return whether the given {@link Resource} can be assigned to the given target {@link ResolvableType} */ private static boolean isValueTypeMatch(Resource<?> resource, ResolvableType target) { if (resource == null || !isRawTypeAssignable(target, resource.getClass())) { return false; } Object content = resource.getContent(); if (content == null) { return false; } ResolvableType type = findGenericType(target, Resource.class); return type != null && type.getGeneric(0).isAssignableFrom(ResolvableType.forClass(content.getClass())); }
private static List<CollectionJsonItem<?>> resourcesToCollectionJsonItems(Resources<?> resources) { return resources.getContent().stream() .map(content -> { if (ClassUtils.isAssignableValue(Resource.class, content)) { Resource resource = (Resource) content; return new CollectionJsonItem<>() .withHref(resource.getRequiredLink(IanaLinkRelation.SELF.value()).getHref()) .withLinks(withoutSelfLink(resource.getLinks())) .withRawData(resource.getContent()); } else { return new CollectionJsonItem<>().withRawData(content); } }) .collect(Collectors.toList()); }
Object content = ((Resource<?>) resource).getContent(); if (content instanceof Iterable) {
@Override public void serialize(Resource<?> value, JsonGenerator gen, SerializerProvider provider) throws IOException { HalFormsDocument<?> doc = HalFormsDocument.forResource(value.getContent()) // .withLinks(value.getLinks()) // .withTemplates(findTemplates(value)); provider .findValueSerializer(HalFormsDocument.class, property) .serialize(doc, gen, provider); }
@Override public Resource<Order> process(Resource<Order> resource) { Order order = resource.getContent(); if (!order.isPaid()) { resource.add(paymentLinks.getPaymentLink(order)); } if (order.isReady()) { resource.add(paymentLinks.getReceiptLink(order)); } return resource; } }
@Override public Resource<Order> process(Resource<Order> resource) { Order order = resource.getContent(); if (!order.isPaid()) { resource.add(entityLinks.linkForSingleResource(order).withRel(CANCEL_REL)); resource.add(entityLinks.linkForSingleResource(order).withRel(UPDATE_REL)); } return resource; } }
/** * Creates a {@link ProjectionResource} for the given {@link TargetAware}. * * @param value must not be {@literal null}. * @return */ private ProjectionResource toResource(TargetAware value) { Object target = value.getTarget(); ResourceMetadata metadata = associations.getMetadataFor(value.getTargetClass()); Links links = metadata.isExported() ? collector.getLinksFor(target) : new Links(); Resource<TargetAware> resource = invoker.invokeProcessorsFor(new Resource<TargetAware>(value, links)); return new ProjectionResource(resource.getContent(), resource.getLinks()); } }
LinkedResourceMethodHandler(Resource resource, RestOperations restOperations, ClientProxyFactory proxyFactory) { super(resource.getContent().getClass()); this.restOperations = restOperations; this.proxyFactory = proxyFactory; this.resource = resource; }
@Override public void serialize(Resource<?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException { String href = value.getRequiredLink(IanaLinkRelation.SELF.value()).getHref(); CollectionJson<?> collectionJson = new CollectionJson() .withVersion("1.0") .withHref(href) .withLinks(withoutSelfLink(value.getLinks())) .withItems(Collections.singletonList(new CollectionJsonItem<>() .withHref(href) .withLinks(withoutSelfLink(value.getLinks())) .withRawData(value.getContent()))) .withQueries(findQueries(value)) .withTemplate(findTemplate(value)); CollectionJsonDocument<?> doc = new CollectionJsonDocument<>(collectionJson); provider .findValueSerializer(CollectionJsonDocument.class, property) .serialize(doc, jgen, provider); }
@PostMapping("/cart/add") public String doAddItem(@RequestParam("itemId") String itemId, @SessionAttribute(name = "cartItems") List<Item> cartItems, Model model) { cartItems.add(itemService.findById(itemId).getContent()); model.addAttribute("cartItems", cartItems); return "fragment/nav/cart"; }
@Override @SuppressWarnings("unchecked") public Class<?> getRelTargetType() { Object peek = peek(); if (peek == null) { return null; } peek = peek instanceof Resource ? ((Resource<Object>) peek).getContent() : peek; return AopUtils.getTargetClass(peek); }
@Override public Resource<InlineRubric> process(Resource<InlineRubric> rubricResource) { InlineRubric rubric = rubricResource.getContent(); if (rubric.getToolItemAssociations() != null && rubric.getToolItemAssociations().size() > 0) { rubric.getMetadata().setLocked(true); } return rubricResource; } }
@Override public Resource<Rubric> process(Resource<Rubric> rubricResource) { Rubric rubric = rubricResource.getContent(); if (rubric.getToolItemAssociations() != null && rubric.getToolItemAssociations().size() > 0) { rubric.getMetadata().setLocked(true); } return rubricResource; } }
@Override public <T> T create(Resource<T> resource, RestOperations restOperations) { @SuppressWarnings("unchecked") Class<T> entityType = (Class<T>) resource.getContent().getClass(); MethodHandlerChain handlerChain = new MethodHandlerChain(asList( new ResourceIdMethodHandler(resource), new LinkedResourceMethodHandler(resource, restOperations, this), new SimplePropertyMethodHandler<>(resource) )); return createProxyInstance(entityType, handlerChain); }
@Test public void testRecursivelyCreateObjectNestedBean() throws Exception { LinkedMultiValueMap<String, String> formValues = new LinkedMultiValueMap<String, String>(); formValues.add("workPerformed.name", "foo"); formValues.add("location", "Harmonie Heilbronn"); Event event = (Event) converter.recursivelyCreateObject(Event.class, formValues, ""); assertEquals("foo", event.getWorkPerformed() .getContent().name); assertEquals("Harmonie Heilbronn", event.location); } }