Skip to content

Commit

Permalink
#3062 - AEM In Unified Shell, The Environment Indicator is displayed … (
Browse files Browse the repository at this point in the history
#3071)

* #3062 - AEM In Unified Shell, The Environment Indicator is displayed multiple times in site Page Editor
  • Loading branch information
davidjgonzalez authored Mar 20, 2023
1 parent d7fdd34 commit 16b159c
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 117 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)
## Unreleased ([details][unreleased changes details])

<!-- Keep this up to date! After a release, change the tag name to the latest release -->

## Fixed

- #3069 - Managed Controlled Processes Dashboard not visible
- #3062 - AEM In Unified Shell, The Environment Indicator is displayed multiple times in site Page Editor;

## Changed

- Added config.rde logger for acs aem commons at Debug.

## 6.0.2 - 2023-03-08

Expand Down
1 change: 1 addition & 0 deletions all/src/main/content/META-INF/vault/filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<workspaceFilter version="1.0">
<filter root="/apps/acs-commons/config"/>
<filter root="/apps/acs-commons/config.rde"/>
<filter root="/apps/acs-commons/config.author"/>
<filter root="/apps/acs-commons/config.publish"/>
</workspaceFilter>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{

This comment has been minimized.

Copy link
@kwin

kwin Mar 20, 2023

Contributor

@davidjgonzalez Please remove this config which pollutes every RDEs log. DEBUG level should only ever be active temporarily

"org.apache.sling.commons.log.names": [
"com.adobe.acs.commons"
],
"org.apache.sling.commons.log.level": "debug"
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Map;
Expand All @@ -31,6 +33,7 @@
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.adobe.acs.commons.util.BufferedHttpServletResponse;
import com.adobe.acs.commons.util.BufferedServletOutput.ResponseWriteMethod;
import com.day.cq.commons.PathInfo;
Expand All @@ -53,16 +56,13 @@
import org.slf4j.LoggerFactory;

/**
*
* The Environment filter consists of 2 filters:
* * the environment filter, which is registered directly to the HTTP whiteboard, and which can cover
* also non-Sling applications (like CRXDE and the OSGI webconsole)
* also non-Sling applications (like CRXDE and the OSGI webconsole)
* * a Sling filter, which is required for the filtering based on the WCM modes.
*
* <p>
* The environment indicator output is written by the "outer" filter, but its decision might be overwritten
* by the Sling Filter. The status is stored as a request attribute.
*
*
*/


Expand All @@ -77,7 +77,7 @@ public class AemEnvironmentIndicatorFilter implements Filter {
private static final Logger log = LoggerFactory.getLogger(AemEnvironmentIndicatorFilter.class);

private static final String DIV_ID = "acs-commons-env-indicator";

static final String INJECT_INDICATOR_PARAMETER = "AemEnvironmentIndicatorFilter.includeIndicator";
static final String NO_EXTENSION_PLACEHOLDER = "<NONE>";

Expand All @@ -91,8 +91,8 @@ public class AemEnvironmentIndicatorFilter implements Filter {
+ "left: 0;"
+ "top: 0;"
+ "right: 0;"
+ "height: 5px;"
+ "z-index: 100000000000000;";
+ "height: 5px;"
+ "z-index: 100000000000000;";

private static final String TITLE_UPDATE_SCRIPT = "<script>(function() { var c = 0; t = '%s' + ' | ' + document.title, "
+ "i = setInterval(function() { if (document.title === t && c++ > 10) { clearInterval(i); } else { document.title = t; } }, 1500); "
Expand Down Expand Up @@ -140,21 +140,21 @@ public class AemEnvironmentIndicatorFilter implements Filter {
private String titlePrefix = DEFAULT_TITLE_PREFIX;

/* Property: Always Include Base CSS */

private boolean alwaysIncludeBaseCss;

@Property(label = "Always Include Base CSS",
description = "Always include the base CSS scoped to #" + DIV_ID + " { .. }",
boolValue = false)
description = "Always include the base CSS scoped to #" + DIV_ID + " { .. }",
boolValue = false)
public static final String PROP_ALWAYS_INCLUDE_BASE_CSS = "always-include-base-css";

/* Property: Always Include Color CSS */

private boolean alwaysIncludeColorCss;

@Property(label = "Always Include Color CSS",
description = "Always include the color CSS scoped to #" + DIV_ID + " { .. }",
boolValue = false)
description = "Always include the color CSS scoped to #" + DIV_ID + " { .. }",
boolValue = false)
public static final String PROP_ALWAYS_INCLUDE_COLOR_CSS = "always-include-color-css";

@Property(label = "Browser Title",
Expand All @@ -163,24 +163,24 @@ public class AemEnvironmentIndicatorFilter implements Filter {
public static final String PROP_TITLE_PREFIX = "browser-title-prefix";

private static final String[] DEFAULT_EXCLUDED_WCMMODES = {"DISABLED"};
@Property (label = "Excluded WCM modes",
description = "Do not display the indicator when these WCM modes",
@Property(label = "Excluded WCM modes",
description = "Do not display the indicator when these WCM modes are active",
cardinality = Integer.MAX_VALUE)
public static final String PROP_EXCLUDED_WCMMODES = "excluded-wcm-modes";
private String[] excludedWCMModes;


private static final String[] DEFAULT_ALLOWED_EXTENSIONS = {"html", "htm", "jsp", NO_EXTENSION_PLACEHOLDER};
@Property (label = "Allowed URI extensions",
description = "Only inject the environment indicator on URI that use these extensions. Use '"+ NO_EXTENSION_PLACEHOLDER + "' to match on no extension.",
@Property(label = "Allowed URI extensions",
description = "Only inject the environment indicator on URI that use these extensions. Use '" + NO_EXTENSION_PLACEHOLDER + "' to match on no extension.",
cardinality = Integer.MAX_VALUE)
public static final String PROP_ALLOWED_EXTENSIONS = "allowed-extensions";
private String[] allowedExtensions;

private String css = "";

private ServiceRegistration filterRegistration;

private ServiceRegistration innerFilterRegistration;

@Override
Expand Down Expand Up @@ -208,7 +208,7 @@ public final void doFilter(final ServletRequest servletRequest, final ServletRes
}

try (BufferedHttpServletResponse capturedResponse =
new BufferedHttpServletResponse(response, new StringWriter(), null)) {
new BufferedHttpServletResponse(response, new StringWriter(), null)) {

request.setAttribute(INJECT_INDICATOR_PARAMETER, Boolean.TRUE);

Expand All @@ -220,8 +220,8 @@ public final void doFilter(final ServletRequest servletRequest, final ServletRes
// Get contents
final String contents = capturedResponse.getBufferedServletOutput()
.getWriteMethod() == ResponseWriteMethod.WRITER
? capturedResponse.getBufferedServletOutput().getBufferedString()
: null;
? capturedResponse.getBufferedServletOutput().getBufferedString()
: null;

if (contents != null) {
final int bodyIndex = StringUtils.lastIndexOf(contents, "</body>");
Expand All @@ -239,13 +239,13 @@ public final void doFilter(final ServletRequest servletRequest, final ServletRes
}
}
}

boolean innerFilterAcceptsInjection(HttpServletRequest request) {
return request.getAttribute(INJECT_INDICATOR_PARAMETER).equals(Boolean.TRUE);
}

void writeEnvironmentIndicator(String css, String innerHTML, String titlePrefix,
PrintWriter printWriter) {
PrintWriter printWriter) {
if (StringUtils.isNotBlank(css)) {
printWriter.write("<style>" + css + " </style>");
printWriter.write("<div id=\"" + DIV_ID + "\">" + innerHTML + "</div>");
Expand All @@ -270,20 +270,20 @@ boolean accepts(final HttpServletRequest request) {
+ "remove the OSGi configuration and disable completely.");
return false;
} else if (isUnsupportedExtension(request.getRequestURI())) {
log.debug("Request's extension does not match allowed extensions");
log.debug("Request's extension does not match allowed extensions [ {} ]", request.getRequestURI());
return false;
} else if (isUnsupportedRequestMethod(request.getMethod())) {
log.debug("Request was not a GET request");
log.debug("Request was not a GET request [ {} ]", request.getRequestURI());
return false;
} else if (isXhr(request.getHeader("X-Requested-With"))) {
log.debug("Request was an XHR");
log.debug("Request was an XHR [ {} ]", request.getRequestURI());
return false;
} else if (hasAemEditorReferrer(request.getHeader("Referer"), request.getRequestURI())) {
log.debug("Request was for a page in an editor");
log.debug("Request was for a page in an editor [ {} ]", request.getRequestURI());
return false;
}
// Checking for WcmMode does not make sense, it is not available here
log.debug("All checks pass, filter can execute");
log.debug("All checks pass, filter can execute [ {} ]", request.getRequestURI());
return true;
}

Expand Down Expand Up @@ -316,10 +316,24 @@ boolean isXhr(final String headerValue) {
return StringUtils.equals(headerValue, "XMLHttpRequest");
}

boolean hasAemEditorReferrer(final String headerValue, final String requestUri) {
return StringUtils.endsWith(headerValue, "/editor.html" + requestUri)
|| StringUtils.endsWith(headerValue, "/cf")
|| isEditExperienceFragmentVariation(headerValue, requestUri);
boolean hasAemEditorReferrer(final String refererHeaderValue, final String requestUri) {
String refererPath = refererHeaderValue;

try {
if (StringUtils.isNotBlank(refererHeaderValue)) {
URI uri = new URI(refererHeaderValue);
refererPath = uri.getPath();
}
} catch (URISyntaxException e) {
log.info("Could not parse the HTTP Requests's referer header value [ {} ] as a URI. Using raw header value", refererHeaderValue);
}

return StringUtils.endsWith(refererHeaderValue, "/editor.html" + requestUri)
|| StringUtils.endsWith(refererHeaderValue, "/cf")
|| isEditExperienceFragmentVariation(refererHeaderValue, requestUri)
// AEM as a Cloud Service
|| StringUtils.endsWith(refererPath, "/editor.html" + requestUri)
|| StringUtils.endsWith(refererPath, "/cf");
}

boolean isEditExperienceFragmentVariation(String headerValue, String requestUri) {
Expand Down Expand Up @@ -359,7 +373,7 @@ protected final void activate(ComponentContext ctx) {

titlePrefix = xss.encodeForJSString(
PropertiesUtil.toString(config.get(PROP_TITLE_PREFIX), "").toString());

excludedWCMModes = PropertiesUtil.toStringArray(config.get(PROP_EXCLUDED_WCMMODES),
DEFAULT_EXCLUDED_WCMMODES);

Expand All @@ -373,10 +387,10 @@ protected final void activate(ComponentContext ctx) {
"(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=*)");
filterRegistration = ctx.getBundleContext().registerService(Filter.class.getName(),
this, filterProps);

// Register the innerFilter so it is invoked after the WcmRequestFilter (Ranking = 2000)
Dictionary<String, Object> innerFilterProps = new Hashtable<>();
innerFilterProps.put(EngineConstants.SLING_FILTER_SCOPE,EngineConstants.FILTER_SCOPE_REQUEST);
innerFilterProps.put(EngineConstants.SLING_FILTER_SCOPE, EngineConstants.FILTER_SCOPE_REQUEST);
innerFilterProps.put(Constants.SERVICE_RANKING, 1000);
Filter innerFilter = new InnerEnvironmentIndicatorFilter(excludedWCMModes);
innerFilterRegistration = ctx.getBundleContext().registerService(Filter.class.getName(), innerFilter, innerFilterProps);
Expand Down Expand Up @@ -434,7 +448,7 @@ String getCss() {
String getTitlePrefix() {
return titlePrefix;
}

protected static class InnerEnvironmentIndicatorFilter implements Filter {

String[] excludedWcmModes;
Expand Down Expand Up @@ -475,5 +489,5 @@ public void destroy() {
}

}

}
Loading

0 comments on commit 16b159c

Please sign in to comment.