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 static org.apache.juneau.internal.CollectionUtils.*; 016import static org.apache.juneau.internal.IOUtils.*; 017 018import java.io.*; 019import java.util.*; 020 021import org.apache.juneau.*; 022import org.apache.juneau.http.*; 023import org.apache.juneau.http.annotation.*; 024 025/** 026 * @deprecated Use {@link org.apache.juneau.http.StreamResource} 027 */ 028@Response 029@Deprecated 030public class StreamResource implements Streamable { 031 032 private final MediaType mediaType; 033 private final byte[][] contents; 034 private final Map<String,Object> headers; 035 036 /** 037 * Creates a new instance of a {@link StreamResourceBuilder} 038 * 039 * @return A new instance of a {@link StreamResourceBuilder} 040 */ 041 public static StreamResourceBuilder create() { 042 return new StreamResourceBuilder(); 043 } 044 045 /** 046 * Constructor. 047 * 048 * @param mediaType The resource media type. 049 * @param headers The HTTP response headers for this streamed resource. 050 * @param contents 051 * The resource contents. 052 * <br>If multiple contents are specified, the results will be concatenated. 053 * <br>Contents can be any of the following: 054 * <ul> 055 * <li><code><jk>byte</jk>[]</code> 056 * <li><code>InputStream</code> 057 * <li><code>Reader</code> - Converted to UTF-8 bytes. 058 * <li><code>File</code> 059 * <li><code>CharSequence</code> - Converted to UTF-8 bytes. 060 * </ul> 061 * @throws IOException 062 */ 063 public StreamResource(MediaType mediaType, Map<String,Object> headers, Object...contents) throws IOException { 064 this.mediaType = mediaType; 065 066 this.headers = immutableMap(headers); 067 068 this.contents = new byte[contents.length][]; 069 for (int i = 0; i < contents.length; i++) { 070 Object c = contents[i]; 071 if (c == null) 072 this.contents[i] = new byte[0]; 073 else if (c instanceof byte[]) 074 this.contents[i] = (byte[])c; 075 else if (c instanceof InputStream) 076 this.contents[i] = readBytes((InputStream)c, 1024); 077 else if (c instanceof File) 078 this.contents[i] = readBytes((File)c); 079 else if (c instanceof Reader) 080 this.contents[i] = read((Reader)c).getBytes(UTF8); 081 else if (c instanceof CharSequence) 082 this.contents[i] = ((CharSequence)c).toString().getBytes(UTF8); 083 else 084 throw new IOException("Invalid class type passed to StreamResource: " + c.getClass().getName()); 085 } 086 } 087 088 /** 089 * Get the HTTP response headers. 090 * 091 * @return 092 * The HTTP response headers. 093 * <br>An unmodifiable map. 094 * <br>Never <jk>null</jk>. 095 */ 096 @ResponseHeader("*") 097 public Map<String,Object> getHeaders() { 098 return headers; 099 } 100 101 @ResponseBody 102 @Override /* Streamable */ 103 public void streamTo(OutputStream os) throws IOException { 104 for (byte[] b : contents) 105 os.write(b); 106 os.flush(); 107 } 108 109 @ResponseHeader("Content-Type") 110 @Override /* Streamable */ 111 public MediaType getMediaType() { 112 return mediaType; 113 } 114}