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-2015 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     * Sets the algorithm used to perform cryptographic signing and/or encryption on the JWT.
067     * <p>
068     * @see JwtHeader#setAlgorithm(org.forgerock.json.jose.jwt.Algorithm)
069     *
070     * @param algorithm The algorithm.
071     * @return This JwtHeaderBuilder.
072     */
073    @SuppressWarnings("unchecked")
074    public B alg(Algorithm algorithm) {
075        header("alg", algorithm.toString());
076        return (B) this;
077    }
078
079    /**
080     * Marks the end to the building of the JWT header.
081     *
082     * @return The parent JwtBuilder for this JwtHeaderBuilder instance.
083     */
084    public T done() {
085        return jwtBuilder;
086    }
087
088    /**
089     * Gets the header parameters for the JWT.
090     *
091     * @return The JWT's header parameters.
092     */
093    protected Map<String, Object> getHeaders() {
094        return headers;
095    }
096
097    /**
098     * Creates a JwtHeader instance from the header parameters set in this builder.
099     *
100     * @return A JwtHeader instance.
101     */
102    protected abstract JwtHeader build();
103}