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 2013-2016 ForgeRock AS.
015 */
016
017package org.forgerock.json.jose.builders;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.forgerock.json.jose.jwt.Algorithm;
023import org.forgerock.json.jose.jwt.JwtHeader;
024
025/**
026 * A base implementation of a JWT header builder that provides a fluent builder pattern to creating JWT headers.
027 * <p>
028 * See {@link JwtHeader} for information on the JwtHeader object that this builder creates.
029 *
030 * @param <T> the type of JwtBuilder that parents this JwtHeaderBuilder.
031 * @param <B> the type of this JwtHeaderBuilder
032 *
033 * @since 2.0.0
034 */
035public abstract class JwtHeaderBuilder<T extends JwtBuilder, B extends JwtHeaderBuilder<T, B>> {
036
037    private final T jwtBuilder;
038
039    private final Map<String, Object> headers = new HashMap<>();
040
041    /**
042     * Constructs a new JwtHeaderBuilder, parented by the given JwtBuilder.
043     *
044     * @param jwtBuilder The JwtBuilder instance that this JwtHeaderBuilder is a child of.
045     */
046    public JwtHeaderBuilder(T jwtBuilder) {
047        this.jwtBuilder = jwtBuilder;
048    }
049
050    /**
051     * Adds a custom header parameter to the JWT header.
052     * <p>
053     * @see JwtHeader#setParameter(String, Object)
054     *
055     * @param key The header parameter key.
056     * @param value The header parameter value.
057     * @return This JwtHeaderBuilder.
058     */
059    @SuppressWarnings("unchecked")
060    public B header(String key, Object value) {
061        headers.put(key, value);
062        return (B) this;
063    }
064
065    /**
066     * Adds a customer header parameter to the JWT header if the value is not null.
067     *
068     * @param key The header parameter key.
069     * @param value The header parameter value, or {@literal null} if not specified.
070     * @return This JwtHeaderBuilder.
071     */
072    @SuppressWarnings("unchecked")
073    public B headerIfNotNull(String key, Object value) {
074        if (value != null) {
075            header(key, value);
076        }
077        return (B) this;
078    }
079
080    /**
081     * Sets the algorithm used to perform cryptographic signing and/or encryption on the JWT.
082     * <p>
083     * @see JwtHeader#setAlgorithm(org.forgerock.json.jose.jwt.Algorithm)
084     *
085     * @param algorithm The algorithm.
086     * @return This JwtHeaderBuilder.
087     */
088    @SuppressWarnings("unchecked")
089    public B alg(Algorithm algorithm) {
090        header("alg", algorithm.toString());
091        return (B) this;
092    }
093
094    /**
095     * Marks the end to the building of the JWT header.
096     *
097     * @return The parent JwtBuilder for this JwtHeaderBuilder instance.
098     */
099    public T done() {
100        return jwtBuilder;
101    }
102
103    /**
104     * Gets the header parameters for the JWT.
105     *
106     * @return The JWT's header parameters.
107     */
108    protected Map<String, Object> getHeaders() {
109        return headers;
110    }
111
112    /**
113     * Creates a JwtHeader instance from the header parameters set in this builder.
114     *
115     * @return A JwtHeader instance.
116     */
117    protected abstract JwtHeader build();
118}