Search in sources :

Example 16 with PageRenderRequestParameters

use of org.apache.tapestry5.services.PageRenderRequestParameters in project tapestry-5 by apache.

the class LinkSourceImplTest method testPageLinkCreation.

private void testPageLinkCreation(String pageName, boolean overrideContext, Object... context) {
    PageActivationContextCollector collector = mockPageActivationContextCollector();
    LinkCreationListener2 listener = mockLinkCreationListener2();
    ComponentEventLinkEncoder linkEncoder = mockComponentEventLinkEncoder();
    Link link = mockLink();
    ComponentClassResolver resolver = mockComponentClassResolver();
    String canonical = "CanonicalPageName";
    RequestGlobals globals = mockRequestGlobals();
    // Ensure that the loopback logic is off; loopback logic is fully tested via an
    // integration test.
    expect(globals.getActivePageName()).andReturn("someOtherPage");
    train_canonicalizePageName(resolver, pageName, canonical);
    if (!overrideContext)
        train_collectPageActivationContext(collector, canonical, context);
    PageRenderRequestParameters parameters = new PageRenderRequestParameters(canonical, new ArrayEventContext(typeCoercer, context), false);
    expect(linkEncoder.createPageRenderLink(parameters)).andReturn(link);
    listener.createdPageRenderLink(link, parameters);
    replay();
    LinkSource source = new LinkSourceImpl(null, collector, typeCoercer, resolver, linkEncoder, globals, null, emptyConfiguration);
    source.getLinkCreationHub().addListener(listener);
    Object[] passedContext = overrideContext ? context : new Object[0];
    Link returnedLink = source.createPageRenderLink(pageName, overrideContext, passedContext);
    assertSame(returnedLink, link);
    verify();
}
Also used : PageRenderRequestParameters(org.apache.tapestry5.services.PageRenderRequestParameters) ComponentEventLinkEncoder(org.apache.tapestry5.services.ComponentEventLinkEncoder) ComponentClassResolver(org.apache.tapestry5.services.ComponentClassResolver) LinkCreationListener2(org.apache.tapestry5.services.LinkCreationListener2) RequestGlobals(org.apache.tapestry5.http.services.RequestGlobals) Link(org.apache.tapestry5.http.Link)

Example 17 with PageRenderRequestParameters

use of org.apache.tapestry5.services.PageRenderRequestParameters in project tapestry-5 by apache.

the class ComponentEventLinkEncoderImpl method decodePageRenderRequest.

public PageRenderRequestParameters decodePageRenderRequest(Request request) {
    boolean explicitLocale = false;
    // The extended name may include a page activation context. The trick is
    // to figure out where the logical page name stops and where the
    // activation context begins. Further, strip out the leading slash.
    String path = request.getPath();
    if (applicationFolderPrefix != null) {
        path = removeApplicationPrefix(path);
    }
    // TAPESTRY-1343: Sometimes path is the empty string (it should always be at least a slash,
    // but Tomcat may return the empty string for a root context request).
    String extendedName = path.length() == 0 ? path : path.substring(1);
    // Ignore trailing slashes in the path.
    while (extendedName.endsWith("/")) {
        extendedName = extendedName.substring(0, extendedName.length() - 1);
    }
    int slashx = extendedName.indexOf('/');
    // So, what can we have left?
    // 1. A page name
    // 2. A locale followed by a page name
    // 3. A page name followed by activation context
    // 4. A locale name, page name, activation context
    // 5. Just activation context (for root Index page)
    // 6. A locale name followed by activation context
    String possibleLocaleName = slashx > 0 ? extendedName.substring(0, slashx) : extendedName;
    if (localizationSetter.setLocaleFromLocaleName(possibleLocaleName)) {
        extendedName = slashx > 0 ? extendedName.substring(slashx + 1) : "";
        explicitLocale = true;
    }
    slashx = extendedName.length();
    boolean atEnd = true;
    while (slashx > 0) {
        String pageName = extendedName.substring(0, slashx);
        String pageActivationContext = atEnd ? "" : extendedName.substring(slashx + 1);
        PageRenderRequestParameters parameters = checkIfPage(request, pageName, pageActivationContext);
        if (parameters != null) {
            return parameters;
        }
        // Work backwards, splitting at the next slash.
        slashx = extendedName.lastIndexOf('/', slashx - 1);
        atEnd = false;
    }
    // OK, maybe its all page activation context for the root Index page.
    PageRenderRequestParameters result = checkIfPage(request, "", extendedName);
    if (result != null && !explicitLocale) {
        setLocaleFromRequest(request);
    }
    return result;
}
Also used : PageRenderRequestParameters(org.apache.tapestry5.services.PageRenderRequestParameters)

Example 18 with PageRenderRequestParameters

use of org.apache.tapestry5.services.PageRenderRequestParameters in project tapestry-5 by apache.

the class ComponentEventLinkEncoderImpl method createPageRenderLink.

public Link createPageRenderLink(PageRenderRequestParameters parameters) {
    StringBuilder builder = new StringBuilder(BUFFER_SIZE);
    // Build up the absolute URI.
    String activePageName = parameters.getLogicalPageName();
    builder.append(contextPath);
    encodeAppFolderAndLocale(builder);
    builder.append(SLASH);
    String encodedPageName = encodePageName(activePageName);
    builder.append(encodedPageName);
    appendContext(encodedPageName.length() > 0, parameters.getActivationContext(), builder);
    Link link = new LinkImpl(builder.toString(), false, requestSecurityManager.checkPageSecurity(activePageName), response, contextPathEncoder, baseURLSource);
    if (parameters.isLoopback()) {
        link.addParameter(TapestryConstants.PAGE_LOOPBACK_PARAMETER_NAME, "t");
    }
    return link;
}
Also used : Link(org.apache.tapestry5.http.Link)

Example 19 with PageRenderRequestParameters

use of org.apache.tapestry5.services.PageRenderRequestParameters in project tapestry-5 by apache.

the class LinkSourceImpl method createPageRenderLink.

public Link createPageRenderLink(String pageName, boolean override, Object... pageActivationContext) {
    // Resolve the page name to its canonical format (the best version for URLs). This also
    // validates
    // the page name.
    String canonical = resolver.canonicalizePageName(pageName);
    Object[] context = (override || pageActivationContext.length != 0) ? pageActivationContext : contextCollector.collectPageActivationContext(canonical);
    boolean loopback = canonical.equals(requestGlobals.getActivePageName()) && pageCache.get(pageName).hasResetListeners();
    PageRenderRequestParameters parameters = new PageRenderRequestParameters(canonical, new ArrayEventContext(typeCoercer, context), loopback);
    Link link = linkEncoder.createPageRenderLink(parameters);
    for (LinkCreationListener2 listener : listeners) listener.createdPageRenderLink(link, parameters);
    return link;
}
Also used : PageRenderRequestParameters(org.apache.tapestry5.services.PageRenderRequestParameters) LinkCreationListener2(org.apache.tapestry5.services.LinkCreationListener2) Link(org.apache.tapestry5.http.Link)

Example 20 with PageRenderRequestParameters

use of org.apache.tapestry5.services.PageRenderRequestParameters in project tapestry-5 by apache.

the class ComponentEventLinkEncoderImplTest method just_the_locale_name.

@Test
public void just_the_locale_name() throws Exception {
    ComponentClassResolver resolver = mockComponentClassResolver();
    Request request = mockRequest();
    Response response = mockResponse();
    LocalizationSetter ls = mockLocalizationSetter();
    train_getPath(request, "/en");
    train_setLocaleFromLocaleName(ls, "en", true);
    train_isPageName(resolver, "", false);
    replay();
    ComponentEventLinkEncoderImpl linkEncoder = new ComponentEventLinkEncoderImpl(resolver, contextPathEncoder, ls, response, null, null, null, true, null, "", null, null);
    PageRenderRequestParameters parameters = linkEncoder.decodePageRenderRequest(request);
    assertNull(parameters);
    verify();
}
Also used : Response(org.apache.tapestry5.http.services.Response) PageRenderRequestParameters(org.apache.tapestry5.services.PageRenderRequestParameters) ComponentClassResolver(org.apache.tapestry5.services.ComponentClassResolver) Request(org.apache.tapestry5.http.services.Request) LocalizationSetter(org.apache.tapestry5.services.LocalizationSetter) Test(org.testng.annotations.Test)

Aggregations

PageRenderRequestParameters (org.apache.tapestry5.services.PageRenderRequestParameters)24 Test (org.testng.annotations.Test)14 Request (org.apache.tapestry5.http.services.Request)12 ComponentClassResolver (org.apache.tapestry5.services.ComponentClassResolver)11 Link (org.apache.tapestry5.http.Link)10 Response (org.apache.tapestry5.http.services.Response)10 ComponentEventLinkEncoder (org.apache.tapestry5.services.ComponentEventLinkEncoder)9 LocalizationSetter (org.apache.tapestry5.services.LocalizationSetter)8 MetaDataLocator (org.apache.tapestry5.services.MetaDataLocator)8 EmptyEventContext (org.apache.tapestry5.internal.EmptyEventContext)6 EventContext (org.apache.tapestry5.EventContext)3 RequestGlobals (org.apache.tapestry5.http.services.RequestGlobals)3 ContextPathEncoder (org.apache.tapestry5.services.ContextPathEncoder)3 LinkCreationListener2 (org.apache.tapestry5.services.LinkCreationListener2)3 Page (org.apache.tapestry5.internal.structure.Page)2 PageRenderRequestHandler (org.apache.tapestry5.services.PageRenderRequestHandler)2 IOException (java.io.IOException)1 Locale (java.util.Locale)1 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)1 ArrayEventContext (org.apache.tapestry5.internal.services.ArrayEventContext)1