1 /* 2 * The contents of this file are subject to the terms of the Common Development and 3 * Distribution License (the License). You may not use this file except in compliance with the 4 * License. 5 * 6 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 7 * specific language governing permission and limitations under the License. 8 * 9 * When distributing Covered Software, include this CDDL Header Notice in each file and include 10 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 11 * Header, with the fields enclosed by brackets [] replaced by your own identifying 12 * information: "Portions copyright [year] [name of copyright owner]". 13 * 14 * Copyright 2013-2016 ForgeRock AS. 15 */ 16 17 package org.forgerock.json.jose.jwe; 18 19 import org.forgerock.json.jose.exceptions.JweException; 20 import org.forgerock.json.jose.jwe.handlers.compression.CompressionHandler; 21 import org.forgerock.json.jose.jwe.handlers.compression.DeflateCompressionHandler; 22 import org.forgerock.json.jose.jwe.handlers.compression.NOPCompressionHandler; 23 import org.forgerock.json.jose.utils.Utils; 24 import org.forgerock.util.encode.Base64url; 25 26 /** 27 * A service to get the appropriate CompressionHandler for a specified Compression algorithm. 28 * <p> 29 * For details of all supported algorithms see {@link CompressionAlgorithm}. 30 * 31 * @since 2.0.0 32 */ 33 public class CompressionManager { 34 35 /** 36 * Gets the appropriate CompressionHandler that can perform the required compression using the given 37 * compression algorithm. 38 * 39 * @param algorithm The Compression algorithm. 40 * @return The CompressionHandler. 41 */ 42 public CompressionHandler getCompressionHandler(CompressionAlgorithm algorithm) { 43 44 switch (algorithm) { 45 case NONE: { 46 return new NOPCompressionHandler(); 47 } 48 case DEF: { 49 return new DeflateCompressionHandler(); 50 } 51 default: { 52 throw new JweException("No Compression Handler for unknown compression algorithm, " 53 + algorithm + "."); 54 } 55 } 56 } 57 58 /** 59 * Convenience method equivalent to 60 * {@code Base64url.encode(getCompressionHandler(compressionAlgorithm).compress(data.getBytes(Utils.CHARSET)))}. 61 * 62 * @param compressionAlgorithm the compression algorithm to use. 63 * @param data the data to compress. 64 * @return the base64url-encoded compressed data. 65 */ 66 public String compress(CompressionAlgorithm compressionAlgorithm, String data) { 67 return Base64url.encode(getCompressionHandler(compressionAlgorithm).compress(data.getBytes(Utils.CHARSET))); 68 } 69 70 /** 71 * Convenience method equivalent to 72 * {@code getCompressionHandler(compressionAlgorithm).decompress(Base64url.decode(data))}. 73 * 74 * @param compressionAlgorithm the compression algorithm to use. 75 * @param data the base64url-encoded data to decompress. 76 * @return the decompressed data. 77 */ 78 public byte[] decompress(CompressionAlgorithm compressionAlgorithm, String data) { 79 return getCompressionHandler(compressionAlgorithm).decompress(Base64url.decode(data)); 80 } 81 }