001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.http.classic.header; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020import static org.apache.juneau.commons.utils.StringUtils.*; 021 022import java.util.*; 023import java.util.function.*; 024 025import org.apache.juneau.bct.assertions.*; 026 027/** 028 * Category of headers that consist of a comma-delimited list of string values. 029 * 030 * <h2 class='figure'>Example</h2> 031 * <p class='bcode'> 032 * Allow: GET, PUT 033 * </p> 034 * 035 * <h2 class='section'>See Also:</h2><ul> 036 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommon">juneau-rest-common</a> 037 * <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a> 038 * </ul> 039 * 040 * @serial exclude 041 */ 042@SuppressWarnings({ 043 "java:S2160", // equals() inherited from BasicHeader compares name+value; typed field is accessed via getValue() 044 "java:S3776" // contains()/containsIgnoreCase() check both the fixed value array and the supplier with nested null-guarded loops 045}) 046public class BasicCsvHeader extends BasicHeader { 047 private static final long serialVersionUID = 1L; 048 049 /** 050 * Static creator. 051 * 052 * @param name The header name. 053 * @param value 054 * The header value. 055 * <br>Must be a comma-delimited list. 056 * <br>Can be <jk>null</jk>. 057 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 058 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 059 */ 060 public static BasicCsvHeader of(String name, String value) { 061 return value == null ? null : new BasicCsvHeader(name, value); 062 } 063 064 /** 065 * Static creator. 066 * 067 * @param name The header name. 068 * @param value 069 * The header value. 070 * <br>Can be <jk>null</jk>. 071 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 072 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 073 */ 074 public static BasicCsvHeader of(String name, String...value) { 075 return value == null ? null : new BasicCsvHeader(name, value); 076 } 077 078 /** 079 * Static creator with delayed value. 080 * 081 * <p> 082 * Header value is re-evaluated on each call to {@link #getValue()}. 083 * 084 * @param name The header name. 085 * @param value 086 * The supplier of the header value. 087 * <br>Can be <jk>null</jk>. 088 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 089 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 090 */ 091 public static BasicCsvHeader of(String name, Supplier<String[]> value) { 092 return value == null ? null : new BasicCsvHeader(name, value); 093 } 094 095 private final String[] value; 096 private final transient Supplier<String[]> supplier; 097 098 /** 099 * Constructor. 100 * 101 * @param name The header name. 102 * @param value 103 * The header value. 104 * <br>Must be a comma-delimited list. 105 * <br>Can be <jk>null</jk> to create a header with no value. 106 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 107 */ 108 public BasicCsvHeader(String name, String value) { 109 super(name, value); 110 this.value = splita(value); 111 this.supplier = null; 112 } 113 114 /** 115 * Constructor. 116 * 117 * @param name The header name. 118 * @param value 119 * The header value. 120 * <br>Can be <jk>null</jk> to create a header with no value. 121 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 122 */ 123 public BasicCsvHeader(String name, String...value) { 124 super(name, join(value, ", ")); 125 this.value = cp(value); 126 this.supplier = null; 127 } 128 129 /** 130 * Constructor with delayed value. 131 * 132 * <p> 133 * Header value is re-evaluated on each call to {@link #getValue()}. 134 * 135 * @param name The header name. 136 * @param value 137 * The supplier of the header value. 138 * <br>Can be <jk>null</jk> to create a header with no value. 139 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 140 */ 141 public BasicCsvHeader(String name, Supplier<String[]> value) { 142 super(name, null); 143 this.value = null; 144 supplier = value; 145 } 146 147 /** 148 * Returns the header value as an array wrapped in an {@link Optional}. 149 * 150 * <p> 151 * The array is a copy of the value of this header. 152 * 153 * @return The header value as an array wrapped in an {@link Optional}. Never <jk>null</jk>. 154 */ 155 public Optional<String[]> asArray() { 156 return o(cp(value())); 157 } 158 159 /** 160 * Returns the header value as a list wrapped in an {@link Optional}. 161 * 162 * <p> 163 * The list is unmodifiable. 164 * 165 * @return The header value as a list wrapped in an {@link Optional}. Never <jk>null</jk>. 166 */ 167 public Optional<List<String>> asList() { 168 return o(u(l(value()))); 169 } 170 171 /** 172 * Provides the ability to perform fluent-style assertions on this header. 173 * 174 * @return A new fluent assertion object. 175 * @throws AssertionError If assertion failed. 176 */ 177 public FluentListAssertion<String,BasicCsvHeader> assertList() { 178 return new FluentListAssertion<>(u(l(value())), this); 179 } 180 181 /** 182 * Returns <jk>true</jk> if this header contains the specified value. 183 * 184 * @param val The value to check for. 185 * @return <jk>true</jk> if this header contains the specified value. 186 */ 187 public boolean contains(String val) { 188 if (nn(value)) 189 for (var v : value) 190 if (eq(v, val)) 191 return true; 192 if (nn(supplier)) { 193 String[] value2 = supplier.get(); 194 if (nn(value2)) 195 for (var v : value2) 196 if (eq(v, val)) 197 return true; 198 199 } 200 return false; 201 } 202 203 /** 204 * Returns <jk>true</jk> if this header contains the specified value using {@link String#equalsIgnoreCase(String)}. 205 * 206 * @param val The value to check for. 207 * @return <jk>true</jk> if this header contains the specified value. 208 */ 209 public boolean containsIgnoreCase(String val) { 210 if (nn(value)) 211 for (var v : value) 212 if (eqic(v, val)) 213 return true; 214 if (nn(supplier)) { 215 String[] value2 = supplier.get(); 216 if (nn(value2)) 217 for (var v : value2) 218 if (eqic(v, val)) 219 return true; 220 } 221 return false; 222 } 223 224 @Override /* Overridden from Header */ 225 public String getValue() { return join(value(), ", "); } 226 227 /** 228 * Return the value if present, otherwise return <c>other</c>. 229 * 230 * <p> 231 * This is a shortened form for calling <c>asArray().orElse(<jv>other</jv>)</c>. 232 * 233 * @param other The value to be returned if there is no value present, can be <jk>null</jk>. 234 * @return The value, if present, otherwise <c>other</c>. 235 */ 236 public String[] orElse(String[] other) { 237 String[] x = value(); 238 return nn(x) ? cp(x) : other; 239 } 240 241 /** 242 * Returns the header value as an array. 243 * 244 * <p> 245 * The array is a copy of the value of this header. 246 * 247 * @return The header value as an array, or <jk>null</jk> if the header has no value. 248 */ 249 public String[] toArray() { 250 return cp(value()); 251 } 252 253 /** 254 * Returns the header value as a list. 255 * 256 * <p> 257 * The list is unmodifiable. 258 * 259 * @return The header value as a list, or <jk>null</jk> if the header has no value. 260 */ 261 public List<String> toList() { 262 return u(l(value())); 263 } 264 265 private String[] value() { 266 if (nn(supplier)) 267 return supplier.get(); 268 return value; 269 } 270}