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 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><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 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 * <h5 class='section'>See Also:</h5> 085 * <ul> 086 * <li class='link'>{@doc juneau-rest-server.RestMethod.ReaderResource} 087 * </ul> 088 */ 089 public static class Builder extends ReaderResource.Builder { 090 VarResolverSession varResolver; 091 092 /** 093 * Specifies the variable resolver to use for this resource. 094 * 095 * @param varResolver The variable resolver. 096 * @return This object (for method chaining). 097 */ 098 public Builder varResolver(VarResolverSession varResolver) { 099 this.varResolver = varResolver; 100 return this; 101 } 102 103 @Override 104 public Builder mediaType(String mediaType) { 105 super.mediaType(mediaType); 106 return this; 107 } 108 109 @Override 110 public Builder mediaType(MediaType mediaType) { 111 super.mediaType(mediaType); 112 return this; 113 } 114 115 @Override 116 public Builder contents(Object...contents) { 117 super.contents(contents); 118 return this; 119 } 120 121 @Override 122 public Builder header(String name, Object value) { 123 super.header(name, value); 124 return this; 125 } 126 127 @Override 128 public Builder headers(Map<String,Object> headers) { 129 super.headers(headers); 130 return this; 131 } 132 133 @Override 134 public Builder cached() { 135 super.cached(); 136 return this; 137 } 138 139 /** 140 * Create a new {@link ResolvingReaderResource} using values in this builder. 141 * 142 * @return A new immutable {@link ResolvingReaderResource} object. 143 * @throws IOException 144 */ 145 @Override 146 public ResolvingReaderResource build() throws IOException { 147 return new ResolvingReaderResource(this); 148 } 149 } 150 151 @ResponseBody 152 @Override /* Writeable */ 153 public Writer writeTo(Writer w) throws IOException { 154 for (Object o : contents) { 155 if (o != null) { 156 if (varSession == null) 157 pipe(o, w); 158 else 159 varSession.resolveTo(read(o), w); 160 } 161 } 162 return w; 163 } 164}