Skip to content

Commit

Permalink
Merge pull request #215 from terasolunaorg/issues/132_backport-1.0.x-…
Browse files Browse the repository at this point in the history
…CompositeRequestDataValueProcessor

[Backport to1.0.x] Supporting RequestDataValueProcessor for Spring 3 and 4. #132
  • Loading branch information
making committed Nov 14, 2014
2 parents bf0c81e + 917f2ae commit 656f5aa
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public class CompositeRequestDataValueProcessor implements
*/
private final List<RequestDataValueProcessor> reversedProcessors;

/**
* Helper for invoke the {@code processAction()} method of {@link RequestDataValueProcessor}.
* @since 1.0.2
*/
private final ProcessActionInvocationHelper processActionInvocationHelper;

/**
* Constructor<br>
* <p>
Expand All @@ -60,21 +66,44 @@ public CompositeRequestDataValueProcessor(
List<RequestDataValueProcessor> reverse = Arrays.asList(processors);
Collections.reverse(reverse);
this.reversedProcessors = Collections.unmodifiableList(reverse);
this.processActionInvocationHelper = new ProcessActionInvocationHelper();
}

/**
* Calls the {@code processAction()} method of all the {@link RequestDataValueProcessor} implementations <br>
* this class holds.
* this class holds. This method is for compatibility with Spring 3.
* @param action action of form tag. must not be null.
* @see org.springframework.web.servlet.support.RequestDataValueProcessor#processAction(javax.servlet.http.HttpServletRequest,
* java.lang.String)
*/
@Override
public String processAction(HttpServletRequest request, String action) {

String result = action;
for (RequestDataValueProcessor processor : processors) {
result = processor.processAction(request, action);
result = processActionInvocationHelper.invokeProcessAction(processor, request, action, null);
if (!action.equals(result)) {
break;
}
}

return result;
}


/**
* Calls the {@code processAction()} method of all the {@link RequestDataValueProcessor} implementations <br>
* this class holds. This method is for compatibility with Spring 4 or higher.
* @param action action of form tag. must not be null.
* @param method http method of form tag.
* @see org.springframework.web.servlet.support.RequestDataValueProcessor#processAction(javax.servlet.http.HttpServletRequest,
* java.lang.String, java.lang.String)
* @since 1.0.2
*/
public String processAction(HttpServletRequest request, String action, String method) {

String result = action;
for (RequestDataValueProcessor processor : processors) {
result = processActionInvocationHelper.invokeProcessAction(processor, request, action, method);
if (!action.equals(result)) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2013-2014 terasoluna.org
*
* 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.terasoluna.gfw.web.mvc.support;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.support.RequestDataValueProcessor;

/**
* Helper class for invoke the {@code processAction()} method of {@link RequestDataValueProcessor}.
* <p>
* This class support the both with Spring3's(legacy) signature and Spring4's signature.
* </p>
* @since 1.0.2
*/
class ProcessActionInvocationHelper {

/**
* Method of {@link RequestDataValueProcessor#processAction} on the runtime environment.
*/
private final Method processActionMethod;

/**
* Flag that indicate whether legacy signature.
*/
private final boolean isLegacySignature;

/**
* Default constructor.
*/
ProcessActionInvocationHelper() {
// Check Spring4's signature
Method targetProcessActionMethod = ReflectionUtils.findMethod(
RequestDataValueProcessor.class, "processAction",
HttpServletRequest.class, String.class, String.class);
boolean isLegacySignature = false;

if (targetProcessActionMethod == null) {
// Check Spring3's signature
targetProcessActionMethod = ReflectionUtils.findMethod(
RequestDataValueProcessor.class, "processAction",
HttpServletRequest.class, String.class);
isLegacySignature = true;
}
if (targetProcessActionMethod == null) {
throw new IllegalStateException("'processActionMethod' is not found. Should never get here!");
}
this.processActionMethod = targetProcessActionMethod;
this.isLegacySignature = isLegacySignature;
}

/**
* Invoke the {@code processAction()} method of specified {@link RequestDataValueProcessor}.
* @param requestDataValueProcessor {@link RequestDataValueProcessor} of invocation target
* @param request current request
* @param action action path of from
* @param method http method of from
* @return action that returned from specified {@link RequestDataValueProcessor}
*/
String invokeProcessAction(
RequestDataValueProcessor requestDataValueProcessor,
HttpServletRequest request, String action, String method) {
if (isLegacySignature) {
return (String) ReflectionUtils.invokeMethod(
this.processActionMethod, requestDataValueProcessor,
request, action);
} else {
return (String) ReflectionUtils.invokeMethod(
this.processActionMethod, requestDataValueProcessor,
request, action, method);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,24 @@ public class RequestDataValueProcessorAdaptor implements
RequestDataValueProcessor {

/**
* returns the action passed as argument as it is.
* returns the action passed as argument as it is. This method is for compatibility with Spring 3.
* @see org.springframework.web.servlet.support.RequestDataValueProcessor#processAction(javax.servlet.http.HttpServletRequest,
* java.lang.String)
*/
@Override
public String processAction(HttpServletRequest request, String action) {
return action;
}

/**
* returns the action passed as argument as it is. This method is for compatibility with Spring 4.
* @see org.springframework.web.servlet.support.RequestDataValueProcessor#processAction(javax.servlet.http.HttpServletRequest,
* java.lang.String, java.lang.String)
* @since 1.0.2
*/
public String processAction(HttpServletRequest request, String action, String method) {
return action;
}

/**
* returns the value passed as argument as it is.
* @see org.springframework.web.servlet.support.RequestDataValueProcessor#processFormFieldValue(javax.servlet.http.HttpServletRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class CompositeRequestDataValueProcessorTest {

private HttpServletRequest request = new MockHttpServletRequest();

RequestDataValueProcessor requestDataValueProcessor = mock(RequestDataValueProcessor.class);
RequestDataValueProcessorAdaptor requestDataValueProcessor = mock(RequestDataValueProcessorAdaptor.class);

@Before
public void setup() {
Expand All @@ -51,10 +51,16 @@ public void setup() {
@Test
public void testProcessActionSameActionAndResult() {
// Set mock behavior
// for Spring3
when(
requestDataValueProcessor.processAction(
(HttpServletRequest) (anyObject()), anyString()))
.thenReturn("action");
// for Spring4
when(
requestDataValueProcessor.processAction(
(HttpServletRequest) (anyObject()), anyString(), anyString()))
.thenReturn("action");
String result = compositeRequestDataValueProcessor.processAction(
request, "action");
assertThat(result, is("action"));
Expand All @@ -63,10 +69,16 @@ public void testProcessActionSameActionAndResult() {
@Test
public void testProcessActionDifferectActionAndResult() {
// Set mock behavior
// for Spring3
when(
requestDataValueProcessor.processAction(
(HttpServletRequest) (anyObject()), anyString()))
.thenReturn("other_action");
// for Spring4
when(
requestDataValueProcessor.processAction(
(HttpServletRequest) (anyObject()), anyString(), anyString()))
.thenReturn("other_action");
String result = compositeRequestDataValueProcessor.processAction(
request, "action");
assertThat(result, is("other_action"));
Expand Down

0 comments on commit 656f5aa

Please sign in to comment.