KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > store > CookieStore


1 /*
2  *
3  * ====================================================================
4  *
5  * Copyright 2004 The Apache Software Foundation
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */

20 package org.apache.slide.projector.store;
21
22 import javax.servlet.http.Cookie JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import org.apache.slide.projector.Store;
27
28 public class CookieStore implements Store {
29     protected HttpServletRequest JavaDoc request;
30     protected HttpServletResponse JavaDoc response;
31
32     public CookieStore(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
33         this.request = request;
34         this.response = response;
35     }
36
37     public void put(String JavaDoc key, Object JavaDoc value, long timeout) {
38         Cookie JavaDoc cookie = null;
39         if ( value instanceof Cookie JavaDoc ) {
40             cookie = (Cookie JavaDoc)value;
41         }
42         if ( value instanceof String JavaDoc ) {
43             cookie = new Cookie JavaDoc(key, (String JavaDoc)value);
44         }
45         if ( cookie != null ) {
46             cookie.setMaxAge((int) (timeout/1000));
47             response.addCookie(cookie);
48         }
49     }
50
51     public void put(String JavaDoc key, Object JavaDoc value) {
52         Cookie JavaDoc cookie = null;
53         if ( value instanceof Cookie JavaDoc ) {
54             cookie = (Cookie JavaDoc)value;
55         }
56         if ( value instanceof String JavaDoc ) {
57             cookie = new Cookie JavaDoc(key, (String JavaDoc)value);
58         }
59         if ( cookie != null ) {
60             response.addCookie(cookie);
61         }
62     }
63
64     public Object JavaDoc get(String JavaDoc key) {
65         Cookie JavaDoc[] cookies = request.getCookies();
66         for ( int i = 0; i < cookies.length; i++ ) {
67             if ( cookies[i].getName().equals(key) ) {
68                 return cookies[i].getValue();
69             }
70         }
71         return null;
72     }
73
74     public void dispose(String JavaDoc key) {
75         request.removeAttribute(key);
76     }
77 }
Popular Tags