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 static org.forgerock.util.Reject.checkNotNull;
019import static org.opends.messages.ConfigMessages.ERR_CONFIG_HTTPENDPOINT_INITIALIZATION_FAILED;
020import static org.opends.server.util.StaticUtils.stackTraceToSingleLineString;
021
022import org.forgerock.i18n.LocalizableException;
023import org.forgerock.opendj.server.config.meta.HTTPAuthorizationMechanismCfgDefn;
024import org.forgerock.opendj.server.config.server.HTTPAuthorizationMechanismCfg;
025import org.opends.server.core.ServerContext;
026import org.opends.server.types.InitializationException;
027
028/**
029 * Creates {@link HttpAuthorizationMechanism} performing the authentication/authorization of incoming {@link Request}.
030 */
031public final class HttpAuthorizationMechanismFactory
032{
033  private final ServerContext serverContext;
034
035  /**
036   * Creates a new authorization mechanism factory.
037   *
038   * @param serverContext
039   *          {@link ServerContext} of this directory server.
040   * @throws NullPointerException
041   *           if serverContext is null
042   */
043  public HttpAuthorizationMechanismFactory(ServerContext serverContext)
044  {
045    this.serverContext = checkNotNull(serverContext, "serverContext cannot be null");
046  }
047
048  /**
049   * Creates a new {@link HttpAuthorizationMechanism} based on the configuration.
050   *
051   * @param config
052   *          The configuration used to build this authorization mechanism.
053   * @return a new {@link HttpAuthorizationMechanism}
054   * @throws InitializationException
055   *           If the filter cannot be created.
056   */
057  public HttpAuthorizationMechanism<?> newInstance(HTTPAuthorizationMechanismCfg config) throws InitializationException
058  {
059    try
060    {
061      @SuppressWarnings("unchecked")
062      final Class<? extends HttpAuthorizationMechanism<?>> endpointClass =
063          (Class<? extends HttpAuthorizationMechanism<?>>)
064            HTTPAuthorizationMechanismCfgDefn.getInstance().getJavaClassPropertyDefinition().loadClass(
065                config.getJavaClass(), HttpAuthorizationMechanism.class);
066
067      return (HttpAuthorizationMechanism<?>) endpointClass
068                          .getDeclaredConstructor(config.configurationClass(), ServerContext.class)
069                          .newInstance(config, serverContext);
070    }
071    catch (Exception e)
072    {
073      if (e instanceof LocalizableException) {
074        throw new InitializationException(((LocalizableException) e).getMessageObject());
075      }
076      if (e.getCause() != null && e.getCause() instanceof LocalizableException)
077      {
078        throw new InitializationException(((LocalizableException) e.getCause()).getMessageObject());
079      }
080      throw new InitializationException(ERR_CONFIG_HTTPENDPOINT_INITIALIZATION_FAILED.get(
081          config.getJavaClass(), config.dn(), stackTraceToSingleLineString(e)), e);
082    }
083  }
084}