package org.rendersnake.ext.spring;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.rendersnake.HtmlCanvas;
import org.rendersnake.HtmlCanvasFactory;
import org.rendersnake.PageContext;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;
/**
* HtmlCanvasArgumentResolver is a Spring Helper that is used
* to prepare html arguments before invoking a Controller method.
* <p><pre>
* {@code
* <!-- This Resolver exists for renderSnake HtmlCanvas parameter processing -->
* <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
* <property name="customArgumentResolver">
* <bean class="org.rendersnake.ext.spring.HtmlCanvasArgumentResolver">
* <property name="shareQueryParameters" value="true"></property>
* <property name="shareSessionAttributes" value="true"></property>
* <property name="shareRequestParameters" value="true"></property>
* </bean>
* </property>
* </bean>}
* </pre></p>
* @author e.micklei
**/
public class HtmlCanvasArgumentResolver implements WebArgumentResolver {
private boolean shareRequestAttributes = false;
private boolean shareSessionAttributes = false;
private boolean shareRequestParameters = false;
/* (non-Javadoc)
* @see org.springframework.web.bind.support.WebArgumentResolver#resolveArgument(org.springframework.core.MethodParameter, org.springframework.web.context.request.NativeWebRequest)
*/
public Object resolveArgument(MethodParameter methodParameter,
NativeWebRequest webRequest) throws Exception {
// only accept HtmlCanvas typed parameter
if (methodParameter.getParameterType() != HtmlCanvas.class)
return UNRESOLVED;
HttpServletRequest request = (HttpServletRequest)(webRequest.getNativeRequest());
HttpServletResponse response = (HttpServletResponse)(webRequest.getNativeResponse());
HtmlCanvas canvas = HtmlCanvasFactory.createCanvas(request,response,response.getWriter());
PageContext context = canvas.getPageContext();
boolean empty = true;
if (this.shareSessionAttributes) {
HttpSession session = request.getSession(false);
if (session != null) {
Enumeration<?> enumerator = session.getAttributeNames();
while(enumerator.hasMoreElements()) {
String name = (String)enumerator.nextElement();
context.set(name,session.getAttribute(name));
empty = false;
}
if (!empty) context.attributes.push();
}
}
empty = true;
if (this.shareRequestParameters) {
Enumeration<?> enumerator = request.getParameterNames();
while(enumerator.hasMoreElements()) {
String name = (String)enumerator.nextElement();
context.set(name,request.getParameter(name));
empty = false;
}
if (!empty) context.attributes.push();
}
empty = true;
if (this.shareRequestAttributes) {
Enumeration<?> enumerator = request.getAttributeNames();
while(enumerator.hasMoreElements()) {
String name = (String)enumerator.nextElement();
if (!name.startsWith("org.springframework")) { // no need for spring internals
context.set(name,request.getAttribute(name));
empty = false;
}
}
if (!empty) context.attributes.push();
}
return canvas;
}
public boolean isShareSessionAttributes() {
return shareSessionAttributes;
}
public void setShareSessionAttributes(boolean shareSessionAttributes) {
this.shareSessionAttributes = shareSessionAttributes;
}
public boolean isShareRequestAttributes() {
return shareRequestAttributes;
}
public void setShareRequestAttributes(boolean shareRequestAttributes) {
this.shareRequestAttributes = shareRequestAttributes;
}
public boolean isShareRequestParameters() {
return shareRequestParameters;
}
public void setShareRequestParameters(boolean shareRequestParameters) {
this.shareRequestParameters = shareRequestParameters;
}
}