arturobernalg commented on code in PR #580: URL: https://github.com/apache/httpcomponents-client/pull/580#discussion_r1817920989
########## httpclient5/src/main/java/org/apache/hc/client5/http/entity/compress/CompressingFactory.java: ########## @@ -0,0 +1,277 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.hc.client5.http.entity.compress; + +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; + +import org.apache.commons.compress.compressors.CompressorException; +import org.apache.commons.compress.compressors.CompressorStreamFactory; +import org.apache.commons.compress.compressors.deflate.DeflateCompressorInputStream; +import org.apache.commons.compress.compressors.deflate.DeflateParameters; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.util.Args; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A factory class for managing compression and decompression of HTTP entities using different compression formats. + * <p> + * This factory uses a cache to optimize access to available input and output stream providers for compression formats. + * It also allows the use of aliases (e.g., "gzip" and "x-gzip") and automatically formats the compression names + * to ensure consistency. + * </p> + * + * <p> + * Supported compression formats include gzip, deflate, and other available formats provided by the + * {@link CompressorStreamFactory}. + * </p> + * + * <p> + * This class is thread-safe and uses {@link AtomicReference} to cache the available input and output stream providers. + * </p> + * + * @since 5.5 + */ +public class CompressingFactory { + + private static final Logger LOG = LoggerFactory.getLogger(CompressingFactory.class); + /** + * Singleton instance of the factory. + */ + public static final CompressingFactory INSTANCE = new CompressingFactory(); + + private final CompressorStreamFactory compressorStreamFactory = new CompressorStreamFactory(); + private final AtomicReference<Set<String>> inputProvidersCache = new AtomicReference<>(); + private final AtomicReference<Set<String>> outputProvidersCache = new AtomicReference<>(); + private final Map<String, String> formattedNameCache = new ConcurrentHashMap<>(); + + /** + * Returns a set of available input stream compression providers. + * + * @return a set of available input stream compression providers in lowercase. + */ + public Set<String> getAvailableInputProviders() { + return inputProvidersCache.updateAndGet(existing -> existing != null ? existing : fetchAvailableInputProviders()); + } + + /** + * Returns a set of available output stream compression providers. + * + * @return a set of available output stream compression providers in lowercase. + */ + public Set<String> getAvailableOutputProviders() { + return outputProvidersCache.updateAndGet(existing -> existing != null ? existing : fetchAvailableOutputProviders()); + } + + /** + * Returns the formatted name of the provided compression format. + * <p> + * If the provided name matches an alias (e.g., "gzip" or "x-gzip"), the method will return the standard name. + * </p> + * + * @param name the compression format name. + * @return the formatted name, or the original name if no alias is found. + * @throws IllegalArgumentException if the name is null or empty. + */ + public String getFormattedName(final String name) { + if (name == null || name.isEmpty()) { + LOG.warn("Compression name is null or empty"); + return null; + } + final String lowerCaseName = name.toLowerCase(Locale.ROOT); + return formattedNameCache.computeIfAbsent(lowerCaseName, key -> { + if ("gzip".equals(key) || "x-gzip".equals(key)) { + return "gz"; + } else if ("compress".equals(key)) { + return "z"; + } + return key; + }); + } + + /** + * Creates an input stream for the specified compression format and decompresses the provided input stream. + * <p> + * This method uses the specified compression name to decompress the input stream and supports the "noWrap" option + * for deflate streams. + * </p> + * + * @param name the compression format. + * @param inputStream the input stream to decompress. + * @param noWrap if true, disables the zlib header and trailer for deflate streams. + * @return the decompressed input stream, or the original input stream if the format is not supported. + */ + public InputStream getCompressorInputStream(final String name, final InputStream inputStream, final boolean noWrap) throws CompressorException { + Args.notNull(inputStream, "InputStream"); + Args.notNull(name, "name"); + final String formattedName = getFormattedName(name); + return isSupported(formattedName, false) + ? createCompressorInputStream(formattedName, inputStream, noWrap) + : inputStream; Review Comment: Thanks, @garydgregory . Agreed—the Javadoc and implementation don’t match. We’ll: - Throw an exception on unsupported formats instead of returning the original stream, aligning with Javadoc expectations. - Rename to getDecompressorInputStream to reflect decompression. - Standardize to “format type” instead of “compression format” to clarify. - These changes should make both the API and docs clearer. Thanks for flagging this. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For additional commands, e-mail: dev-h...@hc.apache.org