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