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