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