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 jakarta.servlet.http.*;
018
019import org.apache.juneau.dto.swagger.Swagger;
020import org.apache.juneau.rest.*;
021import org.apache.juneau.rest.annotation.*;
022import org.apache.juneau.rest.config.*;
023import org.apache.juneau.rest.stats.*;
024import org.apache.juneau.http.annotation.*;
025import org.apache.juneau.http.resource.*;
026import org.apache.juneau.http.response.*;
027
028/**
029 * Identical to {@link BasicRestServlet} but doesn't extend from {@link HttpServlet}.
030 *
031 * <p>
032 * Meant as a base class for child REST resources in servlet containers and Spring Boot environments.
033 *
034 * <p>
035 * Provides support for JSON, XML, HTML, URL-Encoding, UON, XML, OpenAPI, and MessagePack.  See {@link BasicUniversalConfig}
036 * for details.
037 *
038 * <p>
039 * Implements the basic REST endpoints defined in {@link BasicRestOperations}.
040 *
041 * <h5 class='section'>See Also:</h5><ul>
042 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.AnnotatedClasses">@Rest-Annotated Classes</a>
043 * </ul>
044 */
045@Rest
046public abstract class BasicRestObject extends RestObject implements BasicRestOperations, BasicUniversalConfig {
047
048   //-----------------------------------------------------------------------------------------------------------------
049   // BasicRestConfig methods
050   //-----------------------------------------------------------------------------------------------------------------
051
052   @Override /* BasicRestOperations */
053   public Swagger getSwagger(RestRequest req) {
054      return req.getSwagger().orElseThrow(NotFound::new);
055   }
056
057   @Override /* BasicRestOperations */
058   public HttpResource getHtdoc(@Path("/*") String path, Locale locale) throws NotFound {
059      return getContext().getStaticFiles().resolve(path, locale).orElseThrow(NotFound::new);
060   }
061
062   @Override /* BasicRestOperations */
063   public HttpResource getFavIcon() {
064      String favIcon = getContext().getConfig().get("REST/favicon").orElse("images/juneau.png");
065      return getHtdoc(favIcon, null);
066   }
067
068   @Override /* BasicRestOperations */
069   public void error() {}
070
071   @Override /* BasicRestOperations */
072   public RestContextStats getStats(RestRequest req) {
073      return req.getContext().getStats();
074   }
075}