View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package net.sourceforge.jwebunit.tests.util;
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.Cookie;
26  import javax.servlet.http.HttpServlet;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.eclipse.jetty.http.HttpHeader;
31  
32  public class CookiesServlet extends HttpServlet {
33  
34  	private static final long serialVersionUID = 1L;
35  
36  	protected void doGet(HttpServletRequest request,
37  			HttpServletResponse response) throws ServletException, IOException {
38  		doPost(request, response);
39  	}
40  
41  	protected void doPost(HttpServletRequest request,
42  			HttpServletResponse response) throws ServletException, IOException {
43  		response.setContentType("text/html");
44  		PrintWriter out = response.getWriter();
45  		out.write(HtmlHelper.getStart("Submitted cookies"));
46  		out.write("<h1>Submitted cookies</h1>\n<p>Cookies are:");
47  		/*
48  		 * Prints POST and GET cookies as name=value1[,value2...] separated with
49  		 * <br/>
50  		 */
51  
52  		Cookie[] cookies = request.getCookies();
53  		if (cookies != null) {
54  			for (int i = 0; i < cookies.length; i++) {
55  				out.write(cookies[i].getName() + "=" + cookies[i].getValue()
56  						+ "<br/>");
57  			}
58  		}
59  
60  		out.write(" </p>\n");
61  		String ref = request.getHeader("Referer");
62  		if (ref == null) {
63  			if (request.getParameterValues("myReferer") != null) {
64  				ref = request.getParameterValues("myReferer")[0];
65  			}
66  		}
67  		out.write(HtmlHelper.getLinkParagraph("return", ref));
68  
69  		out.write(HtmlHelper.getEnd());
70  		
71  		// to disable explicitly setting the cookie on each request
72  		if (request.getParameter("dont_set") == null) {
73  			Cookie cookie = new Cookie("serveurCookie","foo");
74  			response.addCookie(cookie);
75  		}
76  		
77  		/*
78  		 * To test if several same cookies with same path, domain and name 
79  		 * are passed through to the test API. This "should" not be done by a 
80  		 * server but there are use cases where it has to be done. One example is 
81  		 * the JSESSIONID cookie which is set by Tomcat but has to be modified in a 
82  		 * mod_jk - clustered environment in order to let the client jump to another 
83  		 * worker (-> Tomcat cluster member). However within the web application the 
84  		 * JSESSIONID cookie has already been added to the response and cannot be 
85  		 * removed from there via API. Solution is to set another cookie to overwrite this.  
86  		 * 
87  		 * See http://tools.ietf.org/html/draft-ietf-httpstate-cookie-21#section-5.3, 11
88  		 */
89  		if(request.getParameter("set_by_headers") != null) {
90  			// 1
91  			Cookie jsessionIDCookie = new Cookie("JSESSIONID", "07D486AC962DE67F176F70B7C9816AAE.worker1");
92  			jsessionIDCookie.setPath("/");
93  			// session cookie:
94  			jsessionIDCookie.setMaxAge(-2);
95  			jsessionIDCookie.setDomain("localhost");
96  			response.addCookie(jsessionIDCookie);
97  			
98  			//With Jetty 6+ we are now forced to access low level API to be able to set 2 same named cookies in the same response
99  			org.eclipse.jetty.server.Response responseJetty = (org.eclipse.jetty.server.Response) response;
100 			String cookie1 = responseJetty.getHttpFields().getStringField(HttpHeader.SET_COOKIE);
101 			// 2
102 			String cookie2 = cookie1.replace("worker1", "worker2");
103 			response.addHeader(HttpHeader.SET_COOKIE.asString(), cookie2);
104 		}
105 	}
106 
107 }