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.server.springboot;
018
019import java.util.*;
020
021import org.apache.juneau.commons.inject.*;
022import org.apache.juneau.rest.server.servlet.*;
023import org.springframework.beans.factory.annotation.*;
024import org.springframework.context.*;
025
026/**
027 * Subclass of a {@link RestServlet} that hooks into Spring Boot for using Spring Beans.
028 *
029 * <h5 class='section'>Notes:</h5><ul>
030 *    <li class='note'>
031 *       Users will typically extend from {@link BasicSpringRestServlet} or {@link BasicSpringRestServletGroup}
032 *       instead of this class directly.
033 * </ul>
034 *
035 * <h5 class='section'>See Also:</h5><ul>
036 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestServerSpringboot">juneau-rest-server-springboot</a>
037 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestAnnotatedClasses">@Rest-Annotated Class</a>
038 * </ul>
039 *
040 * @serial exclude
041 */
042public abstract class SpringRestServlet extends RestServlet {
043
044   private static final long serialVersionUID = 1L;
045
046   @Autowired
047   @SuppressWarnings({
048      "java:S6813" // Field injection is structurally required: this abstract base servlet is extended by user subclasses that declare no constructors; Spring instantiates them via the no-arg constructor and injects this transient field afterward. Constructor injection would force every subclass to thread ApplicationContext through a boilerplate super(...) call.
049   })
050   private transient Optional<ApplicationContext> appContext;
051
052   /**
053    * Hook into Spring bean injection framework.
054    *
055    *
056    * <p>
057    * If no {@link ApplicationContext} was injected (e.g. the servlet was created with <c>new</c> outside of Spring,
058    * or the injected {@link Optional} is empty), the returned store degrades gracefully to one with no Spring context
059    * that only resolves beans from the parent chain.
060    *
061    * @param parent Optional parent resource bean store, used as a fallback after Spring's context.
062    * @return A {@link WritableBeanStore} backed by Spring's {@link ApplicationContext}.
063    */
064   @Bean
065   @SuppressWarnings({
066      "resource" // Spring takes ownership of the returned bean and manages its lifecycle
067   })
068   public WritableBeanStore createBeanStore(Optional<BeanStore> parent) {
069      return new SpringBeanStore(Objects.requireNonNullElse(appContext, Optional.<ApplicationContext>empty()).orElse(null), parent.orElse(null));
070   }
071}