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.beans;
014
015import java.util.*;
016
017import org.apache.juneau.rest.*;
018import org.apache.juneau.rest.servlet.*;
019
020/**
021 * A POJO structure that describes the list of child resources associated with a resource.
022 *
023 * <p>
024 * Typically used in top-level GET methods of router resources to render a list of available child resources.
025 *
026 * <h5 class='section'>See Also:</h5><ul>
027 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.UtilityBeans">Utility Beans</a>
028 *    <li class='jic'>{@link BasicGroupOperations}
029
030 * </ul>
031 *
032 * @serial exclude
033 */
034public class ChildResourceDescriptions extends ResourceDescriptions {
035
036   //-----------------------------------------------------------------------------------------------------------------
037   // Static
038   //-----------------------------------------------------------------------------------------------------------------
039
040   private static final long serialVersionUID = 1L;
041
042   /**
043    * Static creator.
044    *
045    * @param req The HTTP servlet request.
046    * @return A new {@link ChildResourceDescriptions} bean.
047    */
048   public static ChildResourceDescriptions of(RestRequest req) {
049      return new ChildResourceDescriptions(req);
050   }
051
052   //-----------------------------------------------------------------------------------------------------------------
053   // Instance
054   //-----------------------------------------------------------------------------------------------------------------
055
056   /**
057    * Constructor.
058    *
059    * @param req The HTTP servlet request.
060    */
061   public ChildResourceDescriptions(RestRequest req) {
062      this(req.getContext(), req, false);
063   }
064
065   /**
066    * Constructor.
067    *
068    * @param context The servlet context that this bean describes.
069    * @param req The HTTP servlet request.
070    */
071   public ChildResourceDescriptions(RestContext context, RestRequest req) {
072      this(context, req, false);
073   }
074
075   /**
076    * Constructor.
077    *
078    * @param context The servlet context that this bean describes.
079    * @param req The HTTP servlet request.
080    * @param sort
081    *    If <jk>true</jk>, list will be ordered by name alphabetically.
082    *    Default is to maintain the order as specified in the annotation.
083    */
084   public ChildResourceDescriptions(RestContext context, RestRequest req, boolean sort) {
085      for (Map.Entry<String,RestContext> e : context.getRestChildren().asMap().entrySet()) {
086         String title = null;
087         try {
088            title = e.getValue().getSwagger(req.getLocale()).map(x -> x == null ? null : x.getInfo()).map(x -> x == null ? null : x.getTitle()).orElse(null);
089         } catch (Exception e1) {
090            title = e1.getLocalizedMessage();
091         }
092         add(new ResourceDescription(e.getKey(), title));
093      }
094      if (sort)
095         Collections.sort(this);
096   }
097
098   /**
099    * Bean constructor.
100    */
101   public ChildResourceDescriptions() {
102      super();
103   }
104}