All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.phloc.web.http.AcceptEncodingList Maven / Gradle / Ivy

/**
 * Copyright (C) 2006-2015 phloc systems
 * http://www.phloc.com
 * office[at]phloc[dot]com
 *
 * 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.phloc.web.http;

import java.util.Locale;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;

import com.phloc.commons.ValueEnforcer;

/**
 * Contains a list of Accept-Encoding values as specified by the HTTP header
 * 
 * @author Philip Helger
 */
@NotThreadSafe
public final class AcceptEncodingList extends AbstractQValueList 
{
  public AcceptEncodingList ()
  {}

  @Nonnull
  private static String _unify (@Nonnull final String sEncoding)
  {
    return sEncoding.toLowerCase (Locale.US);
  }

  public void addEncoding (@Nonnull final String sEncoding, @Nonnegative final double dQuality)
  {
    ValueEnforcer.notEmpty (sEncoding, "Encoding");
    m_aMap.put (_unify (sEncoding), new QValue (dQuality));
  }

  /**
   * Return the associated quality of the given encoding.
   * 
   * @param sEncoding
   *        The encoding name to query. May not be null.
   * @return The matching {@link QValue} and never null.
   */
  @Nonnull
  public QValue getQValueOfEncoding (@Nonnull final String sEncoding)
  {
    ValueEnforcer.notNull (sEncoding, "Encoding");

    // Direct search encoding
    QValue aQuality = m_aMap.get (_unify (sEncoding));
    if (aQuality == null)
    {
      // If not explicitly given, check for "*"
      aQuality = m_aMap.get (AcceptEncodingHandler.ANY_ENCODING);
      if (aQuality == null)
      {
        // Neither encoding nor "*" is present
        // -> assume minimum quality
        return QValue.MIN_QVALUE;
      }
    }
    return aQuality;
  }

  /**
   * Return the associated quality of the given encoding.
   * 
   * @param sEncoding
   *        The encoding name to query. May not be null.
   * @return 0 means not accepted, 1 means fully accepted.
   */
  public double getQualityOfEncoding (@Nonnull final String sEncoding)
  {
    return getQValueOfEncoding (sEncoding).getQuality ();
  }

  /**
   * Check if the passed encoding is supported. Supported means the quality is
   * > 0.
   * 
   * @param sEncoding
   *        The encoding to be checked. May not be null.
   * @return true if the encoding is supported, false
   *         if not.
   */
  public boolean supportsEncoding (@Nonnull final String sEncoding)
  {
    return getQValueOfEncoding (sEncoding).isAboveMinimumQuality ();
  }

  public boolean explicitlySupportsEncoding (@Nonnull final String sEncoding)
  {
    ValueEnforcer.notNull (sEncoding, "Encoding");

    final QValue aQuality = m_aMap.get (_unify (sEncoding));
    return aQuality != null && aQuality.isAboveMinimumQuality ();
  }

  public boolean supportsGZIP ()
  {
    return supportsEncoding (AcceptEncodingHandler.GZIP_ENCODING) ||
           supportsEncoding (AcceptEncodingHandler.X_GZIP_ENCODING);
  }

  /**
   * @return the accepted GZip encoding. May either be
   *         {@link AcceptEncodingHandler#GZIP_ENCODING} or
   *         {@link AcceptEncodingHandler#X_GZIP_ENCODING} or null
   */
  @Nullable
  public String getUsedGZIPEncoding ()
  {
    if (supportsEncoding (AcceptEncodingHandler.GZIP_ENCODING))
      return AcceptEncodingHandler.GZIP_ENCODING;
    if (supportsEncoding (AcceptEncodingHandler.X_GZIP_ENCODING))
      return AcceptEncodingHandler.X_GZIP_ENCODING;
    return null;
  }

  public boolean supportsDeflate ()
  {
    return supportsEncoding (AcceptEncodingHandler.DEFLATE_ENCODING);
  }

  /**
   * @return the accepted GZip encoding. May either be
   *         {@link AcceptEncodingHandler#DEFLATE_ENCODING} or null
   */
  @Nullable
  public String getUsedDeflateEncoding ()
  {
    if (supportsEncoding (AcceptEncodingHandler.DEFLATE_ENCODING))
      return AcceptEncodingHandler.DEFLATE_ENCODING;
    return null;
  }

  public boolean supportsCompress ()
  {
    return supportsEncoding (AcceptEncodingHandler.COMPRESS_ENCODING) ||
           supportsEncoding (AcceptEncodingHandler.X_COMPRESS_ENCODING);
  }

  /**
   * @return the accepted GZip encoding. May either be
   *         {@link AcceptEncodingHandler#COMPRESS_ENCODING} or
   *         {@link AcceptEncodingHandler#X_COMPRESS_ENCODING} or
   *         null
   */
  @Nullable
  public String getUsedCompressEncoding ()
  {
    if (supportsEncoding (AcceptEncodingHandler.COMPRESS_ENCODING))
      return AcceptEncodingHandler.COMPRESS_ENCODING;
    if (supportsEncoding (AcceptEncodingHandler.X_COMPRESS_ENCODING))
      return AcceptEncodingHandler.X_COMPRESS_ENCODING;
    return null;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy