ContentIdPredicate.java

  1. package fr.sii.ogham.testing.assertion.filter;

  2. import static java.util.Collections.list;

  3. import java.util.function.Predicate;

  4. import javax.mail.Header;
  5. import javax.mail.MessagingException;
  6. import javax.mail.Part;

  7. /**
  8.  * Predicate that matches the {@link Part} only if {@link Part#getAllHeaders()}
  9.  * contains a {@code Content-ID} header that exactly matches the provided
  10.  * Content-ID.
  11.  *
  12.  * @author AurĂ©lien Baudet
  13.  *
  14.  */
  15. public class ContentIdPredicate implements Predicate<Part> {
  16.     private final String contentId;

  17.     public ContentIdPredicate(String contentId) {
  18.         super();
  19.         this.contentId = contentId;
  20.     }

  21.     @Override
  22.     public boolean test(Part input) {
  23.         try {
  24.             // @formatter:off
  25.             return list(input.getMatchingHeaders(new String[] { "Content-ID" }))
  26.                     .stream()
  27.                     .map(Header::getValue)
  28.                     .anyMatch(contentId::equals);
  29.             // @formatter:on
  30.         } catch (MessagingException e) {
  31.             throw new AssertionError("Failed to access message", e);
  32.         }
  33.     }

  34.     @Override
  35.     public String toString() {
  36.         return "having Content-ID header '" + contentId + "'";
  37.     }

  38. }