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.support.classic;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.UnsupportedEncodingException;
32  import java.nio.ByteBuffer;
33  import java.nio.charset.UnsupportedCharsetException;
34  import java.util.List;
35  import java.util.concurrent.Executor;
36  import java.util.concurrent.atomic.AtomicReference;
37  
38  import org.apache.hc.core5.concurrent.FutureCallback;
39  import org.apache.hc.core5.http.ContentType;
40  import org.apache.hc.core5.http.EntityDetails;
41  import org.apache.hc.core5.http.Header;
42  import org.apache.hc.core5.http.HttpException;
43  import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
44  import org.apache.hc.core5.http.nio.CapacityChannel;
45  import org.apache.hc.core5.util.Args;
46  
47  /**
48   * {@link AsyncEntityConsumer} implementation that acts as a compatibility
49   * layer for classic {@link InputStream} based interfaces. Blocking input
50   * processing is executed through an {@link Executor}.
51   *
52   * @param <T> entity representation.
53   *
54   * @since 5.0
55   */
56  public abstract class AbstractClassicEntityConsumer<T> implements AsyncEntityConsumer<T> {
57  
58      private enum State { IDLE, ACTIVE, COMPLETED }
59  
60      private final Executor executor;
61      private final SharedInputBuffer buffer;
62      private final AtomicReference<State> state;
63      private final AtomicReference<T> resultRef;
64      private final AtomicReference<Exception> exceptionRef;
65  
66      public AbstractClassicEntityConsumer(final int initialBufferSize, final Executor executor) {
67          this.executor = Args.notNull(executor, "Executor");
68          this.buffer = new SharedInputBuffer(initialBufferSize);
69          this.state = new AtomicReference<>(State.IDLE);
70          this.resultRef = new AtomicReference<>();
71          this.exceptionRef = new AtomicReference<>();
72      }
73  
74      /**
75       * Processes entity data from the given stream.
76       *
77       * @param contentType the entity content type
78       * @param inputStream the input stream
79       * @return the result of entity processing.
80       */
81      protected abstract T consumeData(ContentType contentType, InputStream inputStream) throws IOException;
82  
83      @Override
84      public final void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
85          buffer.updateCapacity(capacityChannel);
86      }
87  
88      @Override
89      public final void streamStart(final EntityDetails entityDetails, final FutureCallback<T> resultCallback) throws HttpException, IOException {
90          final ContentType contentType;
91          try {
92              contentType = ContentType.parse(entityDetails.getContentType());
93          } catch (final UnsupportedCharsetException ex) {
94              throw new UnsupportedEncodingException(ex.getMessage());
95          }
96          if (state.compareAndSet(State.IDLE, State.ACTIVE)) {
97              executor.execute(() -> {
98                  try {
99                      final T result = consumeData(contentType, new ContentInputStream(buffer));
100                     resultRef.set(result);
101                     resultCallback.completed(result);
102                 } catch (final Exception ex) {
103                     buffer.abort();
104                     resultCallback.failed(ex);
105                 } finally {
106                     state.set(State.COMPLETED);
107                 }
108             });
109         }
110     }
111 
112     @Override
113     public final void consume(final ByteBuffer src) throws IOException {
114         buffer.fill(src);
115     }
116 
117     @Override
118     public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
119         buffer.markEndStream();
120     }
121 
122     @Override
123     public final void failed(final Exception cause) {
124         if (exceptionRef.compareAndSet(null, cause)) {
125             releaseResources();
126         }
127     }
128 
129     public final Exception getException() {
130         return exceptionRef.get();
131     }
132 
133     @Override
134     public final T getContent() {
135         return resultRef.get();
136     }
137 
138     @Override
139     public void releaseResources() {
140     }
141 
142 }