|
| 1 | +/* |
| 2 | + * ==================================================================== |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + * ==================================================================== |
| 20 | + * |
| 21 | + * This software consists of voluntary contributions made by many |
| 22 | + * individuals on behalf of the Apache Software Foundation. For more |
| 23 | + * information on the Apache Software Foundation, please see |
| 24 | + * <http://www.apache.org/>. |
| 25 | + * |
| 26 | + */ |
| 27 | +package org.apache.hc.client5.http.entity; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.InputStream; |
| 31 | +import java.io.OutputStream; |
| 32 | + |
| 33 | +import org.apache.commons.compress.compressors.CompressorException; |
| 34 | +import org.apache.hc.core5.http.HttpEntity; |
| 35 | +import org.apache.hc.core5.http.io.entity.HttpEntityWrapper; |
| 36 | +import org.apache.hc.core5.util.Args; |
| 37 | + |
| 38 | + |
| 39 | +/** |
| 40 | + * An {@link HttpEntity} wrapper that applies compression to the content before writing it to |
| 41 | + * an output stream. This class supports various compression algorithms based on the |
| 42 | + * specified content encoding. |
| 43 | + * |
| 44 | + * <p>Compression is performed using {@link CompressorFactory}, which returns a corresponding |
| 45 | + * {@link OutputStream} for the requested compression type. This class does not support |
| 46 | + * reading the content directly through {@link #getContent()} as the content is always compressed |
| 47 | + * during write operations.</p> |
| 48 | + * |
| 49 | + * @since 5.5 |
| 50 | + */ |
| 51 | +public class CompressingEntity extends HttpEntityWrapper { |
| 52 | + |
| 53 | + /** |
| 54 | + * The content encoding type, e.g., "gzip", "deflate", etc. |
| 55 | + */ |
| 56 | + private final String contentEncoding; |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates a new {@link CompressingEntity} that compresses the wrapped entity's content |
| 60 | + * using the specified content encoding. |
| 61 | + * |
| 62 | + * @param entity the {@link HttpEntity} to wrap and compress; must not be {@code null}. |
| 63 | + * @param contentEncoding the content encoding to use for compression, e.g., "gzip". |
| 64 | + */ |
| 65 | + public CompressingEntity(final HttpEntity entity, final String contentEncoding) { |
| 66 | + super(entity); |
| 67 | + this.contentEncoding = Args.notNull(contentEncoding, "Content encoding"); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns the content encoding used for compression. |
| 72 | + * |
| 73 | + * @return the content encoding (e.g., "gzip", "deflate"). |
| 74 | + */ |
| 75 | + @Override |
| 76 | + public String getContentEncoding() { |
| 77 | + return contentEncoding; |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns whether the entity is chunked. This is determined by the wrapped entity. |
| 83 | + * |
| 84 | + * @return {@code true} if the entity is chunked, {@code false} otherwise. |
| 85 | + */ |
| 86 | + @Override |
| 87 | + public boolean isChunked() { |
| 88 | + return super.isChunked(); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * This method is unsupported because the content is meant to be compressed during the |
| 94 | + * {@link #writeTo(OutputStream)} operation. |
| 95 | + * |
| 96 | + * @throws UnsupportedOperationException always, as this method is not supported. |
| 97 | + */ |
| 98 | + @Override |
| 99 | + public InputStream getContent() throws IOException { |
| 100 | + throw new UnsupportedOperationException("Reading content is not supported for CompressingEntity"); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Writes the compressed content to the provided {@link OutputStream}. Compression is performed |
| 105 | + * using the content encoding provided during entity construction. |
| 106 | + * |
| 107 | + * @param outStream the {@link OutputStream} to which the compressed content will be written; must not be {@code null}. |
| 108 | + * @throws IOException if an I/O error occurs during compression or writing. |
| 109 | + * @throws UnsupportedOperationException if the specified compression type is not supported. |
| 110 | + */ |
| 111 | + @Override |
| 112 | + public void writeTo(final OutputStream outStream) throws IOException { |
| 113 | + Args.notNull(outStream, "Output stream"); |
| 114 | + |
| 115 | + // Get the compressor based on the specified content encoding |
| 116 | + final OutputStream compressorStream; |
| 117 | + try { |
| 118 | + compressorStream = CompressorFactory.INSTANCE.getCompressorOutputStream(contentEncoding, outStream); |
| 119 | + } catch (final CompressorException e) { |
| 120 | + throw new IOException("Error initializing decompression stream", e); |
| 121 | + } |
| 122 | + |
| 123 | + if (compressorStream != null) { |
| 124 | + // Write compressed data |
| 125 | + super.writeTo(compressorStream); |
| 126 | + // Close the compressor stream after writing |
| 127 | + compressorStream.close(); |
| 128 | + } else { |
| 129 | + throw new UnsupportedOperationException("Unsupported compression: " + contentEncoding); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments