serkanturgut commented on a change in pull request #153: HTTPCLIENT-1992: 
Impossible to access trailer-headers 
URL: 
https://github.com/apache/httpcomponents-client/pull/153#discussion_r293169857
 
 

 ##########
 File path: 
httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ChunkAwareResponseEntityProxy.java
 ##########
 @@ -0,0 +1,71 @@
+/*
+ * ====================================================================
+ * 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.impl.classic;
+
+import org.apache.hc.client5.http.classic.ExecRuntime;
+import org.apache.hc.core5.function.Supplier;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.Header;
+import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.impl.io.ChunkedInputStream;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+class ChunkAwareResponseEntityProxy extends ResponseEntityProxy {
+
+    private ChunkedInputStream internalChunkedInputStream;
+
+    public static void enhanceChunkedResponseSupport(final ClassicHttpResponse 
response, final ExecRuntime execRuntime) throws IOException {
+        final HttpEntity entity = response.getEntity();
+        if (!(entity.getContent() instanceof ChunkedInputStream)) {
+            throw new IllegalStateException("Underlying stream is not 
ChunkedInputStream, unable to reach footers.");
+        }
+        final ChunkedInputStream content = (ChunkedInputStream) 
entity.getContent();
+        if (entity != null && entity.isStreaming() && execRuntime != null) {
+            response.setEntity(new ChunkAwareResponseEntityProxy(entity, 
execRuntime, content));
+        }
+    }
+
+    ChunkAwareResponseEntityProxy(final HttpEntity entity, final ExecRuntime 
execRuntime, final ChunkedInputStream chunkedInputStream) {
+        super(entity, execRuntime);
+        this.internalChunkedInputStream = chunkedInputStream;
+    }
+
+    @Override
+    public Supplier<List<? extends Header>> getTrailers() {
 
 Review comment:
   I added ``getTrailers()`` method into ``ResponseEntityProxy``, removed the 
if statement in ``MainClientExec`` and did not need another static initializer 
for ``ResponseEntityProxy``.
   
   The following will be the only addition to the code which will be in 
``ResponseEntityProxy``, all other changes in this commit is eliminated now;
   
   ```
      @Override
       public Supplier<List<? extends Header>> getTrailers() {
               return new Supplier<List<? extends Header>>() {
                   @Override
                   public List<? extends Header> get() {
                       final Header[] footers;
                       InputStream underlyingStream = null;
                       try {
                           underlyingStream = getContent();
                       } catch (IOException e) {
                           throw new IllegalStateException("Unable to retrieve 
input stream");
                       }
                       if (underlyingStream instanceof ChunkedInputStream) {
                           ChunkedInputStream chunkedInputStream = 
(ChunkedInputStream) underlyingStream;
                           footers = chunkedInputStream.getFooters();
                       } else {
                           footers = new Header[0];
                       }
                       return Arrays.asList(footers);
                   }
               };
       }
   ```
   
   I can't say it looks cleaner however it reduces the amount of changes and 
need for new class considerably as you mentioned. If this looks good, I will 
change the unit tests accordingly and push the commit.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to