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$IteratorEnumeration
0%
0/5
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.controller.ActionContext;
 31  
 import org.seasar.cubby.controller.ThreadContext;
 32  
 import org.seasar.framework.beans.BeanDesc;
 33  
 import org.seasar.framework.beans.PropertyDesc;
 34  
 import org.seasar.framework.beans.factory.BeanDescFactory;
 35  
 import org.seasar.framework.container.ComponentDef;
 36  
 
 37  
 /**
 38  
  * 
 39  
  * @author baba
 40  
  */
 41  
 public class CubbyHttpServletRequestWrapper extends HttpServletRequestWrapper {
 42  
 
 43  
         private final ActionContext context;
 44  
 
 45  
         public CubbyHttpServletRequestWrapper(final HttpServletRequest request,
 46  
                         final ActionContext context) {
 47  0
                 super(request);
 48  
 
 49  0
                 this.context = context;
 50  0
         }
 51  
 
 52  
         @Override
 53  
         public Object getAttribute(final String name) {
 54  
                 final Object attribute;
 55  0
                 if (ATTR_CONTEXT_PATH.equals(name)) {
 56  0
                         attribute = this.getContextPath();
 57  0
                 } else if (ATTR_ACTION.equals(name)) {
 58  0
                         attribute = context.getAction();
 59  0
                 } else if (ATTR_MESSAGES.equals(name)) {
 60  0
                         attribute = ThreadContext.getMessagesMap();
 61  
                 } else {
 62  0
                         if (context.isInitialized()) {
 63  0
                                 final ComponentDef componentDef = context.getComponentDef();
 64  0
                                 final Class<?> concreteClass = componentDef.getConcreteClass();
 65  0
                                 final BeanDesc beanDesc = BeanDescFactory
 66  
                                                 .getBeanDesc(concreteClass);
 67  0
                                 if (beanDesc.hasPropertyDesc(name)) {
 68  0
                                         final PropertyDesc propertyDesc = beanDesc
 69  
                                                         .getPropertyDesc(name);
 70  0
                                         if (propertyDesc.isReadable()) {
 71  0
                                                 attribute = propertyDesc.getValue(context.getAction());
 72  
                                         } else {
 73  0
                                                 attribute = super.getAttribute(name);
 74  
                                         }
 75  0
                                 } else {
 76  0
                                         attribute = super.getAttribute(name);
 77  
                                 }
 78  0
                         } else {
 79  0
                                 attribute = super.getAttribute(name);
 80  
                         }
 81  
                 }
 82  0
                 return attribute;
 83  
         }
 84  
 
 85  
         @SuppressWarnings("unchecked")
 86  
         @Override
 87  
         public Enumeration getAttributeNames() {
 88  0
                 final List attributeNames = new ArrayList();
 89  
 
 90  0
                 attributeNames.add(ATTR_ACTION);
 91  
 
 92  0
                 final Class<?> concreteClass = context.getComponentDef()
 93  
                                 .getConcreteClass();
 94  0
                 final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(concreteClass);
 95  0
                 for (int i = 0; i < beanDesc.getPropertyDescSize(); i++) {
 96  0
                         PropertyDesc propertyDesc = beanDesc.getPropertyDesc(i);
 97  0
                         if (propertyDesc.isReadable()) {
 98  0
                                 attributeNames.add(propertyDesc.getPropertyName());
 99  
                         }
 100  
                 }
 101  
 
 102  0
                 Enumeration defaultAttributeNames = super.getAttributeNames();
 103  0
                 while (defaultAttributeNames.hasMoreElements()) {
 104  0
                         attributeNames.add((String) defaultAttributeNames.nextElement());
 105  
                 }
 106  0
                 return new IteratorEnumeration(attributeNames.iterator());
 107  
         }
 108  
 
 109  
         static class IteratorEnumeration<T> implements Enumeration<T> {
 110  
 
 111  
                 private final Iterator<T> iterator;
 112  
 
 113  0
                 public IteratorEnumeration(Iterator<T> iterator) {
 114  0
                         this.iterator = iterator;
 115  0
                 }
 116  
 
 117  
                 public boolean hasMoreElements() {
 118  0
                         return iterator.hasNext();
 119  
                 }
 120  
 
 121  
                 public T nextElement() {
 122  0
                         return iterator.next();
 123  
                 }
 124  
 
 125  
         }
 126  
 
 127  
 }