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 org.apache.juneau.http.annotation.*;
016import org.apache.juneau.internal.*;
017import org.apache.juneau.jsonschema.annotation.Items;
018
019/**
020 * Category of headers that consist of a comma-delimited list of string values.
021 *
022 * <p>
023 * <h5 class='figure'>Example</h5>
024 * <p class='bcode w800'>
025 *    Allow: GET, PUT
026 * </p>
027 *
028 * <h5 class='section'>See Also:</h5>
029 * <ul class='doctree'>
030 *    <li class='extlink'>{@doc RFC2616}
031 * </ul>
032 */
033@Header(type="array",collectionFormat="csv",items=@Items(type="string"))
034public class HeaderStringArray {
035
036   private final String[] value;
037
038   /**
039    * Constructor.
040    *
041    * @param value The raw header value.
042    */
043   protected HeaderStringArray(String[] value) {
044      this.value = value;
045   }
046
047   /**
048    * Constructor.
049    *
050    * @param value The raw header value.
051    */
052   protected HeaderStringArray(String value) {
053      this.value = StringUtils.split(value);
054   }
055
056   /**
057    * Returns this header as a simple string value.
058    *
059    * <p>
060    * Functionally equivalent to calling {@link #toString()}.
061    *
062    * @return This header as a simple string.
063    */
064   public String asString() {
065      return StringUtils.joine(value, ',');
066   }
067
068   /**
069    * Returns <jk>true</jk> if this header contains the specified value.
070    *
071    * @param val The value to check for.
072    * @return <jk>true</jk> if this header contains the specified value.
073    */
074   public boolean contains(String val) {
075      if (val != null)
076         for (String v : value)
077            if (val.equals(v))
078               return true;
079      return false;
080   }
081
082   /**
083    * Returns <jk>true</jk> if this header contains the specified value using {@link String#equalsIgnoreCase(String)}.
084    *
085    * @param val The value to check for.
086    * @return <jk>true</jk> if this header contains the specified value.
087    */
088   public boolean containsIC(String val) {
089      if (val != null)
090         for (String v : value)
091            if (val.equalsIgnoreCase(v))
092               return true;
093      return false;
094   }
095
096   @Override /* Object */
097   public String toString() {
098      return asString();
099   }
100}