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      "$W{ContentTypeMenuItem}",
056      "$W{ThemeMenuItem}",
057      "source: $C{Source/gitHub}/org/apache/juneau/examples/rest/$R{servletClassSimple}.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   maxDepth="5",
070   detectRecursions="true",
071   beanFilters={
072      // Interpret these as their parent classes, not subclasses
073      HttpServletRequest.class, HttpSession.class, ServletContext.class,
074   },
075   pojoSwaps={
076      // Add a special filter for Enumerations
077      EnumerationSwap.class
078   }
079)
080public class RequestEchoResource extends BasicRest {
081
082   /**
083    * GET request handler.
084    *
085    * @param req The HTTP servlet request.
086    * @return The same request to serialize as the response.
087    */
088   @RestMethod(name="*", path="/*", converters={Traversable.class,Queryable.class}, summary="Serializes the incoming HttpServletRequest object.")
089   public HttpServletRequest doGet(RestRequest req) {
090      // Just echo the request back as the response.
091      return req;
092   }
093}