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.jwe;
018
019import org.forgerock.json.jose.exceptions.JweException;
020import org.forgerock.json.jose.jwe.handlers.compression.CompressionHandler;
021import org.forgerock.json.jose.jwe.handlers.compression.DeflateCompressionHandler;
022import org.forgerock.json.jose.jwe.handlers.compression.NOPCompressionHandler;
023import org.forgerock.json.jose.utils.Utils;
024import org.forgerock.util.encode.Base64url;
025
026/**
027 * A service to get the appropriate CompressionHandler for a specified Compression algorithm.
028 * <p>
029 * For details of all supported algorithms see {@link CompressionAlgorithm}.
030 *
031 * @since 2.0.0
032 */
033public class CompressionManager {
034
035    /**
036     * Gets the appropriate CompressionHandler that can perform the required compression using the given
037     * compression algorithm.
038     *
039     * @param algorithm The Compression algorithm.
040     * @return The CompressionHandler.
041     */
042    public CompressionHandler getCompressionHandler(CompressionAlgorithm algorithm) {
043
044        switch (algorithm) {
045        case NONE: {
046            return new NOPCompressionHandler();
047        }
048        case DEF: {
049            return new DeflateCompressionHandler();
050        }
051        default: {
052            throw new JweException("No Compression Handler for unknown compression algorithm, "
053                    + algorithm + ".");
054        }
055        }
056    }
057
058    /**
059     * Convenience method equivalent to
060     * {@code Base64url.encode(getCompressionHandler(compressionAlgorithm).compress(data.getBytes(Utils.CHARSET)))}.
061     *
062     * @param compressionAlgorithm the compression algorithm to use.
063     * @param data the data to compress.
064     * @return the base64url-encoded compressed data.
065     */
066    public String compress(CompressionAlgorithm compressionAlgorithm, String data) {
067        return Base64url.encode(getCompressionHandler(compressionAlgorithm).compress(data.getBytes(Utils.CHARSET)));
068    }
069
070    /**
071     * Convenience method equivalent to
072     * {@code getCompressionHandler(compressionAlgorithm).decompress(Base64url.decode(data))}.
073     *
074     * @param compressionAlgorithm the compression algorithm to use.
075     * @param data the base64url-encoded data to decompress.
076     * @return the decompressed data.
077     */
078    public byte[] decompress(CompressionAlgorithm compressionAlgorithm, String data) {
079        return getCompressionHandler(compressionAlgorithm).decompress(Base64url.decode(data));
080    }
081}