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.petstore.springboot;
018
019import java.time.*;
020
021import org.apache.juneau.commons.inject.*;
022import org.apache.juneau.http.response.*;
023import org.apache.juneau.marshall.html.*;
024import org.apache.juneau.marshall.serializer.*;
025import org.apache.juneau.petstore.console.*;
026import org.apache.juneau.petstore.console.data.*;
027import org.apache.juneau.petstore.rest.*;
028import org.apache.juneau.petstore.service.*;
029import org.apache.juneau.rest.server.*;
030import org.apache.juneau.rest.server.springboot.*;
031import org.apache.juneau.rest.server.widget.*;
032
033/**
034 * Root router resource for the Spring Boot deployment.
035 *
036 * <p>
037 * Extends {@link BasicSpringRestServletGroup} (i.e. {@link SpringRestServlet}) so child resources can be resolved
038 * as Spring beans — required for {@link HelloResource} to receive its {@code @Autowired} {@link HelloMessageProvider}.
039 * Mounts the deployment-agnostic {@link PetStoreResource} from {@code juneau-petstore-core} alongside the local
040 * {@link HelloResource} injection demo.
041 *
042 * <h2 class='section'>See Also:</h2><ul>
043 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauPetstore">juneau-petstore</a>
044 * </ul>
045 *
046 * @serial exclude
047 */
048@Rest(
049   title="Juneau Petstore (Spring Boot)",
050   description="Apache Juneau petstore sample application running under Spring Boot.",
051   children={
052      PetstoreConsoleResource.class,
053      ConsoleAssetsRest.class,
054      PetStoreResource.class,
055      PetstoreUiResource.class,
056      HelloResource.class
057   }
058)
059@HtmlDocConfig(
060   widgets={
061      ContentTypeMenuItem.class
062   },
063   navlinks={
064      "api: servlet:/api",
065      "stats: servlet:/stats",
066      "${W:ContentTypeMenuItem}",
067      "source: ${C:Source/gitHub}/org/apache/juneau/petstore/springboot/RootResources.java"
068   },
069   aside={
070      "<div class='text'>",
071      "\t<p>Apache Juneau petstore sample application — Spring Boot deployment.</p>",
072      "\t<p>This is the same Juneau REST surface as the Jetty deployment, deployed unchanged under Spring Boot",
073      "\t   (note: child resources resolve as Spring beans).</p>",
074      "</div>"
075   },
076   asideFloat="RIGHT"
077)
078@SerializerConfig(
079   // Single quotes for consistency with other Juneau samples — simplifies HTML/string assertions in tests.
080   quoteChar="'"
081)
082@SuppressWarnings({
083   "java:S110" // Inheritance depth acceptable for example/demo code
084})
085public class RootResources extends BasicSpringRestServletGroup {
086
087   private static final long serialVersionUID = 1L;
088
089   /**
090    * Registers the one {@link PetStore} shared by the {@code /petstore} API and the console tree, seeded with the
091    * deterministic demo data set.
092    *
093    * <p>
094    * Child resources inherit the group's beans, so a plain {@code @Bean} method is all that is needed.
095    *
096    * @return The seeded {@link PetStore}.
097    */
098   @Bean
099   public PetStore petStore() {
100      var now = Clock.systemUTC();
101      return PetstoreSeed.create().clock(now).populate(new PetStore(now));
102   }
103
104   /**
105    * {@code GET /} goes to the console store page.
106    *
107    * @param req The request.
108    * @return A 303 to {@code /console/store}.
109    */
110   @RestGet(path="/")
111   public SeeOther index(RestRequest req) {
112      return new SeeOther().setLocation(req.getUriResolver().resolve("servlet:/console/store"));
113   }
114}