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 com.acme;
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.util.concurrent.TimeUnit;
24  
25  import javax.servlet.RequestDispatcher;
26  import javax.servlet.ServletException;
27  import javax.servlet.http.Cookie;
28  import javax.servlet.http.HttpServlet;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  
33  /** 
34   * Test Servlet Cookies.
35   */
36  @SuppressWarnings("serial")
37  public class CookieDump extends HttpServlet
38  {
39      int redirectCount=0;
40  
41      /* ------------------------------------------------------------ */
42      protected void handleForm(HttpServletRequest request,
43                            HttpServletResponse response)
44      {
45          String name =  request.getParameter("Name");
46          String value =  request.getParameter("Value");
47          String age =  request.getParameter("Age");
48  
49          if (name!=null && name.length()>0)
50          {
51              Cookie cookie = new Cookie(name,value);
52              if (age!=null && age.length()>0)
53                  cookie.setMaxAge(Integer.parseInt(age));
54              response.addCookie(cookie);
55          }
56      }
57  
58      /* ------------------------------------------------------------ */
59      @Override
60      public void doPost(HttpServletRequest request,
61                         HttpServletResponse response)
62          throws ServletException, IOException
63      {
64          handleForm(request,response);
65          String nextUrl = getURI(request)+"?R="+redirectCount++;
66          String encodedUrl=response.encodeRedirectURL(nextUrl);
67          response.sendRedirect(encodedUrl);
68      }
69  
70      /* ------------------------------------------------------------ */
71      @Override
72      public void doGet(HttpServletRequest request,
73                        HttpServletResponse response)
74          throws ServletException, IOException
75      {
76          handleForm(request,response);
77  
78          response.setContentType("text/html");
79  
80  
81          PrintWriter out = response.getWriter();
82          out.println("<h1>Cookie Dump Servlet:</h1>");
83  
84          Cookie[] cookies = request.getCookies();
85  
86          for (int i=0;cookies!=null && i<cookies.length;i++)
87          {
88              out.println("<b>"+deScript(cookies[i].getName())+"</b>="+deScript(cookies[i].getValue())+"<br/>");
89          }
90  
91          out.println("<form action=\""+response.encodeURL(getURI(request))+"\" method=\"post\">");
92  
93          out.println("<b>Name:</b><input type=\"text\" name=\"Name\" value=\"name\"/><br/>");
94          out.println("<b>Value:</b><input type=\"text\" name=\"Value\" value=\"value\"/><br/>");
95          out.println("<b>Max-Age:</b><input type=\"text\" name=\"Age\" value=\"60\"/><br/>");
96          out.println("<input type=\"submit\" name=\"Action\" value=\"Set\"/>");
97  
98      }
99  
100     /* ------------------------------------------------------------ */
101     @Override
102     public String getServletInfo() {
103         return "Session Dump Servlet";
104     }
105 
106     /* ------------------------------------------------------------ */
107     private String getURI(HttpServletRequest request)
108     {
109         String uri=(String)request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
110         if (uri==null)
111             uri=request.getRequestURI();
112         return uri;
113     }
114 
115     /* ------------------------------------------------------------ */
116     protected String deScript(String string)
117     {
118         if (string==null)
119             return null;
120         string=string.replace("&", "&amp;");
121         string=string.replace( "<", "&lt;");
122         string=string.replace( ">", "&gt;");
123         return string;
124     }
125 
126     @Override
127     public void destroy()
128     {
129         // For testing --stop with STOP.WAIT handling of the jetty-start behavior.
130         if (Boolean.getBoolean("test.slow.destroy"))
131         {
132             try
133             {
134                 TimeUnit.SECONDS.sleep(10);
135             }
136             catch (InterruptedException e)
137             {
138                 // ignore
139             }
140         }
141         super.destroy();
142     }
143 }