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