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   }
052
053   /**
054    * Constructor.
055    *
056    * @param contentType The entity content type.
057    * @param content The entity contents.
058    */
059   public StringEntity(ContentType contentType, String content) {
060      super(contentType, content);
061   }
062
063   /**
064    * Copy constructor.
065    *
066    * @param copyFrom The bean being copied.
067    */
068   protected StringEntity(StringEntity copyFrom) {
069      super(copyFrom);
070   }
071
072   @Override
073   public StringEntity copy() {
074      return new StringEntity(this);
075   }
076
077   //-----------------------------------------------------------------------------------------------------------------
078   // Other methods
079   //-----------------------------------------------------------------------------------------------------------------
080
081   private String content() {
082      return contentOrElse(EMPTY);
083   }
084
085   @Override /* AbstractHttpEntity */
086   public String asString() throws IOException {
087      return content();
088   }
089
090   @Override /* AbstractHttpEntity */
091   public byte[] asBytes() throws IOException {
092      if (isCached() && byteCache == null)
093         byteCache = content().getBytes(getCharset());
094      if (byteCache != null)
095         return byteCache;
096      return content().getBytes(getCharset());
097   }
098
099   @Override /* HttpEntity */
100   public boolean isRepeatable() {
101      return true;
102   }
103
104   @Override /* HttpEntity */
105   public long getContentLength() {
106      if (isCached())
107         return asSafeBytes().length;
108      long l = super.getContentLength();
109      if (l != -1 || isSupplied())
110         return l;
111      String s = content();
112      if (getCharset() == UTF8)
113         for (int i = 0; i < s.length(); i++)
114            if (s.charAt(i) > 127)
115               return -1;
116      return s.length();
117   }
118
119   @Override /* HttpEntity */
120   public InputStream getContent() throws IOException {
121      if (isCached())
122         return new ByteArrayInputStream(asBytes());
123      return new ReaderInputStream(new StringReader(content()), getCharset());
124   }
125
126   @Override /* HttpEntity */
127   public void writeTo(OutputStream out) throws IOException {
128      assertArgNotNull("out", out);
129      if (isCached()) {
130         out.write(asBytes());
131      } else {
132         OutputStreamWriter osw = new OutputStreamWriter(out, getCharset());
133         osw.write(content());
134         osw.flush();
135      }
136   }
137
138   @Override /* HttpEntity */
139   public boolean isStreaming() {
140      return false;
141   }
142
143   // <FluentSetters>
144
145   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
146   public StringEntity setCached() throws IOException{
147      super.setCached();
148      return this;
149   }
150
151   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
152   public StringEntity setCharset(Charset value) {
153      super.setCharset(value);
154      return this;
155   }
156
157   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
158   public StringEntity setChunked() {
159      super.setChunked();
160      return this;
161   }
162
163   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
164   public StringEntity setChunked(boolean value) {
165      super.setChunked(value);
166      return this;
167   }
168
169   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
170   public StringEntity setContent(Object value) {
171      super.setContent(value);
172      return this;
173   }
174
175   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
176   public StringEntity setContent(Supplier<?> value) {
177      super.setContent(value);
178      return this;
179   }
180
181   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
182   public StringEntity setContentEncoding(String value) {
183      super.setContentEncoding(value);
184      return this;
185   }
186
187   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
188   public StringEntity setContentEncoding(ContentEncoding value) {
189      super.setContentEncoding(value);
190      return this;
191   }
192
193   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
194   public StringEntity setContentLength(long value) {
195      super.setContentLength(value);
196      return this;
197   }
198
199   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
200   public StringEntity setContentType(String value) {
201      super.setContentType(value);
202      return this;
203   }
204
205   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
206   public StringEntity setContentType(ContentType value) {
207      super.setContentType(value);
208      return this;
209   }
210
211   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
212   public StringEntity setMaxLength(int value) {
213      super.setMaxLength(value);
214      return this;
215   }
216
217   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
218   public StringEntity setUnmodifiable() {
219      super.setUnmodifiable();
220      return this;
221   }
222
223   // </FluentSetters>
224}
225