001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.servlet;
014
015import java.util.*;
016
017import org.apache.juneau.dto.swagger.Swagger;
018import org.apache.juneau.http.annotation.*;
019import org.apache.juneau.http.resource.*;
020import org.apache.juneau.http.response.*;
021import org.apache.juneau.rest.*;
022import org.apache.juneau.rest.annotation.*;
023import org.apache.juneau.rest.config.*;
024import org.apache.juneau.rest.stats.*;
025
026/**
027 * Subclass of {@link RestServlet} with default settings and standard methods defined.
028 *
029 * <p>
030 * Meant as a base class for top-level REST resources in servlet containers.
031 *
032 * <p>
033 * Provides support for JSON, XML, HTML, URL-Encoding, UON, XML, OpenAPI, and MessagePack.  See {@link BasicUniversalConfig}
034 * for details.
035 *
036 * <p>
037 * Implements the basic REST endpoints defined in {@link BasicRestOperations}.
038 *
039 * <h5 class='section'>See Also:</h5><ul>
040 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.AnnotatedClasses">@Rest-Annotated Classes</a>
041 * </ul>
042 *
043 * @serial exclude
044 */
045@Rest
046public abstract class BasicRestServlet extends RestServlet implements BasicRestOperations, BasicUniversalConfig {
047   private static final long serialVersionUID = 1L;
048
049   //-----------------------------------------------------------------------------------------------------------------
050   // BasicRestConfig methods
051   //-----------------------------------------------------------------------------------------------------------------
052
053   @Override /* BasicRestOperations */
054   public Swagger getSwagger(RestRequest req) {
055      return req.getSwagger().orElseThrow(NotFound::new);
056   }
057
058   @Override /* BasicRestOperations */
059   public HttpResource getHtdoc(@Path("/*") String path, Locale locale) throws NotFound {
060      return getContext().getStaticFiles().resolve(path, locale).orElseThrow(NotFound::new);
061   }
062
063   @Override /* BasicRestOperations */
064   public HttpResource getFavIcon() {
065      String favIcon = getContext().getConfig().get("REST/favicon").orElse("images/juneau.png");
066      return getHtdoc(favIcon, null);
067   }
068
069   @Override /* BasicRestOperations */
070   public void error() {}
071
072   @Override /* BasicRestOperations */
073   public RestContextStats getStats(RestRequest req) {
074      return req.getContext().getStats();
075   }
076}