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.OutputStream;
31  import java.util.Set;
32  import java.util.concurrent.Executor;
33  import java.util.concurrent.atomic.AtomicReference;
34  
35  import org.apache.hc.core5.http.ContentType;
36  import org.apache.hc.core5.http.nio.AsyncEntityProducer;
37  import org.apache.hc.core5.http.nio.DataStreamChannel;
38  import org.apache.hc.core5.util.Args;
39  
40  /**
41   * @since 5.0
42   */
43  public abstract class AbstractClassicEntityProducer implements AsyncEntityProducer {
44  
45      private enum State { IDLE, ACTIVE, COMPLETED }
46  
47      private final SharedOutputBuffer buffer;
48      private final ContentType contentType;
49      private final Executor executor;
50      private final AtomicReference<State> state;
51      private final AtomicReference<Exception> exception;
52  
53      public AbstractClassicEntityProducer(final int initialBufferSize, final ContentType contentType, final Executor executor) {
54          this.buffer = new SharedOutputBuffer(initialBufferSize);
55          this.contentType = contentType;
56          this.executor = Args.notNull(executor, "Executor");
57          this.state = new AtomicReference<>(State.IDLE);
58          this.exception = new AtomicReference<>(null);
59      }
60  
61      @Override
62      public final boolean isRepeatable() {
63          return false;
64      }
65  
66      protected abstract void produceData(ContentType contentType, OutputStream outputStream) throws IOException;
67  
68      @Override
69      public final int available() {
70          return buffer.length();
71      }
72  
73      @Override
74      public final void produce(final DataStreamChannel channel) throws IOException {
75          if (state.compareAndSet(State.IDLE, State.ACTIVE)) {
76              executor.execute(new Runnable() {
77  
78                  @Override
79                  public void run() {
80                      try {
81                          produceData(contentType, new ContentOutputStream(buffer));
82                          buffer.writeCompleted();
83                      } catch (final Exception ex) {
84                          buffer.abort();
85                      } finally {
86                          state.set(State.COMPLETED);
87                      }
88                  }
89  
90              });
91          }
92          buffer.flush(channel);
93      }
94  
95      @Override
96      public final long getContentLength() {
97          return -1;
98      }
99  
100     @Override
101     public final String getContentType() {
102         return contentType != null ? contentType.toString() : null;
103     }
104 
105     @Override
106     public String getContentEncoding() {
107         return null;
108     }
109 
110     @Override
111     public final boolean isChunked() {
112         return false;
113     }
114 
115     @Override
116     public final Set<String> getTrailerNames() {
117         return null;
118     }
119 
120     @Override
121     public final void failed(final Exception cause) {
122         if (exception.compareAndSet(null, cause)) {
123             releaseResources();
124         }
125     }
126 
127     public final Exception getException() {
128         return exception.get();
129     }
130 
131     @Override
132     public void releaseResources() {
133     }
134 
135 }