Ejemplo n.º 1
0
  public static RESTLink assertLinkExist(
      final SingleResourceTransportDto resource,
      final String href,
      final String expectedRel,
      final String expectedType,
      final String expectedTitle) {
    Optional<RESTLink> link =
        tryFind(
            resource.searchLinksByHref(href),
            new Predicate<RESTLink>() {
              @Override
              public boolean apply(final RESTLink input) {
                return expectedRel.equals(input.getRel())
                    && expectedType.equals(input.getType())
                    && expectedTitle.equals(input.getTitle());
              }
            });

    assertTrue(
        link.isPresent(),
        String.format(
            "link with 'href' %s 'rel' %s, type '%s' and title '%s' was not found",
            href, expectedRel, expectedType, expectedTitle));

    return link.get();
  }
  @Override
  public <R extends HttpRequest> R bindToRequest(final R request, final Object input) {
    checkArgument(
        checkNotNull(input, "input") instanceof SingleResourceTransportDto[],
        "this binder is only valid for SingleResourceTransportDto arrays");

    SingleResourceTransportDto[] dtos = (SingleResourceTransportDto[]) input;
    LinksDto refs = new LinksDto();

    for (SingleResourceTransportDto dto : dtos) {
      RESTLink editLink = checkNotNull(dto.getEditLink(), "entity must have an edit link");

      // Do not add repeated references
      if (refs.searchLinkByHref(editLink.getHref()) == null) {
        refs.addLink(new RESTLink(getRelToUse(input), editLink.getHref()));
      }
    }

    return super.bindToRequest(request, refs);
  }
 @Override
 public void handleResponse(final MessageContext msgContext) throws Throwable {
   if (msgContext.getResponseEntity() != null
       && msgContext.getResponseEntity() instanceof SingleResourceTransportDto) {
     SingleResourceTransportDto srtDto =
         (SingleResourceTransportDto) msgContext.getResponseEntity();
     if (srtDto != null) {
       String baseUri =
           StringUtils.removeEnd(msgContext.getUriInfo().getBaseUri().toString(), SLASH);
       if (srtDto instanceof WrapperDto<?>) {
         for (Object obj : ((WrapperDto<?>) srtDto).getCollection()) {
           if (obj instanceof SingleResourceTransportDto) {
             SingleResourceTransportDto srt = (SingleResourceTransportDto) obj;
             srt.setLinks(checkLinks(srt.getLinks(), baseUri));
           }
         }
       } else if (srtDto.getLinks() != null) {
         srtDto.setLinks(checkLinks(srtDto.getLinks(), baseUri));
       }
     }
   }
   super.handleResponse(msgContext);
 }