org.apache.juneau.rest.ReaderResource Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * 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;
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.internal.*;
import org.apache.juneau.rest.annotation.*;
import org.apache.juneau.rest.response.*;
import org.apache.juneau.svl.*;
/**
* Represents the contents of a text file with convenience methods for resolving {@link Parameter} variables and adding
* HTTP response headers.
*
*
* This class is handled special by the {@link WritableHandler} class.
*/
public class ReaderResource implements Writable {
private final MediaType mediaType;
private final String[] contents;
private final VarResolverSession varSession;
private final Map headers;
/**
* Constructor.
*
* @param mediaType The HTTP media type.
* @param contents
* The contents of this resource.
*
If multiple contents are specified, the results will be concatenated.
*
Contents can be any of the following:
*
* CharSequence
* Reader
* File
*
* @throws IOException
*/
protected ReaderResource(MediaType mediaType, Object...contents) throws IOException {
this(mediaType, null, null, contents);
}
/**
* Constructor.
*
* @param mediaType The resource media type.
* @param headers The HTTP response headers for this streamed resource.
* @param varSession Optional variable resolver for resolving variables in the string.
* @param contents
* The resource contents.
*
If multiple contents are specified, the results will be concatenated.
*
Contents can be any of the following:
*
* InputStream
* Reader
- Converted to UTF-8 bytes.
* File
* CharSequence
- Converted to UTF-8 bytes.
*
* @throws IOException
*/
public ReaderResource(MediaType mediaType, Map headers, VarResolverSession varSession, Object...contents) throws IOException {
this.mediaType = mediaType;
this.varSession = varSession;
Map m = new LinkedHashMap();
if (headers != null)
for (Map.Entry e : headers.entrySet())
m.put(e.getKey(), StringUtils.toString(e.getValue()));
this.headers = Collections.unmodifiableMap(m);
this.contents = new String[contents.length];
for (int i = 0; i < contents.length; i++) {
Object c = contents[i];
if (c == null)
this.contents[i] = "";
else if (c instanceof InputStream)
this.contents[i] = read((InputStream)c);
else if (c instanceof File)
this.contents[i] = read((File)c);
else if (c instanceof Reader)
this.contents[i] = read((Reader)c);
else if (c instanceof CharSequence)
this.contents[i] = ((CharSequence)c).toString();
else
throw new IOException("Invalid class type passed to ReaderResource: " + c.getClass().getName());
}
}
/**
* Builder class for constructing {@link ReaderResource} objects.
*/
public static class Builder {
ArrayList