Skip to content

Instantly share code, notes, and snippets.

@dmitrygusev
Created October 14, 2013 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitrygusev/6980210 to your computer and use it in GitHub Desktop.
Save dmitrygusev/6980210 to your computer and use it in GitHub Desktop.
// 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();
// ...
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()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment