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; 014 015import static org.apache.juneau.internal.CollectionUtils.*; 016 017import java.util.*; 018 019import jakarta.servlet.*; 020 021import org.apache.juneau.*; 022import org.apache.juneau.cp.*; 023import org.apache.juneau.internal.*; 024import org.apache.juneau.rest.annotation.*; 025import org.apache.juneau.rest.util.*; 026 027/** 028 * Implements the child resources of a {@link Rest}-annotated class. 029 * 030 * <h5 class='section'>See Also:</h5><ul> 031 * <li class='link'><a class="doclink" href="../../../../index.html#jrs.AnnotatedClasses">@Rest-Annotated Classes</a> 032 * </ul> 033 */ 034public class RestChildren { 035 036 //----------------------------------------------------------------------------------------------------------------- 037 // Static 038 //----------------------------------------------------------------------------------------------------------------- 039 040 /** 041 * Represents a null value for the {@link Rest#restChildrenClass()} annotation. 042 */ 043 @SuppressWarnings("javadoc") 044 public final class Void extends RestChildren { 045 public Void(Builder builder) throws Exception { 046 super(builder); 047 } 048 } 049 050 /** 051 * Static creator. 052 * 053 * @param beanStore The bean store to use for creating beans. 054 * @return A new builder for this object. 055 */ 056 public static Builder create(BeanStore beanStore) { 057 return new Builder(beanStore); 058 } 059 060 //----------------------------------------------------------------------------------------------------------------- 061 // Builder 062 //----------------------------------------------------------------------------------------------------------------- 063 064 /** 065 * Builder class. 066 */ 067 @FluentSetters 068 public static class Builder extends BeanBuilder<RestChildren> { 069 070 final List<RestContext> list; 071 072 /** 073 * Constructor. 074 * 075 * @param beanStore The bean store to use for creating beans. 076 */ 077 protected Builder(BeanStore beanStore) { 078 super(RestChildren.class, beanStore); 079 list = list(); 080 } 081 082 @Override /* BeanBuilder */ 083 protected RestChildren buildDefault() { 084 return new RestChildren(this); 085 } 086 087 //------------------------------------------------------------------------------------------------------------- 088 // Properties 089 //------------------------------------------------------------------------------------------------------------- 090 091 /** 092 * Adds a child resource to this builder. 093 * 094 * @param value The REST context of the child resource. 095 * @return This object. 096 */ 097 public Builder add(RestContext value) { 098 list.add(value); 099 return this; 100 } 101 102 // <FluentSetters> 103 104 @Override /* GENERATED - org.apache.juneau.BeanBuilder */ 105 public Builder impl(Object value) { 106 super.impl(value); 107 return this; 108 } 109 110 @Override /* GENERATED - org.apache.juneau.BeanBuilder */ 111 public Builder type(Class<?> value) { 112 super.type(value); 113 return this; 114 } 115 116 // </FluentSetters> 117 } 118 119 //----------------------------------------------------------------------------------------------------------------- 120 // Instance 121 //----------------------------------------------------------------------------------------------------------------- 122 123 private final Map<String,RestContext> children = synced(map()); 124 125 /** 126 * Constructor. 127 * 128 * @param builder The builder containing the settings for this object. 129 */ 130 public RestChildren(Builder builder) { 131 for (RestContext rc : builder.list) 132 children.put(rc.getPath(), rc); 133 } 134 135 /** 136 * Looks through the registered children of this object and returns the best match. 137 * 138 * @param builder The HTTP call builder. 139 * @return The child that best matches the call, or an empty {@link Optional} if a match could not be made. 140 */ 141 public Optional<RestChildMatch> findMatch(RestSession.Builder builder) { 142 String pi = builder.getPathInfoUndecoded(); 143 if ((! children.isEmpty()) && pi != null && ! pi.equals("/")) { 144 for (RestContext rc : children.values()) { 145 UrlPathMatcher upp = rc.getPathMatcher(); 146 UrlPathMatch uppm = upp.match(builder.getUrlPath()); 147 if (uppm != null) { 148 return optional(RestChildMatch.create(uppm, rc)); 149 } 150 } 151 } 152 return empty(); 153 } 154 155 /** 156 * Returns the children in this object as a map. 157 * 158 * <p> 159 * The keys are the {@link RestContext#getPath() paths} of the child contexts. 160 * 161 * @return The children as an unmodifiable map. 162 */ 163 public Map<String,RestContext> asMap() { 164 return unmodifiable(children); 165 } 166 167 168 //----------------------------------------------------------------------------------------------------------------- 169 // Lifecycle methods. 170 //----------------------------------------------------------------------------------------------------------------- 171 172 /** 173 * Called during servlet initialization on all children to invoke all {@link RestPostInit} child-last methods. 174 * 175 * @throws ServletException Error occurred. 176 */ 177 public void postInit() throws ServletException { 178 for (RestContext childContext : children.values()) 179 childContext.postInit(); 180 } 181 182 /** 183 * Called during servlet initialization on all children to invoke all {@link RestPostInit} child-first methods. 184 * 185 * @throws ServletException Error occurred. 186 */ 187 public void postInitChildFirst() throws ServletException { 188 for (RestContext childContext : children.values()) 189 childContext.postInitChildFirst(); 190 } 191 192 /** 193 * Called during servlet destruction on all children to invoke all {@link RestDestroy} and {@link Servlet#destroy()} methods. 194 */ 195 public void destroy() { 196 for (RestContext r : children.values()) { 197 r.destroy(); 198 if (r.getResource() instanceof Servlet) 199 ((Servlet)r.getResource()).destroy(); 200 } 201 } 202}