Coverage Report - org.seasar.cubby.filter.CubbyHttpServletRequestWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
CubbyHttpServletRequestWrapper
50%
17/34
33%
6/18
0
 
 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.filter;
 17  
 
 18  
 import static org.seasar.cubby.CubbyConstants.ATTR_ACTION;
 19  
 import static org.seasar.cubby.CubbyConstants.ATTR_CONTEXT_PATH;
 20  
 import static org.seasar.cubby.CubbyConstants.ATTR_MESSAGES;
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.Enumeration;
 24  
 import java.util.List;
 25  
 
 26  
 import javax.servlet.http.HttpServletRequest;
 27  
 import javax.servlet.http.HttpServletRequestWrapper;
 28  
 
 29  
 import org.seasar.cubby.CubbyConstants;
 30  
 import org.seasar.cubby.action.Action;
 31  
 import org.seasar.cubby.controller.ThreadContext;
 32  
 import org.seasar.cubby.util.IteratorEnumeration;
 33  
 import org.seasar.framework.beans.BeanDesc;
 34  
 import org.seasar.framework.beans.PropertyDesc;
 35  
 import org.seasar.framework.beans.factory.BeanDescFactory;
 36  
 
 37  
 /**
 38  
  * 特別な属性を取得するためのリクエストのラッパです。
 39  
  * <p>
 40  
  * 以下のような属性を使用することができます。 <table><thead>
 41  
  * <tr>
 42  
  * <th>属性名</th>
 43  
  * <th>値</th>
 44  
  * <th>型</th>
 45  
  * </tr>
 46  
  * </thead><tbody>
 47  
  * <tr>
 48  
  * <td>{@link CubbyConstants#ATTR_CONTEXT_PATH}</td>
 49  
  * <td>コンテキストパス</td>
 50  
  * <td>{@link String}</td>
 51  
  * </tr>
 52  
  * <tr>
 53  
  * <td>{@link CubbyConstants#ATTR_ACTION}</td>
 54  
  * <td>アクション</td>
 55  
  * <td>{@link org.seasar.cubby.action.Action}</td>
 56  
  * </tr>
 57  
  * <tr>
 58  
  * <td>{@link CubbyConstants#ATTR_MESSAGES}</td>
 59  
  * <td>メッセージリソース</td>
 60  
  * <td>{@link java.util.Map}</td>
 61  
  * </tr>
 62  
  * <tr>
 63  
  * <td>アクションのプロパティ名</td>
 64  
  * <td>アクションのプロパティ値</td>
 65  
  * <td>任意</td>
 66  
  * </tr>
 67  
  * </table> これらの属性は通常の属性よりも優先されるのでご注意ください。
 68  
  * </p>
 69  
  * 
 70  
  * @author baba
 71  
  * @since 1.0.0
 72  
  */
 73  
 class CubbyHttpServletRequestWrapper extends HttpServletRequestWrapper {
 74  
 
 75  
         /**
 76  
          * インスタンス化します。
 77  
          * 
 78  
          * @param request
 79  
          *            ラップするリクエスト
 80  
          */
 81  
         public CubbyHttpServletRequestWrapper(final HttpServletRequest request) {
 82  1
                 super(request);
 83  1
         }
 84  
 
 85  
         /**
 86  
          * リクエストの属性を取得します。
 87  
          * 
 88  
          * @param name
 89  
          *            属性名
 90  
          */
 91  
         @Override
 92  
         public Object getAttribute(final String name) {
 93  
                 final Object attribute;
 94  0
                 if (ATTR_CONTEXT_PATH.equals(name)) {
 95  0
                         attribute = this.getContextPath();
 96  0
                 } else if (ATTR_MESSAGES.equals(name)) {
 97  0
                         attribute = ThreadContext.getMessagesMap();
 98  
                 } else {
 99  0
                         final Action action = (Action) super.getAttribute(ATTR_ACTION);
 100  0
                         if (action != null) {
 101  0
                                 final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action
 102  
                                                 .getClass());
 103  0
                                 if (beanDesc.hasPropertyDesc(name)) {
 104  0
                                         final PropertyDesc propertyDesc = beanDesc
 105  
                                                         .getPropertyDesc(name);
 106  0
                                         if (propertyDesc.isReadable()) {
 107  0
                                                 attribute = propertyDesc.getValue(action);
 108  
                                         } else {
 109  0
                                                 attribute = super.getAttribute(name);
 110  
                                         }
 111  0
                                 } else {
 112  0
                                         attribute = super.getAttribute(name);
 113  
                                 }
 114  0
                         } else {
 115  0
                                 attribute = super.getAttribute(name);
 116  
                         }
 117  
                 }
 118  0
                 return attribute;
 119  
         }
 120  
 
 121  
         /**
 122  
          * 属性名の列挙を返します。
 123  
          * 
 124  
          * @return 属性名の列挙
 125  
          */
 126  
         @SuppressWarnings("unchecked")
 127  
         @Override
 128  
         public Enumeration getAttributeNames() {
 129  1
                 final List attributeNames = new ArrayList();
 130  
 
 131  1
                 attributeNames.add(ATTR_CONTEXT_PATH);
 132  1
                 attributeNames.add(ATTR_ACTION);
 133  1
                 attributeNames.add(ATTR_MESSAGES);
 134  
 
 135  1
                 final Action action = (Action) super.getAttribute(ATTR_ACTION);
 136  1
                 if (action != null) {
 137  1
                         final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action
 138  
                                         .getClass());
 139  3
                         for (int i = 0; i < beanDesc.getPropertyDescSize(); i++) {
 140  2
                                 final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(i);
 141  2
                                 if (propertyDesc.isReadable()) {
 142  2
                                         attributeNames.add(propertyDesc.getPropertyName());
 143  
                                 }
 144  
                         }
 145  
                 }
 146  
 
 147  1
                 final Enumeration defaultAttributeNames = super.getAttributeNames();
 148  3
                 while (defaultAttributeNames.hasMoreElements()) {
 149  2
                         attributeNames.add(defaultAttributeNames.nextElement());
 150  
                 }
 151  1
                 return new IteratorEnumeration(attributeNames.iterator());
 152  
         }
 153  
 
 154  
 }