001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.http.classic;
018
019import java.io.*;
020import java.util.function.*;
021
022import org.apache.http.*;
023import org.apache.juneau.http.classic.header.*;
024import org.apache.juneau.http.classic.resource.*;
025
026/**
027 * Standard predefined HTTP resources.
028 *
029 * <p>
030 * Resources are simply {@link HttpEntity} objects with arbitrary additional headers.
031 *
032 * <h5 class='section'>See Also:</h5><ul>
033 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommon">juneau-rest-common</a>
034 * </ul>
035 */
036public class HttpResources {
037
038   /**
039    * Prevents instantiation.
040    */
041   private HttpResources() {}
042
043   /**
044    * Creates a new {@link ByteArrayResource} builder.
045    *
046    * <p>
047    * Assumes no content type.
048    *
049    * @param content The entity content.  Can be <jk>null</jk> (treated as an empty byte array).
050    * @return A new {@link ByteArrayResource} builder.
051    */
052   public static final ByteArrayResource byteArrayResource(byte[] content) {
053      return new ByteArrayResource().setContent(content);
054   }
055
056   /**
057    * Creates a new {@link ByteArrayResource} builder.
058    *
059    * @param content The entity content.  Can be <jk>null</jk> (treated as an empty byte array).
060    * @param contentType The entity content type, or <jk>null</jk> if not specified.
061    * @return A new {@link ByteArrayResource} builder.
062    */
063   public static final ByteArrayResource byteArrayResource(byte[] content, ContentType contentType) {
064      return new ByteArrayResource().setContent(content).setContentType(contentType);
065   }
066
067   /**
068    * Creates a new {@link ByteArrayResource} builder.
069    *
070    * <p>
071    * Assumes no content type.
072    *
073    * @param content The entity content supplier.  Can be <jk>null</jk> (treated as an empty byte array).
074    * @return A new {@link ByteArrayResource} builder.
075    */
076   public static final ByteArrayResource byteArrayResource(Supplier<byte[]> content) {
077      return new ByteArrayResource().setContent(content);
078   }
079
080   /**
081    * Creates a new {@link ByteArrayResource} builder.
082    *
083    * @param content The entity content supplier.  Can be <jk>null</jk> (treated as an empty byte array).
084    * @param contentType The entity content type, or <jk>null</jk> if not specified.
085    * @return A new {@link ByteArrayResource} builder.
086    */
087   public static final ByteArrayResource byteArrayResource(Supplier<byte[]> content, ContentType contentType) {
088      return new ByteArrayResource().setContent(content).setContentType(contentType);
089   }
090
091   /**
092    * Creates a new {@link FileResource} builder.
093    *
094    * <p>
095    * Assumes no content type.
096    *
097    * @param content The entity content.  Can be <jk>null</jk>, in which case a {@link NullPointerException} is thrown when the resource's content is read.
098    * @return A new {@link FileResource} builder.
099    */
100   public static final FileResource fileResource(File content) {
101      return new FileResource().setContent(content);
102   }
103
104   /**
105    * Creates a new {@link FileResource} builder.
106    *
107    * @param content The entity content.  Can be <jk>null</jk>, in which case a {@link NullPointerException} is thrown when the resource's content is read.
108    * @param contentType The entity content type, or <jk>null</jk> if not specified.
109    * @return A new {@link FileResource} builder.
110    */
111   public static final FileResource fileResource(File content, ContentType contentType) {
112      return new FileResource().setContent(content).setContentType(contentType);
113   }
114
115   /**
116    * Creates a new {@link ReaderResource} builder.
117    *
118    * @param content The entity content.  Can be <jk>null</jk>, in which case a {@link NullPointerException} is thrown when the resource's content is read.
119    * @return A new {@link ReaderResource} builder.
120    */
121   public static final ReaderResource readerResource(Reader content) {
122      return new ReaderResource().setContent(content);
123   }
124
125   /**
126    * Creates a new {@link ReaderResource} builder.
127    *
128    * @param content The entity content.  Can be <jk>null</jk>, in which case a {@link NullPointerException} is thrown when the resource's content is read.
129    * @param contentType The entity content type, or <jk>null</jk> if not specified.
130    * @return A new {@link ReaderResource} builder.
131    */
132   public static final ReaderResource readerResource(Reader content, ContentType contentType) {
133      return new ReaderResource().setContent(content).setContentType(contentType);
134   }
135
136   /**
137    * Creates a new {@link StreamResource} builder.
138    *
139    * <p>
140    * Assumes no content type.
141    *
142    * @param content The entity content.  Can be <jk>null</jk>, in which case a {@link NullPointerException} is thrown when the resource's content is read.
143    * @return A new {@link StreamResource} builder.
144    */
145   public static final StreamResource streamResource(InputStream content) {
146      return new StreamResource().setContent(content);
147   }
148
149   /**
150    * Creates a new {@link StreamResource} builder.
151    *
152    * @param content The entity content.  Can be <jk>null</jk>, in which case a {@link NullPointerException} is thrown when the resource's content is read.
153    * @param contentType The entity content type, or <jk>null</jk> if not specified.
154    * @param length The content length, or <c>-1</c> if not known.
155    * @return A new {@link StreamResource} builder.
156    */
157   public static final StreamResource streamResource(InputStream content, long length, ContentType contentType) {
158      return new StreamResource().setContent(content).setContentLength(length).setContentType(contentType);
159   }
160
161   /**
162    * Creates a new builder for a {@link StringResource} builder.
163    *
164    * @param content The entity content.  Can be <jk>null</jk> (treated as an empty string).
165    * @return A new {@link StringResource} builder.
166    */
167   public static final StringResource stringResource(String content) {
168      return new StringResource().setContent(content);
169   }
170
171   /**
172    * Creates a new builder for a {@link StringResource} builder.
173    *
174    * @param content The entity content.  Can be <jk>null</jk> (treated as an empty string).
175    * @param contentType The entity content type, or <jk>null</jk> if not specified.
176    * @return A new {@link StringResource} builder.
177    */
178   public static final StringResource stringResource(String content, ContentType contentType) {
179      return new StringResource().setContent(content).setContentType(contentType);
180   }
181
182   /**
183    * Creates a new builder for a {@link StringResource} builder.
184    *
185    * @param content The entity content.  Can be <jk>null</jk> (treated as an empty string).
186    * @return A new {@link StringResource} builder.
187    */
188   public static final StringResource stringResource(Supplier<String> content) {
189      return new StringResource().setContent(content);
190   }
191
192   /**
193    * Creates a new builder for a {@link StringResource} builder.
194    *
195    * @param content The entity content.  Can be <jk>null</jk> (treated as an empty string).
196    * @param contentType The entity content type, or <jk>null</jk> if not specified.
197    * @return A new {@link StringResource} builder.
198    */
199   public static final StringResource stringResource(Supplier<String> content, ContentType contentType) {
200      return new StringResource().setContent(content).setContentType(contentType);
201   }
202}