View Javadoc

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.HttpSession;
19  
20  import org.seasar.cubby.controller.ThreadContext;
21  import org.seasar.cubby.tags.TokenTag;
22  import org.seasar.cubby.util.TokenHelper;
23  import org.seasar.cubby.validator.ArrayFieldValidator;
24  import org.seasar.cubby.validator.MessageHelper;
25  import org.seasar.cubby.validator.ValidationContext;
26  
27  /**
28   * 2重サブミットの検証をします。
29   * <p>
30   * ポストする画面で{@link TokenTag}を使用して、Actionクラスで TokenValidatorを使用することで、
31   * 2重サブミットを防止します。
32   * </p>
33   * <p>
34   * デフォルトエラーメッセージキー:valid.token
35   * </p>
36   * 
37   * @author agata
38   * @author baba
39   */
40  public class TokenValidator implements ArrayFieldValidator {
41  
42  	private final MessageHelper messageHelper;
43  
44  	/**
45  	 * コンストラクタ
46  	 */
47  	public TokenValidator() {
48  		this("valid.token");
49  	}
50  
51  	/**
52  	 * エラーメッセージキーを指定するコンストラクタ
53  	 * 
54  	 * @param messageKey
55  	 *            エラーメッセージキー
56  	 */
57  	public TokenValidator(final String messageKey) {
58  		this.messageHelper = new MessageHelper(messageKey);
59  	}
60  
61  	public void validate(final ValidationContext context, final Object[] values) {
62  		if (values != null && values.length != 1) {
63  			context.addMessageInfo(this.messageHelper.createMessageInfo());
64  		} else {
65  			final String token = (String) values[0];
66  			final HttpSession session = ThreadContext.getRequest().getSession();
67  			if (!TokenHelper.validateToken(session, token)) {
68  				context.addMessageInfo(this.messageHelper.createMessageInfo());
69  			}
70  		}
71  	}
72  }