Removed ServletRequestBindingTransformer (INT-609).

This commit is contained in:
Mark Fisher
2009-03-15 19:38:56 +00:00
parent b0f8dedf12
commit 7f202eefca
2 changed files with 15 additions and 144 deletions

View File

@@ -1,121 +0,0 @@
/*
* Copyright 2002-2009 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.springframework.integration.http.transformer;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.transformer.AbstractPayloadTransformer;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.servlet.handler.DispatcherServletWebRequest;
/**
* Message Transformer that expects a {@link HttpServletRequest} as input and binds
* its parameter map to a target instance. The target instance may be a non-singleton
* bean as specified by the {@link #setTargetBeanName(String) 'targetBeanName'} property.
* Otherwise, this transformer's target type must provide a default no-arg constructor.
*
* @author Mark Fisher
* @since 1.0.2
*/
public class ServletRequestBindingTransformer<T> extends AbstractPayloadTransformer<HttpServletRequest, T>
implements BeanFactoryAware, InitializingBean {
private final Class<T> targetType;
private volatile String targetBeanName;
private volatile WebBindingInitializer webBindingInitializer;
private volatile BeanFactory beanFactory;
private volatile boolean validated;
public ServletRequestBindingTransformer(Class<T> targetType) {
Assert.notNull(targetType, "targetType must not be null");
this.targetType = targetType;
}
/**
* Specify the name of a bean definition to use when creating the target
* instance. The bean must <em>not</em> be a singleton, and it must be
* compatible with the {@link #targetType}.
* <p>If no 'targetBeanName' value is provided, the target type must
* provide a default, no-arg constructor.
*/
public void setTargetBeanName(String targetBeanName) {
this.targetBeanName = targetBeanName;
}
public void setWebBindingInitializer(WebBindingInitializer webBindingInitializer) {
this.webBindingInitializer = webBindingInitializer;
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public final void afterPropertiesSet() {
this.validateTargetBeanIfNecessary();
}
private void validateTargetBeanIfNecessary() {
if (this.targetBeanName != null && !this.validated) {
Assert.notNull(this.beanFactory, "beanFactory is required for binding to a bean");
if (this.beanFactory.isSingleton(this.targetBeanName)) {
throw new IllegalArgumentException("binding target bean must not be a singleton");
}
this.validated = true;
}
}
@Override
@SuppressWarnings("unchecked")
protected T transformPayload(HttpServletRequest request) throws Exception {
ServletRequestDataBinder binder = new ServletRequestDataBinder(getTarget());
this.initBinder(binder, request);
binder.bind(request);
// this will immediately throw any bind Exceptions
Map map = binder.close();
return (T) map.get(ServletRequestDataBinder.DEFAULT_OBJECT_NAME);
}
private void initBinder(ServletRequestDataBinder binder, HttpServletRequest request) {
if (this.webBindingInitializer != null) {
this.webBindingInitializer.initBinder(binder, new DispatcherServletWebRequest(request));
}
}
@SuppressWarnings("unchecked")
private Object getTarget() throws InstantiationException, IllegalAccessException {
if (this.targetBeanName != null) {
this.validateTargetBeanIfNecessary();
return (T) this.beanFactory.getBean(this.targetBeanName, this.targetType);
}
return this.targetType.newInstance();
}
}

View File

@@ -14,36 +14,32 @@
* limitations under the License.
*/
package org.springframework.integration.http.transformer;
package org.springframework.integration.http.mapper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import javax.servlet.ServletRequest;
import org.junit.Test;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.integration.core.Message;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.http.DataBindingRequestMapper;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
/**
* @author Mark Fisher
*/
public class ServletRequestBindingTransformerTests {
public class DataBindingRequestMapperTests {
@Test
public void bindToType() {
ServletRequestBindingTransformer<TestBean> transformer =
new ServletRequestBindingTransformer<TestBean>(TestBean.class);
public void bindToType() throws Exception {
DataBindingRequestMapper mapper = new DataBindingRequestMapper(TestBean.class);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setParameter("name", "testBean");
request.setParameter("age", "42");
Message<ServletRequest> message = new GenericMessage<ServletRequest>(request);
Message<?> result = transformer.transform(message);
Message<?> result = mapper.mapRequest(request);
assertNotNull(result);
assertEquals(TestBean.class, result.getPayload().getClass());
TestBean payload = (TestBean) result.getPayload();
@@ -52,17 +48,15 @@ public class ServletRequestBindingTransformerTests {
}
@Test
public void bindToTypeWithBindingInitializer() {
ServletRequestBindingTransformer<TestBean> transformer =
new ServletRequestBindingTransformer<TestBean>(TestBean.class);
public void bindToTypeWithBindingInitializer() throws Exception {
DataBindingRequestMapper mapper = new DataBindingRequestMapper(TestBean.class);
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setDirectFieldAccess(true);
transformer.setWebBindingInitializer(initializer);
mapper.setWebBindingInitializer(initializer);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setParameter("name", "testBean");
request.setParameter("age", "42");
Message<ServletRequest> message = new GenericMessage<ServletRequest>(request);
Message<?> result = transformer.transform(message);
Message<?> result = mapper.mapRequest(request);
assertNotNull(result);
assertEquals(TestBean.class, result.getPayload().getClass());
TestBean payload = (TestBean) result.getPayload();
@@ -71,19 +65,17 @@ public class ServletRequestBindingTransformerTests {
}
@Test
public void bindToPrototypeBean() {
public void bindToPrototypeBean() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
MutablePropertyValues properties = new MutablePropertyValues();
properties.addPropertyValue("name", "prototype");
context.registerPrototype("prototypeTarget", TestBean.class, properties);
ServletRequestBindingTransformer<TestBean> transformer =
new ServletRequestBindingTransformer<TestBean>(TestBean.class);
transformer.setTargetBeanName("prototypeTarget");
transformer.setBeanFactory(context);
DataBindingRequestMapper mapper = new DataBindingRequestMapper(TestBean.class);
mapper.setTargetBeanName("prototypeTarget");
mapper.setBeanFactory(context);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setParameter("age", "42");
Message<ServletRequest> message = new GenericMessage<ServletRequest>(request);
Message<?> result = transformer.transform(message);
Message<?> result = mapper.mapRequest(request);
assertNotNull(result);
assertEquals(TestBean.class, result.getPayload().getClass());
TestBean payload = (TestBean) result.getPayload();