001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.injection.handler;
016:
017: import javax.servlet.http.HttpServletRequest;
018:
019: import org.strecks.context.ActionContext;
020: import org.strecks.converter.Converter;
021: import org.strecks.exceptions.ApplicationRuntimeException;
022: import org.strecks.exceptions.ConversionException;
023: import org.strecks.util.Assert;
024: import org.strecks.util.ReflectHelper;
025:
026: /**
027: * Injection handler to find request parameter from the <code>HttpServletRequest</code> by using
028: * the <code>HttpServletRequest.getParameter()</code>. The returned value is type converted using
029: * a <code>Converter</code> instance
030: * @author Phil Zoio
031: */
032: public class RequestParameterInjectionHandler implements
033: InjectionHandler {
034:
035: private String parameterName;
036:
037: /**
038: * The converter used to handle type conversion
039: */
040: private Converter converter;
041:
042: private boolean required;
043:
044: public RequestParameterInjectionHandler(
045: String requestParameterName, Converter converter,
046: boolean required) {
047:
048: super ();
049:
050: Assert.notNull(requestParameterName);
051: Assert.notNull(converter);
052:
053: this .converter = converter;
054: this .parameterName = requestParameterName;
055: this .required = required;
056:
057: }
058:
059: public Converter getConverter() {
060: return converter;
061: }
062:
063: public String getParameterName() {
064: return parameterName;
065: }
066:
067: public Object getValue(ActionContext injectionContext) {
068:
069: HttpServletRequest request = injectionContext.getRequest();
070:
071: String value = request.getParameter(parameterName);
072:
073: if (required && value == null) {
074: throw new IllegalStateException(
075: "Request parameter "
076: + parameterName
077: + " is required but has not been supplied. This is probably a programming error");
078: }
079:
080: return convert(value);
081: }
082:
083: @SuppressWarnings("unchecked")
084: private Object convert(String value) {
085: try {
086: Object toTargetType = getConverter().toTargetType(value);
087: return toTargetType;
088: } catch (ConversionException e) {
089: throw new ApplicationRuntimeException(
090: "Conversion failure using "
091: + converter.getClass().getName()
092: + " in request parameter handler. This is probably a programming error.",
093: e);
094: } catch (ClassCastException e) {
095: Class toConvertType = ReflectHelper.getGenericType(
096: converter.getClass(), Converter.class);
097:
098: throw new ApplicationRuntimeException(
099: "Converter "
100: + converter.getClass().getName()
101: + " is parameterized using type ("
102: + toConvertType
103: + "), does not support conversion from String properties");
104: }
105: }
106:
107: }
|