001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2010–2011 ApexIdentity Inc.
015 * Portions Copyright 2011-2015 ForgeRock AS.
016 */
017
018package org.forgerock.http.protocol;
019
020import java.util.ArrayList;
021import java.util.List;
022
023/**
024 * An HTTP message header.
025 */
026public abstract class Header {
027
028    /**
029     * Returns the name of the header, as it would canonically appear within an
030     * HTTP message.
031     *
032     * @return The name of the header, as it would canonically appear within an
033     *         HTTP message.
034     */
035    public abstract String getName();
036
037    /**
038     * Returns the header as a list of strings. Each {@code String} should
039     * represent the value component of the key-value pair that makes up the
040     * HTTP header - as such, for some {@code Header} implementations each
041     * String in this {@code List} may contain multiple token-separated values.
042     * <p>
043     * The {@code List} returned from this method should not be expected to be
044     * mutable. However, some subclasses of {@code Header} may choose to
045     * implement it as such.
046     *
047     * @return The header as a list of string values.
048     */
049    public abstract List<String> getValues();
050
051    /**
052     * Gets the first value of this header instance. As with {@link #getValues},
053     * the returned {@code String} may contain multiple token-separated values.
054     *
055     * @return The first value, or null if none exist.
056     */
057    public String getFirstValue() {
058        List<String> values = getValues();
059        return values == null || values.size() == 0 ? null : values.get(0);
060    }
061
062    @Override
063    public String toString() {
064        List<String> strings = new ArrayList<>();
065        for (String value : getValues()) {
066            strings.add(getName() + ": " + value);
067        }
068        return strings.toString();
069    }
070
071    @Override
072    public boolean equals(Object o) {
073        if (this == o) {
074            return true;
075        }
076        if (o == null || !(o instanceof Header)) {
077            return false;
078        }
079
080        Header that = (Header) o;
081
082        return getName().equals(that.getName()) && getValues().equals(that.getValues());
083    }
084
085    @Override
086    public int hashCode() {
087        int result = getName().hashCode();
088        result = 31 * result + getValues().hashCode();
089        return result;
090    }
091
092}