Created
August 1, 2012 13:57
-
-
Save sody/3227055 to your computer and use it in GitHub Desktop.
MetaDataLocator Usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void contributeMetaWorker(MappedConfiguration<Class, MetaDataExtractor> configuration) { | |
// register meta extractor for @RequireSession annotation | |
configuration.addInstance(RequireSession.class, RequireSessionExtractor.class); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.