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.rest.helper; 014 015import java.io.*; 016import java.util.*; 017 018import org.apache.juneau.http.*; 019 020/** 021 * Builder class for constructing {@link StreamResource} objects. 022 * 023 * <h5 class='section'>See Also:</h5> 024 * <ul> 025 * <li class='link'>{@doc juneau-rest-server.RestMethod.StreamResource} 026 * </ul> 027 */ 028public final class StreamResourceBuilder { 029 ArrayList<Object> contents = new ArrayList<>(); 030 MediaType mediaType; 031 Map<String,Object> headers = new LinkedHashMap<>(); 032 033 /** 034 * Specifies the resource media type string. 035 * 036 * @param mediaType The resource media type string. 037 * @return This object (for method chaining). 038 */ 039 public StreamResourceBuilder mediaType(String mediaType) { 040 this.mediaType = MediaType.forString(mediaType); 041 return this; 042 } 043 044 /** 045 * Specifies the resource media type string. 046 * 047 * @param mediaType The resource media type string. 048 * @return This object (for method chaining). 049 */ 050 public StreamResourceBuilder mediaType(MediaType mediaType) { 051 this.mediaType = mediaType; 052 return this; 053 } 054 055 /** 056 * Specifies the contents for this resource. 057 * 058 * <p> 059 * This method can be called multiple times to add more content. 060 * 061 * @param contents 062 * The resource contents. 063 * <br>If multiple contents are specified, the results will be concatenated. 064 * <br>Contents can be any of the following: 065 * <ul> 066 * <li><code><jk>byte</jk>[]</code> 067 * <li><code>InputStream</code> 068 * <li><code>Reader</code> - Converted to UTF-8 bytes. 069 * <li><code>File</code> 070 * <li><code>CharSequence</code> - Converted to UTF-8 bytes. 071 * </ul> 072 * @return This object (for method chaining). 073 */ 074 public StreamResourceBuilder contents(Object...contents) { 075 this.contents.addAll(Arrays.asList(contents)); 076 return this; 077 } 078 079 /** 080 * Specifies an HTTP response header value. 081 * 082 * @param name The HTTP header name. 083 * @param value 084 * The HTTP header value. 085 * <br>Will be converted to a <code>String</code> using {@link Object#toString()}. 086 * @return This object (for method chaining). 087 */ 088 public StreamResourceBuilder header(String name, Object value) { 089 this.headers.put(name, value); 090 return this; 091 } 092 093 /** 094 * Specifies HTTP response header values. 095 * 096 * @param headers 097 * The HTTP headers. 098 * <br>Values will be converted to <code>Strings</code> using {@link Object#toString()}. 099 * @return This object (for method chaining). 100 */ 101 public StreamResourceBuilder headers(Map<String,Object> headers) { 102 this.headers.putAll(headers); 103 return this; 104 } 105 106 /** 107 * Create a new {@link StreamResource} using values in this builder. 108 * 109 * @return A new immutable {@link StreamResource} object. 110 * @throws IOException 111 */ 112 public StreamResource build() throws IOException { 113 return new StreamResource(mediaType, headers, contents.toArray()); 114 } 115}