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

com.unboundid.ldap.sdk.experimental.DraftChuLDAPLogSchema00SearchEntry Maven / Gradle / Ivy

/*
 * Copyright 2016-2017 UnboundID Corp.
 * All Rights Reserved.
 */
/*
 * Copyright (C) 2016-2017 UnboundID Corp.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (GPLv2 only)
 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see .
 */
package com.unboundid.ldap.sdk.experimental;



import java.util.Collections;
import java.util.List;

import com.unboundid.ldap.sdk.DereferencePolicy;
import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.Filter;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.OperationType;
import com.unboundid.ldap.sdk.ResultCode;
import com.unboundid.ldap.sdk.SearchRequest;
import com.unboundid.ldap.sdk.SearchScope;
import com.unboundid.util.Debug;
import com.unboundid.util.NotMutable;
import com.unboundid.util.StaticUtils;
import com.unboundid.util.ThreadSafety;
import com.unboundid.util.ThreadSafetyLevel;

import static com.unboundid.ldap.sdk.experimental.ExperimentalMessages.*;



/**
 * This class represents an entry that holds information about a search
 * operation processed by an LDAP server, as per the specification described in
 * draft-chu-ldap-logschema-00.
 */
@NotMutable()
@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
public final class DraftChuLDAPLogSchema00SearchEntry
       extends DraftChuLDAPLogSchema00Entry
{
  /**
   * The name of the attribute used to hold the alias dereference policy.
   */
  public static final String ATTR_DEREFERENCE_POLICY = "reqDerefAliases";



  /**
   * The name of the attribute used to hold the number of entries returned.
   */
  public static final String ATTR_ENTRIES_RETURNED = "reqEntries";



  /**
   * The name of the attribute used to hold the search filter.
   */
  public static final String ATTR_FILTER = "reqFilter";



  /**
   * The name of the attribute used to hold a requested attribute.
   */
  public static final String ATTR_REQUESTED_ATTRIBUTE = "reqAttr";



  /**
   * The name of the attribute used to hold the search scope.
   */
  public static final String ATTR_SCOPE = "reqScope";



  /**
   * The name of the attribute used to hold the requested size limit.
   */
  public static final String ATTR_SIZE_LIMIT = "reqSizeLimit";



  /**
   * The name of the attribute used to hold the requested time limit in seconds.
   */
  public static final String ATTR_TIME_LIMIT_SECONDS = "reqTimeLimit";



  /**
   * The name of the attribute used to hold the value of the typesOnly flag.
   */
  public static final String ATTR_TYPES_ONLY = "reqAttrsOnly";



  /**
   * The serial version UID for this serializable class.
   */
  private static final long serialVersionUID = 948178493925578134L;



  // The types only flag.
  private final boolean typesOnly;

  // The alias dereference policy.
  private final DereferencePolicy dereferencePolicy;

  // The search filter.
  private final Filter filter;

  // The number of entries returned.
  private final Integer entriesReturned;

  // The requested size limit.
  private final Integer requestedSizeLimit;

  // The requested time limit in seconds.
  private final Integer requestedTimeLimitSeconds;

  // The list of requested attributes.
  private final List requestedAttributes;

  // The search scope.
  private final SearchScope scope;



  /**
   * Creates a new instance of this search access log entry from the provided
   * entry.
   *
   * @param  entry  The entry used to create this search access log entry.
   *
   * @throws  LDAPException  If the provided entry cannot be decoded as a valid
   *                         search access log entry as per the specification
   *                         contained in draft-chu-ldap-logschema-00.
   */
  public DraftChuLDAPLogSchema00SearchEntry(final Entry entry)
         throws LDAPException
  {
    super(entry, OperationType.SEARCH);


    // Get the scope.
    final String scopeStr = entry.getAttributeValue(ATTR_SCOPE);
    if (scopeStr == null)
    {
      throw new LDAPException(ResultCode.DECODING_ERROR,
           ERR_LOGSCHEMA_DECODE_MISSING_REQUIRED_ATTR.get(entry.getDN(),
                ATTR_SCOPE));
    }

    final String lowerScope = StaticUtils.toLowerCase(scopeStr);
    if (lowerScope.equals("base"))
    {
      scope = SearchScope.BASE;
    }
    else if (lowerScope.equals("one"))
    {
      scope = SearchScope.ONE;
    }
    else if (lowerScope.equals("sub"))
    {
      scope = SearchScope.SUB;
    }
    else if (lowerScope.equals("subord"))
    {
      scope = SearchScope.SUBORDINATE_SUBTREE;
    }
    else
    {
      throw new LDAPException(ResultCode.DECODING_ERROR,
           ERR_LOGSCHEMA_DECODE_SEARCH_SCOPE_ERROR.get(entry.getDN(),
                ATTR_SCOPE, scopeStr));
    }


    // Get the dereference policy.
    final String derefStr = entry.getAttributeValue(ATTR_DEREFERENCE_POLICY);
    if (derefStr == null)
    {
      throw new LDAPException(ResultCode.DECODING_ERROR,
           ERR_LOGSCHEMA_DECODE_MISSING_REQUIRED_ATTR.get(entry.getDN(),
                ATTR_DEREFERENCE_POLICY));
    }

    final String lowerDeref = StaticUtils.toLowerCase(derefStr);
    if (lowerDeref.equals("never"))
    {
      dereferencePolicy = DereferencePolicy.NEVER;
    }
    else if (lowerDeref.equals("searching"))
    {
      dereferencePolicy = DereferencePolicy.SEARCHING;
    }
    else if (lowerDeref.equals("finding"))
    {
      dereferencePolicy = DereferencePolicy.FINDING;
    }
    else if (lowerDeref.equals("always"))
    {
      dereferencePolicy = DereferencePolicy.ALWAYS;
    }
    else
    {
      throw new LDAPException(ResultCode.DECODING_ERROR,
           ERR_LOGSCHEMA_DECODE_SEARCH_DEREF_ERROR.get(entry.getDN(),
                ATTR_DEREFERENCE_POLICY, derefStr));
    }


    // Get the typesOnly flag.
    final String typesOnlyStr = entry.getAttributeValue(ATTR_TYPES_ONLY);
    if (typesOnlyStr == null)
    {
      throw new LDAPException(ResultCode.DECODING_ERROR,
           ERR_LOGSCHEMA_DECODE_MISSING_REQUIRED_ATTR.get(entry.getDN(),
                ATTR_TYPES_ONLY));
    }

    final String lowerTypesOnly = StaticUtils.toLowerCase(typesOnlyStr);
    if (lowerTypesOnly.equals("true"))
    {
      typesOnly = true;
    }
    else if (lowerTypesOnly.equals("false"))
    {
      typesOnly = false;
    }
    else
    {
      throw new LDAPException(ResultCode.DECODING_ERROR,
           ERR_LOGSCHEMA_DECODE_SEARCH_TYPES_ONLY_ERROR.get(entry.getDN(),
                ATTR_TYPES_ONLY, typesOnlyStr));
    }


    // Get the filter.  For some strange reason, this is allowed to be
    // undefined.
    final String filterStr = entry.getAttributeValue(ATTR_FILTER);
    if (filterStr == null)
    {
      filter = null;
    }
    else
    {
      try
      {
        filter = Filter.create(filterStr);
      }
      catch (final Exception e)
      {
        Debug.debugException(e);
        throw new LDAPException(ResultCode.DECODING_ERROR,
             ERR_LOGSCHEMA_DECODE_SEARCH_FILTER_ERROR.get(entry.getDN(),
                  ATTR_FILTER, filterStr),
             e);
      }
    }


    // Get the set of requested attributes.
    final String[] requestedAttrArray =
         entry.getAttributeValues(ATTR_REQUESTED_ATTRIBUTE);
    if ((requestedAttrArray == null) || (requestedAttrArray.length == 0))
    {
      requestedAttributes = Collections.emptyList();
    }
    else
    {
      requestedAttributes =
           Collections.unmodifiableList(StaticUtils.toList(requestedAttrArray));
    }


    // Get the requested size limit.
    final String sizeLimitStr = entry.getAttributeValue(ATTR_SIZE_LIMIT);
    if (sizeLimitStr == null)
    {
      requestedSizeLimit = null;
    }
    else
    {
      try
      {
        requestedSizeLimit = Integer.parseInt(sizeLimitStr);
      }
      catch (final Exception e)
      {
        Debug.debugException(e);
        throw new LDAPException(ResultCode.DECODING_ERROR,
             ERR_LOGSCHEMA_DECODE_SEARCH_INT_ERROR.get(entry.getDN(),
                  ATTR_SIZE_LIMIT, sizeLimitStr),
             e);
      }
    }


    // Get the requested time limit.
    final String timeLimitStr =
         entry.getAttributeValue(ATTR_TIME_LIMIT_SECONDS);
    if (timeLimitStr == null)
    {
      requestedTimeLimitSeconds = null;
    }
    else
    {
      try
      {
        requestedTimeLimitSeconds = Integer.parseInt(timeLimitStr);
      }
      catch (final Exception e)
      {
        Debug.debugException(e);
        throw new LDAPException(ResultCode.DECODING_ERROR,
             ERR_LOGSCHEMA_DECODE_SEARCH_INT_ERROR.get(entry.getDN(),
                  ATTR_TIME_LIMIT_SECONDS, timeLimitStr),
             e);
      }
    }


    // Get the number of entries returned.
    final String entriesReturnedStr =
         entry.getAttributeValue(ATTR_ENTRIES_RETURNED);
    if (entriesReturnedStr == null)
    {
      entriesReturned = null;
    }
    else
    {
      try
      {
        entriesReturned = Integer.parseInt(entriesReturnedStr);
      }
      catch (final Exception e)
      {
        Debug.debugException(e);
        throw new LDAPException(ResultCode.DECODING_ERROR,
             ERR_LOGSCHEMA_DECODE_SEARCH_INT_ERROR.get(entry.getDN(),
                  ATTR_ENTRIES_RETURNED, entriesReturnedStr),
             e);
      }
    }
  }



  /**
   * Retrieves the scope for the search request described by this search access
   * log entry.
   *
   * @return  The scope for the search request described by this search access
   *          log entry.
   */
  public SearchScope getScope()
  {
    return scope;
  }



  /**
   * Retrieves the alias dereference policy for the search request described by
   * this search access log entry.
   *
   * @return  The alias dereference policy for the search request described by
   *          this search access log entry.
   */
  public DereferencePolicy getDereferencePolicy()
  {
    return dereferencePolicy;
  }



  /**
   * Retrieves the value of the typesOnly flag for the search request described
   * by this search access log entry.
   *
   * @return  The value of the typesOnly flag for the search request described
   *          by this search access log entry.
   */
  public boolean typesOnly()
  {
    return typesOnly;
  }



  /**
   * Retrieves the filter for the search request described by this search access
   * log entry, if available.
   *
   * @return  The filter for the search request described by this search access
   *          log entry, or {@code null} if no filter was included in the access
   *          log entry.
   */
  public Filter getFilter()
  {
    return filter;
  }



  /**
   * Retrieves the requested size limit for the search request described by this
   * search access log entry, if available.
   *
   * @return  The requested size limit for the search request described by this
   *          search access log entry, or {@code null} if no size limit was
   *          included in the access log entry.
   */
  public Integer getRequestedSizeLimit()
  {
    return requestedSizeLimit;
  }



  /**
   * Retrieves the requested time limit (in seconds) for the search request
   * described by this search access log entry, if available.
   *
   * @return  The requested time limit (in seconds) for the search request
   *          described by this search access log entry, or {@code null} if no
   *          time limit was included in the access log entry.
   */
  public Integer getRequestedTimeLimitSeconds()
  {
    return requestedTimeLimitSeconds;
  }



  /**
   * Retrieves the requested attributes for the search request described by this
   * search access log entry, if available.
   *
   * @return  The requested attributes for the search request described by this
   *          search access log entry, or an empty list if no requested
   *          attributes were included in the access log entry.
   */
  public List getRequestedAttributes()
  {
    return requestedAttributes;
  }



  /**
   * Retrieves the number of entries returned to the client in response to the
   * search request described by this search access log entry, if available.
   *
   * @return  The number of entries returned to the client in response to the
   *          search request described by this search access log entry, or
   *          {@code null} if the number of entries returned was not included in
   *          the access log entry.
   */
  public Integer getEntriesReturned()
  {
    return entriesReturned;
  }



  /**
   * Retrieves a {@code SearchRequest} created from this search access log
   * entry.  If the size limit or time limit was not present in the entry, a
   * default of zero will be used.  If the filter was not present in the entry,
   * a default of "(objectClass=*)" will be used.
   *
   * @return  The {@code SearchRequest} created from this search access log
   *          entry.
   */
  public SearchRequest toSearchRequest()
  {
    final int sizeLimit =
         ((requestedSizeLimit == null)
              ? 0
              : requestedSizeLimit);
    final int timeLimit =
         ((requestedTimeLimitSeconds == null)
              ? 0
              : requestedTimeLimitSeconds);
    final Filter f =
         ((filter == null)
              ? Filter.createPresenceFilter("objectClass")
              : filter);

    final String[] attrArray =
         requestedAttributes.toArray(StaticUtils.NO_STRINGS);

    final SearchRequest searchRequest = new SearchRequest(getTargetEntryDN(),
         scope, dereferencePolicy, sizeLimit, timeLimit, typesOnly, f,
         attrArray);
    searchRequest.setControls(getRequestControlArray());
    return searchRequest;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy