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
015import static org.apache.juneau.internal.StringUtils.*;
016import static org.apache.juneau.internal.ObjectUtils.*;
017
018import org.apache.juneau.*;
019
020/**
021 * Represents a validator value.
022 *
023 * <p>
024 * <h5 class='figure'>Example</h5>
025 * <p class='bcode w800'>
026 *    ETag: "123456789"    – A strong ETag validator
027 *    ETag: W/"123456789"  – A weak ETag validator
028 * </p>
029 *
030 * <ul class='seealso'>
031 *    <li class='extlink'>{@doc ExtRFC2616}
032 * </ul>
033 */
034public class EntityTag {
035
036   private final String value;
037   private final boolean isWeak, isAny;
038
039   /**
040    * Creator.
041    *
042    * @param value The validator string value.
043    * @return A new {@link EntityTag} object or <jk>null</jk> if the value was <jk>null</jk>.
044    * @throws IllegalArgumentException If attempting to set an invalid entity tag value.
045    */
046   public static EntityTag of(Object value) {
047      Object o = unwrap(value);
048      return (o == null ? null : new EntityTag(o.toString()));
049   }
050
051   /**
052    * Constructor.
053    *
054    * @param value The validator string value.
055    * @throws IllegalArgumentException If attempting to set an invalid entity tag value.
056    */
057   public EntityTag(String value) {
058      if (value == null)
059         throw new BasicIllegalArgumentException("Invalid value for entity-tag: [null]");
060
061      value = trim(emptyIfNull(value));
062      isWeak = value.startsWith("W/");
063      isAny = "*".equals(value);
064
065      if (! isAny) {
066         if (isWeak)
067            value = value.substring(2);
068         if (value.length() > 1 && value.charAt(0) == '"' && value.charAt(value.length()-1) == '"')
069            value = value.substring(1, value.length()-1);
070         else
071            throw new BasicIllegalArgumentException("Invalid value for entity-tag: [{0}]", isWeak ? ("W/"+value) : value);
072      }
073      this.value = value;
074
075   }
076
077   /**
078    * Returns the validator value stripped of quotes and weak tag.
079    *
080    * @return The validator value.
081    */
082   public String getEntityValue() {
083      return value;
084   }
085
086   /**
087    * Returns <jk>true</jk> if the weak flag is present in the value.
088    *
089    * @return <jk>true</jk> if the weak flag is present in the value.
090    */
091   public boolean isWeak() {
092      return isWeak;
093   }
094
095   /**
096    * Returns <jk>true</jk> if the validator string value is <c>*</c>.
097    *
098    * @return <jk>true</jk> if the validator string value is <c>*</c>.
099    */
100   public boolean isAny() {
101      return isAny;
102   }
103
104   @Override
105   public String toString() {
106      return (isWeak ? "W/" : "") + (isAny() ? "*" : ('"' + value + '"'));
107   }
108}