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.header;
019
020import static org.forgerock.http.header.HeaderUtil.*;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.forgerock.http.protocol.Header;
026import org.forgerock.http.protocol.Message;
027
028/**
029 * Processes the <strong>{@code Connection}</strong> message header. For more
030 * information, see <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616</a>
031 * §14.10.
032 */
033public class ConnectionHeader extends Header {
034    /**
035     * Constructs a new header, initialized from the specified message.
036     *
037     * @param message
038     *            The message to initialize the header from.
039     * @return The parsed header.
040     */
041    public static ConnectionHeader valueOf(final Message message) {
042        return new ConnectionHeader(parseMultiValuedHeader(message, NAME));
043    }
044
045    /**
046     * Constructs a new header, initialized from the specified string value.
047     *
048     * @param string
049     *            The value to initialize the header from.
050     * @return The parsed header.
051     */
052    public static ConnectionHeader valueOf(final String string) {
053        return new ConnectionHeader(parseMultiValuedHeader(string));
054    }
055
056    /** The name of this header. */
057    public static final String NAME = "Connection";
058
059    /** A list of connection tokens. */
060    private final List<String> tokens;
061
062    /**
063     * Constructs a new empty header.
064     */
065    public ConnectionHeader() {
066        this(new ArrayList<String>(1));
067    }
068
069    /**
070     * Constructs a new header with the provided connection tokens.
071     *
072     * @param tokens
073     *            The connection tokens.
074     */
075    public ConnectionHeader(final List<String> tokens) {
076        this.tokens = tokens;
077    }
078
079    @Override
080    public String getName() {
081        return NAME;
082    }
083
084    /**
085     * Returns the list of connection tokens.
086     *
087     * @return The list of connection tokens.
088     */
089    public List<String> getTokens() {
090        return tokens;
091    }
092
093    @Override
094    public List<String> getValues() {
095        return tokens;
096    }
097
098    static class Factory extends HeaderFactory<ConnectionHeader> {
099
100        @Override
101        public ConnectionHeader parse(String value) {
102            return valueOf(value);
103        }
104
105        @Override
106        public ConnectionHeader parse(List<String> values) {
107            return valueOf(join(values, ','));
108        }
109    }
110}