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.internal.*;
016
017/**
018 * Category of headers that consist of a comma-delimited list of string values.
019 * 
020 * <p>
021 * <h5 class='figure'>Example</h5>
022 * <p class='bcode'>
023 *    Allow: GET, PUT
024 * </p>
025 * 
026 * <h5 class='section'>See Also:</h5>
027 * <ul class='doctree'>
028 *    <li class='extlink'><a class='doclink' href='https://www.w3.org/Protocols/rfc2616/rfc2616.html'>Hypertext Transfer Protocol -- HTTP/1.1</a>
029 * </ul>
030 */
031public class HeaderStringArray {
032
033   private final String[] value;
034
035   /**
036    * Constructor.
037    * 
038    * @param value The raw header value.
039    */
040   protected HeaderStringArray(String value) {
041      this.value = StringUtils.split(value);
042   }
043
044   /**
045    * Returns this header as a simple string value.
046    * 
047    * <p>
048    * Functionally equivalent to calling {@link #toString()}.
049    * 
050    * @return This header as a simple string.
051    */
052   public String asString() {
053      return StringUtils.join(value, ',');
054   }
055
056   /**
057    * Returns <jk>true</jk> if this header contains the specified value.
058    * 
059    * @param val The value to check for.
060    * @return <jk>true</jk> if this header contains the specified value.
061    */
062   public boolean contains(String val) {
063      if (val != null)
064         for (String v : value)
065            if (val.equals(v))
066               return true;
067      return false;
068   }
069
070   /**
071    * Returns <jk>true</jk> if this header contains the specified value using {@link String#equalsIgnoreCase(String)}.
072    * 
073    * @param val The value to check for.
074    * @return <jk>true</jk> if this header contains the specified value.
075    */
076   public boolean containsIC(String val) {
077      if (val != null)
078         for (String v : value)
079            if (val.equalsIgnoreCase(v))
080               return true;
081      return false;
082   }
083
084   @Override /* Object */
085   public String toString() {
086      return asString();
087   }
088}