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