Permalink
Browse files

Merge pull request #268 from hdiv/master

RequestDataValueProcessor interface support
  • Loading branch information...
2 parents 5de560f + 26ab642 commit d1a42f26e8519c20dec6b056904ee7e85e5821cf @graemerocher graemerocher committed Oct 8, 2012
@@ -22,6 +22,12 @@ import org.codehaus.groovy.grails.web.mapping.UrlCreator
import org.codehaus.groovy.grails.web.mapping.UrlMappingsHolder
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
import org.springframework.web.context.request.RequestContextHolder
+import org.springframework.web.servlet.support.RequestDataValueProcessor
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
/**
* Implementation of the chain() method for controllers.
@@ -31,6 +37,8 @@ import org.springframework.web.context.request.RequestContextHolder
*/
class ChainMethod {
+ private static final Log LOG = LogFactory.getLog(RedirectDynamicMethod.class);
+
static invoke(target, Map args = [:]) {
def controller = args.controller ?: GrailsNameUtils.getLogicalPropertyName(
target.class.name, ControllerArtefactHandler.TYPE)
@@ -74,7 +82,17 @@ class ChainMethod {
UrlCreator creator = mappings.getReverseMapping(controller, action, plugin, params)
def response = webRequest.getCurrentResponse()
- def url = response.encodeRedirectURL(creator.createURL(controller, action, plugin, params, 'utf-8'))
+ def url = creator.createURL(controller, action, plugin, params, 'utf-8')
+
+ if(appCtx.containsBean("requestDataValueProcessor")) {
+ RequestDataValueProcessor valueProcessor = appCtx.getBean("requestDataValueProcessor")
+ if(valueProcessor != null) {
+ HttpServletRequest request = webRequest.getCurrentRequest()
+ url = response.encodeRedirectURL(valueProcessor.processUrl(request,url))
+ }
+ } else {
+ url = response.encodeRedirectURL(url)
+ }
response.sendRedirect url
}
}
@@ -33,6 +33,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.validation.Errors;
import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.servlet.support.RequestDataValueProcessor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -65,6 +66,7 @@
private boolean useJessionId = false;
private Collection<RedirectEventListener> redirectListeners;
private LinkGenerator linkGenerator;
+ private RequestDataValueProcessor requestDataValueProcessor = null;
/**
*/
@@ -146,6 +148,7 @@ public Object invoke(Object target, String methodName, Object[] arguments) {
// which includes the contextPath
argMap.put(LinkGenerator.ATTRIBUTE_CONTEXT_PATH, BLANK);
return redirectResponse(requestLinkGenerator.getServerBaseURL(), requestLinkGenerator.link(argMap), request, response, permanent);
+
}
private LinkGenerator getLinkGenerator(GrailsWebRequest webRequest) {
@@ -170,8 +173,8 @@ private Object redirectResponse(String serverBaseURL, String actualUri, HttpServ
if (LOG.isDebugEnabled()) {
LOG.debug("Executing redirect with response ["+response+"]");
}
-
- String absoluteURL = actualUri.contains("://") ? actualUri : serverBaseURL + actualUri;
+ String processedActualUri = processedUrl(actualUri, request);
+ String absoluteURL = processedActualUri.contains("://") ? processedActualUri : serverBaseURL + processedActualUri;
String redirectUrl = useJessionId ? response.encodeRedirectURL(absoluteURL) : absoluteURL;
int status = permanent ? HttpServletResponse.SC_MOVED_PERMANENTLY : HttpServletResponse.SC_MOVED_TEMPORARILY;
@@ -184,7 +187,7 @@ private Object redirectResponse(String serverBaseURL, String actualUri, HttpServ
}
}
- request.setAttribute(GRAILS_REDIRECT_ISSUED, actualUri);
+ request.setAttribute(GRAILS_REDIRECT_ISSUED, processedActualUri);
return null;
}
@@ -204,4 +207,24 @@ else if (actionRef instanceof Closure) {
}
return actionName;
}
+
+
+ /**
+ * getter to obtain RequestDataValueProcessor from
+ */
+ private void initRequestDataValueProcessor() {
+ GrailsWebRequest webRequest = (GrailsWebRequest)RequestContextHolder.currentRequestAttributes();
+ ApplicationContext applicationContext = webRequest.getApplicationContext();
+ if (requestDataValueProcessor == null && applicationContext.containsBean("requestDataValueProcessor")){
+ requestDataValueProcessor = applicationContext.getBean("requestDataValueProcessor",RequestDataValueProcessor.class);
+ }
+ }
+ private String processedUrl(String link, HttpServletRequest request) {
+ initRequestDataValueProcessor();
+ String currentLink = link;
+ if(requestDataValueProcessor != null) {
+ currentLink = requestDataValueProcessor.processUrl(request,link);
+ }
+ return currentLink;
+ }
}
@@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
+import org.springframework.web.servlet.support.RequestDataValueProcessor
/**
* The base application tag library for Grails many of which take inspiration from Rails helpers (thanks guys! :)
@@ -58,6 +59,8 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
boolean useJsessionId = false
boolean hasResourceProcessor = false
+ def requestDataValueProcessor = null
+
void afterPropertiesSet() {
def config = applicationContext.getBean(GrailsApplication.APPLICATION_ID).config
if (config.grails.views.enable.jsessionid instanceof Boolean) {
@@ -158,7 +161,9 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
}
// Use resources plugin if present, but only if file is specified - resources require files
// But users often need to link to a folder just using dir
- return ((hasResourceProcessor && attrs.file) ? r.resource(attrs) : linkGenerator.resource(attrs))
+ def url = ((hasResourceProcessor && attrs.file) ? r.resource(attrs) : linkGenerator.resource(attrs));
+
+ return url?processedUrl("${url}",request):url;
}
/**
@@ -175,11 +180,12 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
if (hasResourceProcessor) {
return r.img(attrs)
} else {
- def uri = attrs.uri ?: resource(attrs)
+ def uri = attrs.uri ?processedUrl(attrs.uri,request): resource(attrs)
def excludes = ['dir', 'uri', 'file', 'plugin']
def attrsAsString = attrsToString(attrs.findAll { !(it.key in excludes) })
- return "<img src=\"${uri.encodeAsHTML()}\"${attrsAsString} />"
+ def imgSrc = uri.encodeAsHTML()
+ return "<img src=\"${imgSrc}\"${attrsAsString} />"
}
}
@@ -322,7 +328,7 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
// Allow attrs to overwrite any constants
attrs.each { typeInfo.remove(it.key) }
- out << writer(uri, typeInfo, attrs)
+ out << writer(processedUrl(uri,request), typeInfo, attrs)
out << "\r\n"
}
@@ -365,6 +371,7 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
urlAttrs.params = params
}
def generatedLink = linkGenerator.link(attrs, request.characterEncoding)
+ generatedLink = processedUrl(generatedLink,request);
if (useJsessionId) {
return response.encodeURL(generatedLink)
@@ -435,4 +442,24 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
}
return Metadata.current[attrs.name]
}
+
+ /**
+ * getter to obtain RequestDataValueProcessor from
+ */
+ private initRequestDataValueProcessor() {
+ if (requestDataValueProcessor == null && grailsAttributes.getApplicationContext().containsBean("requestDataValueProcessor")){
+ requestDataValueProcessor = grailsAttributes.getApplicationContext().getBean("requestDataValueProcessor")
+ }
+ }
+ Closure processedUrl = { link,request ->
+ if(!link) {
+ throwTagError('processedUrl missing required attribute ["link"]')
+ }
+ initRequestDataValueProcessor();
+ def currentLink = link;
+ if(requestDataValueProcessor != null) {
+ currentLink = requestDataValueProcessor.processUrl(request,link);
+ }
+ return currentLink;
+ }
}
Oops, something went wrong.

0 comments on commit d1a42f2

Please sign in to comment.