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 org.apache.juneau.commons.inject.*;
020import org.apache.juneau.config.*;
021import org.apache.juneau.microservice.*;
022import org.apache.juneau.microservice.jetty.*;
023import org.apache.juneau.petstore.console.*;
024
025import jakarta.servlet.*;
026
027/**
028 * Petstore Jetty/Microservice deployment entry point.
029 *
030 * <p>
031 * Locates {@code juneau-petstore-jetty.cfg} (in the working directory or on the classpath), wires Jetty via
032 * {@link JettyConfiguration}, and mounts {@link RootResources} as the root servlet.
033 *
034 * <h2 class='section'>See Also:</h2><ul>
035 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauPetstore">juneau-petstore</a>
036 * </ul>
037 */
038public class App {
039
040   /**
041    * Entry point.
042    *
043    * @param args Command-line arguments forwarded to {@link Microservice}.
044    * @throws Exception If the microservice fails to start.
045    */
046   public static void main(String[] args) throws Exception {
047      // @formatter:off
048      Microservice
049         .create()
050         .args(args)
051         .configurations(JettyConfiguration.class, AppConfig.class)
052         .build()
053         .start()
054         .startConsole()
055         .join();
056      // @formatter:on
057   }
058
059   /**
060    * Application-specific configuration class contributing the top-level REST servlet and the console, API and admin
061    * boundary filters.
062    */
063   @Configuration
064   public static class AppConfig {
065
066      /** Name of the deployment's configuration file, read from the working directory. */
067      public static final String CONFIG_NAME = "juneau-petstore-jetty.cfg";
068
069      /**
070       * Provides the deployment's configuration, named explicitly because the working directory is no longer scanned
071       * for <c>*.cfg</c> files.
072       *
073       * @return The configuration.
074       */
075      @Bean
076      public Config config() {
077         return Config.create().name(CONFIG_NAME).build();
078      }
079
080      // Shared by both boundary beans: the console publishes tokens from the instance the API filter verifies them with.
081      private final PetstoreApiBoundary apiBoundary = new PetstoreApiBoundary();
082
083      /**
084       * Provides the top-level REST servlet, auto-mounted by {@code JettyServerComponent} at
085       * {@link org.apache.juneau.rest.server.Rest#path()}.
086       *
087       * @return The root servlet.
088       */
089      @Bean
090      public Servlet rootResources() {
091         return new RootResources();
092      }
093
094      /**
095       * Provides the console's CSRF boundary, covering {@code /console} only.
096       *
097       * <p>
098       * The {@code /petstore} API is covered by {@link #apiBoundary()}; {@code /petstore-ui} is outside both: see {@link PetstoreConsoleBoundary}.
099       * It runs ahead of routing and ahead of authentication, for the request, forward, async and error dispatches;
100       * the include dispatch is deliberately not mapped.  It is built over the same {@link PetstoreApiBoundary} that
101       * {@link #apiBoundary()} mounts, so the API token its pages carry is one the API filter accepts.
102       *
103       * @return The filter registration.
104       */
105      @Bean(name="consoleBoundary")
106      public ServletFilterRegistration consoleBoundary() {
107         return ServletFilterRegistration.create(new PetstoreConsoleBoundary(apiBoundary))
108            .urlPatterns(PetstoreConsoleBoundary.URL_PATTERN)
109            .dispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC, DispatcherType.ERROR)
110            .order(ServletFilterRegistration.HIGHEST_PRECEDENCE)
111            .build();
112      }
113
114      /**
115       * Provides the API's CSRF boundary, covering {@code /petstore/*}.
116       *
117       * <p>
118       * It runs only on the original request: an internal forward or error dispatch to the API is not a new
119       * attacker-controlled request.  The order keeps it just behind {@link #consoleBoundary()}; the two patterns never
120       * overlap.
121       *
122       * @return The filter registration.
123       */
124      @Bean(name="apiBoundary")
125      public ServletFilterRegistration apiBoundary() {
126         return ServletFilterRegistration.create(apiBoundary)
127            .urlPatterns(PetstoreApiBoundary.URL_PATTERN)
128            .dispatcherTypes(DispatcherType.REQUEST)
129            .order(ServletFilterRegistration.HIGHEST_PRECEDENCE + 10)
130            .build();
131      }
132   }
133}