View Javadoc
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.core5.http.nio.entity;
28  
29  import java.io.IOException;
30  import java.nio.ByteBuffer;
31  import java.security.MessageDigest;
32  import java.security.NoSuchAlgorithmException;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  import org.apache.hc.core5.concurrent.FutureCallback;
37  import org.apache.hc.core5.http.EntityDetails;
38  import org.apache.hc.core5.http.Header;
39  import org.apache.hc.core5.http.HttpException;
40  import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
41  import org.apache.hc.core5.http.nio.CapacityChannel;
42  import org.apache.hc.core5.util.Args;
43  
44  /**
45   * @since 5.0
46   */
47  public class DigestingEntityConsumer<T> implements AsyncEntityConsumer<T> {
48  
49      private final AsyncEntityConsumer<T> wrapped;
50      private final List<Header> trailers;
51      private final MessageDigest digester;
52  
53      private volatile byte[] digest;
54  
55      public DigestingEntityConsumer(
56              final String algo,
57              final AsyncEntityConsumer<T> wrapped) throws NoSuchAlgorithmException {
58          this.wrapped = Args.notNull(wrapped, "Entity consumer");
59          this.trailers = new ArrayList<>();
60          this.digester = MessageDigest.getInstance(algo);
61      }
62  
63      @Override
64      public void streamStart(
65              final EntityDetails entityDetails,
66              final FutureCallback<T> resultCallback) throws IOException, HttpException {
67          wrapped.streamStart(entityDetails, resultCallback);
68      }
69  
70      @Override
71      public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
72          wrapped.updateCapacity(capacityChannel);
73      }
74  
75      @Override
76      public int consume(final ByteBuffer src) throws IOException {
77          src.mark();
78          digester.update(src);
79          src.reset();
80          return wrapped.consume(src);
81      }
82  
83      @Override
84      public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
85          if (trailers != null) {
86              this.trailers.addAll(trailers);
87          }
88          digest = digester.digest();
89          wrapped.streamEnd(trailers);
90      }
91  
92      @Override
93      public void failed(final Exception cause) {
94          wrapped.failed(cause);
95      }
96  
97      @Override
98      public T getContent() {
99          return wrapped.getContent();
100     }
101 
102     @Override
103     public void releaseResources() {
104         wrapped.releaseResources();
105     }
106 
107     public List<Header> getTrailers() {
108         return trailers != null ? new ArrayList<>(trailers) : null;
109     }
110 
111     public byte[] getDigest() {
112         return digest;
113     }
114 
115 }