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