Skip to content

Instantly share code, notes, and snippets.

@dmitrygusev
Created October 14, 2013 18:51
Show Gist options
  • Save dmitrygusev/6980210 to your computer and use it in GitHub Desktop.
Save dmitrygusev/6980210 to your computer and use it in GitHub Desktop.

Revisions

  1. dmitrygusev created this gist Oct 14, 2013.
    17 changes: 17 additions & 0 deletions AppModule.java
    Original 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();
    // ...
    62 changes: 62 additions & 0 deletions OfflineLinkSource.java
    Original 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()));
    }
    }