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;
014
015import java.io.*;
016import java.util.function.*;
017
018import org.apache.juneau.http.entity.*;
019import org.apache.juneau.http.header.*;
020import org.apache.juneau.httppart.*;
021import org.apache.juneau.serializer.*;
022
023/**
024 * Standard predefined HTTP entities.
025 *
026 * <h5 class='section'>See Also:</h5><ul>
027 *    <li class='link'><a class="doclink" href="../../../../index.html#juneau-rest-common">juneau-rest-common</a>
028 * </ul>
029 */
030public class HttpEntities {
031
032   /**
033    * Creates a new {@link ByteArrayEntity} builder.
034    *
035    * <p>
036    * Assumes no content type.
037    *
038    * @param content The entity content.  Can be <jk>null</jk>.
039    * @return A new {@link ByteArrayEntity} builder.
040    */
041   public static final ByteArrayEntity byteArrayEntity(byte[] content) {
042      return new ByteArrayEntity().setContent(content);
043   }
044
045   /**
046    * Creates a new {@link ByteArrayEntity} builder.
047    *
048    * @param content The entity content.  Can be <jk>null</jk>.
049    * @param contentType The entity content type, or <jk>null</jk> if not specified.
050    * @return A new {@link ByteArrayEntity} builder.
051    */
052   public static final ByteArrayEntity byteArrayEntity(byte[] content, ContentType contentType) {
053      return new ByteArrayEntity().setContent(content).setContentType(contentType);
054   }
055
056   /**
057    * Creates a new {@link ByteArrayEntity} builder.
058    *
059    * <p>
060    * Assumes no content type.
061    *
062    * @param content The entity content supplier.  Can be <jk>null</jk>.
063    * @return A new {@link ByteArrayEntity} builder.
064    */
065   public static final ByteArrayEntity byteArrayEntity(Supplier<byte[]> content) {
066      return new ByteArrayEntity().setContent(content);
067   }
068
069   /**
070    * Creates a new {@link ByteArrayEntity} builder.
071    *
072    * @param content The entity content supplier.  Can be <jk>null</jk>.
073    * @param contentType The entity content type, or <jk>null</jk> if not specified.
074    * @return A new {@link ByteArrayEntity} builder.
075    */
076   public static final ByteArrayEntity byteArrayEntity(Supplier<byte[]> content, ContentType contentType) {
077      return new ByteArrayEntity().setContent(content).setContentType(contentType);
078   }
079
080   /**
081    * Creates a new {@link FileEntity} builder.
082    *
083    * <p>
084    * Assumes no content type.
085    *
086    * @param content The entity content.  Can be <jk>null</jk>.
087    * @return A new {@link FileEntity} builder.
088    */
089   public static final FileEntity fileEntity(File content) {
090      return new FileEntity().setContent(content);
091   }
092
093   /**
094    * Creates a new {@link FileEntity} builder.
095    *
096    * @param content The entity content.  Can be <jk>null</jk>.
097    * @param contentType The entity content type, or <jk>null</jk> if not specified.
098    * @return A new {@link FileEntity} builder.
099    */
100   public static final FileEntity fileEntity(File content, ContentType contentType) {
101      return new FileEntity().setContent(content).setContentType(contentType);
102   }
103
104   /**
105    * Creates a new {@link ReaderEntity} builder.
106    *
107    * @param content The entity content.  Can be <jk>null</jk>.
108    * @return A new {@link ReaderEntity} builder.
109    */
110   public static final ReaderEntity readerEntity(Reader content) {
111      return new ReaderEntity().setContent(content);
112   }
113
114   /**
115    * Creates a new {@link ReaderEntity} builder.
116    *
117    * @param content The entity content.  Can be <jk>null</jk>.
118    * @param contentType The entity content type, or <jk>null</jk> if not specified.
119    * @return A new {@link ReaderEntity} builder.
120    */
121   public static final ReaderEntity readerEntity(Reader content, ContentType contentType) {
122      return new ReaderEntity().setContent(content).setContentType(contentType);
123   }
124
125   /**
126    * Creates a new {@link SerializedEntity} object.
127    *
128    * @param content
129    *    The Java POJO representing the content.
130    *    <br>Can be <jk>null</jk>.
131    * @param serializer
132    *    The serializer to use to serialize the POJO.
133    *    <br>If <jk>null</jk>, POJO will be converted to a string using {@link Object#toString()}.
134    * @return A new {@link SerializedEntity} object.
135    */
136   public static final SerializedEntity serializedEntity(Object content, Serializer serializer) {
137      return new SerializedEntity().setContent(content).setSerializer(serializer);
138   }
139
140   /**
141    * Creates a new {@link SerializedEntity} object.
142    *
143    * @param content
144    *    The supplier of a Java POJO representing the content.
145    *    <br>Can be <jk>null</jk>.
146    * @param serializer
147    *    The serializer to use to serialize the POJO.
148    *    <br>If <jk>null</jk>, POJO will be converted to a string using {@link Object#toString()}.
149    * @return A new {@link SerializedEntity} object.
150    */
151   public static final SerializedEntity serializedEntity(Supplier<?> content, Serializer serializer) {
152      return new SerializedEntity().setContent(content).setSerializer(serializer);
153   }
154
155   /**
156    * Creates a new {@link SerializedEntity} object.
157    *
158    * @param content
159    *    The Java POJO representing the content.
160    *    <br>Can be <jk>null</jk>.
161    * @param serializer
162    *    The serializer to use to serialize the POJO.
163    *    <br>If <jk>null</jk>, POJO will be converted to a string using {@link Object#toString()}.
164    * @param schema
165    *    Optional HTTP-part schema for providing instructionst to the serializer on the format of the entity.
166    * @return A new {@link SerializedEntity} object.
167    */
168   public static final SerializedEntity serializedEntity(Object content, Serializer serializer, HttpPartSchema schema) {
169      return new SerializedEntity().setContent(content).setSerializer(serializer).setSchema(schema);
170   }
171
172   /**
173    * Creates a new {@link SerializedEntity} object.
174    *
175    * @param content
176    *    The supplier of a Java POJO representing the content.
177    *    <br>Can be <jk>null</jk>.
178    * @param serializer
179    *    The serializer to use to serialize the POJO.
180    *    <br>If <jk>null</jk>, POJO will be converted to a string using {@link Object#toString()}.
181    * @param schema
182    *    Optional HTTP-part schema for providing instructionst to the serializer on the format of the entity.
183    * @return A new {@link SerializedEntity} object.
184    */
185   public static final SerializedEntity serializedEntity(Supplier<?> content, Serializer serializer, HttpPartSchema schema) {
186      return new SerializedEntity().setContent(content).setSerializer(serializer).setSchema(schema);
187   }
188
189   /**
190    * Creates a new {@link StreamEntity} builder.
191    *
192    * <p>
193    * Assumes no content type.
194    *
195    * @param content The entity content.  Can be <jk>null</jk>.
196    * @return A new {@link StreamEntity} builder.
197    */
198   public static final StreamEntity streamEntity(InputStream content) {
199      return new StreamEntity().setContent(content);
200   }
201
202   /**
203    * Creates a new {@link StreamEntity} builder.
204    *
205    * @param content The entity content.  Can be <jk>null</jk>.
206    * @param contentType The entity content type, or <jk>null</jk> if not specified.
207    * @param length The content length, or <c>-1</c> if not known.
208    * @return A new {@link StreamEntity} builder.
209    */
210   public static final StreamEntity streamEntity(InputStream content, long length, ContentType contentType) {
211      return new StreamEntity().setContent(content).setContentLength(length).setContentType(contentType);
212   }
213
214   /**
215    * Creates a new builder for a {@link StringEntity} builder.
216    *
217    * @param content The entity content.  Can be <jk>null</jk>.
218    * @return A new {@link StringEntity} builder.
219    */
220   public static final StringEntity stringEntity(String content) {
221      return new StringEntity().setContent(content);
222   }
223
224   /**
225    * Creates a new builder for a {@link StringEntity} builder.
226    *
227    * @param content The entity content.  Can be <jk>null</jk>.
228    * @param contentType The entity content type, or <jk>null</jk> if not specified.
229    * @return A new {@link StringEntity} builder.
230    */
231   public static final StringEntity stringEntity(String content, ContentType contentType) {
232      return new StringEntity().setContent(content).setContentType(contentType);
233   }
234
235   /**
236    * Creates a new builder for a {@link StringEntity} builder.
237    *
238    * @param content The entity content.  Can be <jk>null</jk>.
239    * @return A new {@link StringEntity} builder.
240    */
241   public static final StringEntity stringEntity(Supplier<String> content) {
242      return new StringEntity().setContent(content);
243   }
244
245   /**
246    * Creates a new builder for a {@link StringEntity} builder.
247    *
248    * @param content The entity content.  Can be <jk>null</jk>.
249    * @param contentType The entity content type, or <jk>null</jk> if not specified.
250    * @return A new {@link StringEntity} builder.
251    */
252   public static final StringEntity stringEntity(Supplier<String> content, ContentType contentType) {
253      return new StringEntity().setContent(content).setContentType(contentType);
254   }
255}