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