/**
   * Invokes all {@link ResourceProcessor} instances registered for the type of the given value and
   * reference type.
   *
   * @param value must not be {@literal null}.
   * @param referenceType must not be {@literal null}.
   * @return
   */
  @SuppressWarnings("unchecked")
  public <T extends ResourceSupport> T invokeProcessorsFor(T value, ResolvableType referenceType) {

    Assert.notNull(value, "Value must not be null!");
    Assert.notNull(referenceType, "Reference type must not be null!");

    // For Resources implementations, process elements first
    if (ResourceProcessorHandlerMethodReturnValueHandler.RESOURCES_TYPE.isAssignableFrom(
        referenceType)) {

      Resources<?> resources = (Resources<?>) value;
      ResolvableType elementTargetType =
          ResolvableType.forClass(Resources.class, referenceType.getRawClass()).getGeneric(0);
      List<Object> result = new ArrayList<Object>(resources.getContent().size());

      for (Object element : resources) {

        ResolvableType elementType = ResolvableType.forClass(element.getClass());

        if (!getRawType(elementTargetType).equals(elementType.getRawClass())) {
          elementTargetType = elementType;
        }

        result.add(invokeProcessorsFor(element, elementTargetType));
      }

      ReflectionUtils.setField(
          ResourceProcessorHandlerMethodReturnValueHandler.CONTENT_FIELD, resources, result);
    }

    return (T) invokeProcessorsFor((Object) value, referenceType);
  }
Ejemplo n.º 2
0
 protected void checkInventory(String productUri, String facilityUri, int quantity) {
   String url = facilityUri + "/inventoryItems?productId=" + getProductIdFromUri(productUri);
   ParameterizedTypeReference<Resources<InventoryItem>> responseType =
       new ParameterizedTypeReference<Resources<InventoryItem>>() {};
   Resources<InventoryItem> inventories =
       restTemplate.exchange(url, HttpMethod.GET, null, responseType).getBody();
   InventoryItem inventory = inventories.getContent().iterator().next();
   assertNotNull("environment", inventory);
   assertEquals("Inventory quantity", quantity, inventory.getQuantityOnHand().intValue());
 }
 // <3>
 @RequestMapping(method = RequestMethod.GET)
 ResponseEntity<Resources<Object>> root() {
   Resources<Object> objects = new Resources<>(Collections.emptyList());
   URI uri =
       MvcUriComponentsBuilder.fromMethodCall(
               MvcUriComponentsBuilder.on(getClass()).getCollection())
           .build()
           .toUri();
   Link link = new Link(uri.toString(), "customers");
   objects.add(link);
   return ResponseEntity.ok(objects);
 }
 private void refreshGalleryList(Resources resources) {
   if (resources != null) {
     List<GalleryResource> galleries = new ArrayList<GalleryResource>(resources.getContent());
     if (null != this.galleryListFragmentListener) {
       this.galleryListFragmentListener.onDownloadGalleriesComplete(galleries);
     }
     ListAdapter listAdapter = new GalleryListAdapter(getActivity(), galleries);
     setListAdapter(listAdapter);
   }
 }
  @Test
  public void accessServiceUsingRestTemplate() {

    // Access root resource

    URI uri = URI.create(String.format(SERVICE_URI, port));
    RequestEntity<Void> request = RequestEntity.get(uri).accept(HAL_JSON).build();
    Resource<Object> rootLinks =
        restOperations.exchange(request, new ResourceType<Object>() {}).getBody();
    Links links = new Links(rootLinks.getLinks());

    // Follow stores link

    Link storesLink = links.getLink("stores").expand();
    request = RequestEntity.get(URI.create(storesLink.getHref())).accept(HAL_JSON).build();
    Resources<Store> stores =
        restOperations.exchange(request, new ResourcesType<Store>() {}).getBody();

    stores.getContent().forEach(store -> log.info("{} - {}", store.name, store.address));
  }
    /**
     * Returns whether the given {@link Resources} instance matches the given {@link
     * ResolvableType}. We predict this by inspecting the first element of the content of the {@link
     * Resources}.
     *
     * @param resources the {@link Resources} to inspect.
     * @param target that target {@link ResolvableType}.
     * @return
     */
    static boolean isValueTypeMatch(Resources<?> resources, ResolvableType target) {

      if (resources == null) {
        return false;
      }

      Collection<?> content = resources.getContent();

      if (content.isEmpty()) {
        return false;
      }

      ResolvableType superType = null;

      for (Class<?> resourcesType :
          Arrays.<Class<?>>asList(resources.getClass(), Resources.class)) {

        superType = ResolvableType.forClass(resourcesType, getRawType(target));

        if (superType != null) {
          break;
        }
      }

      if (superType == null) {
        return false;
      }

      Object element = content.iterator().next();
      ResolvableType resourceType = superType.getGeneric(0);

      if (element instanceof Resource) {
        return ResourceProcessorWrapper.isValueTypeMatch((Resource<?>) element, resourceType);
      } else if (element instanceof EmbeddedWrapper) {
        return isRawTypeAssignable(resourceType, ((EmbeddedWrapper) element).getRelTargetType());
      }

      return false;
    }
 @RequestMapping(method = RequestMethod.GET)
 HttpEntity<Resources<Profile>> showCustomers() {
   Resources<Profile> resources = new Resources<Profile>(this.repository.findAll());
   resources.add(this.entityLinks.linkToCollectionResource(Profile.class));
   return new ResponseEntity<Resources<Profile>>(resources, HttpStatus.OK);
 }