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.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   * {@link AsyncEntityProducer} implementation that acts as a compatibility
42   * layer for classic {@link OutputStream} based interfaces. Blocking output
43   * processing is executed through an {@link Executor}.
44   *
45   * @since 5.0
46   */
47  public abstract class AbstractClassicEntityProducer implements AsyncEntityProducer {
48  
49      private enum State { IDLE, ACTIVE, COMPLETED }
50  
51      private final SharedOutputBuffer buffer;
52      private final ContentType contentType;
53      private final Executor executor;
54      private final AtomicReference<State> state;
55      private final AtomicReference<Exception> exception;
56  
57      public AbstractClassicEntityProducer(final int initialBufferSize, final ContentType contentType, final Executor executor) {
58          this.buffer = new SharedOutputBuffer(initialBufferSize);
59          this.contentType = contentType;
60          this.executor = Args.notNull(executor, "Executor");
61          this.state = new AtomicReference<>(State.IDLE);
62          this.exception = new AtomicReference<>(null);
63      }
64  
65      /**
66       * Writes out entity data into the given stream.
67       *
68       * @param contentType the entity content type
69       * @param outputStream the output stream
70       */
71      protected abstract void produceData(ContentType contentType, OutputStream outputStream) throws IOException;
72  
73      @Override
74      public final boolean isRepeatable() {
75          return false;
76      }
77  
78      @Override
79      public final int available() {
80          return buffer.length();
81      }
82  
83      @Override
84      public final void produce(final DataStreamChannel channel) throws IOException {
85          if (state.compareAndSet(State.IDLE, State.ACTIVE)) {
86              executor.execute(new Runnable() {
87  
88                  @Override
89                  public void run() {
90                      try {
91                          produceData(contentType, new ContentOutputStream(buffer));
92                          buffer.writeCompleted();
93                      } catch (final Exception ex) {
94                          buffer.abort();
95                      } finally {
96                          state.set(State.COMPLETED);
97                      }
98                  }
99  
100             });
101         }
102         buffer.flush(channel);
103     }
104 
105     @Override
106     public final long getContentLength() {
107         return -1;
108     }
109 
110     @Override
111     public final String getContentType() {
112         return contentType != null ? contentType.toString() : null;
113     }
114 
115     @Override
116     public String getContentEncoding() {
117         return null;
118     }
119 
120     @Override
121     public final boolean isChunked() {
122         return false;
123     }
124 
125     @Override
126     public final Set<String> getTrailerNames() {
127         return null;
128     }
129 
130     @Override
131     public final void failed(final Exception cause) {
132         if (exception.compareAndSet(null, cause)) {
133             releaseResources();
134         }
135     }
136 
137     public final Exception getException() {
138         return exception.get();
139     }
140 
141     @Override
142     public void releaseResources() {
143     }
144 
145 }