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 2011-2014 ForgeRock AS. 015 */ 016 017package org.forgerock.openig.filter.oauth2.client; 018 019import org.forgerock.openig.header.Header; 020import org.forgerock.openig.http.Message; 021 022/** 023 * Processes the OAuth 2.0 Bearer <strong>{@code WWW-Authenticate}</strong> 024 * message header. For more information, see <a 025 * href="http://tools.ietf.org/html/rfc6750#section-3">RFC 6750</a>. 026 */ 027public class OAuth2BearerWWWAuthenticateHeader implements Header { 028 029 /** The name of the header that this object represents. */ 030 public static final String NAME = "WWW-Authenticate"; 031 032 /** The possibly null OAuth 2.0 error. */ 033 private OAuth2Error error; 034 035 /** 036 * Constructs a new empty header. 037 */ 038 public OAuth2BearerWWWAuthenticateHeader() { 039 } 040 041 /** 042 * Constructs a new header, initialized from the specified message. 043 * 044 * @param message 045 * the message to initialize the header from. 046 */ 047 public OAuth2BearerWWWAuthenticateHeader(final Message<?> message) { 048 fromMessage(message); 049 } 050 051 /** 052 * Constructs a new header, initialized from the specified string value. 053 * 054 * @param string 055 * the value to initialize the header from. 056 */ 057 public OAuth2BearerWWWAuthenticateHeader(final String string) { 058 fromString(string); 059 } 060 061 @Override 062 public boolean equals(final Object o) { 063 if (o == this) { 064 return true; 065 } else if (o instanceof OAuth2BearerWWWAuthenticateHeader) { 066 final OAuth2BearerWWWAuthenticateHeader other = (OAuth2BearerWWWAuthenticateHeader) o; 067 if (error == null) { 068 return other.error == null; 069 } 070 return error.equals(((OAuth2BearerWWWAuthenticateHeader) o).error); 071 } else { 072 return false; 073 } 074 } 075 076 @Override 077 public void fromMessage(final Message<?> message) { 078 if (message != null && message.getHeaders() != null) { 079 fromString(message.getHeaders().getFirst(NAME)); 080 } 081 } 082 083 @Override 084 public void fromString(final String string) { 085 error = null; 086 if (string != null) { 087 try { 088 error = OAuth2Error.valueOfWWWAuthenticateHeader(string); 089 } catch (final IllegalArgumentException e) { 090 // Ignore parsing errors - just reset the header. 091 } 092 } 093 } 094 095 @Override 096 public String getKey() { 097 return NAME; 098 } 099 100 /** 101 * Returns the OAuth 2.0 error represented by this header. 102 * 103 * @return The OAuth 2.0 error represented by this header. 104 */ 105 public OAuth2Error getOAuth2Error() { 106 return error; 107 } 108 109 @Override 110 public int hashCode() { 111 return error != null ? error.hashCode() : 0; 112 } 113 114 @Override 115 public void toMessage(final Message<?> message) { 116 final String value = toString(); 117 if (value != null) { 118 message.getHeaders().putSingle(NAME, value); 119 } 120 } 121 122 @Override 123 public String toString() { 124 return error != null ? error.toWWWAuthenticateHeader() : null; 125 } 126}