Coverage Report - org.seasar.cubby.internal.util.TokenHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
TokenHelper
89%
17/19
100%
2/2
0
 
 1  
 /*
 2  
  * Copyright 2004-2009 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  
 package org.seasar.cubby.internal.util;
 17  
 
 18  
 import static org.seasar.cubby.CubbyConstants.ATTR_TOKEN;
 19  
 
 20  
 import java.math.BigInteger;
 21  
 import java.util.Map;
 22  
 import java.util.Random;
 23  
 
 24  
 import javax.servlet.http.HttpSession;
 25  
 
 26  
 /**
 27  
  * 2重サブミット防止処理のヘルパークラス
 28  
  * 
 29  
  * @see org.seasar.cubby.tags.TokenTag
 30  
  * @see org.seasar.cubby.validator.validators.TokenValidator
 31  
  * @author agata
 32  
  * @since 1.0.0
 33  
  */
 34  0
 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  3
         private static final Random RANDOM = new Random();
 50  
 
 51  
         /**
 52  
          * ユニークなトークンを生成します。
 53  
          * 
 54  
          * @return ユニークなトークン
 55  
          */
 56  
         public static String generateGUID() {
 57  150009
                 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  45
                 Map<String, String> tokenMap = (Map<String, String>) session
 74  
                                 .getAttribute(ATTR_TOKEN);
 75  45
                 if (tokenMap == null) {
 76  12
                         tokenMap = new LruHashMap<String, String>(TOKEN_HISTORY_SIZE);
 77  12
                         session.setAttribute(ATTR_TOKEN, tokenMap);
 78  
                 }
 79  45
                 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  18
                 final Map<String, String> tokenMap = getTokenMap(session);
 92  18
                 synchronized (tokenMap) {
 93  18
                         tokenMap.put(token, null);
 94  18
                 }
 95  18
         }
 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  27
                 final Map<String, String> tokenMap = getTokenMap(session);
 114  27
                 synchronized (tokenMap) {
 115  27
                         final boolean success = tokenMap.containsKey(token);
 116  27
                         tokenMap.remove(token);
 117  27
                         return success;
 118  0
                 }
 119  
         }
 120  
 
 121  
 }