View Javadoc

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