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.IOUtils.*; 016 017import org.apache.juneau.http.ReaderResource; 018import java.io.*; 019import java.util.*; 020 021import org.apache.juneau.http.*; 022import org.apache.juneau.http.annotation.*; 023import org.apache.juneau.svl.*; 024 025/** 026 * An extension of {@link ReaderResource} that allows automatic resolution of SVL variables. 027 */ 028public class ResolvingReaderResource extends ReaderResource { 029 030 private final VarResolverSession varSession; 031 032 /** 033 * Constructor. 034 * 035 * @param b Builder containing values to initialize this object with. 036 * @throws IOException Thrown by underlying stream. 037 */ 038 protected ResolvingReaderResource(Builder b) throws IOException { 039 this(b.mediaType, b.headers, b.cached, b.varResolver, b.contents.toArray()); 040 } 041 042 /** 043 * Constructor. 044 * 045 * @param mediaType The resource media type. 046 * @param headers The HTTP response headers for this streamed resource. 047 * @param varSession Optional variable resolver for resolving variables in the string. 048 * @param cached 049 * Identifies if this resource is cached in memory. 050 * <br>If <jk>true</jk>, the contents will be loaded into a String for fast retrieval. 051 * @param contents 052 * The resource contents. 053 * <br>If multiple contents are specified, the results will be concatenated. 054 * <br>Contents can be any of the following: 055 * <ul> 056 * <li><c>InputStream</c> 057 * <li><c>Reader</c> - Converted to UTF-8 bytes. 058 * <li><c>File</c> 059 * <li><c>CharSequence</c> - Converted to UTF-8 bytes. 060 * </ul> 061 * @throws IOException Thrown by underlying stream. 062 */ 063 public ResolvingReaderResource(MediaType mediaType, Map<String,Object> headers, boolean cached, VarResolverSession varSession, Object...contents) throws IOException { 064 super(mediaType, headers, cached, contents); 065 this.varSession = varSession; 066 } 067 068 //----------------------------------------------------------------------------------------------------------------- 069 // Builder 070 //----------------------------------------------------------------------------------------------------------------- 071 072 /** 073 * Creates a new instance of a {@link Builder} for this class. 074 * 075 * @return A new instance of a {@link Builder}. 076 */ 077 public static Builder create() { 078 return new Builder(); 079 } 080 081 /** 082 * Builder class for constructing {@link ResolvingReaderResource} objects. 083 * 084 * <ul class='seealso'> 085 * <li class='link'>{@doc juneau-rest-server.RestMethod.ReaderResource} 086 * </ul> 087 */ 088 public static class Builder extends ReaderResource.Builder { 089 VarResolverSession varResolver; 090 091 /** 092 * Specifies the variable resolver to use for this resource. 093 * 094 * @param varResolver The variable resolver. 095 * @return This object (for method chaining). 096 */ 097 public Builder varResolver(VarResolverSession varResolver) { 098 this.varResolver = varResolver; 099 return this; 100 } 101 102 @Override 103 public Builder mediaType(String mediaType) { 104 super.mediaType(mediaType); 105 return this; 106 } 107 108 @Override 109 public Builder mediaType(MediaType mediaType) { 110 super.mediaType(mediaType); 111 return this; 112 } 113 114 @Override 115 public Builder contents(Object...contents) { 116 super.contents(contents); 117 return this; 118 } 119 120 @Override 121 public Builder header(String name, Object value) { 122 super.header(name, value); 123 return this; 124 } 125 126 @Override 127 public Builder headers(Map<String,Object> headers) { 128 super.headers(headers); 129 return this; 130 } 131 132 @Override 133 public Builder cached() { 134 super.cached(); 135 return this; 136 } 137 138 /** 139 * Create a new {@link ResolvingReaderResource} using values in this builder. 140 * 141 * @return A new immutable {@link ResolvingReaderResource} object. 142 * @throws IOException Thrown by underlying stream. 143 */ 144 @Override 145 public ResolvingReaderResource build() throws IOException { 146 return new ResolvingReaderResource(this); 147 } 148 } 149 150 @ResponseBody 151 @Override /* Writeable */ 152 public Writer writeTo(Writer w) throws IOException { 153 for (Object o : contents) { 154 if (o != null) { 155 if (varSession == null) 156 pipe(o, w); 157 else 158 varSession.resolveTo(read(o), w); 159 } 160 } 161 return w; 162 } 163}