001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.rest.processor;
018
019import static org.apache.juneau.common.utils.Utils.*;
020import static org.apache.juneau.internal.CollectionUtils.*;
021
022import java.util.*;
023
024import org.apache.juneau.*;
025import org.apache.juneau.common.utils.*;
026import org.apache.juneau.cp.*;
027import org.apache.juneau.internal.*;
028
029/**
030 * A list of {@link ResponseProcessor} objects.
031 *
032 * <h5 class='section'>See Also:</h5><ul>
033 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/ResponseProcessors">Response Processors</a>
034 * </ul>
035 */
036public class ResponseProcessorList {
037
038   //-----------------------------------------------------------------------------------------------------------------
039   // Static
040   //-----------------------------------------------------------------------------------------------------------------
041
042   /**
043    * Static creator.
044    *
045    * @param beanStore The bean store to use for creating beans.
046    * @return A new builder for this object.
047    */
048   public static Builder create(BeanStore beanStore) {
049      return new Builder(beanStore);
050   }
051
052   //-----------------------------------------------------------------------------------------------------------------
053   // Builder
054   //-----------------------------------------------------------------------------------------------------------------
055
056   /**
057    * Builder class.
058    */
059   public static class Builder extends BeanBuilder<ResponseProcessorList> {
060
061      List<Object> entries;
062
063      /**
064       * Constructor.
065       *
066       * @param beanStore The bean store to use for creating beans.
067       */
068      protected Builder(BeanStore beanStore) {
069         super(ResponseProcessorList.class, beanStore);
070         this.entries = list();
071      }
072
073      @Override /* BeanBuilder */
074      protected ResponseProcessorList buildDefault() {
075         return new ResponseProcessorList(this);
076      }
077
078      //-------------------------------------------------------------------------------------------------------------
079      // Properties
080      //-------------------------------------------------------------------------------------------------------------
081
082      /**
083       * Appends the specified rest response processor classes to the list.
084       *
085       * @param values The values to add.
086       * @return This object.
087       * @throws IllegalArgumentException if any class does not extend from {@link ResponseProcessor}.
088       */
089      public Builder add(Class<?>...values) {
090         addAll(entries, (Object[])Utils.assertClassArrayArgIsType("values", ResponseProcessor.class, values));
091         return this;
092      }
093
094      /**
095       * Appends the specified rest response processor objects to the list.
096       *
097       * @param values The values to add.
098       * @return This object.
099       */
100      public Builder add(ResponseProcessor...values) {
101         addAll(entries, (Object[])values);
102         return this;
103      }
104      @Override /* Overridden from BeanBuilder */
105      public Builder impl(Object value) {
106         super.impl(value);
107         return this;
108      }
109
110      @Override /* Overridden from BeanBuilder */
111      public Builder type(Class<?> value) {
112         super.type(value);
113         return this;
114      }
115   }
116
117   //-----------------------------------------------------------------------------------------------------------------
118   // Instance
119   //-----------------------------------------------------------------------------------------------------------------
120
121   private final ResponseProcessor[] entries;
122
123   /**
124    * Constructor.
125    *
126    * @param builder The builder containing the contents for this list.
127    */
128   protected ResponseProcessorList(Builder builder) {
129      BeanStore bs = builder.beanStore();
130      entries =
131         builder
132            .entries
133            .stream()
134            .map(x -> instantiate(x, bs))
135            .toArray(ResponseProcessor[]::new);
136   }
137
138   private static ResponseProcessor instantiate(Object o, BeanStore bs) {
139      if (o instanceof ResponseProcessor)
140         return (ResponseProcessor)o;
141      try {
142         return bs.createBean(ResponseProcessor.class).type((Class<?>)o).run();
143      } catch (ExecutableException e) {
144         throw new ConfigException(e, "Could not instantiate class {0}", o);
145      }
146   }
147
148   /**
149    * Returns the entries in this list.
150    *
151    * @return The entries in this list.
152    */
153   public ResponseProcessor[] toArray() {
154      return entries;
155   }
156}