Debian Codegraph @

tests/reproducers/signed/SavingCookies/srcs/SavingCookies.java

raw
1 /* SavingCookies.java
2 Store cookies in the java cookie store, and go to a page that confirms they are there.
3
4 Copyright (C) 2012 Red Hat, Inc.
5
6 This file is part of IcedTea.
7
8 IcedTea is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, version 2.
11
12 IcedTea is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with IcedTea; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
21
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
26
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version.
38 */
39 import java.applet.Applet;
40 import java.io.IOException;
41 import java.net.CookieHandler;
42 import java.net.CookieManager;
43 import java.net.HttpCookie;
44 import java.net.URI;
45 import java.net.URL;
46 import java.text.DateFormat;
47 import java.text.SimpleDateFormat;
48 import java.util.ArrayList;
49 import java.util.Calendar;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Locale;
53 import java.util.Map;
54
55 public class SavingCookies extends Applet {
56 // Set up a response header with Set-Cookie
57 static private Map<String, List<String>> responseHeaders(String cookie) {
58 List<String> cookies = new ArrayList<String>();
59 cookies.add(cookie);
60 Map<String, List<String>> rh = new HashMap<String, List<String>>();
61 rh.put("Set-Cookie", cookies);
62 rh.put("Cookie", cookies);
63 return rh;
64 }
65
66 static private void saveCookie(URI uri, String cookie) throws IOException {
67 CookieHandler handler = CookieHandler.getDefault();
68
69 if (handler == null) {
70 System.out.println("Failing due to lack of CookieHandler class!");
71 return;
72 }
73
74 System.out.println("Using CookieHandler class: " + handler.getClass().getCanonicalName());
75 System.out.println("Putting " + cookie + " at " + uri.toString());
76 handler.put(uri, responseHeaders(cookie));
77 }
78
79 /* If a show-document param was set, go there */
80 private void gotoNextDocument() {
81 URL baseURL = getCodeBase();
82 String nextDocument = getParameter("show-document");
83 if (nextDocument != null) {
84 try {
85 System.out.println("Calling showDocument(" + nextDocument + ")");
86 getAppletContext().showDocument(new URL(baseURL.toString() + nextDocument));
87 } catch (Exception e) {
88 e.printStackTrace();
89 }
90 }
91 }
92
93 static private String formatCookie(String cookie, boolean persistent) {
94 if (persistent) {
95 // Put in the cookie date format
96 final int TIMEOUT = 3600;
97 DateFormat df = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss 'GMT'", Locale.US);
98 Calendar cal = Calendar.getInstance();
99 cal.add(Calendar.SECOND, TIMEOUT);
100 cookie += "; ";
101 cookie += "Expires=" + df.format(cal.getTime()) + "; ";
102 cookie += "Max-Age=" + TIMEOUT + "; ";
103 }
104 return cookie;
105 }
106
107 @Override
108 public void start() {
109 System.out.println("Entered SavingCookies.java");
110
111 URL baseURL = getCodeBase();
112 try {
113 saveCookie(baseURL.toURI(), formatCookie(getParameter("cookie"),
114 "yes".equals(getParameter("persistent"))));
115 } catch (Exception e) {
116 e.printStackTrace();
117 }
118
119 System.out.println("Finished SavingCookies.java");
120
121 gotoNextDocument();
122 }
123 }