/**
   * Returns the reference of the target according to the a list of properties.
   *
   * @param resolver The resolver.
   * @return The target reference.
   * @throws ResourceException
   */
  protected Reference getTargetRef(Resolver<String> resolver) throws ResourceException {
    final Template targetTemplate = new Template(getTargetUri());
    Reference result = new Reference(targetTemplate.format(resolver));

    if (result.isRelative()) {
      result.setBaseRef(getMailboxUri());
      result = result.getTargetRef();
    }

    return result;
  }
  /**
   * Returns the reference of a mail according to its identifier.
   *
   * @param identifier The identifier of a mail.
   * @return The URI of the mail.
   * @throws ResourceException
   */
  protected Reference getMailRef(String identifier) throws ResourceException {
    final Template mailTemplate = new Template(getMailUriTemplate());
    Reference result = new Reference(mailTemplate.format(new MailResolver(identifier)));

    if (result.isRelative()) {
      result.setBaseRef(getMailboxUri());
      result = result.getTargetRef();
    }

    return result;
  }
  public void testEmptyRef() {
    Reference reference = new Reference();
    reference.setAuthority("testAuthority"); // must not produce NPE

    reference = new Reference();
    reference.setBaseRef("http://localhost"); // must not produce NPE

    reference = new Reference();
    reference.setFragment("fragment"); // must not produce NPE

    reference = new Reference();
    reference.setHostDomain("localhost"); // must not produce NPE
    assertEquals("localhost", reference.getAuthority());
    reference.setHostPort(new Integer(4711)); // must not produce NPE
    assertEquals("localhost:4711", reference.getAuthority());
    reference.setUserInfo("sdgj:skdfj"); // must not produce NPE
    assertEquals("sdgj:skdfj@localhost:4711", reference.getAuthority());

    reference = new Reference();
    reference.setIdentifier("http://host/abc/wkj"); // must not produce NPE

    reference = new Reference();
    reference.setPath("loc/alhost"); // must not produce NPE

    reference = new Reference();
    reference.setProtocol(Protocol.HTTPS); // must not produce NPE

    reference = new Reference();
    reference.setQuery("a=b&c=&g=1"); // must not produce NPE

    reference = new Reference();
    reference.setRelativePart("http://localhost"); // must not produce NPE

    reference = new Reference();
    reference.setScheme("skjf"); // must not produce NPE

    reference = new Reference();
    reference.setSchemeSpecificPart("host/afjhsd"); // must not produce NPE

    reference = new Reference();
    final List<String> segments = new ArrayList<String>();
    segments.add("skhf");
    segments.add("sgdfg");
    segments.add("xiz");
    reference.setSegments(segments); // must not produce NPE
  }
  /**
   * Returns the feed representation.
   *
   * @return The feed representation.
   * @throws Exception
   */
  public Feed getFeed() throws Exception {
    final Reference feedRef = getHref();

    if (feedRef.isRelative()) {
      feedRef.setBaseRef(getWorkspace().getService().getReference());
    }

    final Request request = new Request(Method.GET, feedRef.getTargetRef());
    final Response response = getWorkspace().getService().getClientDispatcher().handle(request);

    if (response.getStatus().equals(Status.SUCCESS_OK)) {
      return new Feed(response.getEntity());
    }

    throw new Exception(
        "Couldn't get the feed representation. Status returned: " + response.getStatus());
  }
  /**
   * Deletes a mail after it has been processed.
   *
   * @param mailIdentifier The identifier of the mail
   * @throws ResourceException
   */
  protected void deleteMail(String mailIdentifier) throws ResourceException {
    // A - Build the mail URI
    Reference mailRef = getMailRef(mailIdentifier);

    if (mailRef.isRelative()) {
      mailRef.setBaseRef(getMailboxUri());
      mailRef = mailRef.getTargetRef();
    }

    // B - Delete the mail
    final Request request = new Request(Method.DELETE, mailRef);
    if (getMailboxChallengeScheme() != null) {
      final ChallengeResponse challengeResponse =
          new ChallengeResponse(
              getMailboxChallengeScheme(), getMailboxLogin(), getMailboxPassword());
      request.setChallengeResponse(challengeResponse);
    }
    final Response response = getContext().getClientDispatcher().handle(request);

    if (response.getStatus().isError()) {
      throw new ResourceException(
          Status.SERVER_ERROR_INTERNAL, "Unable to delete the mail from the mailbox");
    }
  }