All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.vt.middleware.ldap.handler.FqdnSearchResultHandler Maven / Gradle / Ivy

There is a newer version: 3.3.9
Show newest version
/*
  $Id: FqdnSearchResultHandler.java 1877 2011-04-05 14:42:37Z dfisher $

  Copyright (C) 2003-2010 Virginia Tech.
  All rights reserved.

  SEE LICENSE FOR MORE INFORMATION

  Author:  Middleware Services
  Email:   [email protected]
  Version: $Revision: 1877 $
  Updated: $Date: 2011-04-05 10:42:37 -0400 (Tue, 05 Apr 2011) $
*/
package edu.vt.middleware.ldap.handler;

import java.net.URI;
import javax.naming.CompositeName;
import javax.naming.InvalidNameException;
import javax.naming.directory.SearchResult;

/**
 * FqdnSearchResultHandler ensures that the DN of a search result
 * is fully qualified. Any non-relative names will have the URL removed if
 * {@link #getRemoveUrls()} is true.
 *
 * @author  Middleware Services
 * @version  $Revision: 1877 $ $Date: 2011-04-05 10:42:37 -0400 (Tue, 05 Apr 2011) $
 */
public class FqdnSearchResultHandler extends CopySearchResultHandler
{

  /** Whether to remove the URL from any DNs which are not relative. */
  private boolean removeUrls = true;


  /**
   * Returns whether the URL will be removed from any DNs which are not
   * relative. The default value is true.
   *
   * @return  boolean
   */
  public boolean getRemoveUrls()
  {
    return this.removeUrls;
  }


  /**
   * Sets whether the URL will be removed from any DNs which are not relative
   * The default value is true.
   *
   * @param  b  boolean
   */
  public void setRemoveUrls(final boolean b)
  {
    this.removeUrls = b;
  }


  /** {@inheritDoc} */
  protected String processDn(final SearchCriteria sc, final SearchResult sr)
  {
    String newDn = null;
    final String resultName = sr.getName();
    if (resultName != null) {
      StringBuffer fqName = null;
      if (sr.isRelative()) {
        if (sc.getDn() != null) {
          if (!"".equals(resultName)) {
            fqName = new StringBuffer(readCompositeName(resultName)).append(
              ",").append(sc.getDn());
          } else {
            fqName = new StringBuffer(sc.getDn());
          }
        } else {
          fqName = new StringBuffer(readCompositeName(resultName));
        }
      } else {
        if (this.removeUrls) {
          fqName = new StringBuffer(
            URI.create(readCompositeName(resultName)).getPath().substring(1));
        } else {
          fqName = new StringBuffer(readCompositeName(resultName));
        }
      }
      newDn = fqName.toString();
    }
    return newDn;
  }


  /**
   * Uses a CompositeName to parse the supplied string.
   *
   * @param  s  String composite name to read
   * @return  String ldap name
   */
  private String readCompositeName(final String s)
  {
    String name = "";
    try {
      final CompositeName cName = new CompositeName(s);
      name = cName.get(0);
    } catch (InvalidNameException e) {
      if (this.logger.isErrorEnabled()) {
        this.logger.error("Error formatting name: " + s, e);
      }
    }
    return name;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy