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