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.convention;
018
019import org.apache.juneau.http.*;
020import org.apache.juneau.rest.server.*;
021import org.apache.juneau.rest.server.servlet.*;
022
023/**
024 * Standalone servlet companion of the {@link FaviconMixin} mixin.
025 *
026 * <p>
027 * Mounts as a <b>sibling top-level servlet</b> at {@code /favicon.ico} and serves the configured favicon
028 * bytes by delegating to a shared flavor-neutral {@link FaviconProvider} worker bean &mdash; the same worker the
029 * {@link FaviconMixin mixin} and {@link FaviconResource child} flavors use, so the forms cannot drift.
030 * Browsers fetch {@code /favicon.ico} from the site root, so a standalone servlet at that fixed path is
031 * the natural deployment for sites that do not compose the mixin into an existing host resource.
032 *
033 * <h5 class='figure'>Microservice registration:</h5>
034 *
035 * <p class='bjava'>
036 *    <ja>@Bean</ja> Servlet favicon() {
037 *       <jk>return new</jk> FaviconServlet();  <jc>// auto-mounted at /favicon.ico</jc>
038 *    }
039 * </p>
040 *
041 * <h5 class='section'>See Also:</h5><ul>
042 *    <li class='jc'>{@link FaviconMixin}
043 *    <li class='jc'>{@link FaviconProvider}
044 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerComposition">REST Server — Mixins and Multi-Mount Paths</a>
045 * </ul>
046 *
047 * @serial exclude
048 * @since 10.0.0
049 */
050// @formatter:off
051@Rest(paths="/favicon.ico")
052public class FaviconServlet extends RestServlet {
053
054   private static final long serialVersionUID = 1L;
055
056   private final transient FaviconProvider worker;
057
058   /** No-arg constructor &mdash; uses a default {@link FaviconProvider} worker. */
059   public FaviconServlet() {
060      this(new FaviconProvider());
061   }
062
063   /**
064    * Worker constructor.
065    *
066    * @param worker The shared flavor-neutral favicon worker this servlet delegates to. Must not be
067    *    {@code null}.
068    */
069   protected FaviconServlet(FaviconProvider worker) {
070      this.worker = worker;
071   }
072
073   /**
074    * [GET /*] &mdash; serve the configured favicon bytes.
075    *
076    * @return The favicon as an {@link HttpResource} with proper headers.
077    */
078   @RestGet(
079      path="/*",
080      summary="Favorites icon",
081      description="Browser favorites icon (favicon.ico).",
082      swagger=@OpSwagger(ignore=true)
083   )
084   public HttpResource getFavicon() {
085      return worker.serve();
086   }
087}