Created
October 14, 2013 18:51
-
-
Save dmitrygusev/6980210 to your computer and use it in GitHub Desktop.
Revisions
-
dmitrygusev created this gist
Oct 14, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ // Here is how I use it in my AppModule.java, note there may be no any HTTP Request when this code executing: import xxx.web.pages.oauth.Callback; // ... @Contribute(OAuthServiceCatalog.class) public void contributeOAuthServices(MappedConfiguration<String, OAuthServiceConfig> configuration, // ... OfflineLinkSource offlineLinkSource) { // ... Link callback = offlineLinkSource.createPageRenderLink(Callback.class); callback.addParameter(Button.SERVICE, XXX_EXTERNAL_SYSTEM); String uri = callback.toAbsoluteURI(); // ... This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,62 @@ package com.anjlab.tapestry5.services; import org.apache.tapestry5.Link; import org.apache.tapestry5.SymbolConstants; import org.apache.tapestry5.internal.services.LinkImpl; import org.apache.tapestry5.ioc.annotations.Inject; import org.apache.tapestry5.ioc.annotations.Symbol; import org.apache.tapestry5.services.BaseURLSource; import org.apache.tapestry5.services.ComponentClassResolver; public class OfflineLinkSource { public static final String SERVICE_ID = "OfflineLinkSource"; private @Inject @Symbol(SymbolConstants.SECURE_ENABLED) boolean secureEnabled; private @Inject BaseURLSource baseURLSource; private @Inject ComponentClassResolver resolver; public Link createPageRenderLink(final String pageName) { return new LinkImpl("/", false, null, null, null, null) { @Override public String toAbsoluteURI() { StringBuilder builder = new StringBuilder(baseURLSource.getBaseURL(secureEnabled)) .append('/') .append(resolver.canonicalizePageName(pageName).toLowerCase()); String sep = "?"; for (String name : getParameterNames()) { String[] values = getParameterValues(name); for (String value : values) { builder.append(sep); // We assume that the name is URL safe and that the value will already have been URL // encoded if it is not known to be URL safe. builder.append(name); builder.append("="); builder.append(value); sep = "&"; } } return builder.toString(); } }; } public Link createPageRenderLink(Class<?> pageClass) { return createPageRenderLink(resolver.resolvePageClassNameToPageName(pageClass.getName())); } }