Skip to content

Commit

Permalink
#23276 Refactor previous solution.
Browse files Browse the repository at this point in the history
* We created a IAm subtype enum to manage different page types instead of creating a new IAm type.

* We use tuples to manage isPageAsset and resourceResolveType methods.
  • Loading branch information
daniel.colina committed Apr 28, 2023
1 parent bbcb902 commit fa3d6e8
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import com.dotmarketing.cache.FieldsCache;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.filters.CMSFilter;
import com.dotmarketing.filters.CMSUrlUtil;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
Expand All @@ -87,6 +88,7 @@
import com.liferay.portal.language.LanguageUtil;
import com.liferay.portal.model.User;
import com.liferay.util.StringPool;
import io.vavr.Tuple2;

import java.net.MalformedURLException;
import java.util.ArrayList;
Expand Down Expand Up @@ -279,11 +281,14 @@ private void checkInternalLink(final Contentlet contentlet,

final CMSUrlUtil cmsUrlUtils = CMSUrlUtil.getInstance();
final long languageId = contentlet.getLanguageId();

final Tuple2<Boolean, CMSFilter.IAmSubType> isPageAsset = cmsUrlUtils.isPageAsset(testurl, host, languageId);

// tests
if(isUrlMap (testurl) ||
cmsUrlUtils.isFileAsset(testurl, host, languageId) ||
cmsUrlUtils.isFolder (testurl, host) ||
cmsUrlUtils.isPageAsset(testurl, host, languageId) ||
isPageAsset._1() ||
cmsUrlUtils.isVanityUrl(testurl, host, languageId)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.liferay.portal.auth.PrincipalThreadLocal;
import com.liferay.portal.model.User;
import com.liferay.portal.servlet.PortletSessionPool;
import io.vavr.Tuple2;
import io.vavr.control.Try;
import org.apache.commons.lang.StringUtils;

Expand Down Expand Up @@ -591,7 +592,10 @@ protected boolean checkAccessFilters(final String uri, final Host host,

private boolean isFile(final String uri, final Host host, final long languageId) {

if (CMSFilter.IAm.FILE != this.cmsUrlUtil.resolveResourceType(null, uri, host, languageId)) {
Tuple2<CMSFilter.IAm, CMSFilter.IAmSubType> resourceType =
this.cmsUrlUtil.resolveResourceType(null, uri, host, languageId);

if (CMSFilter.IAm.FILE != resourceType._1()) {

final String uriWithoutQueryString = this.cmsUrlUtil.getUriWithoutQueryString(uri.toLowerCase());
return uriWithoutQueryString.endsWith(".jpg") ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.web.WebAPILocator;
import com.dotmarketing.exception.DotRuntimeException;
import com.dotmarketing.filters.CMSFilter;
import com.dotmarketing.filters.CMSFilter.IAm;
import com.dotmarketing.filters.CMSUrlUtil;
import com.dotmarketing.filters.Constants;
import com.dotmarketing.portlets.languagesmanager.model.Language;
import com.dotmarketing.util.WebKeys;
import io.vavr.Tuple2;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
Expand Down Expand Up @@ -107,15 +109,14 @@ private IAm resolveResourceType(final String uri, final Host site, final long la
return IAm.FILE;
}
}





if (CMSUrlUtil.getInstance().isFileAsset(uri, site, languageId)) {
return IAm.FILE;
} else if (CMSUrlUtil.getInstance().isPageAsset(uri, site, languageId)) {


Tuple2<Boolean, CMSFilter.IAmSubType> isPage = CMSUrlUtil.getInstance().isPageAsset(uri, site, languageId);

if (isPage._1()) {
return IAm.PAGE;
} else if (CMSUrlUtil.getInstance().isFileAsset(uri, site, languageId)) {
return IAm.FILE;
} else if (CMSUrlUtil.getInstance().isFolder(uri, site)) {
return IAm.FOLDER;
} else {
Expand Down
37 changes: 16 additions & 21 deletions dotCMS/src/main/java/com/dotmarketing/filters/CMSFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.dotmarketing.portlets.rules.business.RulesEngine;
import com.dotmarketing.portlets.rules.model.Rule;
import com.dotmarketing.util.*;
import io.vavr.Tuple2;
import io.vavr.control.Try;

import javax.servlet.*;
Expand All @@ -32,25 +33,18 @@ public class CMSFilter implements Filter {
public static final String CMS_INDEX_PAGE = Config.getStringProperty("CMS_INDEX_PAGE", "index");

/*
* This enum is used to determine what the current request is.
* These enums are used to determine what the current request is.
* It is used to determine if the request is a page, a file, a folder, or nothing in the CMS.
* In some cases we have to know if the request is a page or if it is an index page. For example, if the request is
* an UrlMapping and ends with slash, then it needs to be treated as a page and not as an index page.
* an UrlMapping and ends with slash, then it needs to be treated as a page and not as an index page. To achieve this
* we use the IAmSubType enum.
* */
public enum IAm {
PAGE(true),
INDEX_PAGE(true),
FOLDER(false),
FILE(false),
NOTHING_IN_THE_CMS(false);

private final boolean isPage;
IAm(boolean isPage) {
this.isPage = isPage;
}
public boolean isPage() {
return this.isPage;
}
PAGE, FOLDER, FILE, NOTHING_IN_THE_CMS
}

public enum IAmSubType {
PAGE_INDEX, PAGE_URL_MAP, DEFAULT
}

@Override
Expand Down Expand Up @@ -107,24 +101,25 @@ private void doFilterInternal(ServletRequest req, ServletResponse res, FilterCha
}


final IAm iAm = this.urlUtil.resolveResourceType(IAm.NOTHING_IN_THE_CMS, uri, site, languageId);
final Tuple2<IAm,IAmSubType> iAm =
this.urlUtil.resolveResourceType(IAm.NOTHING_IN_THE_CMS, uri, site, languageId);

// if I am a folder without a slash

if (iAm == IAm.FOLDER && !uri.endsWith("/")) {
if (iAm._1() == IAm.FOLDER && !uri.endsWith("/")) {
response.setHeader("Location", UtilMethods.isSet(queryString) ? uri + "/?" + queryString : uri + "/");
Try.run(()->response.setStatus(301));
return;
}


// if I am a Page with a trailing slash
if (iAm == IAm.INDEX_PAGE && uri.endsWith("/")) {
if (iAm._1() == IAm.PAGE && iAm._2() == IAmSubType.PAGE_INDEX && uri.endsWith("/")) {
uri = uri + CMS_INDEX_PAGE;
}


if (iAm.isPage()) {
if (iAm._1() == IAm.PAGE) {
countPageVisit(request);
countSiteVisit(request, response);
request.setAttribute(Constants.CMS_FILTER_URI_OVERRIDE,
Expand All @@ -133,7 +128,7 @@ private void doFilterInternal(ServletRequest req, ServletResponse res, FilterCha
this.urlUtil.getQueryStringFromUri (uri):queryString;
}

if (iAm == IAm.FILE) {
if (iAm._1() == IAm.FILE) {
Identifier ident;
try {
// Serving the file through the /dotAsset servlet
Expand All @@ -155,7 +150,7 @@ private void doFilterInternal(ServletRequest req, ServletResponse res, FilterCha
return;
}

if (iAm.isPage()) {
if (iAm._1() == IAm.PAGE) {

final StringWriter forward = new StringWriter().append("/servlets/VelocityServlet");

Expand Down
60 changes: 36 additions & 24 deletions dotCMS/src/main/java/com/dotmarketing/filters/CMSUrlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.dotmarketing.exception.DotRuntimeException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.filters.CMSFilter.IAm;
import com.dotmarketing.filters.CMSFilter.IAmSubType;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.contentlet.model.ContentletVersionInfo;
import com.dotmarketing.portlets.languagesmanager.model.Language;
Expand All @@ -18,6 +19,7 @@
import com.dotmarketing.util.UtilMethods;
import com.liferay.portal.model.User;
import com.liferay.util.Xss;
import io.vavr.Tuple2;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -35,6 +37,7 @@
import java.util.stream.Stream;

import static com.dotmarketing.business.PermissionAPI.PERMISSION_READ;
import static com.dotmarketing.filters.CMSFilter.CMS_INDEX_PAGE;
import static com.dotmarketing.filters.Constants.CMS_FILTER_QUERY_STRING_OVERRIDE;
import static com.dotmarketing.filters.Constants.CMS_FILTER_URI_OVERRIDE;
import static java.util.stream.Collectors.toSet;
Expand Down Expand Up @@ -106,27 +109,33 @@ public boolean isPageAsset(Versionable asset) {
* @param languageId
* @return
*/
public IAm resolveResourceType(final IAm iAm,
final String uri,
final Host site,
final long languageId) {
public Tuple2<IAm, IAmSubType> resolveResourceType(final IAm iAm,
final String uri,
final Host site,
final long languageId) {

final String uriWithoutQueryString = this.getUriWithoutQueryString (uri);
if (isFileAsset(uriWithoutQueryString, site, languageId)) {
return IAm.FILE;
}
if (isPageAsset(uriWithoutQueryString, site, languageId)) {
return IAm.PAGE;
return new Tuple2<>(IAm.FILE, IAmSubType.DEFAULT);
}

Tuple2<Boolean, IAmSubType> isPage = isPageAsset(uriWithoutQueryString, site, languageId);

if (isPage._1()) {
return new Tuple2<>(IAm.PAGE, isPage._2());
}

if(isFolder(uriWithoutQueryString, site)) {
// resolves correctly for folders with index pages
return uriWithoutQueryString.endsWith("/") && isPageAsset(uriWithoutQueryString + CMSFilter.CMS_INDEX_PAGE, site, languageId)
? IAm.INDEX_PAGE
: IAm.FOLDER;
isPage = isPageAsset(uriWithoutQueryString + CMS_INDEX_PAGE, site, languageId);

return uriWithoutQueryString.endsWith("/") && isPage._1()
? new Tuple2<>(IAm.PAGE, IAmSubType.PAGE_INDEX)
: new Tuple2<>(IAm.FOLDER,IAmSubType.DEFAULT);

}

return IAm.NOTHING_IN_THE_CMS;
return new Tuple2<>(IAm.NOTHING_IN_THE_CMS, IAmSubType.DEFAULT);


} // resolveResourceType.
Expand All @@ -138,22 +147,22 @@ public IAm resolveResourceType(final IAm iAm,
* @param languageId The current language Id
* @return true if the URI is a Page Asset, false if not
*/
public boolean isPageAsset(String uri, Host host, Long languageId) {
public Tuple2<Boolean, IAmSubType> isPageAsset(String uri, Host host, Long languageId) {
Identifier id;
if (!UtilMethods.isSet(uri)) {
return false;
return new Tuple2<>(false, IAmSubType.DEFAULT);
}
try {
id = APILocator.getIdentifierAPI().find(host, uri);
} catch (Exception e) {
Logger.error(this.getClass(), UNABLE_TO_FIND + uri);
return false;
return new Tuple2<>(false, IAmSubType.DEFAULT);
}
if (id == null || id.getId() == null) {
return false;
return new Tuple2<>(false, IAmSubType.DEFAULT);
}
if (HTMLPAGE.equals(id.getAssetType())) {
return true;
return new Tuple2<>(true, IAmSubType.DEFAULT);
}
if (CONTENTLET.equals(id.getAssetType())) {
try {
Expand Down Expand Up @@ -184,14 +193,14 @@ public boolean isPageAsset(String uri, Host host, Long languageId) {

}
if (!cinfo.isPresent() || cinfo.get().getWorkingInode().equals(NOT_FOUND)) {
return false;//At this point we know is not a page
return new Tuple2<>(false, IAmSubType.DEFAULT);//At this point we know is not a page
}
Contentlet c = APILocator.getContentletAPI().find(cinfo.get().getWorkingInode(), APILocator.systemUser(),false);
return c.isHTMLPage();
return new Tuple2<>(c.isHTMLPage(), IAmSubType.DEFAULT);

} catch (Exception e) {
Logger.error(this.getClass(), UNABLE_TO_FIND + uri);
return false;
return new Tuple2<>(false, IAmSubType.DEFAULT);
}
}

Expand All @@ -203,10 +212,10 @@ public boolean isPageAsset(String uri, Host host, Long languageId) {
host,
APILocator.getUserAPI().getSystemUser());

return APILocator.getURLMapAPI().isUrlPattern(urlMapContext);
return new Tuple2<>(APILocator.getURLMapAPI().isUrlPattern(urlMapContext), IAmSubType.PAGE_URL_MAP);
} catch (final DotDataException | DotSecurityException e){
Logger.error(this.getClass(), e.getMessage());
return false;
return new Tuple2<>(false, IAmSubType.DEFAULT);
}
}

Expand Down Expand Up @@ -312,7 +321,7 @@ public boolean isVanityUrl(final String uri,
*
* @param uri The current uri
* @param host The current host
* @param languageId The current language Id
* @param language The current language Id
* @return true if the URI is a vanity URL, false if not
*/
public boolean isVanityUrl(final String uri,
Expand Down Expand Up @@ -394,9 +403,12 @@ public boolean canRead(Identifier ident, long languageId, User user) {
* @return true if is a File Asset or Vanity Url or Page Asset or Folder, false if not
*/
public boolean amISomething(String uri, Host host, Long languageId) {

Tuple2<Boolean, IAmSubType> isPage = urlUtil.isPageAsset(uri, host, languageId);

return (urlUtil.isFileAsset(uri, host, languageId) || urlUtil
.isVanityUrl(uri, host, languageId)
|| urlUtil.isPageAsset(uri, host, languageId) || urlUtil.isFolder(uri, host));
|| isPage._1() || urlUtil.isFolder(uri, host));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ private Tuple2<File,String> getFileFromTimeMachine(final Host host, String uri,

String mimeType = APILocator.getFileAssetAPI().getMimeType(file.getName());
if (mimeType == null || "unknown".equals(mimeType)) {
mimeType = (CMSUrlUtil.getInstance().isPageAsset(uri, host, Long.parseLong(selectedLangId))) ? MediaType.TEXT_HTML : MediaType.APPLICATION_OCTET_STREAM;

Tuple2<Boolean, CMSFilter.IAmSubType>
isPage = CMSUrlUtil.getInstance().isPageAsset(uri, host, Long.parseLong(selectedLangId));
mimeType = (isPage._1())
? MediaType.TEXT_HTML
: MediaType.APPLICATION_OCTET_STREAM;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ public void testGetURIFromRequestWhenFilterIsSet() {
assertEquals("dotcms+test.txt", result);
}



}

0 comments on commit fa3d6e8

Please sign in to comment.