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 static java.util.Collections.singletonList; 020import static org.forgerock.http.header.HeaderUtil.parseSingleValuedHeader; 021 022import java.util.Collections; 023import java.util.List; 024 025import org.forgerock.http.protocol.Header; 026import org.forgerock.http.protocol.Message; 027 028/** 029 * Processes the OAuth 2.0 Bearer <strong>{@code WWW-Authenticate}</strong> 030 * message header. For more information, see <a 031 * href="http://tools.ietf.org/html/rfc6750#section-3">RFC 6750</a>. 032 */ 033public class OAuth2BearerWWWAuthenticateHeader extends Header { 034 035 /** The name of the header that this object represents. */ 036 public static final String NAME = "WWW-Authenticate"; 037 038 /** The possibly null OAuth 2.0 error. */ 039 private final OAuth2Error error; 040 041 /** 042 * Constructs a new empty header. 043 */ 044 public OAuth2BearerWWWAuthenticateHeader() { 045 this(null); 046 } 047 048 /** 049 * Constructs a new header with the provided error. 050 * 051 * @param error 052 * The possibly null OAuth 2.0 error. 053 */ 054 public OAuth2BearerWWWAuthenticateHeader(OAuth2Error error) { 055 this.error = error; 056 } 057 058 /** 059 * Constructs a new header, initialized from the specified message. 060 * 061 * @param message 062 * The message to initialize the header from. 063 * @return The parsed header. 064 */ 065 public static OAuth2BearerWWWAuthenticateHeader valueOf(final Message message) { 066 return valueOf(parseSingleValuedHeader(message, NAME)); 067 } 068 069 /** 070 * Constructs a new header, initialized from the specified string value. 071 * 072 * @param string 073 * The value to initialize the header from. 074 * @return The parsed header. 075 */ 076 public static OAuth2BearerWWWAuthenticateHeader valueOf(final String string) { 077 if (string != null) { 078 try { 079 return new OAuth2BearerWWWAuthenticateHeader(OAuth2Error 080 .valueOfWWWAuthenticateHeader(string)); 081 } catch (final IllegalArgumentException e) { 082 // Ignore parsing errors - just reset the header. 083 } 084 } 085 return new OAuth2BearerWWWAuthenticateHeader(); 086 } 087 088 @Override 089 public String getName() { 090 return NAME; 091 } 092 093 /** 094 * Returns the OAuth 2.0 error represented by this header. 095 * 096 * @return The OAuth 2.0 error represented by this header. 097 */ 098 public OAuth2Error getOAuth2Error() { 099 return error; 100 } 101 102 @Override 103 public List<String> getValues() { 104 return error != null ? singletonList(error.toWWWAuthenticateHeader()) : Collections.<String>emptyList(); 105 } 106}