View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.ibatis.struts.httpmap;
17  
18  import com.ibatis.struts.httpmap.BaseHttpMap;
19  
20  import javax.servlet.http.Cookie;
21  import javax.servlet.http.HttpServletRequest;
22  import java.util.Enumeration;
23  
24  /***
25   * Map to wrap cookie names and values (READ ONLY).
26   * <p/>
27   * Date: Mar 11, 2004 11:31:35 PM
28   *
29   * @author Clinton Begin
30   */
31  public class CookieMap extends BaseHttpMap {
32  
33    private Cookie[] cookies;
34  
35    public CookieMap(HttpServletRequest request) {
36      cookies = request.getCookies();
37    }
38  
39    protected Enumeration getNames() {
40      return new CookieEnumerator(cookies);
41    }
42  
43    protected Object getValue(Object key) {
44      for (int i = 0; i < cookies.length; i++) {
45        if (key.equals(cookies[i].getName())) {
46          return cookies[i].getValue();
47        }
48      }
49      return null;
50    }
51  
52    protected void putValue(Object key, Object value) {
53      throw new UnsupportedOperationException();
54    }
55  
56    protected void removeValue(Object key) {
57      throw new UnsupportedOperationException();
58    }
59  
60    /***
61     * Cookie Enumerator Class
62     */
63    private class CookieEnumerator implements Enumeration {
64  
65      private int i = 0;
66  
67      private Cookie[] cookieArray;
68  
69      public CookieEnumerator(Cookie[] cookies) {
70        this.cookieArray = cookies;
71      }
72  
73      public synchronized boolean hasMoreElements() {
74        return cookieArray.length > i;
75      }
76  
77      public synchronized Object nextElement() {
78        Object element = cookieArray[i];
79        i++;
80        return element;
81      }
82  
83    }
84  
85  }