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-2014 ForgeRock AS. 016 */ 017 018package org.forgerock.openig.header; 019 020import java.util.ArrayList; 021import java.util.List; 022 023import org.forgerock.openig.http.Message; 024 025/** 026 * Processes the <strong>{@code Connection}</strong> message header. For more information, see 027 * <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616</a> §14.10. 028 */ 029public class ConnectionHeader implements Header { 030 031 /** The name of the header that this object represents. */ 032 public static final String NAME = "Connection"; 033 034 /** A list of connection-tokens. */ 035 private final List<String> tokens = new ArrayList<String>(); 036 037 /** 038 * Constructs a new empty header. 039 */ 040 public ConnectionHeader() { 041 // Nothing to do. 042 } 043 044 /** 045 * Constructs a new header, initialized from the specified message. 046 * 047 * @param message the message to initialize the header from. 048 */ 049 public ConnectionHeader(Message<?> message) { 050 fromMessage(message); 051 } 052 053 /** 054 * Constructs a new header, initialized from the specified string value. 055 * 056 * @param string the value to initialize the header from. 057 */ 058 public ConnectionHeader(String string) { 059 fromString(string); 060 } 061 062 private void clear() { 063 tokens.clear(); 064 } 065 066 /** 067 * Returns the list of connection-tokens. 068 * 069 * @return The list of connection-tokens. 070 */ 071 public List<String> getTokens() { 072 return tokens; 073 } 074 075 @Override 076 public String getKey() { 077 return NAME; 078 } 079 080 @Override 081 public void fromMessage(Message<?> message) { 082 if (message != null && message.getHeaders() != null) { 083 fromString(HeaderUtil.join(message.getHeaders().get(NAME), ',')); 084 } 085 } 086 087 @Override 088 public void fromString(String string) { 089 clear(); 090 if (string != null) { 091 tokens.addAll(HeaderUtil.split(string, ',')); 092 } 093 } 094 095 @Override 096 public void toMessage(Message<?> message) { 097 String value = toString(); 098 if (value != null) { 099 message.getHeaders().putSingle(NAME, value); 100 } 101 } 102 103 @Override 104 public String toString() { 105 // will return null if empty 106 return HeaderUtil.join(tokens, ','); 107 } 108 109 @Override 110 public boolean equals(Object o) { 111 return o == this || (o instanceof ConnectionHeader 112 && tokens.equals(((ConnectionHeader) o).tokens)); 113 } 114 115 @Override 116 public int hashCode() { 117 return tokens.hashCode(); 118 } 119}