Coverage Report - org.seasar.cubby.validator.validators.TokenValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
TokenValidator
89%
17/19
62%
5/8
3.333
 
 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.validator.validators;
 17  
 
 18  
 import javax.servlet.http.HttpServletRequest;
 19  
 import javax.servlet.http.HttpSession;
 20  
 
 21  
 import org.seasar.cubby.action.MessageInfo;
 22  
 import org.seasar.cubby.internal.controller.ThreadContext;
 23  
 import org.seasar.cubby.internal.util.TokenHelper;
 24  
 import org.seasar.cubby.tags.TokenTag;
 25  
 import org.seasar.cubby.validator.ArrayFieldValidator;
 26  
 import org.seasar.cubby.validator.ValidationContext;
 27  
 
 28  
 /**
 29  
  * 2 重サブミットの検証をします。
 30  
  * <p>
 31  
  * ポストする画面で {@link TokenTag} を使用し、アクションクラスでこのクラスで検証することで 2 重サブミットを防止します。
 32  
  * </p>
 33  
  * <p>
 34  
  * <table>
 35  
  * <caption>検証エラー時に設定するエラーメッセージ</caption> <tbody>
 36  
  * <tr>
 37  
  * <th scope="row">デフォルトのキー</th>
 38  
  * <td>valid.token</td>
 39  
  * </tr>
 40  
  * <tr>
 41  
  * <th scope="row">置換文字列</th>
 42  
  * <td>
 43  
  * <ol start="0">
 44  
  * <li>フィールド名</li>
 45  
  * </ol></td>
 46  
  * </tr>
 47  
  * </tbody>
 48  
  * </table>
 49  
  * </p>
 50  
  * 
 51  
  * @author agata
 52  
  * @author baba
 53  
  * @since 1.0.0
 54  
  */
 55  
 public class TokenValidator implements ArrayFieldValidator {
 56  
 
 57  
         /**
 58  
          * メッセージキー。
 59  
          */
 60  
         private final String messageKey;
 61  
 
 62  
         /**
 63  
          * コンストラクタ。
 64  
          */
 65  
         public TokenValidator() {
 66  6
                 this("valid.token");
 67  6
         }
 68  
 
 69  
         /**
 70  
          * エラーメッセージキーを指定するコンストラクタ
 71  
          * 
 72  
          * @param messageKey
 73  
          *            エラーメッセージキー
 74  
          */
 75  6
         public TokenValidator(final String messageKey) {
 76  6
                 this.messageKey = messageKey;
 77  6
         }
 78  
 
 79  
         /**
 80  
          * {@inheritDoc}
 81  
          */
 82  
         public void validate(final ValidationContext context, final Object[] values) {
 83  12
                 if (values == null) {
 84  0
                         return;
 85  
                 }
 86  
 
 87  12
                 if (values.length == 1) {
 88  12
                         final String token = (String) values[0];
 89  12
                         final HttpServletRequest request = ThreadContext.getRequest();
 90  9
                         final HttpSession session = request.getSession(false);
 91  9
                         if (session == null) {
 92  0
                                 return;
 93  
                         }
 94  9
                         if (TokenHelper.validateToken(session, token)) {
 95  3
                                 return;
 96  
                         }
 97  
                 }
 98  
 
 99  6
                 final MessageInfo messageInfo = new MessageInfo();
 100  6
                 messageInfo.setKey(this.messageKey);
 101  6
                 context.addMessageInfo(messageInfo);
 102  6
         }
 103  
 }