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.client5.http.async.methods;
29  
30  import java.nio.charset.Charset;
31  import java.nio.charset.StandardCharsets;
32  
33  import org.apache.hc.core5.http.ContentType;
34  import org.apache.hc.core5.util.Args;
35  
36  /**
37   * Message body representation as a simple text string or an array of bytes.
38   *
39   * @since 5.0
40   */
41  public final class SimpleBody {
42  
43      private final byte[] bodyAsBytes;
44      private final String bodyAsText;
45      private final ContentType contentType;
46  
47      SimpleBody(final byte[] bodyAsBytes, final String bodyAsText, final ContentType contentType) {
48          this.bodyAsBytes = bodyAsBytes;
49          this.bodyAsText = bodyAsText;
50          this.contentType = contentType;
51      }
52  
53      static SimpleBody create(final String body, final ContentType contentType) {
54          Args.notNull(body, "Body");
55          if (body.length() > 2048) {
56              return new SimpleBody(null, body, contentType);
57          }
58          final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset();
59          final byte[] bytes = body.getBytes(charset != null ? charset : StandardCharsets.US_ASCII);
60          return new SimpleBody(bytes, null, contentType);
61      }
62  
63      static SimpleBody create(final byte[] body, final ContentType contentType) {
64          Args.notNull(body, "Body");
65          return new SimpleBody(body, null, contentType);
66      }
67  
68      public ContentType getContentType() {
69          return contentType;
70      }
71  
72      public byte[] getBodyBytes() {
73          if (bodyAsBytes != null) {
74              return bodyAsBytes;
75          } else if (bodyAsText != null) {
76              final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset();
77              return bodyAsText.getBytes(charset != null ? charset : StandardCharsets.US_ASCII);
78          } else {
79              return null;
80          }
81      }
82  
83      public String getBodyText() {
84          if (bodyAsBytes != null) {
85              final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset();
86              return new String(bodyAsBytes, charset != null ? charset : StandardCharsets.US_ASCII);
87          } else {
88              return bodyAsText;
89          }
90      }
91  
92      public boolean isText() {
93          return bodyAsText != null;
94      }
95  
96      public boolean isBytes() {
97          return bodyAsBytes != null;
98      }
99  
100     @Override
101     public String toString() {
102         return "SimpleBody{content length=" + (bodyAsBytes != null ? bodyAsBytes.length : "chunked") +
103                 ", content type=" + contentType + "}";
104     }
105 
106 }
107