View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.tests.webapp;
20  
21  import java.io.IOException;
22  import java.util.Enumeration;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServlet;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  /**
30   * Servlet declaring the various do* methods.
31   * 
32   * The Jetty internals for OPTIONS should detect the declared do* methods and
33   * return an appropriate listing of available OPTIONS on an OPTIONS request.
34   */
35  public class HttpMethodsServlet extends HttpServlet
36  {
37      private static final long serialVersionUID = 1L;
38  
39      /**
40       * @see HttpServlet#HttpServlet()
41       */
42      public HttpMethodsServlet()
43      {
44          super();
45      }
46  
47      /**
48       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
49       *      response)
50       */
51      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
52      {
53          /* do nothing */
54      }
55  
56      /**
57       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
58       *      response)
59       */
60      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
61      {
62          /* do nothing */
63      }
64  
65      /**
66       * @see HttpServlet#doPut(HttpServletRequest, HttpServletResponse)
67       */
68      protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
69      {
70          /* do nothing */
71      }
72  
73      /**
74       * @see HttpServlet#doDelete(HttpServletRequest, HttpServletResponse)
75       */
76      protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
77      {
78          /* do nothing */
79      }
80  
81      /**
82       * @see HttpServlet#doHead(HttpServletRequest, HttpServletResponse)
83       */
84      protected void doHead(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
85      {
86          /* do nothing */
87      }
88  
89      /**
90       * @see HttpServlet#doTrace(HttpServletRequest, HttpServletResponse)
91       */
92      protected void doTrace(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
93      {
94          response.addHeader("Content-Type","message/http");
95          StringBuffer msg = new StringBuffer();
96          msg.append(request.getMethod()).append(' ');
97          msg.append(request.getRequestURI()).append(' ');
98          msg.append(request.getProtocol()).append("\n");
99  
100         // Now the headers
101         Enumeration enNames = request.getHeaderNames();
102         while (enNames.hasMoreElements())
103         {
104             String name = (String)enNames.nextElement();
105             Enumeration enValues = request.getHeaders(name);
106             while (enValues.hasMoreElements())
107             {
108                 String value = (String)enValues.nextElement();
109                 msg.append(name).append(": ").append(value).append("\n");
110             }
111         }
112         msg.append("\n");
113 
114         response.getWriter().print(msg.toString());
115     }
116 }