public CmsResource initResource(
        CmsResource resource,
        CmsObject cms,
        HttpServletRequest req,
        HttpServletResponse res) throws CmsResourceInitException, CmsPermissionViolationException {

        // only do something if the resource was not found
        if (resource == null) {
            String uri = cms.getRequestContext().getUri();
            // check if the resource starts with the PERMALINK_HANDLER
            Matcher matcher = m_simplePermalinkPattern.matcher(uri);
            if (matcher.find()) {
                CmsResource resource1 = resource;
                // get the id of the real resource
                String id = matcher.group(1);
                String storedSiteRoot = cms.getRequestContext().getSiteRoot();
                try {
                    // we now must switch to the root site to read the resource
                    cms.getRequestContext().setSiteRoot("/");
                    // read the resource
                    boolean online = cms.getRequestContext().getCurrentProject().isOnlineProject();
                    CmsResourceFilter filter = online ? CmsResourceFilter.DEFAULT : CmsResourceFilter.IGNORE_EXPIRATION;
                    resource1 = cms.readDefaultFile(id, filter);
                } catch (CmsPermissionViolationException e) {
                    throw e;
                } catch (Throwable e) {
                    CmsMessageContainer msg = Messages.get().container(Messages.ERR_PERMALINK_1, id);
                    if (LOG.isErrorEnabled()) {
                        LOG.error(msg.key(), e);
                    }
                    throw new CmsResourceInitException(msg, e);
                } finally {
                    // restore the siteroot
                    cms.getRequestContext().setSiteRoot(storedSiteRoot);
                    // resource may be null in case of an error
                    if (resource1 != null) {
                        // modify the uri to the one of the real resource
                        cms.getRequestContext().setUri(cms.getSitePath(resource1));
                    }
                }
                resource = resource1;
            } else {
                matcher = m_detailPattern.matcher(uri);
                // detail page permalink. Handle the cases 'getI18NInfo' and 'showResource' differently:
                // In the 'showResource' case, we do a redirect to the real detail page URL
                // In the 'getI18NInfo' case, we return the container page so the locale in the CmsRequestContext is set correctly for the 'showResource' case
                if (matcher.find()) {
                    try {
                        CmsUUID pageId = new CmsUUID(matcher.group(1));
                        CmsUUID detailId = new CmsUUID(matcher.group(2));
                        CmsResource pageResource = cms.readResource(pageId);
                        if (res != null) {
                            CmsResource detailResource = cms.readResource(detailId);
                            String detailName = cms.getDetailName(
                                detailResource,
                                cms.getRequestContext().getLocale(), // the locale in the request context should be the locale of the container page
                                OpenCms.getLocaleManager().getDefaultLocales());
                            CmsResource parentFolder;
                            if (pageResource.isFile()) {
                                parentFolder = cms.readParentFolder(pageResource.getStructureId());
                            } else {
                                parentFolder = pageResource;
                            }
                            String baseLink = OpenCms.getLinkManager().substituteLink(cms, parentFolder);
                            String redirectLink = baseLink + (baseLink.endsWith("/") ? "" : "/") + detailName;
                            CmsResourceInitException resInitException = new CmsResourceInitException(getClass());

                            resInitException.setClearErrors(true);
                            res.sendRedirect(redirectLink);
                            throw resInitException;
                        } else {
                            // we're being called from getI18NInfo; assume that the locale for the container page is the locale we want
                            return pageResource;
                        }
                    } catch (CmsResourceInitException e) {
                        throw e;
                    } catch (Exception e) {
                        LOG.error(e.getLocalizedMessage(), e);
                        throw new CmsResourceInitException(getClass());
                    }
                }
                return null;
            }
        }
        return resource;
    }
public CmsResource initResource ( CmsResource resource , CmsObject cms , HttpServletRequest req , HttpServletResponse res ) throws CmsResourceInitException , CmsPermissionViolationException { if ( resource == null ) { String uri = cms . getRequestContext ( ) . getUri ( ) ; Matcher matcher = m_simplePermalinkPattern . matcher ( uri ) ; if ( matcher . find ( ) ) { CmsResource resource1 = resource ; String id = matcher . group ( 1 ) ; String storedSiteRoot = cms . getRequestContext ( ) . getSiteRoot ( ) ; try { cms . getRequestContext ( ) . setSiteRoot ( "/" ) ; boolean online = cms . getRequestContext ( ) . getCurrentProject ( ) . isOnlineProject ( ) ; CmsResourceFilter filter = online ? CmsResourceFilter . DEFAULT : CmsResourceFilter . IGNORE_EXPIRATION ; resource1 = cms . readDefaultFile ( id , filter ) ; } catch ( CmsPermissionViolationException e ) { throw e ; } catch ( Throwable e ) { CmsMessageContainer msg = Messages . get ( ) . container ( Messages . ERR_PERMALINK_1 , id ) ; if ( LOG . isErrorEnabled ( ) ) { LOG . error ( msg . key ( ) , e ) ; } throw new CmsResourceInitException ( msg , e ) ; } finally { cms . getRequestContext ( ) . setSiteRoot ( storedSiteRoot ) ; if ( resource1 != null ) { cms . getRequestContext ( ) . setUri ( cms . getSitePath ( resource1 ) ) ; } } resource = resource1 ; } else { matcher = m_detailPattern . matcher ( uri ) ; if ( matcher . find ( ) ) { try { CmsUUID pageId = new CmsUUID ( matcher . group ( 1 ) ) ; CmsUUID detailId = new CmsUUID ( matcher . group ( 2 ) ) ; CmsResource pageResource = cms . readResource ( pageId ) ; if ( res != null ) { CmsResource detailResource = cms . readResource ( detailId ) ; String detailName = cms . getDetailName ( detailResource , cms . getRequestContext ( ) . getLocale ( ) , OpenCms . getLocaleManager ( ) . getDefaultLocales ( ) ) ; CmsResource parentFolder ; if ( pageResource . isFile ( ) ) { parentFolder = cms . readParentFolder ( pageResource . getStructureId ( ) ) ; } else { parentFolder = pageResource ; } String baseLink = OpenCms . getLinkManager ( ) . substituteLink ( cms , parentFolder ) ; String redirectLink = baseLink + ( baseLink . endsWith ( "/" ) ? "" : "/" ) + detailName ; CmsResourceInitException resInitException = new CmsResourceInitException ( getClass ( ) ) ; resInitException . setClearErrors ( true ) ; res . sendRedirect ( redirectLink ) ; throw resInitException ; } else { return pageResource ; } } catch ( CmsResourceInitException e ) { throw e ; } catch ( Exception e ) { LOG . error ( e . getLocalizedMessage ( ) , e ) ; throw new CmsResourceInitException ( getClass ( ) ) ; } } return null ; } } return resource ; }
@see org.opencms.main.I_CmsResourceInit#initResource(org.opencms.file.CmsResource, org.opencms.file.CmsObject, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)