Skip to content

Commit

Permalink
fix(velocity): this ensures that whatever language the user is viewin… (
Browse files Browse the repository at this point in the history
#26036)

* fix(velocity): this ensures that whatever language the user is viewing the site in, that the pages will be cached separately.

ref: #22534

* fix(velocity): this ensures that whatever language the user is viewing the site in, that the pages will be cached separately.

ref: #22534

* fix: invalid page cache #22534

* fix: invalid page cache #22534

* fix: invalid page cache #22534

* CR Feedback: Using Lazy objects for reading configuration properties.

* Implementing SonarQube feedback.

---------

Co-authored-by: Jose Castro <[email protected]>
  • Loading branch information
wezell and jcastro-dotcms authored Sep 14, 2023
1 parent e19679c commit 96bd74a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package com.dotcms.rendering.velocity.services;

import com.dotcms.util.ConversionUtils;
import com.dotcms.variant.VariantAPI;
import com.dotmarketing.util.UtilMethods;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.CacheLocator;
Expand All @@ -24,8 +17,14 @@
import com.dotmarketing.util.InodeUtils;
import com.dotmarketing.util.PageMode;
import com.dotmarketing.util.TagUtil;
import com.dotmarketing.util.UtilMethods;
import com.liferay.portal.model.User;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
* @author will
*
Expand Down Expand Up @@ -212,19 +211,9 @@ public InputStream writeObject(final VelocityResourceKey key) throws DotDataExce
private HTMLPageAsset getPage(VelocityResourceKey key)
throws DotDataException, DotSecurityException {

try {
return APILocator.getHTMLPageAssetAPI()
.fromContentlet(APILocator.getContentletAPI()
.findContentletByIdentifier(key.id1, key.mode.showLive,
ConversionUtils.toLong(key.language),
key.variant, sysUser(), true));
} catch (DotStateException e) {
return APILocator.getHTMLPageAssetAPI()
.fromContentlet(APILocator.getContentletAPI()
.findContentletByIdentifier(key.id1, key.mode.showLive,
ConversionUtils.toLong(key.language),
VariantAPI.DEFAULT_VARIANT.name(), sysUser(), true));
}
return (HTMLPageAsset) APILocator.getHTMLPageAssetAPI().findByIdLanguageVariantFallback(key.id1, ConversionUtils.toLong(key.language), key.variant, key.mode.showLive, sysUser(), true);


}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ public static final VelocityModeHandler modeHandler(final PageMode mode, final H

public final Template getTemplate(final IHTMLPage page, final PageMode mode) {
final String currentVariantId = WebAPILocator.getVariantWebAPI().currentVariantId();

final long viewingLang = WebAPILocator.getLanguageWebAPI().getLanguage(request).getId();
return VelocityUtil.getEngine().getTemplate(
VelocityResourceKey.getHTMLPageFilePath(page.getIdentifier(), mode,
page.getLanguageId(), currentVariantId));
viewingLang, currentVariantId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public void setContainersRemap(List<ContainerRemapTuple> containersReMap) {

/**
* Returns page based on identifier and respects the language fallback settings
* @param id
* @param identifier
* @param tryLang
* @param live
* @param user
Expand All @@ -314,7 +314,7 @@ public void setContainersRemap(List<ContainerRemapTuple> containersReMap) {

/**
* Returns page based on identifier and respects the language fallback settings
* @param id identifier of page
* @param identifier identifier of page
* @param tryLang requested language
* @param live live or working version of page
* @param user user to perform operation with
Expand All @@ -326,4 +326,26 @@ public void setContainersRemap(List<ContainerRemapTuple> containersReMap) {
IHTMLPage findByIdLanguageFallback(String identifier, long tryLang, boolean live, User user, boolean respectFrontEndPermissions)
throws DotDataException, DotSecurityException;



/**
* Returns page based on identifier and respects the language fallback settings
* If a non-standard variant is passed in and it is not found, this will also fall back to the
* DEFAULT variant as well.
* @param identifier identifier of page
* @param tryLang requested language
* @param tryVariant requested variant
* @param live live or working version of page
* @param user user to perform operation with
* @param respectFrontEndPermissions respect front end permissions
*
* @return
* @throws DotDataException
* @throws DotSecurityException
*/
IHTMLPage findByIdLanguageVariantFallback(String identifier, long tryLang, String tryVariant, boolean live, User user, boolean respectFrontEndPermissions)
throws DotSecurityException;



}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.dotcms.mock.request.MockSessionRequest;
import com.dotcms.mock.response.BaseResponse;
import com.dotcms.rendering.velocity.servlet.VelocityModeHandler;
import com.dotcms.variant.VariantAPI;
import com.dotmarketing.beans.Host;
import com.dotmarketing.beans.Identifier;
import com.dotmarketing.beans.MultiTree;
Expand All @@ -21,6 +22,7 @@
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.business.IdentifierAPI;
import com.dotmarketing.business.PermissionAPI;
import com.dotmarketing.business.PermissionLevel;
import com.dotmarketing.business.UserAPI;
import com.dotmarketing.business.VersionableAPI;
import com.dotmarketing.business.web.LanguageWebAPI;
Expand Down Expand Up @@ -53,22 +55,29 @@
import com.dotmarketing.util.WebKeys;
import com.google.common.collect.ImmutableList;
import com.liferay.portal.model.User;
import io.vavr.Lazy;
import io.vavr.control.Try;
import org.apache.velocity.exception.ResourceNotFoundException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.exception.ResourceNotFoundException;

public class HTMLPageAssetAPIImpl implements HTMLPageAssetAPI {

public static final Lazy<String> CMS_INDEX_PAGE = Lazy.of(() -> Config.getStringProperty(
"CMS_INDEX_PAGE", "index"));
public static final Lazy<Boolean> DEFAULT_PAGE_TO_DEFAULT_LANGUAGE =
Lazy.of(() -> Config.getBooleanProperty("DEFAULT_PAGE_TO_DEFAULT_LANGUAGE", true));

public class HTMLPageAssetAPIImpl implements HTMLPageAssetAPI {
public static final String CMS_INDEX_PAGE = Config.getStringProperty("CMS_INDEX_PAGE", "index");
public static final String DEFAULT_HTML_PAGE_ASSET_STRUCTURE_HOST_FIELD = "defaultHTMLPageAssetStructure";

private final PermissionAPI permissionAPI;
Expand Down Expand Up @@ -193,7 +202,7 @@ public HTMLPageAsset fromContentlet(Contentlet con) {
try {
contentletAPI.copyProperties((Contentlet) pa, con.getMap());
} catch (Exception e) {
throw new DotStateException("Page Copy Failed", e);
throw new DotStateException("Page Copy Failed on Contentlet Inode: " + con.getInode(), e);
}
pa.setHost(con.getHost());
pa.setVariantId(con.getVariantId());
Expand All @@ -206,7 +215,7 @@ public HTMLPageAsset fromContentlet(Contentlet con) {
pa.setFolder(folder.getInode());
}catch(Exception e){
pa=new HTMLPageAsset();
Logger.warn(this, "Unable to convert contentlet to page asset " + con, e);
Logger.warn(this, "Unable to convert contentlet to page asset: " + con, e);
}
}

Expand All @@ -217,11 +226,11 @@ public HTMLPageAsset fromContentlet(Contentlet con) {
if(identifier != null && UtilMethods.isSet(identifier.getAssetName())){
pa.setPageUrl(identifier.getAssetName());
} else {
Logger.warn(this, "Unable to convert Contentlet to page asset, error at set PageUrl " + con);
Logger.warn(this, "Unable to convert Contentlet to page asset, error at set PageUrl: " + con);
}
}catch(Exception e){
pa=new HTMLPageAsset();
Logger.warn(this, "Unable to convert Contentlet to page asset " + con, e);
Logger.warn(this, "Unable to convert Contentlet to page asset: " + con, e);
}
}

Expand All @@ -248,7 +257,7 @@ public IHTMLPage getPageByPath(String uri, Host host, Long languageId, Boolean l
try {
id = identifierAPI.find(host, uri);
} catch (Exception e) {
Logger.error(this.getClass(), "Unable to find" + uri);
Logger.error(this.getClass(), "Unable to find URI: " + uri);
return null;
}
}
Expand Down Expand Up @@ -280,7 +289,7 @@ public IHTMLPage getPageByPath(String uri, Host host, Long languageId, Boolean l
}

} catch (Exception e) {
Logger.error(this.getClass(), "Unable to find" + uri);
Logger.error(this.getClass(), "Unable to find URI: " + uri);
return null;
}
}
Expand All @@ -289,12 +298,12 @@ public IHTMLPage getPageByPath(String uri, Host host, Long languageId, Boolean l

private Identifier getIndexPageIdentifier(final String folderURI, final Host host) {
final String indexPageUri = folderURI.endsWith("/") ?
folderURI + CMS_INDEX_PAGE : folderURI + "/" + CMS_INDEX_PAGE;
folderURI + CMS_INDEX_PAGE.get() : folderURI + "/" + CMS_INDEX_PAGE.get();

try {
return identifierAPI.find(host, indexPageUri);
} catch (Exception e) {
Logger.error(this.getClass(), "Unable to find" + folderURI);
Logger.error(this.getClass(), "Unable to find folder URI: " + folderURI);
return null;
}
}
Expand Down Expand Up @@ -911,5 +920,38 @@ private IHTMLPage findPageInDefaultLanguageDifferentThanProvided(String identifi
return htmlPage;
}

@Override
public IHTMLPage findByIdLanguageVariantFallback(@NotNull final String identifier, final long tryLang,
@NotNull final String tryVariant, final boolean live,
@NotNull final User user,
final boolean respectFrontEndPermissions) throws DotSecurityException {
final long defaultLang = APILocator.getLanguageAPI().getDefaultLanguage().getId();
final boolean fallbackLang = tryLang != defaultLang && DEFAULT_PAGE_TO_DEFAULT_LANGUAGE.get();

// given lang and variant
HTMLPageAsset asset = Try.of(() -> fromContentlet(contentletAPI
.findContentletByIdentifier(identifier, live, tryLang, tryVariant, APILocator.systemUser(), true))).getOrNull();

if (asset == null) {

// given lang and DEFAULT varian
asset = Try.of(() -> fromContentlet(contentletAPI.findContentletByIdentifier(identifier, live, tryLang, VariantAPI.DEFAULT_VARIANT.name(), APILocator.systemUser(), true))).getOrNull();
}

if (asset == null && fallbackLang) {
// DEFAULT lang and given variant
asset = Try.of(() -> fromContentlet(contentletAPI.findContentletByIdentifier(identifier, live, defaultLang, tryVariant, APILocator.systemUser(), true))).getOrNull();
if (asset == null) {
// DEFAULT lang and DEFAULT variant
asset = Try.of(() -> fromContentlet(contentletAPI.findContentletByIdentifier(identifier, live, defaultLang, VariantAPI.DEFAULT_VARIANT.name(), APILocator.systemUser(), true))).getOrNull();
}
}
if (asset == null) {
throw new DotStateException("Unable to find page that matches. id:" + identifier + " lang:" + tryLang + " variant:" + tryVariant);
}

permissionAPI.checkPermission(asset, PermissionLevel.READ, user);
return asset;
}

}

0 comments on commit 96bd74a

Please sign in to comment.