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.helper;
014
015import java.util.*;
016
017import org.apache.juneau.rest.*;
018
019/**
020 * A POJO structure that describes the list of child resources associated with a resource.
021 *
022 * <p>
023 * Typically used in top-level GET methods of router resources to render a list of available child resources.
024 *
025 * <ul class='seealso'>
026 *    <li class='link'>{@doc juneau-rest-server.RestMethod.PredefinedHelperBeans}
027 * </ul>
028 */
029public class ChildResourceDescriptions extends ResourceDescriptions {
030
031   private static final long serialVersionUID = 1L;
032
033   /**
034    * Constructor.
035    *
036    * @param req The HTTP servlet request.
037    */
038   public ChildResourceDescriptions(RestRequest req) {
039      this(req.getContext(), req, false);
040   }
041
042   /**
043    * Constructor.
044    *
045    * @param context The servlet context that this bean describes.
046    * @param req The HTTP servlet request.
047    */
048   public ChildResourceDescriptions(RestContext context, RestRequest req) {
049      this(context, req, false);
050   }
051
052   /**
053    * Constructor.
054    *
055    * @param context The servlet context that this bean describes.
056    * @param req The HTTP servlet request.
057    * @param sort
058    *    If <jk>true</jk>, list will be ordered by name alphabetically.
059    *    Default is to maintain the order as specified in the annotation.
060    */
061   public ChildResourceDescriptions(RestContext context, RestRequest req, boolean sort) {
062      for (Map.Entry<String,RestContext> e : context.getChildResources().entrySet()) {
063         String title = null;
064         try {
065            title = e.getValue().getInfoProvider().getTitle(req);
066         } catch (Exception e1) {
067            title = e1.getLocalizedMessage();
068         }
069         add(new ResourceDescription(e.getKey(), title));
070      }
071      if (sort)
072         Collections.sort(this);
073   }
074
075   /**
076    * Bean constructor.
077    */
078   public ChildResourceDescriptions() {
079      super();
080   }
081}