Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Download here: mgnlsupport-1.0-SNAPSHOT.jar

Warning

Please change / update module dependencies based on your webapp

Using and Configuring

After successfully installed the module, when accessing a nonexistence page such as http://localhost:8080/travel/ abc, you will have this:

...

Basically I just created a custom module with below structure and provided you with a site aware URI mapping as below:

Sample code

Class: info.magnolia.virtualuri.mapping.SiteAwareExceptionMapping

Code Block
languagejava
titleSiteAwareExceptionMapping
linenumberstrue
collapsetrue
package info.magnolia.virtualuri.mapping;

import java.net.URI;
import java.util.Optional;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import info.magnolia.cms.util.UrlPattern;
import info.magnolia.context.MgnlContext;
import info.magnolia.module.site.Site;
import info.magnolia.multisite.sites.MultiSiteManager;

public class SiteAwareExceptionMapping extends DefaultVirtualUriMapping {
    
    public static final String SERVLET_ERROR_REQUEST_URI_ATTRIBUTE_NAME = "javax.servlet.error.request_uri";
    
    private String siteName;

    @Injectprivate MultiSiteManagerString msmtoUri;
    
    @Override
    public Optional<Result> mapUri(URI uri) {
        UrlPattern pattern = getPattern();
        if (pattern != null && pattern.match(uri.getPath())) {
            String ctxPath = MgnlContext.getContextPath();
            HttpServletRequest req = MgnlContext.getWebContext().getRequest();
            String exceptionUri = req.getAttribute(SERVLET_ERROR_REQUEST_URI_ATTRIBUTE_NAME).toString(return Optional.of(new Result(toUri, toUri.length(), this));
            String resourceUri = exceptionUri.substring(ctxPath.length());} 
            Site site = msm.getAssignedSite(req.getServerName(), resourceUrireturn Optional.empty();
    }

    public    if (StringUtils.equalsIgnoreCase(siteName, site.getName())String getSiteName() {
                return super.mapUri(uri)siteName;
            }

    public void setSiteName(String siteName) }{
 
       this.siteName return Optional.empty()= siteName;
    }

    public String getSiteNamegetToUri() {
        return siteNametoUri;
    }

    public void setSiteNamesetToUri(String siteNametoUri) {
        this.siteNametoUri = siteNametoUri;
    }
    
    
}


This source code is provided without any guarantee as a community creative & publicly available one. Please use it with cares and a bit or risks.

Sample configuration

File config.modules.mgnlsupport.virtualUriMappings.fallback-exception-mapping.yaml

Code Block
'fallback-exception-mapping': 
  'class': 'info.magnolia.virtualuri.mapping.SiteAwareExceptionMapping'
  'fromUri': '/.exception'
  'siteName': 'fallback'
  'toUri': 'forward:/exception'

File config.modules.mgnlsupport.virtualUriMappings.travel-exception-mapping.yaml

Code Block
'travel-exception-mapping': 
  'class': 'info.magnolia.virtualuri.mapping.SiteAwareExceptionMapping'
  'fromUri': '/.exception'
  'siteName': 'travel'
  'toUri': 'forward:/travel/exception'


References

This is an upgraded version of How to setup a custom 404 handler as of January 2018 because the previous one has been out dated and did not maintained for such a long time. We're trying to bring values to customers who are using and contributing to Magnolia CMS.

...