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.processor;
014
015import static org.apache.juneau.common.internal.ArgUtils.*;
016import static org.apache.juneau.internal.CollectionUtils.*;
017
018import java.util.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.cp.*;
022import org.apache.juneau.internal.*;
023
024/**
025 * A list of {@link ResponseProcessor} objects.
026 *
027 * <h5 class='section'>See Also:</h5><ul>
028 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.ResponseProcessors">Response Processors</a>
029 * </ul>
030 */
031public class ResponseProcessorList {
032
033   //-----------------------------------------------------------------------------------------------------------------
034   // Static
035   //-----------------------------------------------------------------------------------------------------------------
036
037   /**
038    * Static creator.
039    *
040    * @param beanStore The bean store to use for creating beans.
041    * @return A new builder for this object.
042    */
043   public static Builder create(BeanStore beanStore) {
044      return new Builder(beanStore);
045   }
046
047   //-----------------------------------------------------------------------------------------------------------------
048   // Builder
049   //-----------------------------------------------------------------------------------------------------------------
050
051   /**
052    * Builder class.
053    */
054   @FluentSetters
055   public static class Builder extends BeanBuilder<ResponseProcessorList> {
056
057      List<Object> entries;
058
059      /**
060       * Constructor.
061       *
062       * @param beanStore The bean store to use for creating beans.
063       */
064      protected Builder(BeanStore beanStore) {
065         super(ResponseProcessorList.class, beanStore);
066         this.entries = list();
067      }
068
069      @Override /* BeanBuilder */
070      protected ResponseProcessorList buildDefault() {
071         return new ResponseProcessorList(this);
072      }
073
074      //-------------------------------------------------------------------------------------------------------------
075      // Properties
076      //-------------------------------------------------------------------------------------------------------------
077
078      /**
079       * Appends the specified rest response processor classes to the list.
080       *
081       * @param values The values to add.
082       * @return This object.
083       * @throws IllegalArgumentException if any class does not extend from {@link ResponseProcessor}.
084       */
085      public Builder add(Class<?>...values) {
086         addAll(entries, (Object[])assertClassArrayArgIsType("values", ResponseProcessor.class, values));
087         return this;
088      }
089
090      /**
091       * Appends the specified rest response processor objects to the list.
092       *
093       * @param values The values to add.
094       * @return This object.
095       */
096      public Builder add(ResponseProcessor...values) {
097         addAll(entries, (Object[])values);
098         return this;
099      }
100
101      // <FluentSetters>
102
103      @Override /* GENERATED - org.apache.juneau.BeanBuilder */
104      public Builder impl(Object value) {
105         super.impl(value);
106         return this;
107      }
108
109      @Override /* GENERATED - org.apache.juneau.BeanBuilder */
110      public Builder type(Class<?> value) {
111         super.type(value);
112         return this;
113      }
114
115      // </FluentSetters>
116   }
117
118   //-----------------------------------------------------------------------------------------------------------------
119   // Instance
120   //-----------------------------------------------------------------------------------------------------------------
121
122   private final ResponseProcessor[] entries;
123
124   /**
125    * Constructor.
126    *
127    * @param builder The builder containing the contents for this list.
128    */
129   protected ResponseProcessorList(Builder builder) {
130      BeanStore bs = builder.beanStore();
131      entries =
132         builder
133            .entries
134            .stream()
135            .map(x -> instantiate(x, bs))
136            .toArray(ResponseProcessor[]::new);
137   }
138
139   private static ResponseProcessor instantiate(Object o, BeanStore bs) {
140      if (o instanceof ResponseProcessor)
141         return (ResponseProcessor)o;
142      try {
143         return bs.createBean(ResponseProcessor.class).type((Class<?>)o).run();
144      } catch (ExecutableException e) {
145         throw new ConfigException(e, "Could not instantiate class {0}", o);
146      }
147   }
148
149   /**
150    * Returns the entries in this list.
151    *
152    * @return The entries in this list.
153    */
154   public ResponseProcessor[] toArray() {
155      return entries;
156   }
157}