diff --git a/dotCMS/src/main/java/com/dotcms/rendering/velocity/servlet/VelocityServlet.java b/dotCMS/src/main/java/com/dotcms/rendering/velocity/servlet/VelocityServlet.java index 0ffee2a0c893..6bb0a8f66529 100644 --- a/dotCMS/src/main/java/com/dotcms/rendering/velocity/servlet/VelocityServlet.java +++ b/dotCMS/src/main/java/com/dotcms/rendering/velocity/servlet/VelocityServlet.java @@ -18,6 +18,7 @@ import com.dotmarketing.filters.Constants; import com.dotmarketing.portlets.htmlpageasset.business.render.HTMLPageAssetNotFoundException; import com.dotmarketing.portlets.htmlpageasset.business.render.HTMLPageAssetRenderedAPI; +import com.dotmarketing.portlets.htmlpageasset.business.render.PageContext; import com.dotmarketing.portlets.htmlpageasset.business.render.PageContextBuilder; import com.dotmarketing.util.Logger; import com.dotmarketing.util.LoginMode; @@ -61,6 +62,7 @@ public VelocityServlet() { public static PageMode processPageMode (final User user, final HttpServletRequest request) { final LoginMode loginMode = LoginMode.get(request); + Logger.debug(VelocityServlet.class, "VelocityServlet_processPageMode LoginMode: " + loginMode.toString()); if (LoginMode.UNKNOWN == loginMode) { @@ -100,22 +102,29 @@ private static boolean useNavigateMode(final HttpServletRequest request, LoginMo @Override @CloseDB protected final void service(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { + Logger.debug(this, "======Starting VelocityServlet_service====="); final VelocityRequestWrapper request =VelocityRequestWrapper.wrapVelocityRequest(req); final String uri = CMSUrlUtil.getCurrentURI(request); HttpServletRequestThreadLocal.INSTANCE.setRequest(request); HttpServletResponseThreadLocal.INSTANCE.setResponse(response); + Logger.debug(this, "VelocityServlet_service Uri: " + uri); + final User user = (userApi.getLoggedInUser(request)!=null) - ? userApi.getLoggedInUser(request) - : userApi.getLoggedInFrontendUser(request) !=null - ? userApi.getLoggedInFrontendUser(request) - : userApi.getAnonymousUserNoThrow(); - + ? userApi.getLoggedInUser(request) + : userApi.getLoggedInFrontendUser(request) !=null + ? userApi.getLoggedInFrontendUser(request) + : userApi.getAnonymousUserNoThrow(); + + Logger.debug(this, "VelocityServlet_service User " + user.toString()); + request.setRequestUri(uri); final PageMode mode = processPageMode(user, request); + Logger.debug(this, "VelocityServlet_service Pagemode: " + mode.toString()); // if you are hitting the servlet without running through the other filters if (uri == null) { + Logger.debug(this, "VelocityServlet_service uri is null"); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "VelocityServlet called without running through the CMS Filter"); Logger.error(this.getClass(), "You cannot call the VelocityServlet without passing the requested url via a requestAttribute called " @@ -125,8 +134,9 @@ protected final void service(HttpServletRequest req, HttpServletResponse respons // if you are not running ee if ((DbConnectionFactory.isMsSql() && LicenseUtil.getLevel() < LicenseLevel.PROFESSIONAL.level) - || (DbConnectionFactory.isOracle() && LicenseUtil.getLevel() < LicenseLevel.PRIME.level) - || (!LicenseUtil.isASAllowed())) { + || (DbConnectionFactory.isOracle() && LicenseUtil.getLevel() < LicenseLevel.PRIME.level) + || (!LicenseUtil.isASAllowed())) { + Logger.debug(this, "VelocityServlet_service Enterprise License is required"); Logger.error(this, "Enterprise License is required"); response.sendError(HttpServletResponse.SC_NOT_FOUND); return; @@ -135,15 +145,18 @@ protected final void service(HttpServletRequest req, HttpServletResponse respons // try to get the page try { + final PageContext pageContextBuild = PageContextBuilder.builder() + .setPageUri(uri) + .setUser(user) + .setPageMode(mode) + .build(); + Logger.debug(this, "VelocityServlet_service PageContext: " + pageContextBuild.toString()); final String pageHtml = htmlPageAssetRenderedAPI.getPageHtml( - PageContextBuilder.builder() - .setPageUri(uri) - .setUser(user) - .setPageMode(mode) - .build(), + pageContextBuild, request, response ); + Logger.debug(this, "VelocityServlet_service pageHtml: " + pageHtml); response.getOutputStream().write(pageHtml.getBytes()); } catch (ResourceNotFoundException rnfe) { Logger.warnAndDebug(this.getClass(), "ResourceNotFoundException" + rnfe.toString(), rnfe); diff --git a/dotCMS/src/main/java/com/dotmarketing/business/IdentifierFactoryImpl.java b/dotCMS/src/main/java/com/dotmarketing/business/IdentifierFactoryImpl.java index af8ad3396d61..d2f20ec1ec87 100644 --- a/dotCMS/src/main/java/com/dotmarketing/business/IdentifierFactoryImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/business/IdentifierFactoryImpl.java @@ -93,7 +93,11 @@ protected void updateIdentifierURI(Versionable webasset, Folder folder) throws D * @return */ private Identifier check404(Identifier value) { - return value!=null && value.getAssetType()!=null && value.getAssetType().equals(IdentifierAPI.IDENT404) ? new Identifier() : value; + final boolean is404 = value!=null && value.getAssetType()!=null && value.getAssetType().equals(IdentifierAPI.IDENT404); + if(is404){ + Logger.debug(this, "404 Identifier found: " + value.toString()); + } + return is404 ? new Identifier() : value; } /** diff --git a/dotCMS/src/main/java/com/dotmarketing/business/VersionableFactoryImpl.java b/dotCMS/src/main/java/com/dotmarketing/business/VersionableFactoryImpl.java index 19f0df921ec0..d515816dbd08 100644 --- a/dotCMS/src/main/java/com/dotmarketing/business/VersionableFactoryImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/business/VersionableFactoryImpl.java @@ -346,6 +346,7 @@ public Optional getContentletVersionInfo( ContentletVersionInfo contentVersionInfo = this.icache.getContentVersionInfo(identifier, lang, variantName); if(contentVersionInfo!=null && fourOhFour.equals(contentVersionInfo.getWorkingInode())) { + Logger.debug(this, "404 ContentletVersionInfo found for id: " + identifier + " lang: " + lang + " variant: " + variantName); return Optional.empty(); }else if(contentVersionInfo!=null ){ return Optional.of(contentVersionInfo); diff --git a/dotCMS/src/main/java/com/dotmarketing/filters/CMSFilter.java b/dotCMS/src/main/java/com/dotmarketing/filters/CMSFilter.java index 0f653450916f..2125bc4faa03 100644 --- a/dotCMS/src/main/java/com/dotmarketing/filters/CMSFilter.java +++ b/dotCMS/src/main/java/com/dotmarketing/filters/CMSFilter.java @@ -10,13 +10,11 @@ import com.dotmarketing.business.APILocator; import com.dotmarketing.business.web.WebAPILocator; import com.dotmarketing.db.DbConnectionFactory; -import com.dotmarketing.exception.DotDataException; 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 org.apache.commons.logging.LogFactory; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; @@ -90,8 +88,11 @@ private void doFilterInternal(ServletRequest req, ServletResponse res, FilterCha String uri = urlUtil.getURIFromRequest(request); String queryString = urlUtil.getURLQueryStringFromRequest(request); + Logger.debug(this.getClass(), "------CMSFilter----- Starting for Uri: " + uri); + Logger.debug(this.getClass(), "CMSFilter site = " + site.getIdentifier()); + Logger.debug(this.getClass(), "CMSFilter Lang = " + languageId); + Logger.debug(this.getClass(), "CMSFilter queryString = " + queryString); - Logger.debug(this.getClass(), "CMS Filter URI = " + uri); /* * If someone is trying to go right to an asset without going through the cms, give them a @@ -106,8 +107,11 @@ private void doFilterInternal(ServletRequest req, ServletResponse res, FilterCha final Tuple2 iAm = this.urlUtil.resolveResourceType(IAm.NOTHING_IN_THE_CMS, uri, site, languageId); + Logger.debug(this.getClass(), "CMSFilter iAm = " + iAm); + // if I am a folder without a slash if (iAm._1() == IAm.FOLDER && !uri.endsWith("/")) { + Logger.debug(this.getClass(), "CMSFilter iAm Folder without slash"); response.setHeader("Location", UtilMethods.isSet(queryString) ? uri + "/?" + queryString : uri + "/"); Try.run(()->response.setStatus(301)); return; @@ -115,32 +119,41 @@ private void doFilterInternal(ServletRequest req, ServletResponse res, FilterCha // if I am a Page with a trailing slash if (iAm._1() == IAm.PAGE && iAm._2() == IAmSubType.PAGE_INDEX && uri.endsWith("/")) { + Logger.debug(this.getClass(), "CMSFilter iAm Index_Page"); uri = uri + CMS_INDEX_PAGE; } if (iAm._1() == IAm.PAGE) { + Logger.debug(this.getClass(), "CMSFilter iAm Page"); countPageVisit(request); countSiteVisit(request, response); + final String uriWithoutQueryString = this.urlUtil.getUriWithoutQueryString(uri); + Logger.debug(this.getClass(), "CMSFilter uriWithoutQueryString = " + uriWithoutQueryString); request.setAttribute(Constants.CMS_FILTER_URI_OVERRIDE, - this.urlUtil.getUriWithoutQueryString(uri)); - queryString = (null == queryString)? - this.urlUtil.getQueryStringFromUri (uri):queryString; + uriWithoutQueryString); + final String queryStringFromUri = this.urlUtil.getQueryStringFromUri(uri); + Logger.debug(this.getClass(), "CMSFilter queryStringFromUri = " + queryStringFromUri); + queryString = (null == queryString) ? queryStringFromUri : queryString; } if (iAm._1() == IAm.FILE) { + Logger.debug(this.getClass(), "CMSFilter iAm File"); Identifier ident; try { // Serving the file through the /dotAsset servlet StringWriter forward = new StringWriter(); forward.append("/dotAsset/"); - + Logger.debug(this.getClass(), "CMSFilter URI = " + uri); + Logger.debug(this.getClass(), "CMSFilter site = " + site.getIdentifier()); + Logger.debug(this.getClass(), "CMSFilter Lang = " + languageId); ident = APILocator.getIdentifierAPI().find(site, uri); + Logger.debug(this.getClass(), "CMSFilter Id " + ident == null? "Not Found" : ident.toString()); request.setAttribute(Constants.CMS_FILTER_IDENTITY, ident); // If language is in session, set as query string forward.append('?').append(WebKeys.HTMLPAGE_LANGUAGE + "=").append(String.valueOf(languageId)); - + Logger.debug(this.getClass(), "CMSFilter forward = " + forward.toString()); request.getRequestDispatcher(forward.toString()).forward(request, response); } catch (Exception e) { @@ -151,7 +164,7 @@ private void doFilterInternal(ServletRequest req, ServletResponse res, FilterCha } if (iAm._1() == IAm.PAGE) { - + Logger.debug(this.getClass(), "CMSFilter iAm Page"); final StringWriter forward = new StringWriter().append("/servlets/VelocityServlet"); if (UtilMethods.isSet(queryString)) { @@ -162,17 +175,21 @@ private void doFilterInternal(ServletRequest req, ServletResponse res, FilterCha forward.append('?'); forward.append(queryString); } + Logger.debug(this.getClass(), "CMSFilter forward = " + forward.toString()); request.getRequestDispatcher(forward.toString()).forward(request, response); return; } // nothing to do here if (uri.startsWith("/contentAsset/") && response.isCommitted()) { + Logger.debug(this.getClass(), "CMSFilter uri statrs with /contentAsset/ and response is committed"); return; } // allow vanities to forward to a dA asset if(request instanceof VanityUrlRequestWrapper && !response.isCommitted() && (uri.startsWith("/dA/") || uri.startsWith("/contentAsset/")) ) { + Logger.debug(this.getClass(), "CMSFilter uri statrs with /dA/ or /contentAsset/ and response is not committed"); + Logger.debug(this.getClass(), "CMSFilter URI = " + uri); request.getRequestDispatcher(uri).forward(request, response); return; } diff --git a/dotCMS/src/main/java/com/dotmarketing/filters/CMSUrlUtil.java b/dotCMS/src/main/java/com/dotmarketing/filters/CMSUrlUtil.java index 4bbce28cfbd5..884019a17d49 100644 --- a/dotCMS/src/main/java/com/dotmarketing/filters/CMSUrlUtil.java +++ b/dotCMS/src/main/java/com/dotmarketing/filters/CMSUrlUtil.java @@ -23,7 +23,6 @@ import com.dotmarketing.util.UUIDUtil; import com.dotmarketing.util.UtilMethods; import com.liferay.portal.model.User; -import com.liferay.util.StringPool; import com.liferay.util.Xss; import io.vavr.Tuple; import io.vavr.Tuple2; @@ -40,8 +39,6 @@ import java.util.List; import java.util.Optional; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -128,6 +125,10 @@ public Tuple2 resolveResourceType(final IAm iAm, final String uri, final Host site, final long languageId) { + Logger.debug(this.getClass(), "CMSUrlUtil_resolveResourceType"); + Logger.debug(this.getClass(), "CMSUrlUtil_resolveResourceType URI = " + uri); + Logger.debug(this.getClass(), "CMSUrlUtil_resolveResourceType site = " + site.getIdentifier()); + Logger.debug(this.getClass(), "CMSUrlUtil_resolveResourceType lang = " + languageId); final String uriWithoutQueryString = this.getUriWithoutQueryString (uri); if (isFileAsset(uriWithoutQueryString, site, languageId)) { @@ -137,6 +138,7 @@ public Tuple2 resolveResourceType(final IAm iAm, Tuple2 isPage = resolvePageAssetSubtype(uriWithoutQueryString, site, languageId); if (isPage._1()) { + Logger.debug(this.getClass(), "CMSUrlUtil_resolveResourceType is a Page"); return Tuple.of(IAm.PAGE, isPage._2()); } @@ -147,7 +149,7 @@ public Tuple2 resolveResourceType(final IAm iAm, : Tuple.of(IAm.FOLDER,IAmSubType.NONE); } - + Logger.debug(this.getClass(), "CMSUrlUtil_resolveResourceType is a NOTHING_IN_THE_CMS"); return Tuple.of(IAm.NOTHING_IN_THE_CMS, IAmSubType.NONE); } // resolveResourceType. @@ -166,6 +168,11 @@ public boolean isPageAsset(final String uri, final Host host, final long languag * and the IAmSubType will be the type of page asset when the boolean is true */ public Tuple2 resolvePageAssetSubtype(final String uri, final Host host, final Long languageId) { + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtype"); + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtype URI = " + uri); + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtypet site = " + host.getIdentifier()); + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtype lang = " + languageId); + Identifier id; if (!UtilMethods.isSet(uri)) { return Tuple.of(false, IAmSubType.NONE); @@ -176,13 +183,16 @@ public Tuple2 resolvePageAssetSubtype(final String uri, fin Logger.error(this.getClass(), UNABLE_TO_FIND + uri); return Tuple.of(false, IAmSubType.NONE); } + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtype Id " + id == null? "Not Found" : id.toString()); if (id == null || id.getId() == null) { return Tuple.of(false, IAmSubType.NONE); } if (HTMLPAGE.equals(id.getAssetType())) { + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtype Id AssetType is Page"); return Tuple.of(true, IAmSubType.NONE); } if (CONTENTLET.equals(id.getAssetType())) { + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtype Id AssetType is Contentlet"); try { //Get the list of languages use by the application @@ -191,9 +201,11 @@ public Tuple2 resolvePageAssetSubtype(final String uri, fin //First try with the given language Optional cinfo = APILocator.getVersionableAPI() .getContentletVersionInfo(id.getId(), languageId); + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtype contentletVersionInfo for Lang " + (cinfo.isEmpty() ? "Not Found" : cinfo.toString())); if (cinfo.isEmpty() || cinfo.get().getWorkingInode().equals(NOT_FOUND)) { for (Language language : languages) { + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtype contentletVersionInfo for lang not found trying with all langs"); /* If we found nothing with the given language it does not mean is not a page, could be just a page but it does not exist for the given language. @@ -203,6 +215,7 @@ public Tuple2 resolvePageAssetSubtype(final String uri, fin cinfo = APILocator.getVersionableAPI() .getContentletVersionInfo(id.getId(), language.getId()); if (cinfo.isPresent() && !cinfo.get().getWorkingInode().equals(NOT_FOUND)) { + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtype contentletVersionInfo found " + cinfo.toString()); //Found it break; } @@ -211,9 +224,12 @@ public Tuple2 resolvePageAssetSubtype(final String uri, fin } if (cinfo.isEmpty() || cinfo.get().getWorkingInode().equals(NOT_FOUND)) { + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtype is not a Page returning false"); return Tuple.of(false, IAmSubType.NONE);//At this point we know is not a page } + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtype Trying to get Contentlet"); Contentlet c = APILocator.getContentletAPI().find(cinfo.get().getWorkingInode(), APILocator.systemUser(),false); + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtype Contentlet found " + c.toString()); return Tuple.of(c.isHTMLPage(), IAmSubType.NONE); } catch (Exception e) { @@ -231,7 +247,7 @@ public Tuple2 resolvePageAssetSubtype(final String uri, fin APILocator.getUserAPI().getSystemUser()); boolean isUrlMap = APILocator.getURLMapAPI().isUrlPattern(urlMapContext); - + Logger.debug(this.getClass(), "CMSUrlUtil_resolvePageAssetSubtype Id AssetType is UrlMap " + isUrlMap); return Tuple.of(isUrlMap, isUrlMap ? IAmSubType.PAGE_URL_MAP : IAmSubType.NONE); } catch (final DotDataException | DotSecurityException e){ Logger.error(this.getClass(), e.getMessage()); @@ -249,6 +265,10 @@ public Tuple2 resolvePageAssetSubtype(final String uri, fin * @return true if the URI is a File Asset, false if not */ public boolean isFileAsset(String uri, Host host, Long languageId) { + Logger.debug(this.getClass(), "CMSUrlUtil_isFileAsset"); + Logger.debug(this.getClass(), "CMSUrlUtil_isFileAsset URI = " + uri); + Logger.debug(this.getClass(), "CMSUrlUtil_isFileAsset site = " + host.getIdentifier()); + Logger.debug(this.getClass(), "CMSUrlUtil_isFileAsset lang = " + languageId); // languageId is not used now, but will be used in future functionality. Issue #7141 @@ -259,33 +279,41 @@ public boolean isFileAsset(String uri, Host host, Long languageId) { Logger.error(this.getClass(), UNABLE_TO_FIND + uri); return false; } + Logger.debug(this.getClass(), "CMSUrlUtil_isFileAsset Id " + (id == null? "Not Found" : id.toString())); if (id == null || id.getId() == null) { return false; } if (FILE_ASSET.equals(id.getAssetType())) { + Logger.debug(this.getClass(), "CMSUrlUtil_isFileAsset Id AssetType is FileAsset"); return true; } if (CONTENTLET.equals(id.getAssetType())) { + Logger.debug(this.getClass(), "CMSUrlUtil_isFileAsset Id AssetType is Contentlet"); try { Optional cinfo = APILocator.getVersionableAPI() .getContentletVersionInfo(id.getId(), languageId); - + Logger.debug(this.getClass(), "CMSUrlUtil_isFileAsset contentletVersionInfo for Lang " + (cinfo.isEmpty() ? "Not Found" : cinfo.toString())); if ((cinfo.isEmpty() || cinfo.get().getWorkingInode().equals(NOT_FOUND)) && Config .getBooleanProperty("DEFAULT_FILE_TO_DEFAULT_LANGUAGE", false)) { + Logger.debug(this.getClass(), "CMSUrlUtil_isFileAsset contentletVersionInfo for lang not found trying defaultLang"); //Get the Default Language Language defaultLang = APILocator.getLanguageAPI().getDefaultLanguage(); //If the fallback to Default Language is set to true, let's see if the requested file is stored with Default Language cinfo = APILocator.getVersionableAPI() .getContentletVersionInfo(id.getId(), defaultLang.getId()); + Logger.debug(this.getClass(), "CMSUrlUtil_isFileAsset contentletVersionInfo for defaultLang " + (cinfo.isEmpty() ? "Not Found" : cinfo.toString())); } if (cinfo.isEmpty() || cinfo.get().getWorkingInode().equals(NOT_FOUND)) { + Logger.debug(this.getClass(), "CMSUrlUtil_isFileAsset is not a FileAsset returning false"); return false;//At this point we know is not a File Asset } else { + Logger.debug(this.getClass(), "CMSUrlUtil_isFileAsset Trying to get Contentlet"); Contentlet c = APILocator.getContentletAPI() .find(cinfo.get().getWorkingInode(), APILocator.getUserAPI().getSystemUser(), false); + Logger.debug(this.getClass(), "CMSUrlUtil_isFileAsset Contentlet found " + c.toString()); return (c.getContentType().baseType() == BaseContentType.FILEASSET); } } catch (Exception e) { @@ -318,6 +346,7 @@ public boolean isFolder(String uri, Host host) { try { id = APILocator.getIdentifierAPI().find(host, uri); + Logger.debug(this.getClass(), "CMSUrlUtil_isFolder Id " + (id == null? "Not Found" : id.toString())); if (id == null || id.getId() == null) { return false; } @@ -482,8 +511,7 @@ private String getRequestPath(final HttpServletRequest request){ /** * Verifies if the URI was overridden by a filter */ - Boolean wasURIOverridden(HttpServletRequest request) - throws UnsupportedEncodingException { + Boolean wasURIOverridden(HttpServletRequest request) { return (request.getAttribute(CMS_FILTER_URI_OVERRIDE) != null); } @@ -491,8 +519,7 @@ Boolean wasURIOverridden(HttpServletRequest request) * Search for an overridden query string by a filter and if nothing is found the query string * will be read from the request. */ - String getURLQueryStringFromRequest(HttpServletRequest request) - throws UnsupportedEncodingException { + String getURLQueryStringFromRequest(HttpServletRequest request) { return (request.getAttribute(CMS_FILTER_QUERY_STRING_OVERRIDE) != null) ? (String) request .getAttribute(CMS_FILTER_QUERY_STRING_OVERRIDE) diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/htmlpageasset/business/HTMLPageAssetAPIImpl.java b/dotCMS/src/main/java/com/dotmarketing/portlets/htmlpageasset/business/HTMLPageAssetAPIImpl.java index 0293b447f585..fcbdee5af0fe 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/htmlpageasset/business/HTMLPageAssetAPIImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/htmlpageasset/business/HTMLPageAssetAPIImpl.java @@ -253,6 +253,7 @@ public HTMLPageAsset fromContentlet(Contentlet con) { @CloseDBIfOpened @Override public IHTMLPage getPageByPath(final String uri, final Host site, final Long languageId, final Boolean live) { + Logger.debug(this.getClass(), "HTMLPageAssetAPIImpl_getPageByPath URI: " + uri + " Site: " + site + " LanguageId: " + languageId + " Live: " + live); Identifier id; if(!UtilMethods.isSet(uri)){ return null; @@ -270,7 +271,7 @@ public IHTMLPage getPageByPath(final String uri, final Host site, final Long lan return null; } } - + Logger.debug(this.getClass(), "HTMLPageAssetAPIImpl_getPageByPath Identifier: " + (id== null? "Not Found" : id.toString())); if (id == null || id.getId() == null) { return null; } @@ -278,21 +279,23 @@ public IHTMLPage getPageByPath(final String uri, final Host site, final Long lan if (Identifier.ASSET_TYPE_CONTENTLET.equals(id.getAssetType())) { try { final String currentVariantId = WebAPILocator.getVariantWebAPI().currentVariantId(); + Logger.debug(this.getClass(), "HTMLPageAssetAPIImpl_getPageByPath currentVariantId: " + currentVariantId); Optional cinfo = versionableAPI .getContentletVersionInfo( id.getId(), languageId, currentVariantId); - + Logger.debug(this.getClass(), "HTMLPageAssetAPIImpl_getPageByPath contentletVersionInfo: " + (cinfo.isEmpty() ? "Not Found" : cinfo.toString())); if (cinfo.isEmpty() || cinfo.get().getWorkingInode().equals(CMSUrlUtil.NOT_FOUND)) { cinfo = versionableAPI.getContentletVersionInfo( id.getId(), languageId); if (cinfo.isEmpty() || cinfo.get().getWorkingInode().equals(CMSUrlUtil.NOT_FOUND)) { + Logger.debug(this.getClass(), "HTMLPageAssetAPIImpl_getPageByPath contentletVersionInfo not found"); return null; } } final Contentlet contentlet = this.contentletAPI.find(live ? cinfo.get().getLiveInode() : cinfo.get().getWorkingInode(), this.userAPI.getSystemUser(), false); - + Logger.debug(this.getClass(), "HTMLPageAssetAPIImpl_getPageByPath contentlet: " + contentlet.toString()); if (BaseContentType.HTMLPAGE.getType() == contentlet.getContentType().baseType().getType()) { return fromContentlet(contentlet); } diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/htmlpageasset/business/render/HTMLPageAssetRenderedAPIImpl.java b/dotCMS/src/main/java/com/dotmarketing/portlets/htmlpageasset/business/render/HTMLPageAssetRenderedAPIImpl.java index a1219b8d3f82..aacf5f028e5a 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/htmlpageasset/business/render/HTMLPageAssetRenderedAPIImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/htmlpageasset/business/render/HTMLPageAssetRenderedAPIImpl.java @@ -290,10 +290,13 @@ public String getPageHtml( final HttpServletRequest request, final HttpServletResponse response) throws DotSecurityException, DotDataException { + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_getPageHtml Getting the HTML for the context: " + context.toString()); final Host host = this.hostWebAPI.getCurrentHost(request, context.getUser()); + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_getPageHtml Host object retrieved from the request is: " + host.getIdentifier()); final HTMLPageUrl htmlPageUrl = getHtmlPageAsset(context, host, request); final HTMLPageAsset page = htmlPageUrl.getHTMLPage(); + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_getPageHtml HTMLPageUrl: " + htmlPageUrl.toString()); final String pageHTML = new HTMLPageAssetRenderedBuilder() .setHtmlPageAsset(page) @@ -306,10 +309,12 @@ public String getPageHtml( .getPageHTML(context.getPageMode()); if (context.getPageMode() == PageMode.LIVE && ConfigExperimentUtil.INSTANCE.isExperimentAutoJsInjection()) { + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_getPageHtml experiments is running"); return experimentWebAPI.getCode(host, request) .map(jsCodeToBeInjected -> injectJSCode(pageHTML, jsCodeToBeInjected)) .orElse(pageHTML); } else { + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_getPageHtml Page HTML: " + pageHTML); return pageHTML; } } @@ -340,19 +345,24 @@ private String injectJSCode(final String pageHTML, final String JsCode) { */ private HTMLPageUrl getHtmlPageAsset(final PageContext context, final Host host, final HttpServletRequest request) throws DotDataException, DotSecurityException { + Logger.debug(this, "--HTMLPageAssetRenderedAPIImpl_getHtmlPageAsset--"); Optional htmlPageUrlOptional = findPageByContext(host, context); if (htmlPageUrlOptional.isEmpty()) { + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_getHtmlPageAsset htmlPageUrlOptional is Empty trying to find by URL Map"); htmlPageUrlOptional = findByURLMap(context, host, request); } if(htmlPageUrlOptional.isEmpty()){ + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_getHtmlPageAsset htmlPageUrlOptional is Empty throwing HTMLPageAssetNotFoundException"); throw new HTMLPageAssetNotFoundException(context.getPageUri()); } final HTMLPageUrl htmlPageUrl = htmlPageUrlOptional.get(); + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_getHtmlPageAsset htmlPageUrl: " + htmlPageUrl.htmlPage.toString()); checkPagePermission(context, htmlPageUrl.htmlPage); + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_getHtmlPageAsset User has permission to access the page"); return htmlPageUrl; } @@ -369,6 +379,7 @@ private HTMLPageUrl getHtmlPageAsset(final PageContext context, final Host host, */ private void checkPagePermission(final PageContext context, final IHTMLPage htmlPageAsset) throws DotDataException, DotSecurityException { + Logger.debug(this, "--HTMLPageAssetRenderedAPIImpl_checkPagePermission--"); final boolean doesUserHavePermission = this.permissionAPI.doesUserHavePermission( htmlPageAsset, @@ -380,6 +391,7 @@ private void checkPagePermission(final PageContext context, final IHTMLPage html final String message = String.format("User: %s does not have permissions %s for page %s", context.getUser(), PermissionLevel.READ, htmlPageAsset.getURI()); + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_checkPagePermission " + message); throw new DotSecurityException(message); } } @@ -403,10 +415,12 @@ private Optional findPageByContext(final Host host, final PageConte final String uri = context.getPageUri(); final PageMode mode = context.getPageMode(); final String pageUri = (UUIDUtil.isUUID(uri) ||( uri.length()>0 && '/' == uri.charAt(0))) ? uri : ("/" + uri); + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_findPageByContext user: " + user + " uri: " + uri + " mode: " + mode + " host: " + host + " pageUri: " + pageUri); final HTMLPageAsset htmlPageAsset = (HTMLPageAsset) (UUIDUtil.isUUID(pageUri) ? this.htmlPageAssetAPI.findPage(pageUri, user, mode.respectAnonPerms) : getPageByUri(mode, host, pageUri)); + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_findPageByContext htmlPageAsset: " + (htmlPageAsset == null ? "Not Found" : htmlPageAsset.toString())); return Optional.ofNullable(htmlPageAsset == null ? null : new HTMLPageUrl(htmlPageAsset)); } @@ -466,12 +480,16 @@ private IHTMLPage getPageByUri(final PageMode mode, final Host host, final Strin final HttpServletRequest request = HttpServletRequestThreadLocal.INSTANCE.getRequest(); final Language defaultLanguage = this.languageAPI.getDefaultLanguage(); final Language language = this.getCurrentLanguage(request); + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_getPageByUri pageUri: " + pageUri + " host: " + host + " language: " + language + " mode: " + mode); IHTMLPage htmlPage = this.htmlPageAssetAPI.getPageByPath(pageUri, host, language.getId(), mode.showLive); + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_getPageByUri page found: " + htmlPage); + if (htmlPage == null && !defaultLanguage.equals(language) && APILocator.getLanguageAPI().canDefaultPageToDefaultLanguage()) { + Logger.debug(this, "HTMLPageAssetRenderedAPIImpl_getPageByUri page not found trying to get the default language"); htmlPage = this.htmlPageAssetAPI.getPageByPath(pageUri, host, defaultLanguage.getId(), mode.showLive); @@ -541,6 +559,16 @@ public HTMLPageAsset getHTMLPage() { return htmlPage; } + //Create toString method + @Override + public String toString() { + return "HTMLPageUrl{" + + "urlMapInfo=" + (urlMapInfo != null ? urlMapInfo.toString() : "null") + + ", htmlPage=" + (htmlPage != null ? htmlPage.toString() : "null") + + ", hasLive=" + hasLive() + + '}'; + } + } /** diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/htmlpageasset/business/render/PageContext.java b/dotCMS/src/main/java/com/dotmarketing/portlets/htmlpageasset/business/render/PageContext.java index 408399b5885f..4ba8dc79a65a 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/htmlpageasset/business/render/PageContext.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/htmlpageasset/business/render/PageContext.java @@ -112,4 +112,16 @@ public VanityURLView getVanityUrl() { return vanityUrl; } + //Create a toString method + @Override + public String toString() { + return "PageContext{" + + "user=" + user + + ", pageUri='" + pageUri + '\'' + + ", pageMode=" + pageMode + + ", page=" + page + + ", graphQL=" + graphQL + + ", parseJSON=" + parseJSON + + '}'; + } } diff --git a/dotCMS/src/main/java/com/dotmarketing/servlets/SpeedyAssetServlet.java b/dotCMS/src/main/java/com/dotmarketing/servlets/SpeedyAssetServlet.java index a404395c05ae..81ba862e5e95 100644 --- a/dotCMS/src/main/java/com/dotmarketing/servlets/SpeedyAssetServlet.java +++ b/dotCMS/src/main/java/com/dotmarketing/servlets/SpeedyAssetServlet.java @@ -31,7 +31,6 @@ import com.liferay.portal.language.LanguageException; import com.liferay.portal.language.LanguageUtil; import com.liferay.portal.model.Company; -import com.liferay.portal.model.User; public class SpeedyAssetServlet extends HttpServlet { @@ -49,8 +48,8 @@ public void init(ServletConfig config) throws ServletException { protected void service(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - + throws ServletException, IOException { + Logger.debug(this, "======Starting SpeedyAssetServlet_service====="); /* * Getting host object form the session @@ -58,7 +57,9 @@ protected void service(HttpServletRequest request, HttpServletResponse response) HostWebAPI hostWebAPI = WebAPILocator.getHostWebAPI(); Host host; try { + Logger.debug(this, "SpeedyAssetServlet_service Getting host object from the request"); host = hostWebAPI.getCurrentHost(request); + Logger.debug(this, "SpeedyAssetServlet_service Host object retrieved from the request is: " + host.getIdentifier()); } catch (Exception e) { Logger.error(this, "Unable to retrieve current request host"); throw new ServletException(e.getMessage(), e); @@ -67,15 +68,19 @@ protected void service(HttpServletRequest request, HttpServletResponse response) // Checking if host is active boolean hostlive; boolean _adminMode = UtilMethods.isAdminMode(request, response); + Logger.debug(this, "SpeedyAssetServlet_service Is Admin Mode: " + _adminMode); try { hostlive = APILocator.getVersionableAPI().hasLiveVersion(host); + Logger.debug(this, "SpeedyAssetServlet_service host has live version: " + hostlive); } catch (Exception e1) { + Logger.debug(this, "SpeedyAssetServlet_service Exception trying to check if host has live version: " + e1.getMessage()); UtilMethods.closeDbSilently(); throw new ServletException(e1); } if (!_adminMode && !hostlive) { try { + Logger.debug(this, "SpeedyAssetServlet_service Host is not live and is not admin mode, sending service unavailable error"); Company company = PublicCompanyFactory.getDefaultCompany(); response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, LanguageUtil.get(company.getCompanyId(), company.getLocale(), "server-unavailable-error-message")); @@ -88,98 +93,102 @@ protected void service(HttpServletRequest request, HttpServletResponse response) PageMode mode = PageMode.get(request); - HttpSession session = request.getSession(false); + Logger.debug(this, "SpeedyAssetServlet_service PageMode: " + mode); - //GIT-4506 + //GIT-4506 - boolean serveWorkingVersion = !mode.showLive; + boolean serveWorkingVersion = !mode.showLive; + Logger.debug(this, "SpeedyAssetServlet_service Serve Working Version: " + serveWorkingVersion); - User user = null; - try { - if (session != null) - user = (com.liferay.portal.model.User) session.getAttribute(com.dotmarketing.util.WebKeys.CMS_USER); - if(user==null){ - user = WebAPILocator.getUserWebAPI().getLoggedInUser(request); - } - } catch (Exception nsue) { - Logger.warn(this, "Exception trying to getUser: " + nsue.getMessage(), nsue); - } - - Identifier id = resolveIdentifier(request); - if(id==null){ - Logger.debug(this, "Invalid identifier passed: url = " + request.getRequestURI()); - response.sendError(404); - return; + Identifier id = resolveIdentifier(request); + Logger.debug(this, "SpeedyAssetServlet_service Identifier Resolved: " + id); + if(id==null){ + Logger.debug(this, "SpeedyAssetServlet_service Invalid identifier passed: url = " + request.getRequestURI()); + response.sendError(404); + return; } - //Language is in request, let's load it. Otherwise use the language in session - long lang = WebAPILocator.getLanguageWebAPI().getLanguage(request).getId(); - try{ - Optional cvi = APILocator.getVersionableAPI().getContentletVersionInfo(id.getId(), lang); - - if(cvi.isEmpty() && Config.getBooleanProperty("DEFAULT_FILE_TO_DEFAULT_LANGUAGE", false)){ - cvi = APILocator.getVersionableAPI().getContentletVersionInfo(id.getId(), APILocator.getLanguageAPI().getDefaultLanguage().getId()); - } - - if(cvi.isEmpty()) { - throw new DotDataException("Can't find Contentlet-Version-Info. Identifier: " - + id.getId(), ". Lang: " + APILocator.getLanguageAPI().getDefaultLanguage().getId()); - } - - String conInode = serveWorkingVersion ? cvi.get().getWorkingInode() : cvi.get().getLiveInode(); - String referrer = "/contentAsset/raw-data/" + conInode + "/fileAsset/?byInode=true"; - request.getRequestDispatcher(referrer).forward(request, response); - } + //Language is in request, let's load it. Otherwise use the language in session + long lang = WebAPILocator.getLanguageWebAPI().getLanguage(request).getId(); + Logger.debug(this, "SpeedyAssetServlet_service Language: " + lang); + try{ + Optional cvi = APILocator.getVersionableAPI().getContentletVersionInfo(id.getId(), lang); + Logger.debug(this.getClass(), "SpeedyAssetServlet_service contentletVersionInfo: " + (cvi.isEmpty() ? "Not Found" : cvi.toString())); + if(cvi.isEmpty() && Config.getBooleanProperty("DEFAULT_FILE_TO_DEFAULT_LANGUAGE", false)){ + Logger.debug(this, "SpeedyAssetServlet_service Contentlet-Version-Info is empty, trying to get default language"); + cvi = APILocator.getVersionableAPI().getContentletVersionInfo(id.getId(), APILocator.getLanguageAPI().getDefaultLanguage().getId()); + Logger.debug(this.getClass(), "SpeedyAssetServlet_service contentletVersionInfo for defaultLang " + (cvi.isEmpty() ? "Not Found" : cvi.toString())); + } + + if(cvi.isEmpty()) { + Logger.debug(this.getClass(), "SpeedyAssetServlet_service contentletVersionInfo is empty, throwing exception"); + throw new DotDataException("Can't find Contentlet-Version-Info. Identifier: " + + id.getId(), ". Lang: " + APILocator.getLanguageAPI().getDefaultLanguage().getId()); + } + + String conInode = serveWorkingVersion ? cvi.get().getWorkingInode() : cvi.get().getLiveInode(); + Logger.debug(this.getClass(), "SpeedyAssetServlet_service contentletInode to serve: " + conInode); + String referrer = "/contentAsset/raw-data/" + conInode + "/fileAsset/?byInode=true"; + Logger.debug(this.getClass(), "SpeedyAssetServlet_service dispatched to: " + referrer); + request.getRequestDispatcher(referrer).forward(request, response); + } catch(Exception e){ - Logger.warn(this, "Exception trying to file: " +e); + Logger.warn(this, "Exception trying to file: " +e); } - } + } private Identifier resolveIdentifier(HttpServletRequest request) { - Identifier ident = (Identifier) request.getAttribute(Constants.CMS_FILTER_IDENTITY); - if(ident==null){ - if(request.getParameter("path")==null) { - // Getting the identifier from the path like /dotAsset/{identifier}.{ext} E.G. /dotAsset/1234.js - StringTokenizer _st = new StringTokenizer(request.getRequestURI(), "/"); - - Logger.debug(this, "Requesting by url: " + request.getRequestURI()); - - String _fileName = null; - while(_st.hasMoreElements()){ - _fileName = _st.nextToken(); - } - - Logger.debug(this, "Parsed filename: " + _fileName); - - String identifier = UtilMethods.getFileName(_fileName); - - Logger.debug(SpeedyAssetServlet.class, "Loading identifier: " + identifier); - try { - ident = APILocator.getIdentifierAPI().find(identifier); - } catch (DotDataException e) { - Logger.debug(this.getClass(), e.getMessage()); - - } - }else if( request.getParameter("path")!=null){ - Host host; - try { - host = WebAPILocator.getHostWebAPI().getCurrentHost(request); - ident = APILocator.getIdentifierAPI().find(host, request.getParameter("path")); - } catch (DotDataException | PortalException | SystemException | DotSecurityException e) { - Logger.debug(this.getClass(), e.getMessage()); - } + Logger.debug(this, "--SpeedyAssetServlet_resolveIdentifier from Request--"); + Identifier ident = (Identifier) request.getAttribute(Constants.CMS_FILTER_IDENTITY); + Logger.debug(this, "SpeedyAssetServlet_resolveIdentifier Identifier from attribute: " + (ident == null ? "Not Found" : ident.toString())); + if(ident==null){ + if(request.getParameter("path")==null) { + Logger.debug(this, "SpeedyAssetServlet_resolveIdentifier Param 'Path' is null, getting from the URI"); + // Getting the identifier from the path like /dotAsset/{identifier}.{ext} E.G. /dotAsset/1234.js + StringTokenizer _st = new StringTokenizer(request.getRequestURI(), "/"); + + Logger.debug(this, "SpeedyAssetServlet_resolveIdentifier Requesting by url: " + request.getRequestURI()); + + String _fileName = null; + while(_st.hasMoreElements()){ + _fileName = _st.nextToken(); + } + + Logger.debug(this, "SpeedyAssetServlet_resolveIdentifier Parsed filename: " + _fileName); + + String identifier = UtilMethods.getFileName(_fileName); + + Logger.debug(SpeedyAssetServlet.class, "SpeedyAssetServlet_resolveIdentifier Loading identifier: " + identifier); + try { + ident = APILocator.getIdentifierAPI().find(identifier); + Logger.debug(this.getClass(), "SpeedyAssetServlet_resolveIdentifier Identifier: " + (ident == null? "Not Found" : ident.toString())); + } catch (DotDataException e) { + Logger.debug(this.getClass(), e.getMessage()); + + } + }else if( request.getParameter("path")!=null){ + Logger.debug(this, "SpeedyAssetServlet_resolveIdentifier Param 'Path' is not null, path: " + request.getParameter("path")); + Host host; + try { + host = WebAPILocator.getHostWebAPI().getCurrentHost(request); + Logger.debug(this, "SpeedyAssetServlet_service Host object retrieved from the request is: " + host.getIdentifier()); + ident = APILocator.getIdentifierAPI().find(host, request.getParameter("path")); + Logger.debug(this.getClass(), "SpeedyAssetServlet_resolveIdentifier Identifier: " + (ident == null? "Not Found" : ident.toString())); + } catch (DotDataException | PortalException | SystemException | DotSecurityException e) { + Logger.debug(this.getClass(), e.getMessage()); + } + } } - } - return ident; + return ident; }