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.api;
017
018import static org.opends.messages.ConfigMessages.*;
019import static org.opends.server.util.StaticUtils.*;
020
021import java.util.List;
022
023import org.forgerock.http.HttpApplication;
024import org.forgerock.http.HttpApplicationException;
025import org.forgerock.i18n.LocalizableException;
026import org.forgerock.i18n.LocalizableMessage;
027import org.forgerock.opendj.server.config.server.HTTPEndpointCfg;
028import org.opends.server.core.ServerContext;
029import org.opends.server.types.InitializationException;
030
031/**
032 * Endpoint attach an {@link HttpApplication} to an URI.
033 *
034 * @param <C>
035 *          Type of the configuration used by this {@link HttpEndpoint}
036 */
037public abstract class HttpEndpoint<C extends HTTPEndpointCfg>
038{
039  /** Configuration of this endpoint. */
040  protected final C configuration;
041
042  /** Context of this LDAP server. */
043  protected final ServerContext serverContext;
044
045  /**
046   * Create a new {@link HttpEndpoint} with the given configuration.
047   *
048   * @param configuration
049   *          Configuration of this {@link HttpEndpoint}
050   * @param serverContext
051   *          Context of this LDAP server
052   */
053  public HttpEndpoint(C configuration, ServerContext serverContext)
054  {
055    this.configuration = configuration;
056    this.serverContext = serverContext;
057  }
058
059  /**
060   * Check that the configuration of this {@link HttpEndpoint} is valid. This
061   * default implementation try to instantiate and start the underlying
062   * {@link HttpApplication}.
063   *
064   * @param unacceptableReasons
065   *          A list that can be used to hold messages about why the
066   *          configuration is not acceptable.
067   * @return true if the configuration is valid.
068   */
069  public boolean isConfigurationValid(List<LocalizableMessage> unacceptableReasons)
070  {
071    HttpApplication dummyApplication = null;
072    try
073    {
074      dummyApplication = newHttpApplication();
075      dummyApplication.start();
076      return true;
077    }
078    catch (HttpApplicationException e)
079    {
080      if (e instanceof LocalizableException)
081      {
082        unacceptableReasons.add(((LocalizableException) e).getMessageObject());
083        return false;
084      }
085      unacceptableReasons.add(ERR_CONFIG_HTTPENDPOINT_INVALID_CONFIGURATION
086          .get(configuration.dn(), stackTraceToSingleLineString(e)));
087      return false;
088    }
089    catch (InitializationException ie)
090    {
091      unacceptableReasons.add(ie.getMessageObject());
092      return false;
093    }
094    finally
095    {
096      if (dummyApplication != null)
097      {
098        dummyApplication.stop();
099      }
100    }
101  }
102
103  /**
104   * Create a new HttpApplication.
105   *
106   * @return an {@link HttpApplication} configured and ready to be started.
107   * @throws InitializationException
108   *           If the application cannot be created.
109   */
110  public abstract HttpApplication newHttpApplication() throws InitializationException;
111
112}