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