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.common.internal.*;
023import org.apache.juneau.http.header.*;
024import org.apache.juneau.internal.*;
025
026/**
027 * A repeatable entity that obtains its content from a {@link File}.
028 *
029 * <h5 class='section'>See Also:</h5><ul>
030 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
031 * </ul>
032 */
033@FluentSetters
034public class FileEntity extends BasicHttpEntity {
035
036   //-----------------------------------------------------------------------------------------------------------------
037   // Instance
038   //-----------------------------------------------------------------------------------------------------------------
039
040   private byte[] byteCache;
041   private String stringCache;
042
043   /**
044    * Constructor.
045    */
046   public FileEntity() {
047      super();
048   }
049
050   /**
051    * Constructor.
052    *
053    * @param contentType The entity content type.
054    * @param content The entity contents.
055    */
056   public FileEntity(ContentType contentType, File content) {
057      super(contentType, content);
058   }
059
060   /**
061    * Copy constructor.
062    *
063    * @param copyFrom The bean being copied.
064    */
065   protected FileEntity(FileEntity copyFrom) {
066      super(copyFrom);
067   }
068
069   @Override
070   public FileEntity copy() {
071      return new FileEntity(this);
072   }
073
074   //-----------------------------------------------------------------------------------------------------------------
075   // Other methods
076   //-----------------------------------------------------------------------------------------------------------------
077
078   private File content() {
079      File f = contentOrElse((File)null);
080      if (f == null)
081         throw new RuntimeException("File is null.");
082      if (! f.exists())
083         throw new RuntimeException("File "+f.getAbsolutePath()+" does not exist.");
084      if (! f.canRead())
085         throw new RuntimeException("File "+f.getAbsolutePath()+" is not readable.");
086      return f;
087   }
088
089   @Override /* AbstractHttpEntity */
090   public String asString() throws IOException {
091      if (isCached() && stringCache == null)
092         stringCache = read(new InputStreamReader(new FileInputStream(content()), getCharset()), getMaxLength());
093      if (stringCache != null)
094         return stringCache;
095      return read(new InputStreamReader(new FileInputStream(content()), getCharset()), getMaxLength());
096   }
097
098   @Override /* AbstractHttpEntity */
099   public byte[] asBytes() throws IOException {
100      if (isCached() && byteCache == null)
101         byteCache = readBytes(content(), getMaxLength());
102      if (byteCache != null)
103         return byteCache;
104      return readBytes(content());
105   }
106
107   @Override /* HttpEntity */
108   public boolean isRepeatable() {
109      return true;
110   }
111
112   @Override /* HttpEntity */
113   public long getContentLength() {
114      return content().length();
115   }
116
117   @Override /* HttpEntity */
118   public InputStream getContent() throws IOException {
119      if (isCached())
120         return new ByteArrayInputStream(asBytes());
121      return new FileInputStream(content());
122   }
123
124   @Override /* HttpEntity */
125   public void writeTo(OutputStream out) throws IOException {
126      assertArgNotNull("out", out);
127
128      if (isCached()) {
129         out.write(asBytes());
130      } else {
131         try (InputStream is = getContent()) {
132            IOUtils.pipe(is, out, getMaxLength());
133         }
134      }
135   }
136
137   // <FluentSetters>
138
139   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
140   public FileEntity setCached() throws IOException{
141      super.setCached();
142      return this;
143   }
144
145   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
146   public FileEntity setCharset(Charset value) {
147      super.setCharset(value);
148      return this;
149   }
150
151   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
152   public FileEntity setChunked() {
153      super.setChunked();
154      return this;
155   }
156
157   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
158   public FileEntity setChunked(boolean value) {
159      super.setChunked(value);
160      return this;
161   }
162
163   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
164   public FileEntity setContent(Object value) {
165      super.setContent(value);
166      return this;
167   }
168
169   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
170   public FileEntity setContent(Supplier<?> value) {
171      super.setContent(value);
172      return this;
173   }
174
175   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
176   public FileEntity setContentEncoding(String value) {
177      super.setContentEncoding(value);
178      return this;
179   }
180
181   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
182   public FileEntity setContentEncoding(ContentEncoding value) {
183      super.setContentEncoding(value);
184      return this;
185   }
186
187   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
188   public FileEntity setContentLength(long value) {
189      super.setContentLength(value);
190      return this;
191   }
192
193   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
194   public FileEntity setContentType(String value) {
195      super.setContentType(value);
196      return this;
197   }
198
199   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
200   public FileEntity setContentType(ContentType value) {
201      super.setContentType(value);
202      return this;
203   }
204
205   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
206   public FileEntity setMaxLength(int value) {
207      super.setMaxLength(value);
208      return this;
209   }
210
211   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
212   public FileEntity setUnmodifiable() {
213      super.setUnmodifiable();
214      return this;
215   }
216
217   // </FluentSetters>
218}