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      /**
69       * Gets the content type.
70       *
71       * @return the content type.
72       */
73      public ContentType getContentType() {
74          return contentType;
75      }
76  
77      /**
78       * Gets the body as a byte array.
79       *
80       * @return the body as a byte array.
81       */
82      public byte[] getBodyBytes() {
83          if (bodyAsBytes != null) {
84              return bodyAsBytes;
85          } else if (bodyAsText != null) {
86              final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset();
87              return bodyAsText.getBytes(charset != null ? charset : StandardCharsets.US_ASCII);
88          } else {
89              return null;
90          }
91      }
92  
93      /**
94       * Gets the body as a String.
95       *
96       * @return the body as a String.
97       */
98      public String getBodyText() {
99          if (bodyAsBytes != null) {
100             final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset();
101             return new String(bodyAsBytes, charset != null ? charset : StandardCharsets.US_ASCII);
102         }
103         return bodyAsText;
104     }
105 
106     /**
107      * Tests whether the body is currently cached as a String.
108      *
109      * @return whether the body is currently cached as a String.
110      */
111     public boolean isText() {
112         return bodyAsText != null;
113     }
114 
115     /**
116      * Tests whether the body is currently cached as a byte array.
117      *
118      * @return whether the body is currently cached as a byte array.
119      */
120     public boolean isBytes() {
121         return bodyAsBytes != null;
122     }
123 
124     @Override
125     public String toString() {
126         return "SimpleBody{content length=" + (bodyAsBytes != null ? bodyAsBytes.length : "chunked") +
127                 ", content type=" + contentType + "}";
128     }
129 
130 }
131