Coverage Report - org.seasar.cubby.interceptor.InitializeInterceptor
 
Classes in this File Line Coverage Branch Coverage Complexity
InitializeInterceptor
0%
0/25
0%
0/4
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.interceptor;
 17  
 
 18  
 import static org.seasar.cubby.CubbyConstants.ATTR_PARAMS;
 19  
 
 20  
 import java.util.Map;
 21  
 
 22  
 import javax.servlet.http.HttpServletRequest;
 23  
 
 24  
 import org.aopalliance.intercept.MethodInterceptor;
 25  
 import org.aopalliance.intercept.MethodInvocation;
 26  
 import org.seasar.cubby.CubbyConstants;
 27  
 import org.seasar.cubby.action.Action;
 28  
 import org.seasar.cubby.action.ActionResult;
 29  
 import org.seasar.cubby.controller.ActionContext;
 30  
 import org.seasar.cubby.controller.CubbyConfiguration;
 31  
 import org.seasar.cubby.controller.RequestParser;
 32  
 import org.seasar.cubby.controller.ThreadContext;
 33  
 import org.seasar.cubby.dxo.FormDxo;
 34  
 
 35  
 /**
 36  
  * コントローラの初期化や実行結果のrequest/sessionへの反映などを行うインターセプタです。
 37  
  * {@link Action#initialize()}、{@link Action#prerender()} の実行を行います。
 38  
  * 
 39  
  * @author agata
 40  
  * @author baba
 41  
  * @since 1.0.0
 42  
  */
 43  0
 public class InitializeInterceptor implements MethodInterceptor {
 44  
 
 45  
         /** リクエスト。 */
 46  
         private HttpServletRequest request;
 47  
 
 48  
         /** アクションのコンテキスト。 */
 49  
         private ActionContext context;
 50  
 
 51  
         /**
 52  
          * リクエストを設定します。
 53  
          * 
 54  
          * @param request
 55  
          *            リクエスト
 56  
          */
 57  
         public void setRequest(final HttpServletRequest request) {
 58  0
                 this.request = request;
 59  0
         }
 60  
 
 61  
         /**
 62  
          * アクションのコンテキストを設定します。
 63  
          * 
 64  
          * @param context
 65  
          *            アクションのコンテキスト
 66  
          */
 67  
         public void setActionContext(final ActionContext context) {
 68  0
                 this.context = context;
 69  0
         }
 70  
 
 71  
         /**
 72  
          * {@inheritDoc}
 73  
          * <p>
 74  
          * 以下のようなフローでアクションメソッドを実行します。
 75  
          * <ul>
 76  
          * <li>リクエストパラメータを{@link Map}に変換してリクエストの属性{@link CubbyConstants#ATTR_PARAMS}に設定します。</li>
 77  
          * <li>リクエストの属性{@link CubbyConstants#ATTR_PARAMS}の値をフォームオブジェクトにバインドします。</li>
 78  
          * <li>実際のアクションメソッドを呼び出します。</li>
 79  
          * <li>アクションの{@link Action#prerender()}を呼び出します。</li>
 80  
          * <li>アクションメソッドの実行結果を返します。</li>
 81  
          * </ul>
 82  
          * </p>
 83  
          */
 84  
         public Object invoke(final MethodInvocation invocation) throws Throwable {
 85  0
                 setupParams(context, request);
 86  0
                 setupForm(context, request);
 87  
 
 88  0
                 final Action action = context.getAction();
 89  0
                 action.initialize();
 90  
 
 91  0
                 final ActionResult result = (ActionResult) invocation.proceed();
 92  0
                 if (result != null) {
 93  0
                         result.prerender(context);
 94  
                 }
 95  
 
 96  0
                 return result;
 97  
         }
 98  
 
 99  
         /**
 100  
          * リクエストパラメータを{@link Map}に変換してリクエストの属性{@link CubbyConstants#ATTR_PARAMS}に設定します。
 101  
          * 
 102  
          * @param context
 103  
          *            アクションのコンテキスト
 104  
          * @param request
 105  
          *            リクエスト
 106  
          */
 107  
         void setupParams(final ActionContext context,
 108  
                         final HttpServletRequest request) {
 109  0
                 final CubbyConfiguration configuration = ThreadContext
 110  
                                 .getConfiguration();
 111  0
                 final RequestParser requestParser = configuration.getRequestParser();
 112  0
                 final Map<String, Object[]> parameterMap = requestParser
 113  
                                 .getParameterMap(request);
 114  0
                 request.setAttribute(ATTR_PARAMS, parameterMap);
 115  0
         }
 116  
 
 117  
         /**
 118  
          * リクエストの属性{@link CubbyConstants#ATTR_PARAMS}の値をフォームオブジェクトにバインドします。
 119  
          * 
 120  
          * @param context
 121  
          *            アクションのコンテキスト
 122  
          * @param request
 123  
          *            リクエスト
 124  
          */
 125  
         private void setupForm(final ActionContext context,
 126  
                         final HttpServletRequest request) {
 127  0
                 final Object formBean = context.getFormBean();
 128  0
                 if (formBean != null) {
 129  0
                         final FormDxo formDxo = context.getFormDxo();
 130  0
                         final Map<String, Object[]> params = getAttribute(request,
 131  
                                         ATTR_PARAMS);
 132  0
                         formDxo.convert(params, formBean);
 133  
                 }
 134  0
         }
 135  
 
 136  
         /**
 137  
          * リクエストから属性を取得します。
 138  
          * 
 139  
          * @param <T>
 140  
          *            取得する属性の型
 141  
          * @param request
 142  
          *            リクエスト
 143  
          * @param name
 144  
          *            属性名
 145  
          * @return 属性
 146  
          */
 147  
         @SuppressWarnings("unchecked")
 148  
         private static <T> T getAttribute(final HttpServletRequest request,
 149  
                         final String name) {
 150  0
                 return (T) request.getAttribute(name);
 151  
         }
 152  
 
 153  
 }