View Javadoc

    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.plugins;
    17
    18  import static org.seasar.cubby.CubbyConstants.ATTR_CONVERSION_FAILURES;
    19  import static org.seasar.cubby.CubbyConstants.ATTR_PARAMS;
    20  import static org.seasar.cubby.CubbyConstants.ATTR_VALIDATION_FAIL;
    21  import static org.seasar.cubby.validator.ValidationUtils.getValidation;
    22  import static org.seasar.cubby.validator.ValidationUtils.getValidationRules;
    23
    24  import java.util.List;
    25  import java.util.Map;
    26
    27  import javax.servlet.http.HttpServletRequest;
    28
    29  import org.seasar.cubby.action.ActionContext;
    30  import org.seasar.cubby.action.ActionResult;
    31  import org.seasar.cubby.action.Validation;
    32  import org.seasar.cubby.internal.controller.ConversionFailure;
    33  import org.seasar.cubby.internal.controller.RequestParameterBinder;
    34  import org.seasar.cubby.internal.controller.impl.RequestParameterBinderImpl;
    35  import org.seasar.cubby.internal.util.RequestUtils;
    36  import org.seasar.cubby.plugin.AbstractPlugin;
    37  import org.seasar.cubby.plugin.ActionInvocation;
    38  import org.seasar.cubby.validator.ValidationException;
    39  import org.seasar.cubby.validator.ValidationFailBehaviour;
    40  import org.seasar.cubby.validator.ValidationRules;
    41
    42  /**
    43   * 要求パラメータからフォームオブジェクトへの値のバインドと、それに伴う型変換、要求パラメータの検証を行うプラグインです。
    44   *
    45   * @author baba
    46   */
    47  public class ValidationPlugin extends AbstractPlugin {
    48
    49  	/** 要求パラメータをオブジェクトへバインドするクラス。 */
    50  	private final RequestParameterBinder requestParameterBinder = new RequestParameterBinderImpl();
    51
    52  	/**
    53  	 * {@inheritDoc}
    54  	 * <p>
    55  	 * アクションの実行前に入力値を検証します。
    56  	 * </p>
    57  	 */
    58  	@Override
    59  	public ActionResult invokeAction(final ActionInvocation invocation)
    60  			throws Exception {
    61  		final HttpServletRequest request = invocation.getRequest();
    62  		final Map<String, Object[]> parameterMap = RequestUtils.getAttribute(
    63  				request, ATTR_PARAMS);
    64
    65  		final ActionContext actionContext = invocation.getActionContext();
    66  		final Object formBean = actionContext.getFormBean();
    67
    68  		if (formBean != null) {
    69  			final List<ConversionFailure> conversionFailures = requestParameterBinder
    70  					.bind(parameterMap, formBean, actionContext);
    71  			request.setAttribute(ATTR_CONVERSION_FAILURES, conversionFailures);
    72  		}
    73
    74  		try {
    75  			final Validation validation = getValidation(actionContext
    76  					.getActionMethod());
    77  			if (validation != null) {
    78  				final ValidationRules validationRules = getValidationRules(
    79  						actionContext.getAction(), validation.rules());
    80  				validationRules.validate(parameterMap, formBean, actionContext
    81  						.getActionErrors());
    82  			}
    83  			return invocation.proceed();
    84  		} catch (final ValidationException e) {
    85  			request.setAttribute(ATTR_VALIDATION_FAIL, Boolean.TRUE);
    86  			final ValidationFailBehaviour behaviour = e.getBehaviour();
    87  			return behaviour.getValidationErrorActionResult(actionContext);
    88  		}
    89  	}
    90
    91  }