001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2016 ForgeRock AS.
015 */
016package org.opends.server.protocols.http.authz;
017
018import org.forgerock.http.Filter;
019import org.forgerock.opendj.ldap.DN;
020import org.forgerock.opendj.rest2ldap.authz.ConditionalFilters.Condition;
021import org.forgerock.opendj.rest2ldap.authz.ConditionalFilters.ConditionalFilter;
022import org.forgerock.opendj.server.config.server.HTTPAuthorizationMechanismCfg;
023
024/**
025 * Provides foundation for http authorization mechanisms.
026 *
027 * @param <T>
028 *          Type of the configuration specific to the {@link HttpAuthorizationMechanism}.
029 */
030public abstract class HttpAuthorizationMechanism<T extends HTTPAuthorizationMechanismCfg>
031  implements ConditionalFilter, Comparable<HttpAuthorizationMechanism<?>>
032{
033  private final DN configDN;
034  private final int priority;
035
036  /**
037   * Create a new {@link HttpAuthorizationMechanism}.
038   *
039   * @param configDN
040   *          DN where the configuration of this {@link HttpAuthorizationMechanism} resides.
041   * @param priority
042   *          Priority of evaluation when multiple {@link HttpAuthorizationMechanism} are present. Authorization
043   *          mechanism with lower value will processed before the ones with bigger values.
044   */
045  public HttpAuthorizationMechanism(DN configDN, int priority)
046  {
047    this.configDN = configDN;
048    this.priority = priority;
049  }
050
051  @Override
052  public final Filter getFilter()
053  {
054    return getDelegate().getFilter();
055  }
056
057  @Override
058  public final Condition getCondition()
059  {
060    return getDelegate().getCondition();
061  }
062
063  @Override
064  public final int compareTo(HttpAuthorizationMechanism<?> other)
065  {
066    return Integer.compare(priority, other.priority);
067  }
068
069  abstract ConditionalFilter getDelegate();
070
071  @Override
072  public String toString()
073  {
074    return configDN.rdn(0).toString();
075  }
076}