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