001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.http.entity;
014
015import static org.apache.juneau.common.internal.ArgUtils.*;
016import static org.apache.juneau.common.internal.IOUtils.*;
017
018import java.io.*;
019import java.nio.charset.*;
020import java.util.function.*;
021
022import org.apache.juneau.http.header.*;
023import org.apache.juneau.internal.*;
024
025/**
026 * A self contained, repeatable entity that obtains its content from a {@link String}.
027 *
028 * <h5 class='section'>See Also:</h5><ul>
029 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
030 * </ul>
031 */
032@FluentSetters
033public class StringEntity extends BasicHttpEntity {
034
035   //-----------------------------------------------------------------------------------------------------------------
036   // Static
037   //-----------------------------------------------------------------------------------------------------------------
038
039   private static final String EMPTY = "";
040
041   //-----------------------------------------------------------------------------------------------------------------
042   // Instance
043   //-----------------------------------------------------------------------------------------------------------------
044
045   private byte[] byteCache;
046
047   /**
048    * Constructor.
049    */
050   public StringEntity() {
051      super();
052   }
053
054   /**
055    * Constructor.
056    *
057    * @param contentType The entity content type.
058    * @param content The entity contents.
059    */
060   public StringEntity(ContentType contentType, String content) {
061      super(contentType, content);
062   }
063
064   /**
065    * Copy constructor.
066    *
067    * @param copyFrom The bean being copied.
068    */
069   protected StringEntity(StringEntity copyFrom) {
070      super(copyFrom);
071   }
072
073   @Override
074   public StringEntity copy() {
075      return new StringEntity(this);
076   }
077
078   //-----------------------------------------------------------------------------------------------------------------
079   // Other methods
080   //-----------------------------------------------------------------------------------------------------------------
081
082   private String content() {
083      return contentOrElse(EMPTY);
084   }
085
086   @Override /* AbstractHttpEntity */
087   public String asString() throws IOException {
088      return content();
089   }
090
091   @Override /* AbstractHttpEntity */
092   public byte[] asBytes() throws IOException {
093      if (isCached() && byteCache == null)
094         byteCache = content().getBytes(getCharset());
095      if (byteCache != null)
096         return byteCache;
097      return content().getBytes(getCharset());
098   }
099
100   @Override /* HttpEntity */
101   public boolean isRepeatable() {
102      return true;
103   }
104
105   @Override /* HttpEntity */
106   public long getContentLength() {
107      if (isCached())
108         return asSafeBytes().length;
109      long l = super.getContentLength();
110      if (l != -1 || isSupplied())
111         return l;
112      String s = content();
113      if (getCharset() == UTF8)
114         for (int i = 0; i < s.length(); i++)
115            if (s.charAt(i) > 127)
116               return -1;
117      return s.length();
118   }
119
120   @Override /* HttpEntity */
121   public InputStream getContent() throws IOException {
122      if (isCached())
123         return new ByteArrayInputStream(asBytes());
124      return new ReaderInputStream(new StringReader(content()), getCharset());
125   }
126
127   @Override /* HttpEntity */
128   public void writeTo(OutputStream out) throws IOException {
129      assertArgNotNull("out", out);
130      if (isCached()) {
131         out.write(asBytes());
132      } else {
133         OutputStreamWriter osw = new OutputStreamWriter(out, getCharset());
134         osw.write(content());
135         osw.flush();
136      }
137   }
138
139   @Override /* HttpEntity */
140   public boolean isStreaming() {
141      return false;
142   }
143
144   // <FluentSetters>
145
146   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
147   public StringEntity setCached() throws IOException{
148      super.setCached();
149      return this;
150   }
151
152   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
153   public StringEntity setCharset(Charset value) {
154      super.setCharset(value);
155      return this;
156   }
157
158   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
159   public StringEntity setChunked() {
160      super.setChunked();
161      return this;
162   }
163
164   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
165   public StringEntity setChunked(boolean value) {
166      super.setChunked(value);
167      return this;
168   }
169
170   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
171   public StringEntity setContent(Object value) {
172      super.setContent(value);
173      return this;
174   }
175
176   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
177   public StringEntity setContent(Supplier<?> value) {
178      super.setContent(value);
179      return this;
180   }
181
182   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
183   public StringEntity setContentEncoding(String value) {
184      super.setContentEncoding(value);
185      return this;
186   }
187
188   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
189   public StringEntity setContentEncoding(ContentEncoding value) {
190      super.setContentEncoding(value);
191      return this;
192   }
193
194   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
195   public StringEntity setContentLength(long value) {
196      super.setContentLength(value);
197      return this;
198   }
199
200   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
201   public StringEntity setContentType(String value) {
202      super.setContentType(value);
203      return this;
204   }
205
206   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
207   public StringEntity setContentType(ContentType value) {
208      super.setContentType(value);
209      return this;
210   }
211
212   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
213   public StringEntity setMaxLength(int value) {
214      super.setMaxLength(value);
215      return this;
216   }
217
218   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
219   public StringEntity setUnmodifiable() {
220      super.setUnmodifiable();
221      return this;
222   }
223
224   // </FluentSetters>
225}
226