All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juneau.rest.helper.StreamResource Maven / Gradle / Ivy

There is a newer version: 9.0.1
Show newest version
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
// * with the License.  You may obtain a copy of the License at                                                              *
// *                                                                                                                         *
// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
// *                                                                                                                         *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
// * specific language governing permissions and limitations under the License.                                              *
// ***************************************************************************************************************************
package org.apache.juneau.rest.helper;

import static org.apache.juneau.internal.CollectionUtils.*;
import static org.apache.juneau.internal.IOUtils.*;

import java.io.*;
import java.util.*;

import org.apache.juneau.*;
import org.apache.juneau.http.*;
import org.apache.juneau.http.annotation.*;

/**
 * Represents the contents of a byte stream file with convenience methods for adding HTTP response headers.
 *
 * 

*
These objects can to be returned as responses by REST methods. * *

* StreamResources are meant to be thread-safe and reusable objects. *
The contents of the request passed into the constructor are immediately converted to read-only byte arrays. * *

* Instances of this class can be built using {@link StreamResourceBuilder}. * *

See Also:
*
    *
*/ @Response public class StreamResource implements Streamable { private final MediaType mediaType; private final byte[][] contents; private final Map headers; /** * Creates a new instance of a {@link StreamResourceBuilder} * * @return A new instance of a {@link StreamResourceBuilder} */ public static StreamResourceBuilder create() { return new StreamResourceBuilder(); } /** * Constructor. * * @param mediaType The resource media type. * @param headers The HTTP response headers for this streamed resource. * @param contents * The resource contents. *
If multiple contents are specified, the results will be concatenated. *
Contents can be any of the following: *
    *
  • byte[] *
  • InputStream *
  • Reader - Converted to UTF-8 bytes. *
  • File *
  • CharSequence - Converted to UTF-8 bytes. *
* @throws IOException */ public StreamResource(MediaType mediaType, Map headers, Object...contents) throws IOException { this.mediaType = mediaType; this.headers = immutableMap(headers); this.contents = new byte[contents.length][]; for (int i = 0; i < contents.length; i++) { Object c = contents[i]; if (c == null) this.contents[i] = new byte[0]; else if (c instanceof byte[]) this.contents[i] = (byte[])c; else if (c instanceof InputStream) this.contents[i] = readBytes((InputStream)c, 1024); else if (c instanceof File) this.contents[i] = readBytes((File)c); else if (c instanceof Reader) this.contents[i] = read((Reader)c).getBytes(UTF8); else if (c instanceof CharSequence) this.contents[i] = ((CharSequence)c).toString().getBytes(UTF8); else throw new IOException("Invalid class type passed to StreamResource: " + c.getClass().getName()); } } /** * Get the HTTP response headers. * * @return * The HTTP response headers. *
An unmodifiable map. *
Never null. */ @ResponseHeader("*") public Map getHeaders() { return headers; } @ResponseBody @Override /* Streamable */ public void streamTo(OutputStream os) throws IOException { for (byte[] b : contents) os.write(b); os.flush(); } @ResponseHeader("Content-Type") @Override /* Streamable */ public MediaType getMediaType() { return mediaType; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy