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.examples.rest;
014
015import jakarta.servlet.*;
016import jakarta.servlet.http.*;
017
018import org.apache.juneau.annotation.*;
019import org.apache.juneau.html.annotation.*;
020import org.apache.juneau.http.annotation.*;
021import org.apache.juneau.rest.*;
022import org.apache.juneau.rest.annotation.*;
023import org.apache.juneau.rest.converter.*;
024import org.apache.juneau.rest.servlet.*;
025import org.apache.juneau.rest.widget.*;
026import org.apache.juneau.serializer.annotation.*;
027import org.apache.juneau.swaps.*;
028
029/**
030 * Sample REST resource for echoing HttpServletRequests back to the browser.
031 *
032 * <h5 class='section'>See Also:</h5><ul>
033
034 * </ul>
035 */
036@Rest(
037   path="/echo",
038   title="Request echo service",
039   description="Echos the current HttpServletRequest object back to the browser.",
040   swagger=@Swagger(
041      contact=@Contact(name="Juneau Developer",email="dev@juneau.apache.org"),
042      license=@License(name="Apache 2.0",url="http://www.apache.org/licenses/LICENSE-2.0.html"),
043      version="2.0",
044      termsOfService="You are on your own.",
045      externalDocs=@ExternalDocs(description="Apache Juneau",url="http://juneau.apache.org")
046   )
047)
048@HtmlDocConfig(
049   widgets={
050      ContentTypeMenuItem.class
051   },
052   navlinks={
053      "up: request:/..",
054      "api: servlet:/api",
055      "$W{ContentTypeMenuItem}",
056      "source: $C{Source/gitHub}/org/apache/juneau/examples/rest/RequestEchoResource.java"
057   },
058   aside={
059      "<div style='max-width:400px;min-width:200px' class='text'>",
060      "  <p>Shows how even arbitrary POJOs such as <c>HttpServletRequest</c> can be serialized by the framework.</p>",
061      "  <p>Also shows how to specify serializer properties, filters, and swaps at the servlet level to control how POJOs are serialized.</p>",
062      "  <p>Also provides an example of how to use the Traversable and Queryable APIs.</p>",
063      "</div>"
064   },
065   nowrap="false"
066)
067@BeanConfig(
068   swaps={
069      // Add a special filter for Enumerations
070      EnumerationSwap.class
071   }
072)
073@SerializerConfig(
074   maxDepth="5",
075   detectRecursions="true"
076)
077@Bean(on="HttpServletRequest",interfaceClass=HttpServletRequest.class)
078@Bean(on="HttpSession",interfaceClass=HttpSession.class)
079@Bean(on="ServletContext",interfaceClass=ServletContext.class)
080public class RequestEchoResource extends BasicRestObject {
081
082   /**
083    * [HTTP GET /echo/*]
084    * GET request handler.
085    *
086    * @param req The HTTP servlet request.
087    * @return The same request to serialize as the response.
088    */
089   @RestOp(method="*", path="/*", converters={Traversable.class,Queryable.class}, summary="Serializes the incoming HttpServletRequest object.")
090   public HttpServletRequest doGet(RestRequest req) {
091      // Just echo the request back as the response.
092      return req.getHttpServletRequest();
093   }
094}