Coverage Report - org.seasar.cubby.validator.validators.TokenValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
TokenValidator
93%
14/15
75%
6/8
2.333
 
 1  
 /*
 2  
  * Copyright 2004-2008 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.controller.ThreadContext;
 22  
 import org.seasar.cubby.tags.TokenTag;
 23  
 import org.seasar.cubby.util.TokenHelper;
 24  
 import org.seasar.cubby.validator.ArrayFieldValidator;
 25  
 import org.seasar.cubby.validator.MessageHelper;
 26  
 import org.seasar.cubby.validator.ValidationContext;
 27  
 import org.seasar.framework.message.MessageFormatter;
 28  
 
 29  
 /**
 30  
  * 2重サブミットの検証をします。
 31  
  * <p>
 32  
  * ポストする画面で{@link TokenTag}を使用して、Actionクラスで TokenValidatorを使用することで、
 33  
  * 2重サブミットを防止します。
 34  
  * </p>
 35  
  * <p>
 36  
  * デフォルトエラーメッセージキー:valid.token
 37  
  * </p>
 38  
  * 
 39  
  * @author agata
 40  
  * @author baba
 41  
  * @since 1.0.0
 42  
  */
 43  
 public class TokenValidator implements ArrayFieldValidator {
 44  
 
 45  
         private final MessageHelper messageHelper;
 46  
 
 47  
         /**
 48  
          * コンストラクタ
 49  
          */
 50  
         public TokenValidator() {
 51  3
                 this("valid.token");
 52  3
         }
 53  
 
 54  
         /**
 55  
          * エラーメッセージキーを指定するコンストラクタ
 56  
          * 
 57  
          * @param messageKey
 58  
          *            エラーメッセージキー
 59  
          */
 60  3
         public TokenValidator(final String messageKey) {
 61  3
                 this.messageHelper = new MessageHelper(messageKey);
 62  3
         }
 63  
 
 64  
         /**
 65  
          * {@inheritDoc}
 66  
          */
 67  
         public void validate(final ValidationContext context, final Object[] values) {
 68  5
                 if (values != null && values.length != 1) {
 69  0
                         context.addMessageInfo(this.messageHelper.createMessageInfo());
 70  
                 } else {
 71  5
                         final String token = (String) values[0];
 72  5
                         final HttpServletRequest request = ThreadContext.getRequest();
 73  5
                         if (request == null) {
 74  1
                                 throw new IllegalStateException(MessageFormatter.getMessage("ECUB0401", null));
 75  
                         }
 76  4
                         final HttpSession session = request.getSession();
 77  4
                         if (!TokenHelper.validateToken(session, token)) {
 78  3
                                 context.addMessageInfo(this.messageHelper.createMessageInfo());
 79  
                         }
 80  
                 }
 81  4
         }
 82  
 }