UnboundID Server SDK

Ping Identity
UnboundID Server SDK Documentation

ExampleServletFilter.java

/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * docs/licenses/cddl.txt
 * or http://www.opensource.org/licenses/cddl1.php.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * docs/licenses/cddl.txt.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2011-2024 Ping Identity Corporation
 */
package com.unboundid.directory.sdk.examples;



import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;



/**
 * This class provides an instance of a servlet filter that will be used in
 * conjunction with the example servlet.  It doesn't do anything except register
 * that it has been invoked.
 */
public final class ExampleServletFilter
       implements Filter
{
  // Indicates whether the destroy method has been invoked.
  private volatile boolean destroyInvoked;

  // Indicates whether the pre-chain processing has been invoked.
  private volatile boolean preChainInvoked;

  // Indicates whether the init method has been invoked.
  private volatile boolean initInvoked;

  // Indicates whether the post-chain processing has been invoked.
  private volatile boolean postChainInvoked;



  /**
   * Creates a new instance of this servlet filter.
   */
  public ExampleServletFilter()
  {
    initInvoked      = false;
    preChainInvoked  = false;
    postChainInvoked = false;
    destroyInvoked   = false;
  }



  /**
   * Performs any necessary initialization processing.
   *
   * @param  config  The configuration for this filter.
   */
  public void init(final FilterConfig config)
  {
    initInvoked = true;
  }



  /**
   * Performs the core processing for this filter.
   *
   * @param  request   Information about the request from the client.
   * @param  response  Information about the response to the client.
   * @param  chain     The chain of additional filters to process.
   */
  public void doFilter(final ServletRequest request,
                       final ServletResponse response,
                       final FilterChain chain)
  {
    preChainInvoked = true;

    try
    {
      chain.doFilter(request, response);
    }
    catch (final Exception e)
    {
      // This shouldn't happen.
      throw new RuntimeException(e);
    }
    finally
    {
      postChainInvoked = true;
    }
  }



  /**
   * Performs any necessary cleanup for this filter.
   */
  public void destroy()
  {
    destroyInvoked = true;
  }



  /**
   * Indicates whether the init method has been invoked.
   *
   * @return  {@code true} if the init method has been invoked, or
   *          {@code false} if not.
   */
  public boolean initInvoked()
  {
    return initInvoked;
  }



  /**
   * Indicates whether pre-chain processing in the doFilter method has been
   * invoked.
   *
   * @return  {@code true} if pre-chain processing in the doFilter method has
   *          been invoked, or {@code false} if not.
   */
  public boolean preChainInvoked()
  {
    return preChainInvoked;
  }



  /**
   * Indicates whether post-chain processing in the doFilter method has been
   * invoked.
   *
   * @return  {@code true} if post-chain processing in the doFilter method has
   *          been invoked, or {@code false} if not.
   */
  public boolean postChainInvoked()
  {
    return postChainInvoked;
  }



  /**
   * Indicates whether the destroy method has been invoked.
   *
   * @return  {@code true} if the destroy method has been invoked, or
   *          {@code false} if not.
   */
  public boolean destroyInvoked()
  {
    return destroyInvoked;
  }
}