Package org.strecks.injection.handler

Source Code of org.strecks.injection.handler.RequestParameterInjectionHandler

/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package org.strecks.injection.handler;

import javax.servlet.http.HttpServletRequest;

import org.strecks.context.ActionContext;
import org.strecks.converter.Converter;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.strecks.exceptions.ConversionException;
import org.strecks.util.Assert;
import org.strecks.util.ReflectHelper;

/**
* Injection handler to find request parameter from the <code>HttpServletRequest</code> by using
* the <code>HttpServletRequest.getParameter()</code>. The returned value is type converted using
* a <code>Converter</code> instance
* @author Phil Zoio
*/
public class RequestParameterInjectionHandler implements InjectionHandler
{

  private String parameterName;

  /**
   * The converter used to handle type conversion
   */
  private Converter converter;

  private boolean required;

  public RequestParameterInjectionHandler(String requestParameterName, Converter converter, boolean required)
  {

    super();

    Assert.notNull(requestParameterName);
    Assert.notNull(converter);

    this.converter = converter;
    this.parameterName = requestParameterName;
    this.required = required;

  }

  public Converter getConverter()
  {
    return converter;
  }

  public String getParameterName()
  {
    return parameterName;
  }

  public Object getValue(ActionContext injectionContext)
  {

    HttpServletRequest request = injectionContext.getRequest();

    String value = request.getParameter(parameterName);

    if (required && value == null)
    {
      throw new IllegalStateException("Request parameter " + parameterName
          + " is required but has not been supplied. This is probably a programming error");
    }

    return convert(value);
  }

  @SuppressWarnings("unchecked")
  private Object convert(String value)
  {
    try
    {
      Object toTargetType = getConverter().toTargetType(value);
      return toTargetType;
    }
    catch (ConversionException e)
    {
      throw new ApplicationRuntimeException("Conversion failure using " + converter.getClass().getName()
          + " in request parameter handler. This is probably a programming error.", e);
    }
    catch (ClassCastException e)
    {
      Class toConvertType = ReflectHelper.getGenericType(converter.getClass(), Converter.class);

      throw new ApplicationRuntimeException("Converter " + converter.getClass().getName()
          + " is parameterized using type (" + toConvertType
          + "), does not support conversion from String properties");
    }
  }

}
TOP

Related Classes of org.strecks.injection.handler.RequestParameterInjectionHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.