Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@Advise(serviceInterface=LoginContextService.class)
public void adviseSecuritySaveRequest(MethodAdviceReceiver receiver,
final @Inject @Symbol(SymbolConstants.CHARSET) String requestEncoding,
final RequestGlobals requestGlobals, final ComponentEventLinkEncoder linkEncoder,
final LinkSource linkSource, final Cookies cookies,
TapestrySessionFactory sessionFactory)
throws SecurityException, NoSuchMethodException
{
@SuppressWarnings("unchecked")
Method method = receiver.getInterface().getDeclaredMethod("saveRequest");
receiver.adviseMethod(method,
new SaveRequestMethodAdvice(requestGlobals, cookies, linkSource, linkEncoder,
requestEncoding, sessionFactory));
}
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.util.WebUtils;
import org.apache.tapestry5.EventContext;
import org.apache.tapestry5.Link;
import org.apache.tapestry5.internal.services.LinkSource;
import org.apache.tapestry5.internal.services.RequestImpl;
import org.apache.tapestry5.internal.services.ResponseImpl;
import org.apache.tapestry5.internal.services.TapestrySessionFactory;
import org.apache.tapestry5.plastic.MethodAdvice;
import org.apache.tapestry5.plastic.MethodInvocation;
import org.apache.tapestry5.services.ComponentEventLinkEncoder;
import org.apache.tapestry5.services.ComponentEventRequestParameters;
import org.apache.tapestry5.services.Cookies;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.RequestGlobals;
public class SaveRequestMethodAdvice implements MethodAdvice
{
private final RequestGlobals requestGlobals;
private final Cookies cookies;
private final LinkSource linkSource;
private final ComponentEventLinkEncoder linkEncoder;
private final String requestEncoding;
private final TapestrySessionFactory sessionFactory;
public SaveRequestMethodAdvice(RequestGlobals requestGlobals,
Cookies cookies, LinkSource linkSource,
ComponentEventLinkEncoder linkEncoder,
String requestEncoding, TapestrySessionFactory sessionFactory)
{
this.requestGlobals = requestGlobals;
this.cookies = cookies;
this.linkSource = linkSource;
this.linkEncoder = linkEncoder;
this.requestEncoding = requestEncoding;
this.sessionFactory = sessionFactory;
}
private Cookie createSavedRequestCookie()
{
String requestUri;
HttpServletRequest servletRequest = requestGlobals.getHTTPServletRequest();
Request request = requestGlobals.getRequest();
if (request == null)
{
// Not sure in what circumstances request may be null here, but it is sometimes
// So we should restore request/response to globals
// XXX Is there any better place for this?
request = new RequestImpl(servletRequest, requestEncoding, sessionFactory);
ResponseImpl response = new ResponseImpl(servletRequest, requestGlobals.getHTTPServletResponse());
requestGlobals.storeRequestResponse(request, response);
}
if (!StringUtils.equalsIgnoreCase("GET", request.getMethod()))
{
// POST request? => Redirect to target page via HTTP GET?
ComponentEventRequestParameters eventParameters =
linkEncoder.decodeComponentEventRequest(request);
if (eventParameters != null)
{
// Event URL => Redirect to target page via HTTP GET
requestUri = createPageRenderLink(eventParameters);
}
else
{
// REST API call? => Don't do redirects
return null;
}
}
else
{
ComponentEventRequestParameters eventParameters =
linkEncoder.decodeComponentEventRequest(request);
if (eventParameters != null)
{
// Event URL => Redirect to target page via HTTP GET
requestUri = createPageRenderLink(eventParameters);
}
else
{
// Page render request? => Keep the same URL
requestUri = WebUtils.getPathWithinApplication(servletRequest);
if (servletRequest.getQueryString() != null)
{
requestUri += "?" + servletRequest.getQueryString();
}
}
}
Cookie cookie = new Cookie(WebUtils.SAVED_REQUEST_KEY, requestUri);
String contextPath = servletRequest.getContextPath();
if ("".equals(contextPath)) contextPath = "/";
cookie.setPath(contextPath);
return cookie;
}
protected String createPageRenderLink(ComponentEventRequestParameters eventParameters)
{
EventContext eventContext = eventParameters.getPageActivationContext();
Link link = linkSource.createPageRenderLink(
eventParameters.getActivePageName(), true,
(Object[]) eventContext.toStrings());
String requestUri = link.toRedirectURI();
return requestUri;
}
@Override
public void advise(MethodInvocation invocation)
{
Cookie cookie = createSavedRequestCookie();
if (cookie == null)
{
cookies.removeCookieValue(WebUtils.SAVED_REQUEST_KEY);
}
else
{
requestGlobals.getHTTPServletResponse().addCookie(cookie);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment