/**
   * Adds the specified resource to the protection space specified by the given realm. All resources
   * at or deeper than the depth of the last symbolic element in the path of the given resource URL
   * are assumed to be in the same protection space.
   *
   * @param resourceUrl the URL identifying the resources to be added to the specified protection
   *     space. For example, "http://www.hostname.com/folder/".
   * @param realm the name of the protection space. For example, "*****@*****.**"
   */
  public void addProtectionSpace(URL resourceUrl, String realm) {
    Assert.isNotNull(resourceUrl);
    Assert.isNotNull(realm);

    if (!resourceUrl.getFile().endsWith("/")) { // $NON-NLS-1$
      resourceUrl = URLTool.getParent(resourceUrl);
    }

    String oldRealm = getProtectionSpace(resourceUrl);
    if (oldRealm != null && oldRealm.equals(realm)) {
      return;
    }

    String url1 = resourceUrl.toString();
    Enumeration urls = protectionSpace.keys();
    while (urls.hasMoreElements()) {
      String url2 = (String) urls.nextElement();
      if (url1.startsWith(url2) || url2.startsWith(url1)) {
        protectionSpace.remove(url2);
        break;
      }
    }

    protectionSpace.put(url1, realm);
    needsSaving = true;
  }
  /**
   * Returns the protection space (realm) for the specified resource, or <code>null</code> if the
   * realm is unknown.
   *
   * @param resourceUrl the URL of the resource whose protection space is returned. For example,
   *     "http://www.hostname.com/folder/".
   * @return the protection space (realm) for the specified resource, or <code>null</code> if the
   *     realm is unknown
   */
  public String getProtectionSpace(URL resourceUrl) {
    while (resourceUrl != null) {
      String realm = (String) protectionSpace.get(resourceUrl.toString());
      if (realm != null) {
        return realm;
      }
      resourceUrl = URLTool.getParent(resourceUrl);
    }

    return null;
  }