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