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<>(null);
71          this.exceptionRef = new AtomicReference<>(null);
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(new Runnable() {
98  
99                  @Override
100                 public void run() {
101                     try {
102                         final T result = consumeData(contentType, new ContentInputStream(buffer));
103                         resultRef.set(result);
104                         resultCallback.completed(result);
105                     } catch (final Exception ex) {
106                         buffer.abort();
107                         resultCallback.failed(ex);
108                     } finally {
109                         state.set(State.COMPLETED);
110                     }
111                 }
112 
113             });
114         }
115     }
116 
117     @Override
118     public final void consume(final ByteBuffer src) throws IOException {
119         buffer.fill(src);
120     }
121 
122     @Override
123     public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
124         buffer.markEndStream();
125     }
126 
127     @Override
128     public final void failed(final Exception cause) {
129         if (exceptionRef.compareAndSet(null, cause)) {
130             releaseResources();
131         }
132     }
133 
134     public final Exception getException() {
135         return exceptionRef.get();
136     }
137 
138     @Override
139     public final T getContent() {
140         return resultRef.get();
141     }
142 
143     @Override
144     public void releaseResources() {
145     }
146 
147 }