Skip to content

Commit

Permalink
Merge pull request #82 from adobe/features/remove-ca-config
Browse files Browse the repository at this point in the history
Style merger mode and HTML sanitizing mode move from CaConfig to page properties
  • Loading branch information
edoardo-goracci committed Apr 5, 2022
2 parents fa76c34 + e671ed0 commit b2e43aa
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 150 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.annotation.Annotation;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -35,14 +34,13 @@
import org.apache.jackrabbit.JcrConstants;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.caconfig.ConfigurationBuilder;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.engine.SlingRequestProcessor;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.email.core.components.config.StylesInlinerContextAwareConfiguration;
import com.adobe.cq.email.core.components.enumerations.HtmlSanitizingMode;
import com.adobe.cq.email.core.components.enumerations.StyleMergerMode;
import com.adobe.cq.email.core.components.internal.css.CssInliner;
Expand All @@ -62,6 +60,8 @@ public class StylesInlinerFilter implements Filter {
private static final Logger LOG = LoggerFactory.getLogger(CssInliner.class);
static final String RESOURCE_TYPE = "core/email/components/page";
static final String PROCESSED_ATTRIBUTE = "styles_filter_processed";
static final String STYLE_MERGER_MODE_PROPERTY = "styleMergerMode";
static final String HTML_SANITIZING_MODE_PROPERTY = "htmlSanitizingMode";

@Reference
private transient RequestResponseFactory requestResponseFactory;
Expand Down Expand Up @@ -101,11 +101,10 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
HttpServletResponse response = requestResponseFactory.createResponse(out);
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
requestProcessor.processRequest(req, response, request.getResourceResolver());
StylesInlinerContextAwareConfiguration configuration = getConfiguration(resource);
StyleInlinerConfig config = getConfig(contentResource);
String htmlWithInlineStyles =
stylesInlinerService.getHtmlWithInlineStyles(request.getResourceResolver(), out.toString(StandardCharsets.UTF_8.name()),
StyleMergerMode.getByValue(configuration.stylesMergingMode()),
HtmlSanitizingMode.getByValue(configuration.htmlSanitizingMode()));
config.getStyleMergerMode(), config.getHtmlSanitizingMode());
servletResponse.setContentType("text/html");
servletResponse.setCharacterEncoding(StandardCharsets.UTF_8.name());
PrintWriter pw = servletResponse.getWriter();
Expand Down Expand Up @@ -144,33 +143,36 @@ private boolean hasBeenProcessed(SlingHttpServletRequest request) {
}
}

private StylesInlinerContextAwareConfiguration getConfiguration(Resource resource) {
StylesInlinerContextAwareConfiguration fallback = new StylesInlinerContextAwareConfiguration() {
@Override
public Class<? extends Annotation> annotationType() {
return StylesInlinerContextAwareConfiguration.class;
}

@Override
public String stylesMergingMode() {
return null;
}

@Override
public String htmlSanitizingMode() {
return null;
}
};
private StyleInlinerConfig getConfig(Resource contentResource) {
StyleInlinerConfig fallback = new StyleInlinerConfig(StyleMergerMode.PROCESS_SPECIFICITY, HtmlSanitizingMode.FULL);
try {
ConfigurationBuilder configurationBuilder = resource.adaptTo(ConfigurationBuilder.class);
if (Objects.isNull(configurationBuilder)) {
return fallback;
}
return configurationBuilder.as(StylesInlinerContextAwareConfiguration.class);
ValueMap valueMap = contentResource.getValueMap();
String styleMergerMode = valueMap.get(STYLE_MERGER_MODE_PROPERTY, String.class);
String htmlSanitizingMode = valueMap.get(HTML_SANITIZING_MODE_PROPERTY, String.class);
return new StyleInlinerConfig(StyleMergerMode.getByValue(styleMergerMode), HtmlSanitizingMode.getByValue(htmlSanitizingMode));
} catch (Throwable e) {
LOG.warn("Error retrieving configuration: " + e.getMessage(), e);
LOG.warn("Error retrieving Style Inliner config: " + e.getMessage(), e);
}
return fallback;
}

private static class StyleInlinerConfig {
private final StyleMergerMode styleMergerMode;
private final HtmlSanitizingMode htmlSanitizingMode;

public StyleInlinerConfig(StyleMergerMode styleMergerMode,
HtmlSanitizingMode htmlSanitizingMode) {
this.styleMergerMode = styleMergerMode;
this.htmlSanitizingMode = htmlSanitizingMode;
}

public StyleMergerMode getStyleMergerMode() {
return styleMergerMode;
}

public HtmlSanitizingMode getHtmlSanitizingMode() {
return htmlSanitizingMode;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.annotation.Annotation;
import java.util.Objects;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -31,7 +29,7 @@
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.caconfig.ConfigurationBuilder;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.engine.SlingRequestProcessor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -41,7 +39,6 @@
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import com.adobe.cq.email.core.components.config.StylesInlinerContextAwareConfiguration;
import com.adobe.cq.email.core.components.enumerations.HtmlSanitizingMode;
import com.adobe.cq.email.core.components.enumerations.StyleMergerMode;
import com.adobe.cq.email.core.components.services.StylesInlinerService;
Expand Down Expand Up @@ -75,9 +72,9 @@ class StylesInlinerFilterTest {
@Mock
Resource resource;
@Mock
Resource jcrContentNode;
Resource contentResource;
@Mock
ConfigurationBuilder configurationBuilder;
ValueMap valueMap;
@Mock
SlingHttpServletRequest request;
@Mock
Expand All @@ -99,8 +96,9 @@ void setUp() throws IOException {
);
when(request.getResource()).thenReturn(resource);
when(resource.getPath()).thenReturn("TEST_PATH");
when(resource.getChild(eq(JcrConstants.JCR_CONTENT))).thenReturn(jcrContentNode);
when(jcrContentNode.getResourceType()).thenReturn(StylesInlinerFilter.RESOURCE_TYPE);
when(resource.getChild(eq(JcrConstants.JCR_CONTENT))).thenReturn(contentResource);
when(contentResource.getResourceType()).thenReturn(StylesInlinerFilter.RESOURCE_TYPE);
when(contentResource.getValueMap()).thenReturn(valueMap);
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
when(requestResponseFactory.createRequest(eq("GET"), eq("TEST_PATH.html"), anyMap())).thenReturn(httpServletRequest);
doAnswer(i -> {
Expand Down Expand Up @@ -133,7 +131,8 @@ void noGetRenderedHtmlAttributeInRequest() throws ServletException, IOException

@Test
void noConfig() throws ServletException, IOException {
when(resource.adaptTo(eq(ConfigurationBuilder.class))).thenReturn(null);
when(valueMap.get(eq(StylesInlinerFilter.STYLE_MERGER_MODE_PROPERTY), eq(String.class))).thenReturn(null);
when(valueMap.get(eq(StylesInlinerFilter.HTML_SANITIZING_MODE_PROPERTY), eq(String.class))).thenReturn(null);
sut.doFilter(request, resp, filterChain);
verifyZeroInteractions(filterChain);
verify(printWriter).write(eq(OUTPUT_PROCESSING_CSS_SPECIFICITY));
Expand All @@ -142,7 +141,8 @@ void noConfig() throws ServletException, IOException {
@Test
@MockitoSettings(strictness = Strictness.LENIENT)
void noConfig_GetRenderedHtmlAttributeInRequest() throws ServletException, IOException {
when(resource.adaptTo(eq(ConfigurationBuilder.class))).thenReturn(null);
when(valueMap.get(eq(StylesInlinerFilter.STYLE_MERGER_MODE_PROPERTY), eq(String.class))).thenReturn(null);
when(valueMap.get(eq(StylesInlinerFilter.HTML_SANITIZING_MODE_PROPERTY), eq(String.class))).thenReturn(null);
when(request.getAttribute(StylesInlinerFilter.PROCESSED_ATTRIBUTE)).thenReturn(true);
sut.doFilter(request, resp, filterChain);
verify(filterChain).doFilter(eq(request), eq(resp));
Expand All @@ -151,8 +151,9 @@ void noConfig_GetRenderedHtmlAttributeInRequest() throws ServletException, IOExc

@Test
void noStyleMergerMode() throws ServletException, IOException {
when(resource.adaptTo(eq(ConfigurationBuilder.class))).thenReturn(configurationBuilder);
when(configurationBuilder.as(StylesInlinerContextAwareConfiguration.class)).thenReturn(create(null));
when(valueMap.get(eq(StylesInlinerFilter.STYLE_MERGER_MODE_PROPERTY), eq(String.class))).thenReturn(null);
when(valueMap.get(eq(StylesInlinerFilter.HTML_SANITIZING_MODE_PROPERTY), eq(String.class))).thenReturn(
HtmlSanitizingMode.FULL.name());
sut.doFilter(request, resp, filterChain);
verifyZeroInteractions(filterChain);
verify(printWriter).write(eq(OUTPUT_PROCESSING_CSS_SPECIFICITY));
Expand All @@ -161,18 +162,21 @@ void noStyleMergerMode() throws ServletException, IOException {
@Test
@MockitoSettings(strictness = Strictness.LENIENT)
void noStyleMergerMode_GetRenderedHtmlAttributeInRequest() throws ServletException, IOException {
when(resource.adaptTo(eq(ConfigurationBuilder.class))).thenReturn(configurationBuilder);
when(valueMap.get(eq(StylesInlinerFilter.STYLE_MERGER_MODE_PROPERTY), eq(String.class))).thenReturn(null);
when(valueMap.get(eq(StylesInlinerFilter.HTML_SANITIZING_MODE_PROPERTY), eq(String.class))).thenReturn(
HtmlSanitizingMode.FULL.name());
when(request.getAttribute(StylesInlinerFilter.PROCESSED_ATTRIBUTE)).thenReturn(true);
when(configurationBuilder.as(StylesInlinerContextAwareConfiguration.class)).thenReturn(create(null));
sut.doFilter(request, resp, filterChain);
verify(filterChain).doFilter(eq(request), eq(resp));
verifyZeroInteractions(printWriter);
}

@Test
void processingCssSpecificity() throws Exception {
when(resource.adaptTo(eq(ConfigurationBuilder.class))).thenReturn(configurationBuilder);
when(configurationBuilder.as(StylesInlinerContextAwareConfiguration.class)).thenReturn(create(StyleMergerMode.PROCESS_SPECIFICITY));
when(valueMap.get(eq(StylesInlinerFilter.STYLE_MERGER_MODE_PROPERTY), eq(String.class))).thenReturn(
StyleMergerMode.PROCESS_SPECIFICITY.name());
when(valueMap.get(eq(StylesInlinerFilter.HTML_SANITIZING_MODE_PROPERTY), eq(String.class))).thenReturn(
HtmlSanitizingMode.FULL.name());
sut.doFilter(request, resp, filterChain);
verifyZeroInteractions(filterChain);
verify(printWriter).write(eq(OUTPUT_PROCESSING_CSS_SPECIFICITY));
Expand All @@ -181,18 +185,22 @@ void processingCssSpecificity() throws Exception {
@Test
@MockitoSettings(strictness = Strictness.LENIENT)
void processingCssSpecificity_GetRenderedHtmlAttributeInRequest() throws Exception {
when(resource.adaptTo(eq(ConfigurationBuilder.class))).thenReturn(configurationBuilder);
when(valueMap.get(eq(StylesInlinerFilter.STYLE_MERGER_MODE_PROPERTY), eq(String.class))).thenReturn(
StyleMergerMode.PROCESS_SPECIFICITY.name());
when(valueMap.get(eq(StylesInlinerFilter.HTML_SANITIZING_MODE_PROPERTY), eq(String.class))).thenReturn(
HtmlSanitizingMode.FULL.name());
when(request.getAttribute(StylesInlinerFilter.PROCESSED_ATTRIBUTE)).thenReturn(true);
when(configurationBuilder.as(StylesInlinerContextAwareConfiguration.class)).thenReturn(create(StyleMergerMode.PROCESS_SPECIFICITY));
sut.doFilter(request, resp, filterChain);
verify(filterChain).doFilter(eq(request), eq(resp));
verifyZeroInteractions(printWriter);
}

@Test
void ignoringCssSpecificity() throws Exception {
when(resource.adaptTo(eq(ConfigurationBuilder.class))).thenReturn(configurationBuilder);
when(configurationBuilder.as(StylesInlinerContextAwareConfiguration.class)).thenReturn(create(StyleMergerMode.IGNORE_SPECIFICITY));
when(valueMap.get(eq(StylesInlinerFilter.STYLE_MERGER_MODE_PROPERTY), eq(String.class))).thenReturn(
StyleMergerMode.IGNORE_SPECIFICITY.name());
when(valueMap.get(eq(StylesInlinerFilter.HTML_SANITIZING_MODE_PROPERTY), eq(String.class))).thenReturn(
HtmlSanitizingMode.FULL.name());
sut.doFilter(request, resp, filterChain);
verifyZeroInteractions(filterChain);
verify(printWriter).write(eq(OUTPUT_IGNORING_CSS_SPECIFICITY));
Expand All @@ -201,18 +209,22 @@ void ignoringCssSpecificity() throws Exception {
@Test
@MockitoSettings(strictness = Strictness.LENIENT)
void ignoringCssSpecificity_GetRenderedHtmlAttributeInRequest() throws Exception {
when(resource.adaptTo(eq(ConfigurationBuilder.class))).thenReturn(configurationBuilder);
when(valueMap.get(eq(StylesInlinerFilter.STYLE_MERGER_MODE_PROPERTY), eq(String.class))).thenReturn(
StyleMergerMode.IGNORE_SPECIFICITY.name());
when(valueMap.get(eq(StylesInlinerFilter.HTML_SANITIZING_MODE_PROPERTY), eq(String.class))).thenReturn(
HtmlSanitizingMode.FULL.name());
when(request.getAttribute(StylesInlinerFilter.PROCESSED_ATTRIBUTE)).thenReturn(true);
when(configurationBuilder.as(StylesInlinerContextAwareConfiguration.class)).thenReturn(create(StyleMergerMode.IGNORE_SPECIFICITY));
sut.doFilter(request, resp, filterChain);
verify(filterChain).doFilter(eq(request), eq(resp));
verifyZeroInteractions(printWriter);
}

@Test
void alwaysAppendingCssProperties() throws Exception {
when(resource.adaptTo(eq(ConfigurationBuilder.class))).thenReturn(configurationBuilder);
when(configurationBuilder.as(StylesInlinerContextAwareConfiguration.class)).thenReturn(create(StyleMergerMode.ALWAYS_APPEND));
when(valueMap.get(eq(StylesInlinerFilter.STYLE_MERGER_MODE_PROPERTY), eq(String.class))).thenReturn(
StyleMergerMode.ALWAYS_APPEND.name());
when(valueMap.get(eq(StylesInlinerFilter.HTML_SANITIZING_MODE_PROPERTY), eq(String.class))).thenReturn(
HtmlSanitizingMode.FULL.name());
sut.doFilter(request, resp, filterChain);
verifyZeroInteractions(filterChain);
verify(printWriter).write(eq(OUTPUT_ALWAYS_APPENDING_CSS_PROPERTIES));
Expand All @@ -221,30 +233,14 @@ void alwaysAppendingCssProperties() throws Exception {
@Test
@MockitoSettings(strictness = Strictness.LENIENT)
void alwaysAppendingCssProperties_GetRenderedHtmlAttributeInRequest() throws Exception {
when(resource.adaptTo(eq(ConfigurationBuilder.class))).thenReturn(configurationBuilder);
when(valueMap.get(eq(StylesInlinerFilter.STYLE_MERGER_MODE_PROPERTY), eq(String.class))).thenReturn(
StyleMergerMode.ALWAYS_APPEND.name());
when(valueMap.get(eq(StylesInlinerFilter.HTML_SANITIZING_MODE_PROPERTY), eq(String.class))).thenReturn(
HtmlSanitizingMode.FULL.name());
when(request.getAttribute(StylesInlinerFilter.PROCESSED_ATTRIBUTE)).thenReturn(true);
when(configurationBuilder.as(StylesInlinerContextAwareConfiguration.class)).thenReturn(create(StyleMergerMode.ALWAYS_APPEND));
sut.doFilter(request, resp, filterChain);
verify(filterChain).doFilter(eq(request), eq(resp));
verifyZeroInteractions(printWriter);
}

private StylesInlinerContextAwareConfiguration create(StyleMergerMode mode) {
return new StylesInlinerContextAwareConfiguration() {
@Override
public Class<? extends Annotation> annotationType() {
return StylesInlinerContextAwareConfiguration.class;
}

@Override
public String stylesMergingMode() {
return Objects.isNull(mode) ? null : mode.name();
}

@Override
public String htmlSanitizingMode() {
return HtmlSanitizingMode.FULL.name();
}
};
}
}
Loading

0 comments on commit b2e43aa

Please sign in to comment.