View Javadoc

1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  
17  package org.seasar.cubby.internal.util;
18  
19  import static org.seasar.cubby.CubbyConstants.ATTR_TOKEN;
20  
21  import java.math.BigInteger;
22  import java.util.Map;
23  import java.util.Random;
24  
25  import javax.servlet.http.HttpSession;
26  
27  /**
28   * 2重サブミット防止処理のヘルパークラス
29   * 
30   * @see org.seasar.cubby.tags.TokenTag
31   * @see org.seasar.cubby.validator.validators.TokenValidator
32   * @author agata
33   */
34  public class TokenHelper {
35  
36  	/**
37  	 * トークン用Mapに保持するトークンの個数(1セッションあたり何個のトークンを保持するか?)
38  	 */
39  	private static final int TOKEN_HISTORY_SIZE = 16;
40  
41  	/**
42  	 * デフォルトのトークン用パラメータ名
43  	 */
44  	public static final String DEFAULT_TOKEN_NAME = "cubby.token";
45  
46  	/**
47  	 * トークン生成用のランダムクラス
48  	 */
49  	private static final Random RANDOM = new Random();
50  
51  	/**
52  	 * ユニークなトークンを生成します。
53  	 * 
54  	 * @return ユニークなトークン
55  	 */
56  	public static String generateGUID() {
57  		return new BigInteger(165, RANDOM).toString(36).toUpperCase();
58  	}
59  
60  	/**
61  	 * トークン用の <code>Map</code> をセッションから取得します。
62  	 * <p>
63  	 * セッション中にトークン用の <code>Map</code> が存在しない場合、新規に生成します。 トークン用のマップは
64  	 * {@link LruHashMap} を使い、トークンの保持上限付きの <code>Map</code> になります。
65  	 * </p>
66  	 * 
67  	 * @param session
68  	 *            セッション
69  	 * @return トークン用のマップ
70  	 */
71  	@SuppressWarnings("unchecked")
72  	public static Map<String, String> getTokenMap(final HttpSession session) {
73  		Map<String, String> tokenMap = (Map<String, String>) session
74  				.getAttribute(ATTR_TOKEN);
75  		if (tokenMap == null) {
76  			tokenMap = new LruHashMap<String, String>(TOKEN_HISTORY_SIZE);
77  			session.setAttribute(ATTR_TOKEN, tokenMap);
78  		}
79  		return tokenMap;
80  	}
81  
82  	/**
83  	 * トークンをセッション中のトークン用の <code>Map</code> にセットします。
84  	 * 
85  	 * @param session
86  	 *            セッション
87  	 * @param token
88  	 *            トークン文字列
89  	 */
90  	public static void setToken(final HttpSession session, final String token) {
91  		final Map<String, String> tokenMap = getTokenMap(session);
92  		synchronized (tokenMap) {
93  			tokenMap.put(token, null);
94  		}
95  	}
96  
97  	/**
98  	 * パラメータ中のトークン文字列とセッション中のトークン文字列を検証します。
99  	 * <p>
100 	 * セッション中に格納されたトークン用の <code>Map</code> のキーに指定されたトークン文字列が含まれるかどうかを判定し、
101 	 * <code>Map</code> から取り除きます。
102 	 * </p>
103 	 * 
104 	 * @param session
105 	 *            セッション
106 	 * @param token
107 	 *            トークン文字列
108 	 * @return 指定されたトークン文字列がセッション中に存在したら<code>true</code>、それ以外は
109 	 *         <code>false</code>
110 	 */
111 	public static boolean validateToken(final HttpSession session,
112 			final String token) {
113 		final Map<String, String> tokenMap = getTokenMap(session);
114 		synchronized (tokenMap) {
115 			final boolean success = tokenMap.containsKey(token);
116 			tokenMap.remove(token);
117 			return success;
118 		}
119 	}
120 
121 }