Coverage Report - org.seasar.cubby.tags.TokenTag
 
Classes in this File Line Coverage Branch Coverage Complexity
TokenTag
100%
20/20
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.tags;
 17  
 
 18  
 import static org.seasar.cubby.tags.TagUtils.toAttr;
 19  
 
 20  
 import java.io.IOException;
 21  
 
 22  
 import javax.servlet.http.HttpServletRequest;
 23  
 import javax.servlet.http.HttpSession;
 24  
 import javax.servlet.jsp.JspException;
 25  
 import javax.servlet.jsp.JspWriter;
 26  
 import javax.servlet.jsp.PageContext;
 27  
 
 28  
 import org.seasar.cubby.internal.util.StringUtils;
 29  
 import org.seasar.cubby.internal.util.TokenHelper;
 30  
 import org.seasar.cubby.validator.validators.TokenValidator;
 31  
 
 32  
 /**
 33  
  * 2重サブミット防止用の <code>&lt;input type="hidden"/&gt;</code> タグを出力するタグ。
 34  
  * <p>
 35  
  * このタグが呼び出されると一意なトークン文字列を生成して hidden とセッションに格納します。 サブミットされた先の処理の検証フェーズで、ポストされた
 36  
  * hidden 値とセッション中の値を比較して、 一致しない場合、不正な経路からのアクセスとみなしてエラー処理を行います。
 37  
  * </p>
 38  
  * 
 39  
  * @see TokenValidator#validate(org.seasar.cubby.validator.ValidationContext,
 40  
  *      Object[])
 41  
  * @author agata
 42  
  * @since 1.0.0
 43  
  */
 44  12
 public class TokenTag extends DynamicAttributesSimpleTagSupport {
 45  
 
 46  
         private String name;
 47  
 
 48  
         /**
 49  
          * name属性を設定します。
 50  
          * 
 51  
          * @param name
 52  
          *            name属性
 53  
          */
 54  
         public void setName(final String name) {
 55  3
                 this.name = name;
 56  3
         }
 57  
 
 58  
         /**
 59  
          * {@inheritDoc}
 60  
          */
 61  
         @Override
 62  
         public void doTag() throws JspException, IOException {
 63  9
                 final PageContext context = (PageContext) getJspContext();
 64  9
                 final JspWriter out = context.getOut();
 65  
 
 66  9
                 final String token = TokenHelper.generateGUID();
 67  9
                 final PageContext pageContext = (PageContext) getJspContext();
 68  9
                 final HttpServletRequest request = (HttpServletRequest) pageContext
 69  
                                 .getRequest();
 70  9
                 final HttpSession session = request.getSession();
 71  9
                 TokenHelper.setToken(session, token);
 72  
 
 73  9
                 out.append("<input type=\"hidden\" name=\"");
 74  9
                 if (StringUtils.isEmpty(name)) {
 75  6
                         out.append(TokenHelper.DEFAULT_TOKEN_NAME);
 76  
                 } else {
 77  3
                         out.append(name);
 78  
                 }
 79  9
                 out.append("\" value=\"");
 80  9
                 out.append(token);
 81  9
                 out.append("\" ");
 82  9
                 out.write(toAttr(getDynamicAttributes()));
 83  9
                 out.append("/>");
 84  9
         }
 85  
 
 86  
 }