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 java.io.*;
016import java.util.*;
017
018import org.apache.juneau.http.*;
019import org.apache.juneau.svl.*;
020
021/**
022 * Builder class for constructing {@link ReaderResource} objects.
023 *
024 * <h5 class='section'>See Also:</h5>
025 * <ul>
026 *    <li class='link'>{@doc juneau-rest-server.RestMethod.ReaderResource}
027 * </ul>
028 */
029public final class ReaderResourceBuilder {
030   ArrayList<Object> contents = new ArrayList<>();
031   MediaType mediaType;
032   VarResolverSession varResolver;
033   Map<String,Object> headers = new LinkedHashMap<>();
034
035   /**
036    * Specifies the resource media type string.
037    *
038    * @param mediaType The resource media type string.
039    * @return This object (for method chaining).
040    */
041   public ReaderResourceBuilder mediaType(String mediaType) {
042      this.mediaType = MediaType.forString(mediaType);
043      return this;
044   }
045
046   /**
047    * Specifies the resource media type string.
048    *
049    * @param mediaType The resource media type string.
050    * @return This object (for method chaining).
051    */
052   public ReaderResourceBuilder mediaType(MediaType mediaType) {
053      this.mediaType = mediaType;
054      return this;
055   }
056
057   /**
058    * Specifies the contents for this resource.
059    *
060    * <p>
061    * This method can be called multiple times to add more content.
062    *
063    * @param contents
064    *    The resource contents.
065    *    <br>If multiple contents are specified, the results will be concatenated.
066    *    <br>Contents can be any of the following:
067    *    <ul>
068    *       <li><code>InputStream</code>
069    *       <li><code>Reader</code> - Converted to UTF-8 bytes.
070    *       <li><code>File</code>
071    *       <li><code>CharSequence</code> - Converted to UTF-8 bytes.
072    *    </ul>
073    * @return This object (for method chaining).
074    */
075   public ReaderResourceBuilder contents(Object...contents) {
076      this.contents.addAll(Arrays.asList(contents));
077      return this;
078   }
079
080   /**
081    * Specifies an HTTP response header value.
082    *
083    * @param name The HTTP header name.
084    * @param value
085    *    The HTTP header value.
086    *    <br>Will be converted to a <code>String</code> using {@link Object#toString()}.
087    * @return This object (for method chaining).
088    */
089   public ReaderResourceBuilder header(String name, Object value) {
090      this.headers.put(name, value);
091      return this;
092   }
093
094   /**
095    * Specifies HTTP response header values.
096    *
097    * @param headers
098    *    The HTTP headers.
099    *    <br>Values will be converted to <code>Strings</code> using {@link Object#toString()}.
100    * @return This object (for method chaining).
101    */
102   public ReaderResourceBuilder headers(Map<String,Object> headers) {
103      this.headers.putAll(headers);
104      return this;
105   }
106
107   /**
108    * Specifies the variable resolver to use for this resource.
109    *
110    * @param varResolver The variable resolver.
111    * @return This object (for method chaining).
112    */
113   public ReaderResourceBuilder varResolver(VarResolverSession varResolver) {
114      this.varResolver = varResolver;
115      return this;
116   }
117
118   /**
119    * Create a new {@link ReaderResource} using values in this builder.
120    *
121    * @return A new immutable {@link ReaderResource} object.
122    * @throws IOException
123    */
124   public ReaderResource build() throws IOException {
125      return new ReaderResource(mediaType, headers, varResolver, contents.toArray());
126   }
127}