avatar
redirect Page AEM Sling Model AEM

Firstly, let us assume a fundamental chain of interconnected pages within an AEM project. Here, our objective is to address logic on Login component and focus on handling the logic to navigate to the Dashboard page.

Next up, when you are using the Login Component in your AEM project, you declare a variable called redirectionPath in the properties and conduct to authoring it directly in the Touch UI dialog.

Since the Login page and Dashboard page each use a different template, we will approach new logic by leverage the Constructor method of the Login Sling Model.

» LoginModelImpl

package com.flagtick.core.models.impl;

import com.adobe.acs.commons.models.injectors.annotation.AemObject;
import com.day.cq.wcm.api.Page;
import com.flagtick.core.models.LoginModel;
import com.flagtick.core.utils.Constants;
import com.flagtick.core.utils.LinkUtils;
import com.flagtick.core.utils.PageUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import javax.annotation.PostConstruct;
import java.util.Iterator;

@Model(adaptables = SlingHttpServletRequest.class,
        adapters = LoginModel.class,
        defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL,
        resourceType = LoginModelImpl.RESOURCE_TYPE)
public class LoginModelImpl implements LoginModel {

	static final String RESOURCE_TYPE = "flagtick/components/content/login";

	@SlingObject
    private ResourceResolver resolver;

    @AemObject
    private Page currentPage;

	private String redirectionPath;

    @PostConstruct
    protected void init() {
        redirectionPath = "";
        Page root = currentPage.getAbsoluteParent(3);
        Iterator<Page> iterator = root.listChildren();
        while (iterator.hasNext()) {
            Page child = iterator.next();
            if (PageUtils.isPageOfTemplate(child, Constants.DASHBOARD_TEMPLATE)) {
                redirectionPath = resolver.map(LinkUtils.sanitizeLink(child.getPath()));
            }
        }
    }

    @Override
    public String getRedirectionPath() {
        return redirectionPath;
    }
}

Note: We will look at all the child pages under the English page, check what type of pages they are, and compare that with the DASHBOARD_TEMPLATE. This helps us set up where to go after a successful login.

» LoginModel

package com.flagtick.core.models;

public interface LoginModel {
    @Override
    public String getRedirectionPath() {
        return redirectionPath;
    }
}

» Constants

package com.flagtick.core.utils;

public class Constants {
    public static final String DASHBOARD_TEMPLATE = "/conf/flagtick/settings/wcm/templates/dashboard-template";
}

» LinkUtils

package com.flagtick.core.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LinkUtils {

	public static final String CONTENT_DAM_ROOT_PATH = "/content/dam";
    public static final String HTML_EXTENSION = ".html";
	public static final String DEFAULT_PROTOCOL = "https://";
    private static final Logger LOG = LoggerFactory.getLogger(LinkUtils.class);

    private LinkUtils() {
        throw new IllegalStateException("Utility class");
    }
    public static String sanitizeLink(String link) {
        if (StringUtils.isBlank(link)) {
            return "";
        } else if (link.startsWith("/content/") && !link.startsWith(CONTENT_DAM_ROOT_PATH)) {
            return link + HTML_EXTENSION;
        }
        return fixExternalLink(link);
    }
	private static String fixExternalLink(String url) {
        if (!isInternalLink(url) && !isExternalLink(url)) {
            return DEFAULT_PROTOCOL.concat(url);
        }
        return url;
    }
}

» PageUtils

package com.flagtick.core.utils;

import com.day.cq.wcm.api.Page;

public class PageUtils {

	public static Boolean isPageOfTemplate(Page page, String templateName) {
        if (page != null && templateName != null && page.getTemplate() != null) {
            return templateName.equalsIgnoreCase(page.getTemplate().getPath());
        } else {
            return false;
        }
    }
}

Or you can review relationships among these packages and classes represented in a simple tree diagram.

+-- com
    +-- flagtick
        +-- core
            +-- utils
            |   +-- LinkUtils
            |   +-- PageUtils
            |   +-- Constants
            |
            +-- models
                +-- LoginModel (interface)
                |
                +-- models
                    +-- impl
                        +-- LoginModelImpl

If you prefer an alternative approach, you can use resourceType=granite/ui/components/coral/foundation/form/pathbrowser to facilitate authoring via the Touch UI dialog in the Login component.

» _cq_dialog/.content.xml

<redirectionPath
	jcr:primaryType="nt:unstructured"
	sling:resourceType="granite/ui/components/coral/foundation/form/pathbrowser"
	fieldLabel="Redirection Page"
	name="./redirectionPath"
	rootPath="/content/flagtick/us/en"/>
You need to login to do this manipulation!