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  
28  package org.apache.hc.core5.http.io.entity;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.util.Collections;
34  import java.util.List;
35  import java.util.Set;
36  
37  import org.apache.hc.core5.function.Supplier;
38  import org.apache.hc.core5.http.ContentType;
39  import org.apache.hc.core5.http.Header;
40  import org.apache.hc.core5.http.HttpEntity;
41  import org.apache.hc.core5.util.Args;
42  
43  /**
44   * Abstract base class for mutable entities. Provides the commonly used attributes for streamed and
45   * self-contained implementations.
46   *
47   * @since 4.0
48   */
49  public abstract class AbstractHttpEntity implements HttpEntity {
50  
51      static final int OUTPUT_BUFFER_SIZE = 4096;
52  
53      private final String contentType;
54      private final String contentEncoding;
55      private final boolean chunked;
56  
57      protected AbstractHttpEntity(final String contentType, final String contentEncoding, final boolean chunked) {
58          this.contentType = contentType;
59          this.contentEncoding = contentEncoding;
60          this.chunked = chunked;
61      }
62  
63      protected AbstractHttpEntity(final ContentType contentType, final String contentEncoding, final boolean chunked) {
64          this.contentType = contentType != null ? contentType.toString() : null;
65          this.contentEncoding = contentEncoding;
66          this.chunked = chunked;
67      }
68  
69      protected AbstractHttpEntity(final String contentType, final String contentEncoding) {
70          this(contentType, contentEncoding, false);
71      }
72  
73      protected AbstractHttpEntity(final ContentType contentType, final String contentEncoding) {
74          this(contentType, contentEncoding, false);
75      }
76  
77      public static void writeTo(final HttpEntity entity, final OutputStream outStream) throws IOException {
78          Args.notNull(entity, "Entity");
79          Args.notNull(outStream, "Output stream");
80          try (final InputStream inStream = entity.getContent()) {
81              if (inStream != null) {
82                  int count;
83                  final byte[] tmp = new byte[OUTPUT_BUFFER_SIZE];
84                  while ((count = inStream.read(tmp)) != -1) {
85                      outStream.write(tmp, 0, count);
86                  }
87              }
88          }
89      }
90  
91      @Override
92      public void writeTo(final OutputStream outStream) throws IOException {
93          writeTo(this, outStream);
94      }
95  
96      @Override
97      public final String getContentType() {
98          return contentType;
99      }
100 
101     @Override
102     public final String getContentEncoding() {
103         return contentEncoding;
104     }
105 
106     @Override
107     public final boolean isChunked() {
108         return chunked;
109     }
110 
111     @Override
112     public boolean isRepeatable() {
113         return false;
114     }
115 
116     @Override
117     public Supplier<List<? extends Header>> getTrailers() {
118         return null;
119     }
120 
121     @Override
122     public Set<String> getTrailerNames() {
123         return Collections.emptySet();
124     }
125 
126     @Override
127     public String toString() {
128         final StringBuilder sb = new StringBuilder();
129         sb.append("[Entity-Class: ");
130         sb.append(getClass().getSimpleName());
131         sb.append(", Content-Type: ");
132         sb.append(contentType);
133         sb.append(", Content-Encoding: ");
134         sb.append(contentEncoding);
135         sb.append(", chunked: ");
136         sb.append(chunked);
137         sb.append(']');
138         return sb.toString();
139     }
140 
141 }