View Javadoc

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