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.io.UnsupportedEncodingException;
31  import java.nio.charset.UnsupportedCharsetException;
32  
33  import org.apache.hc.core5.concurrent.FutureCallback;
34  import org.apache.hc.core5.http.ContentType;
35  import org.apache.hc.core5.http.EntityDetails;
36  import org.apache.hc.core5.http.HttpException;
37  import org.apache.hc.core5.http.config.CharCodingConfig;
38  import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
39  import org.apache.hc.core5.util.Args;
40  
41  /**
42   * Abstract text entity content consumer.
43   *
44   * @since 5.0
45   *
46   * @param <T> entity representation.
47   */
48  public abstract class AbstractCharAsyncEntityConsumer<T> extends AbstractCharDataConsumer implements AsyncEntityConsumer<T> {
49  
50      private volatile FutureCallback<T> resultCallback;
51      private volatile T content;
52  
53      protected AbstractCharAsyncEntityConsumer(final int bufSize, final CharCodingConfig charCodingConfig) {
54          super(bufSize, charCodingConfig);
55      }
56  
57      public AbstractCharAsyncEntityConsumer() {
58      }
59  
60      /**
61       * Triggered to signal beginning of entity content stream.
62       *
63       * @param contentType the entity content type
64       */
65      protected abstract void streamStart(ContentType contentType) throws HttpException, IOException;
66  
67      /**
68       * Triggered to generate entity representation.
69       *
70       * @return the entity content
71       */
72      protected abstract T generateContent() throws IOException;
73  
74      @Override
75      public final void streamStart(
76              final EntityDetails entityDetails,
77              final FutureCallback<T> resultCallback) throws IOException, HttpException {
78          Args.notNull(resultCallback, "Result callback");
79          this.resultCallback = resultCallback;
80          try {
81              final ContentType contentType = entityDetails != null ? ContentType.parse(entityDetails.getContentType()) : null;
82              setCharset(contentType != null ? contentType.getCharset() : null);
83              streamStart(contentType);
84          } catch (final UnsupportedCharsetException ex) {
85              throw new UnsupportedEncodingException(ex.getMessage());
86          }
87      }
88  
89      @Override
90      protected final void completed() throws IOException {
91          content = generateContent();
92          if (resultCallback != null) {
93              resultCallback.completed(content);
94          }
95          releaseResources();
96      }
97  
98      @Override
99      public final void failed(final Exception cause) {
100         if (resultCallback != null) {
101             resultCallback.failed(cause);
102         }
103         releaseResources();
104     }
105 
106     @Override
107     public final T getContent() {
108         return content;
109     }
110 
111 }