001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.http;
014
015/**
016 * Represents a validator value.
017 *
018 * <p>
019 * <h5 class='figure'>Example</h5>
020 * <p class='bcode w800'>
021 *    ETag: "123456789"    – A strong ETag validator
022 *    ETag: W/"123456789"  – A weak ETag validator
023 * </p>
024 *
025 * <ul class='seealso'>
026 *    <li class='extlink'>{@doc RFC2616}
027 * </ul>
028 */
029public class EntityValidator {
030
031   private final String value;
032   private final boolean isWeak;
033
034   /**
035    * Constructor.
036    *
037    * @param value The validator string value.
038    */
039   protected EntityValidator(String value) {
040      value = value.trim();
041      isWeak = value.startsWith("W/");
042      if (isWeak)
043         value = value.substring(2);
044      if (value.length() > 1 && value.charAt(0) == '"' && value.charAt(value.length()-1) == '"')
045         value = value.substring(1, value.length()-1);
046      this.value = value;
047   }
048
049   /**
050    * Returns the validator value stripped of quotes and weak tag.
051    *
052    * @return The validator value.
053    */
054   public String asString() {
055      return value;
056   }
057
058   /**
059    * Returns <jk>true</jk> if the weak flag is present in the value.
060    *
061    * @return <jk>true</jk> if the weak flag is present in the value.
062    */
063   public boolean isWeak() {
064      return isWeak;
065   }
066
067   /**
068    * Returns <jk>true</jk> if the validator string value is <c>*</c>.
069    *
070    * @return <jk>true</jk> if the validator string value is <c>*</c>.
071    */
072   public boolean isAny() {
073      return "*".equals(value);
074   }
075
076   @Override
077   public String toString() {
078      return (isWeak ? "W/" : "") + (isAny() ? "*" : ('"' + value + '"'));
079   }
080}