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