// Copyright 2010 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.enterprise.secmgr.http;

import java.io.IOException;
import java.net.URL;

/**
 * Definition of the interface for HTTP authenticators.
 */
public interface HttpAuthenticator {

  /**
   * Is this authenticator applicable to a given response?
   *
   * @param previousResult The response generated by the previous request.
   * @return True if this authenticator is applicable to <code>previousResult</code>.
   */
  public boolean isApplicable(PageFetcherResult previousResult);

  /**
   * @return The preference rank of this authenticator.  Provides common support
   *     for preference ranking of the authenticators, which is used when a
   *     server supports multiple authentication mechanisms.  In that case, the
   *     client (this program) should choose one of the available mechanisms,
   *     preferably the one with the "strongest" security; this preference
   *     ranking is a way to decide which mechanism to choose.  Every
   *     authenticator must have a unique rank, so that the authenticators form
   *     a total ordering.
   */
  public int getPreferenceRank();

  /**
   * Provide HTTP authentication services based on a given response.  Must not
   * be called if {@link #isApplicable} returns false.
   *
   * @param previousResult The response generated by the previous request.
   * @param requester The HttpRequester that performed the previous request.
   * @param url The URL for the previous request.
   * @param getBody True if the previous request wanted the response body.
   * @return If the authenticator takes action, a new FetchPageResult to use in
   *     place of <code>previousResult</code>.  Otherwise, return null.
   * @throws IOException if there are I/O problems during authentication.
   */
  public PageFetcherResult apply(PageFetcherResult previousResult, HttpRequester requester, URL url,
      boolean getBody)
      throws IOException;
}
