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.http.*;
019import org.apache.juneau.http.header.*;
020import org.apache.juneau.http.resource.*;
021
022/**
023 * Standard predefined HTTP resources.
024 *
025 * <p>
026 * Resources are simply {@link HttpEntity} objects with arbitrary additional headers.
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 */
032public class HttpResources {
033
034   /**
035    * Creates a new {@link ByteArrayResource} builder.
036    *
037    * <p>
038    * Assumes no content type.
039    *
040    * @param content The entity content.  Can be <jk>null</jk>.
041    * @return A new {@link ByteArrayResource} builder.
042    */
043   public static final ByteArrayResource byteArrayResource(byte[] content) {
044      return new ByteArrayResource().setContent(content);
045   }
046
047   /**
048    * Creates a new {@link ByteArrayResource} builder.
049    *
050    * @param content The entity content.  Can be <jk>null</jk>.
051    * @param contentType The entity content type, or <jk>null</jk> if not specified.
052    * @return A new {@link ByteArrayResource} builder.
053    */
054   public static final ByteArrayResource byteArrayResource(byte[] content, ContentType contentType) {
055      return new ByteArrayResource().setContent(content).setContentType(contentType);
056   }
057
058   /**
059    * Creates a new {@link ByteArrayResource} builder.
060    *
061    * <p>
062    * Assumes no content type.
063    *
064    * @param content The entity content supplier.  Can be <jk>null</jk>.
065    * @return A new {@link ByteArrayResource} builder.
066    */
067   public static final ByteArrayResource byteArrayResource(Supplier<byte[]> content) {
068      return new ByteArrayResource().setContent(content);
069   }
070
071   /**
072    * Creates a new {@link ByteArrayResource} builder.
073    *
074    * @param content The entity content supplier.  Can be <jk>null</jk>.
075    * @param contentType The entity content type, or <jk>null</jk> if not specified.
076    * @return A new {@link ByteArrayResource} builder.
077    */
078   public static final ByteArrayResource byteArrayResource(Supplier<byte[]> content, ContentType contentType) {
079      return new ByteArrayResource().setContent(content).setContentType(contentType);
080   }
081
082   /**
083    * Creates a new {@link FileResource} builder.
084    *
085    * <p>
086    * Assumes no content type.
087    *
088    * @param content The entity content.  Can be <jk>null</jk>.
089    * @return A new {@link FileResource} builder.
090    */
091   public static final FileResource fileResource(File content) {
092      return new FileResource().setContent(content);
093   }
094
095   /**
096    * Creates a new {@link FileResource} builder.
097    *
098    * @param content The entity content.  Can be <jk>null</jk>.
099    * @param contentType The entity content type, or <jk>null</jk> if not specified.
100    * @return A new {@link FileResource} builder.
101    */
102   public static final FileResource fileResource(File content, ContentType contentType) {
103      return new FileResource().setContent(content).setContentType(contentType);
104   }
105
106   /**
107    * Creates a new {@link ReaderResource} builder.
108    *
109    * @param content The entity content.  Can be <jk>null</jk>.
110    * @return A new {@link ReaderResource} builder.
111    */
112   public static final ReaderResource readerResource(Reader content) {
113      return new ReaderResource().setContent(content);
114   }
115
116   /**
117    * Creates a new {@link ReaderResource} builder.
118    *
119    * @param content The entity content.  Can be <jk>null</jk>.
120    * @param contentType The entity content type, or <jk>null</jk> if not specified.
121    * @return A new {@link ReaderResource} builder.
122    */
123   public static final ReaderResource readerResource(Reader content, ContentType contentType) {
124      return new ReaderResource().setContent(content).setContentType(contentType);
125   }
126
127   /**
128    * Creates a new {@link StreamResource} builder.
129    *
130    * <p>
131    * Assumes no content type.
132    *
133    * @param content The entity content.  Can be <jk>null</jk>.
134    * @return A new {@link StreamResource} builder.
135    */
136   public static final StreamResource streamResource(InputStream content) {
137      return new StreamResource().setContent(content);
138   }
139
140   /**
141    * Creates a new {@link StreamResource} builder.
142    *
143    * @param content The entity content.  Can be <jk>null</jk>.
144    * @param contentType The entity content type, or <jk>null</jk> if not specified.
145    * @param length The content length, or <c>-1</c> if not known.
146    * @return A new {@link StreamResource} builder.
147    */
148   public static final StreamResource streamResource(InputStream content, long length, ContentType contentType) {
149      return new StreamResource().setContent(content).setContentLength(length).setContentType(contentType);
150   }
151
152   /**
153    * Creates a new builder for a {@link StringResource} builder.
154    *
155    * @param content The entity content.  Can be <jk>null</jk>.
156    * @return A new {@link StringResource} builder.
157    */
158   public static final StringResource stringResource(String content) {
159      return new StringResource().setContent(content);
160   }
161
162   /**
163    * Creates a new builder for a {@link StringResource} builder.
164    *
165    * @param content The entity content.  Can be <jk>null</jk>.
166    * @param contentType The entity content type, or <jk>null</jk> if not specified.
167    * @return A new {@link StringResource} builder.
168    */
169   public static final StringResource stringResource(String content, ContentType contentType) {
170      return new StringResource().setContent(content).setContentType(contentType);
171   }
172
173   /**
174    * Creates a new builder for a {@link StringResource} builder.
175    *
176    * @param content The entity content.  Can be <jk>null</jk>.
177    * @return A new {@link StringResource} builder.
178    */
179   public static final StringResource stringResource(Supplier<String> content) {
180      return new StringResource().setContent(content);
181   }
182
183   /**
184    * Creates a new builder for a {@link StringResource} builder.
185    *
186    * @param content The entity content.  Can be <jk>null</jk>.
187    * @param contentType The entity content type, or <jk>null</jk> if not specified.
188    * @return A new {@link StringResource} builder.
189    */
190   public static final StringResource stringResource(Supplier<String> content, ContentType contentType) {
191      return new StringResource().setContent(content).setContentType(contentType);
192   }
193}