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.jetty;
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.servlet.*;
031import org.apache.juneau.rest.server.widget.*;
032
033/**
034 * Root router resource for the Jetty deployment.
035 *
036 * <p>
037 * Mounts the deployment-agnostic {@link PetStoreResource} and {@link PetstoreConsoleResource} (the admin console at
038 * {@code /console}) from {@code juneau-petstore-core}.
039 *
040 * <p>
041 * Also showcases the Config API + SVL variable resolver: the root page's header is driven by
042 * {@code ${C:Petstore/appName}}, which resolves against the {@code [Petstore]} section of
043 * {@code juneau-petstore-jetty.cfg} (the same {@code ${C:...}} pattern already used by the {@code source} navlink
044 * below, which resolves {@code ${C:Source/gitHub}}).  Jetty-only — Spring Boot's embedded container has no
045 * {@code .cfg}/{@code Config} equivalent.
046 *
047 * <p>
048 * Registers the one seeded {@link PetStore} shared by every child resource.
049 *
050 * <h2 class='section'>Example:</h2>
051 * <p class='bjava'>
052 *    <jc>// Children declare a field and the group's bean back-fills it.</jc>
053 *    <ja>@Bean</ja>
054 *    <jk>private transient</jk> PetStore <jf>store</jf>;
055 * </p>
056 *
057 * <h2 class='section'>See Also:</h2><ul>
058 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauPetstore">juneau-petstore</a>
059 * </ul>
060 *
061 * @serial exclude
062 */
063@Rest(
064   title="Juneau Petstore (Jetty)",
065   description="Apache Juneau petstore sample application running under Jetty/Microservice.",
066   children={
067      PetstoreConsoleResource.class,
068      ConsoleAssetsRest.class,
069      PetStoreResource.class,
070      PetstoreUiResource.class
071   }
072)
073@HtmlDocConfig(
074   widgets={
075      ContentTypeMenuItem.class
076   },
077   header={
078      "<h3>${C:Petstore/appName}</h3>"
079   },
080   navlinks={
081      "api: servlet:/api",
082      "stats: servlet:/stats",
083      "${W:ContentTypeMenuItem}",
084      "source: ${C:Source/gitHub}/org/apache/juneau/petstore/jetty/RootResources.java"
085   },
086   aside={
087      "<div class='text'>",
088      "\t<p>Apache Juneau petstore sample application.</p>",
089      "\t<p>This is a 'router' page that serves as a jumping-off point to the petstore CRUD resource and",
090      "\t   the microservice admin endpoints.</p>",
091      "\t<p>Click <span class='link'>API</span> for the generated swagger doc, or <span class='link'>STATS</span>",
092      "\t   for runtime statistics.</p>",
093      "</div>"
094   },
095   asideFloat="RIGHT"
096)
097@SerializerConfig(
098   // For consistency with other Juneau samples — single quotes simplify HTML/string assertions in tests.
099   quoteChar="'"
100)
101@SuppressWarnings({
102   "java:S110" // Inheritance depth acceptable for example/demo code
103})
104public class RootResources extends BasicRestServletGroup {
105
106   private static final long serialVersionUID = 1L;
107
108   /**
109    * Registers the one {@link PetStore} shared by the {@code /petstore} API and the console tree, seeded with the
110    * deterministic demo data set.
111    *
112    * <p>
113    * Child resources inherit the group's beans, so a plain {@code @Bean} method is all that is needed.
114    *
115    * <p>
116    * The store runs on the system clock unless the {@value PetstoreSeed#FIXED_NOW_ENV} environment variable is set, which pins
117    * "now" so the store page's day figures render the same on every run (the docs screenshots rely on this).  Its value
118    * is an ISO-8601 instant such as {@code 2026-10-01T12:00:00Z}, or {@code seed} for the seed data's own clock.
119    *
120    * @return The seeded {@link PetStore}.
121    */
122   @Bean
123   public PetStore petStore() {
124      var clock = PetstoreSeed.fixedNowClock();
125      if (clock == null) {
126         var now = Clock.systemUTC();
127         return PetstoreSeed.create().clock(now).populate(new PetStore(now));
128      }
129      return PetstoreSeed.create().clock(clock).populate(new PetStore(clock));
130   }
131
132   /**
133    * {@code GET /} goes to the console store page.
134    *
135    * @param req The request.
136    * @return A 303 to {@code /console/store}.
137    */
138   @RestGet(path="/")
139   public SeeOther index(RestRequest req) {
140      return new SeeOther().setLocation(req.getUriResolver().resolve("servlet:/console/store"));
141   }
142}