Skip to content

Instantly share code, notes, and snippets.

@sody
Created August 1, 2012 13:57
Show Gist options
  • Save sody/3227055 to your computer and use it in GitHub Desktop.
Save sody/3227055 to your computer and use it in GitHub Desktop.
MetaDataLocator Usage
public static void contributeMetaWorker(MappedConfiguration<Class, MetaDataExtractor> configuration) {
// register meta extractor for @RequireSession annotation
configuration.addInstance(RequireSession.class, RequireSessionExtractor.class);
}
public class RequireSessionExtractor implements MetaDataExtractor<RequireSession> {
public static final String VALUE_KEY = "require-session-value";
public static final String REDIRECT_PAGE_KEY = "require-session-redirect-page";
public void extractMetaData(final MutableComponentModel model, final RequireSession annotation) {
// cache meta values in component model
model.setMeta(VALUE_KEY, annotation.value().getName());
model.setMeta(REDIRECT_PAGE_KEY, annotation.redirectPage());
}
}
public class RequireSessionFilter implements ComponentRequestFilter {
private Response response;
private ApplicationStateManager applicationStateManager;
private PageRenderLinkSource pageRenderLinkSource;
private MetaDataLocator metaDataLocator;
public RequireSessionFilter(Response response,
ApplicationStateManager applicationStateManager,
PageRenderLinkSource pageRenderLinkSource,
MetaDataLocator metaDataLocator) {
this.response = response;
this.applicationStateManager = applicationStateManager;
this.pageRenderLinkSource = pageRenderLinkSource;
this.metaDataLocator = metaDataLocator;
}
public void handleComponentEvent(ComponentEventRequestParameters parameters, ComponentRequestHandler handler)
throws IOException {
if (redirectIfObjectNotInSession(parameters.getActivePageName())) {
return;
}
handler.handleComponentEvent(parameters);
}
public void handlePageRender(PageRenderRequestParameters parameters, ComponentRequestHandler handler)
throws IOException {
if (redirectIfObjectNotInSession(parameters.getLogicalPageName())) {
return;
}
handler.handlePageRender(parameters);
}
private boolean redirectIfObjectNotInSession(String pageName)
throws IOException {
// find meta in component model
final Class<?> stateClass = metaDataLocator.findMeta(RequireSessionExtractor.VALUE_KEY, pageName, Class.class);
if (stateClass != null && !applicationStateManager.exists(stateClass)) {
final String redirectPage = metaDataLocator.findMeta(RequireSessionExtractor.REDIRECT_PAGE_KEY, pageName, String.class);
final Link redirectPageLink = pageRenderLinkSource.createPageRenderLink(redirectPage);
response.sendRedirect(redirectPageLink);
return true;
}
return false;
}
}
@josetesan
Copy link

Uhm .. should work fine, but looks like my expectedObjectInSession is a custom Object ( of class UserInfo ), a container bean.

And this does not work properly, as it can't find a Coercion from java.lang.String ( the name ) to java.lang.Class ( the bean type?)

Thanks anyway.

@sody
Copy link
Author

sody commented Aug 3, 2012

Sorry, I didn't try to run this example, just wrote a piece of code. You can add coercion from String to Class to fix this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment