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 * <h5 class='section'>See Also:</h5>
026 * <ul>
027 *    <li class='link'>{@doc juneau-rest-server.RestMethod.PredefinedHelperBeans}
028 * </ul>
029 */
030public class ChildResourceDescriptions extends ResourceDescriptions {
031
032   private static final long serialVersionUID = 1L;
033
034   /**
035    * Constructor.
036    *
037    * @param req The HTTP servlet request.
038    * @throws Exception
039    */
040   public ChildResourceDescriptions(RestRequest req) throws Exception {
041      this(req.getContext(), req, false);
042   }
043
044   /**
045    * Constructor.
046    *
047    * @param context The servlet context that this bean describes.
048    * @param req The HTTP servlet request.
049    * @throws Exception
050    */
051   public ChildResourceDescriptions(RestContext context, RestRequest req) throws Exception {
052      this(context, req, false);
053   }
054
055   /**
056    * Constructor.
057    *
058    * @param context The servlet context that this bean describes.
059    * @param req The HTTP servlet request.
060    * @param sort
061    *    If <jk>true</jk>, list will be ordered by name alphabetically.
062    *    Default is to maintain the order as specified in the annotation.
063    * @throws Exception
064    */
065   public ChildResourceDescriptions(RestContext context, RestRequest req, boolean sort) throws Exception {
066      for (Map.Entry<String,RestContext> e : context.getChildResources().entrySet())
067         add(new ResourceDescription(e.getKey(), e.getValue().getInfoProvider().getTitle(req)));
068      if (sort)
069         Collections.sort(this);
070   }
071
072   /**
073    * Bean constructor.
074    */
075   public ChildResourceDescriptions() {
076      super();
077   }
078}