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