You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by je...@apache.org on 2019/08/20 18:16:47 UTC
[sling-whiteboard] 01/03: initial commit
This is an automated email from the ASF dual-hosted git repository.
jeb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
commit 137c6aae1cf3de4b385299a5785e17318386669b
Author: JE Bailey <ja...@sas.com>
AuthorDate: Tue Aug 13 13:03:45 2019 -0400
initial commit
---
transformer/.gitignore | 17 ++
transformer/CODE_OF_CONDUCT.md | 22 ++
transformer/CONTRIBUTING.md | 24 +++
transformer/Jenkinsfile | 20 ++
transformer/LICENSE | 202 +++++++++++++++++
transformer/README.md | 9 +
transformer/pom.xml | 240 +++++++++++++++++++++
.../ProcessingComponentConfiguration.java | 41 ++++
.../sling/transformer/ProcessingContext.java | 55 +++++
.../org/apache/sling/transformer/Processor.java | 57 +++++
.../sling/transformer/ProcessorConfiguration.java | 44 ++++
.../apache/sling/transformer/ProcessorFactory.java | 36 ++++
.../apache/sling/transformer/ProcessorManager.java | 40 ++++
.../sling/transformer/TransformationStep.java | 10 +
.../org/apache/sling/transformer/Transformer.java | 50 +++++
.../sling/transformer/TransformerFactory.java | 51 +++++
.../sling/transformer/impl/LinkTransformer.java | 35 +++
.../org/apache/sling/transformer/impl/Process.java | 73 +++++++
.../sling/transformer/impl/ProcessManager.java | 24 +++
.../sling/transformer/impl/RewriterResponse.java | 188 ++++++++++++++++
.../transformer/impl/ServletProcessingContext.java | 91 ++++++++
.../transformer/impl/TransformationFilter.java | 93 ++++++++
.../impl/TransformationStepWrapper.java | 25 +++
.../transformer/impl/WebConsoleConfigPrinter.java | 74 +++++++
.../org/apache/sling/transformer/package-info.java | 24 +++
.../apache/sling/rewriter/it/tests/EmojiIT.java | 60 ++++++
.../rewriter/it/tests/RewriterTestSupport.java | 94 ++++++++
.../impl/ProcessorConfigurationImplTest.java | 193 +++++++++++++++++
.../test/resources/apps/esp/page/emoji/html.esp | 28 +++
.../src/test/resources/content/rewriter.json | 11 +
30 files changed, 1931 insertions(+)
diff --git a/transformer/.gitignore b/transformer/.gitignore
new file mode 100644
index 0000000..5b783ed
--- /dev/null
+++ b/transformer/.gitignore
@@ -0,0 +1,17 @@
+/target
+.idea
+.classpath
+.metadata
+.project
+.settings
+.externalToolBuilders
+maven-eclipse.xml
+*.swp
+*.iml
+*.ipr
+*.iws
+*.bak
+.vlt
+.DS_Store
+jcr.log
+atlassian-ide-plugin.xml
diff --git a/transformer/CODE_OF_CONDUCT.md b/transformer/CODE_OF_CONDUCT.md
new file mode 100644
index 0000000..0fa18e5
--- /dev/null
+++ b/transformer/CODE_OF_CONDUCT.md
@@ -0,0 +1,22 @@
+<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
+Apache Software Foundation Code of Conduct
+====
+
+Being an Apache project, Apache Sling adheres to the Apache Software Foundation's [Code of Conduct](https://www.apache.org/foundation/policies/conduct.html).
diff --git a/transformer/CONTRIBUTING.md b/transformer/CONTRIBUTING.md
new file mode 100644
index 0000000..ac82a1a
--- /dev/null
+++ b/transformer/CONTRIBUTING.md
@@ -0,0 +1,24 @@
+<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
+Contributing
+====
+
+Thanks for choosing to contribute!
+
+You will find all the necessary details about how you can do this at https://sling.apache.org/contributing.html.
diff --git a/transformer/Jenkinsfile b/transformer/Jenkinsfile
new file mode 100644
index 0000000..f582519
--- /dev/null
+++ b/transformer/Jenkinsfile
@@ -0,0 +1,20 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+slingOsgiBundleBuild()
diff --git a/transformer/LICENSE b/transformer/LICENSE
new file mode 100644
index 0000000..d645695
--- /dev/null
+++ b/transformer/LICENSE
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/transformer/README.md b/transformer/README.md
new file mode 100644
index 0000000..40e71e8
--- /dev/null
+++ b/transformer/README.md
@@ -0,0 +1,9 @@
+[<img src="https://sling.apache.org/res/logos/sling.png"/>](https://sling.apache.org)
+
+ [![Build Status](https://builds.apache.org/buildStatus/icon?job=Sling/sling-org-apache-sling-rewriter/master)](https://builds.apache.org/job/Sling/job/sling-org-apache-sling-rewriter/job/master) [![Test Status](https://img.shields.io/jenkins/t/https/builds.apache.org/job/Sling/job/sling-org-apache-sling-rewriter/job/master.svg)](https://builds.apache.org/job/Sling/job/sling-org-apache-sling-rewriter/job/master/test_results_analyzer/) [![Maven Central](https://maven-badges.herokuapp.com/ [...]
+
+# Apache Sling Rewriter
+
+This module is part of the [Apache Sling](https://sling.apache.org) project.
+
+[More details](http://sling.apache.org/documentation/bundles/output-rewriting-pipelines-org-apache-sling-rewriter.html)
diff --git a/transformer/pom.xml b/transformer/pom.xml
new file mode 100644
index 0000000..92cd653
--- /dev/null
+++ b/transformer/pom.xml
@@ -0,0 +1,240 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling-bundle-parent</artifactId>
+ <version>35</version>
+ <relativePath />
+ </parent>
+
+ <artifactId>org.apache.sling.transformer</artifactId>
+ <version>1.2.3-SNAPSHOT</version>
+
+ <name>Apache Sling Transformer</name>
+ <description>
+ The Apache Sling Transformer allows configurable post processing of any output
+ from Apache Sling. The output can be processed by a configurable processor
+ or a configurable pipeline.
+ </description>
+
+ <properties>
+ <org.ops4j.pax.exam.version>4.13.1</org.ops4j.pax.exam.version>
+ </properties>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>biz.aQute.bnd</groupId>
+ <artifactId>bnd-maven-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>biz.aQute.bnd</groupId>
+ <artifactId>bnd-baseline-maven-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>generate-adapter-metadata</id>
+ <phase>process-classes</phase>
+ <goals>
+ <goal>generate-adapter-metadata</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-failsafe-plugin</artifactId>
+ <executions>
+ <execution>
+ <goals>
+ <goal>integration-test</goal>
+ <goal>verify</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <systemProperties>
+ <property>
+ <name>bundle.filename</name>
+ <value>${basedir}/target/${project.build.finalName}.jar</value>
+ </property>
+ </systemProperties>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.servicemix.tooling</groupId>
+ <artifactId>depends-maven-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.4</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.api</artifactId>
+ <version>2.11.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.commons.osgi</artifactId>
+ <version>2.0.6</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.commons.html</artifactId>
+ <scope>provided</scope>
+ <version>1.1.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>adapter-annotations</artifactId>
+ <version>1.0.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.resource.presence</artifactId>
+ <version>0.0.2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.paxexam</artifactId>
+ <version>3.0.0</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>javax.servlet-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <!-- OSGi -->
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.annotation.versioning</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>osgi.cmpn</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>osgi.core</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <!-- logging -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <!-- testing -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.sling-mock.junit4</artifactId>
+ <version>2.3.12</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.logging-mock</artifactId>
+ <version>2.0.0</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>1.9.5</version>
+ <scope>test</scope>
+ </dependency>
+ <!-- Apache Felix -->
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.framework</artifactId>
+ <version>6.0.3</version>
+ <scope>test</scope>
+ </dependency>
+ <!-- jsoup -->
+ <dependency>
+ <groupId>org.jsoup</groupId>
+ <artifactId>jsoup</artifactId>
+ <version>1.12.1</version>
+ <scope>test</scope>
+ </dependency>
+ <!-- testing -->
+ <dependency>
+ <groupId>org.apache.servicemix.bundles</groupId>
+ <artifactId>org.apache.servicemix.bundles.hamcrest</artifactId>
+ <version>1.3_1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam</artifactId>
+ <version>${org.ops4j.pax.exam.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam-cm</artifactId>
+ <version>${org.ops4j.pax.exam.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam-container-forked</artifactId>
+ <version>${org.ops4j.pax.exam.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam-junit4</artifactId>
+ <version>${org.ops4j.pax.exam.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam-link-mvn</artifactId>
+ <version>${org.ops4j.pax.exam.version}</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/transformer/src/main/java/org/apache/sling/transformer/ProcessingComponentConfiguration.java b/transformer/src/main/java/org/apache/sling/transformer/ProcessingComponentConfiguration.java
new file mode 100644
index 0000000..06a7e5c
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/ProcessingComponentConfiguration.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.transformer;
+
+import org.apache.sling.api.resource.ValueMap;
+
+
+/**
+ * Configuration of a processing component.
+ */
+public interface ProcessingComponentConfiguration {
+
+ /** Configuration for an optional component, only transformers support this option. */
+ String CONFIGURATION_COMPONENT_OPTIONAL = "component-optional";
+
+ /**
+ * The name/type of the component.
+ * @return A unique name for this component.
+ */
+ String getType();
+
+ /**
+ * Return the configuration for this component.
+ * @return The configuration for this component or an empty map if there is none.
+ */
+ ValueMap getConfiguration();
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/ProcessingContext.java b/transformer/src/main/java/org/apache/sling/transformer/ProcessingContext.java
new file mode 100644
index 0000000..2720a8a
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/ProcessingContext.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.transformer;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+
+/**
+ * The context for a processor invocation.
+ */
+public interface ProcessingContext {
+
+ /**
+ * The current request.
+ */
+ SlingHttpServletRequest getRequest();
+
+ /**
+ * The current response.
+ */
+ SlingHttpServletResponse getResponse();
+
+ /**
+ * The content type of the response.
+ */
+ String getContentType();
+
+ /**
+ * The writer.
+ */
+ PrintWriter getWriter() throws IOException;
+
+ /**
+ * The output stream.
+ */
+ OutputStream getOutputStream() throws IOException;
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/Processor.java b/transformer/src/main/java/org/apache/sling/transformer/Processor.java
new file mode 100644
index 0000000..21bf02e
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/Processor.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.transformer;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import org.xml.sax.ContentHandler;
+
+/**
+ * A <code>Processor</code> post processes the output.
+ */
+public interface Processor {
+
+ /**
+ * Initialize this component.
+ * @param context The invocation context.
+ * @param config The configuration for this component.
+ * @throws IOException If an error occurs
+ */
+ void init(ProcessingContext context,
+ ProcessorConfiguration config)
+ throws IOException;
+
+ /**
+ * Get the writer to write the output to.
+ * @return A print writer.
+ */
+ PrintWriter getWriter();
+
+ /**
+ * Get the content handler to write the output to.
+ * @return A content handler
+ */
+ ContentHandler getContentHandler();
+
+ /**
+ * Clean up the processor.
+ * This method should always be called after a processor run, regardless if it was successful or failed.
+ * @param errorOccurred Indicates if an error occurred during processing
+ */
+ void finished(boolean errorOccurred) throws IOException;
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/ProcessorConfiguration.java b/transformer/src/main/java/org/apache/sling/transformer/ProcessorConfiguration.java
new file mode 100644
index 0000000..fb133a6
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/ProcessorConfiguration.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.transformer;
+
+import java.util.Map;
+
+
+/**
+ * Configuration of a processor.
+ */
+public interface ProcessorConfiguration {
+
+ /**
+ * The name/type of the component.
+ * @return A unique name for this component.
+ */
+ String getType();
+
+ /**
+ * Return the configuration for this component.
+ * @return The configuration for this component or an empty map if there is none.
+ */
+ Map<String, Object> getConfiguration();
+
+ /**
+ * Check if this processor configuration matches for the current request.
+ * @return true or false
+ */
+ boolean match(ProcessingContext context);
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/ProcessorFactory.java b/transformer/src/main/java/org/apache/sling/transformer/ProcessorFactory.java
new file mode 100644
index 0000000..c8a5d03
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/ProcessorFactory.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.transformer;
+
+
+/**
+ * The <code>ProcessorFactory</code> is a service which creates
+ * {@link Processor}s on demand. The processor is used to rewrite
+ * the generated content.
+ *
+ * The factory is referenced using a service property named
+ * 'pipeline.type'. Each factory should have a unique value
+ * for this property.
+ */
+public interface ProcessorFactory {
+
+ /**
+ * Create a new processor.
+ * @return A new processor.
+ */
+ Processor createProcessor();
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/ProcessorManager.java b/transformer/src/main/java/org/apache/sling/transformer/ProcessorManager.java
new file mode 100644
index 0000000..6d88809
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/ProcessorManager.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.transformer;
+
+import java.util.List;
+
+/**
+ * This service manages the processor configurations.
+ */
+public interface ProcessorManager {
+
+ /**
+ * Return the list of currently available processor configurations.
+ *
+ * @return The list of processor configurations in the order to check.
+ */
+ List<ProcessorConfiguration> getProcessorConfigurations();
+
+ /**
+ * Return a pipeline for a pipeline configuration.
+ *
+ * @throws org.apache.sling.api.SlingException
+ * If an error occurs during setup
+ */
+ Processor getProcessor(ProcessorConfiguration configuration, ProcessingContext context);
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/TransformationStep.java b/transformer/src/main/java/org/apache/sling/transformer/TransformationStep.java
new file mode 100644
index 0000000..38d64ab
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/TransformationStep.java
@@ -0,0 +1,10 @@
+package org.apache.sling.transformer;
+
+import org.apache.sling.commons.html.HtmlElement;
+import org.apache.sling.transformer.impl.Process;
+
+public interface TransformationStep {
+
+ public void handle(HtmlElement element, Process process);
+
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/Transformer.java b/transformer/src/main/java/org/apache/sling/transformer/Transformer.java
new file mode 100644
index 0000000..6a83092
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/Transformer.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.transformer;
+
+import java.io.IOException;
+
+import org.xml.sax.ContentHandler;
+
+/**
+ * The <code>Transformer</code> interface defines the middle of a rewriter pipeline.
+ */
+public interface Transformer extends ContentHandler {
+
+ /**
+ * Initialize this component.
+ * @param context The invocation context.
+ * @param config The configuration for this component.
+ */
+ void init(ProcessingContext context, ProcessingComponentConfiguration config)
+ throws IOException;
+
+ /**
+ * Set the content handler the transformer should stream to.
+ * @param handler Another transformer or a serializer.
+ */
+ void setContentHandler(ContentHandler handler);
+
+ /**
+ * Dispose the transformer.
+ * This method is always invoked by the rewriter in order to
+ * allow the transformer to release any resources etc. After
+ * this method has been called the instance is considered
+ * unusable.
+ */
+ void dispose();
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/TransformerFactory.java b/transformer/src/main/java/org/apache/sling/transformer/TransformerFactory.java
new file mode 100644
index 0000000..681dd9b
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/TransformerFactory.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.transformer;
+
+
+/**
+ * The <code>TransformerFactory</code> is a service which creates
+ * {@link Transformer}s on demand. The created transformers form
+ * the middle part of the rewriter pipeline.
+ *
+ * The factories itself are not chained but the resulting transformers
+ * are. On each pipeline call new instances are created.
+ *
+ * The factory is referenced using a service property named
+ * 'pipeline.type'. Each factory should have a unique value
+ * for this property.
+ *
+ * With the optional property 'pipeline.mode' set to the value
+ * 'global' the transformer is used for each and every pipeline regardless
+ * of the actual configuration for this pipeline.
+ * All available global transformers with a service ranking below
+ * zero are chained right after the generator. All available global
+ * transformers with a service ranking higher or equal to zero are
+ * chained right before the serializer. Therefore the property
+ * "service.ranking" should be used for the factory in combination
+ * with 'pipeline.mode'.
+ * To be compatible with possible future uses of the 'pipeline.mode'
+ * property, it should only be used with the value 'global'.
+ */
+public interface TransformerFactory {
+
+ /**
+ * Create a new transformer for the pipeline.
+ * @return A new transformer.
+ */
+ Transformer createTransformer();
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/impl/LinkTransformer.java b/transformer/src/main/java/org/apache/sling/transformer/impl/LinkTransformer.java
new file mode 100644
index 0000000..1ac0836
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/LinkTransformer.java
@@ -0,0 +1,35 @@
+package org.apache.sling.transformer.impl;
+
+import org.apache.sling.commons.html.HtmlElement;
+import org.apache.sling.commons.html.HtmlElementType;
+import org.apache.sling.transformer.TransformationStep;
+import org.osgi.service.component.annotations.Component;
+
+@Component(property = {
+ "extension=html",
+ "path=/content/*",
+ "type=REQUEST"
+})
+public class LinkTransformer implements TransformationStep {
+
+ public void handle(HtmlElement element, Process process) {
+ if (element.getType() != HtmlElementType.START_TAG) {
+ process.next(element);
+ return;
+ }
+ if (element.containsAttribute("href")) {
+ String value = element.getAttributeValue("href");
+ if (value != null && value.startsWith("/")) {
+ element.setAttribute("href", "http://www.apache.org" + value);
+ }
+ }
+ if (element.containsAttribute("src")) {
+ String value = element.getAttributeValue("src");
+ if (value != null && value.startsWith("/")) {
+ element.setAttribute("src", "http://www.apache.org" + value);
+ }
+ }
+ process.next(element);
+ }
+
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/impl/Process.java b/transformer/src/main/java/org/apache/sling/transformer/impl/Process.java
new file mode 100644
index 0000000..c2f7855
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/Process.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.transformer.impl;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.BiConsumer;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+import org.apache.sling.commons.html.HtmlElement;
+import org.osgi.annotation.versioning.ProviderType;
+
+/**
+ * Utility class that allows you to define a flatMap process in the form of a
+ * BiConsumer<Element,TagMapping> lambda.
+ *
+ * This allows you to use the next() method to collect the elements that will be
+ * passed on to the stream method. This can modify the eventual output and assists in use cases where
+ * there is a need to add or remove elements
+ *
+ */
+@ProviderType
+public class Process {
+
+ private List<HtmlElement> list = new ArrayList<>();
+ private Map<String,Object> context = new HashMap<>();
+
+
+ private Process() {
+ }
+
+ /**
+ * Collects all the elements that are either being passed through or created in
+ * the accept method of the consumer so that they may be passed on to the next
+ * process.
+ */
+ public void next(HtmlElement... elements) {
+ Collections.addAll(list, elements);
+ }
+
+ Function<HtmlElement, Stream<HtmlElement>> createFlatMap(BiConsumer<HtmlElement, Process> consumer, Process mapper) {
+ return element -> {
+ list.clear();
+ consumer.accept(element, mapper);
+ return list.stream();
+ };
+ }
+
+ public static Function<HtmlElement, Stream<HtmlElement>> map(BiConsumer<HtmlElement, Process> consumer) {
+ Process mapper = new Process();
+ return mapper.createFlatMap(consumer, mapper);
+ }
+
+ public Stream<HtmlElement> getElements() {
+ return list.stream();
+ }
+
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/impl/ProcessManager.java b/transformer/src/main/java/org/apache/sling/transformer/impl/ProcessManager.java
new file mode 100644
index 0000000..61cb16e
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/ProcessManager.java
@@ -0,0 +1,24 @@
+package org.apache.sling.transformer.impl;
+
+import java.util.Map;
+
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+
+@Component
+public class ProcessManager {
+
+
+ @Reference
+ private Process processes;
+
+ @Reference
+ void bindProcess(Process process, Map<String, ?> properties) {
+
+ }
+
+ void unbindProcess(Process process) {
+
+ }
+
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/impl/RewriterResponse.java b/transformer/src/main/java/org/apache/sling/transformer/impl/RewriterResponse.java
new file mode 100644
index 0000000..f21bab2
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/RewriterResponse.java
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.transformer.impl;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.sling.adapter.annotations.Adaptable;
+import org.apache.sling.adapter.annotations.Adapter;
+import org.apache.sling.api.SlingException;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper;
+import org.apache.sling.transformer.ProcessingContext;
+import org.apache.sling.transformer.Processor;
+import org.apache.sling.transformer.ProcessorConfiguration;
+import org.apache.sling.transformer.ProcessorManager;
+import org.xml.sax.ContentHandler;
+
+/**
+ * This response is used to pass the output through the rewriter pipeline.
+ */
+@Adaptable(adaptableClass=SlingHttpServletResponse.class, adapters={
+ @Adapter(value=ContentHandler.class,
+ condition="When the response is being processed through the Sling Rewriter filter.")
+})
+class RewriterResponse
+ extends SlingHttpServletResponseWrapper {
+
+ /** The current request. */
+ private final SlingHttpServletRequest request;
+
+ /** The processor. */
+ private Processor processor;
+
+ /** wrapped rewriter/servlet writer */
+ private PrintWriter writer;
+
+ /** response content type */
+ private String contentType;
+
+ /** The processor manager. */
+ private final ProcessorManager processorManager;
+
+ /**
+ * Initializes a new instance.
+ * @param request The sling request.
+ * @param delegatee The SlingHttpServletResponse wrapped by this instance.
+ */
+ public RewriterResponse(SlingHttpServletRequest request,
+ SlingHttpServletResponse delegatee,
+ ProcessorManager processorManager) {
+ super(delegatee);
+ this.processorManager = processorManager;
+ this.request = request;
+ }
+
+ /**
+ * @see javax.servlet.ServletResponseWrapper#setContentType(java.lang.String)
+ */
+ public void setContentType(String type) {
+ this.contentType = type;
+ super.setContentType(type);
+ }
+
+ /**
+ * Wraps the underlying writer by a rewriter pipeline.
+ *
+ * @see javax.servlet.ServletResponseWrapper#getWriter()
+ */
+ public PrintWriter getWriter() throws IOException {
+ if ( this.processor != null && this.writer == null ) {
+ return new PrintWriter(new Writer() {
+
+ @Override
+ public void close() throws IOException {
+ // nothing to do
+ }
+
+ @Override
+ public void flush() throws IOException {
+ // nothing to do
+ }
+
+ @Override
+ public void write(char[] cbuf, int off, int len)
+ throws IOException {
+ // nothing to do
+ }
+ });
+ }
+ if (writer == null) {
+ this.processor = this.getProcessor();
+ if ( this.processor != null ) {
+ this.writer = this.processor.getWriter();
+ }
+ if ( this.writer == null ) {
+ this.writer = super.getWriter();
+ }
+ }
+ return writer;
+ }
+
+ /**
+ * @see javax.servlet.ServletResponseWrapper#flushBuffer()
+ */
+ public void flushBuffer() throws IOException {
+ if (writer != null) {
+ writer.flush();
+ } else {
+ super.flushBuffer();
+ }
+ }
+
+ /**
+ * Inform this response that the request processing is finished.
+ * @throws IOException
+ */
+ public void finished(final boolean errorOccured) throws IOException {
+ if ( this.processor != null ) {
+ this.processor.finished(errorOccured);
+ this.processor = null;
+ }
+ }
+
+ /**
+ * If we have a pipeline configuration for the current request,
+ * we can adapt this response to a content handler.
+ * @see org.apache.sling.api.adapter.Adaptable#adaptTo(java.lang.Class)
+ */
+ public <T> T adaptTo(Class<T> type) {
+ if ( type == ContentHandler.class ) {
+ this.processor = this.getProcessor();
+ if ( this.processor != null ) {
+ @SuppressWarnings("unchecked")
+ final T object = (T)this.processor.getContentHandler();
+ return object;
+ }
+ }
+ return super.adaptTo(type);
+ }
+
+ /**
+ * Search the first matching processor
+ */
+ private Processor getProcessor() {
+ final ProcessingContext processorContext = new ServletProcessingContext(this.request, this, this.getSlingResponse(), this.contentType);
+ Processor found = null;
+ final List<ProcessorConfiguration> processorConfigs = this.processorManager.getProcessorConfigurations();
+ final Iterator<ProcessorConfiguration> i = processorConfigs.iterator();
+ while ( found == null && i.hasNext() ) {
+ final ProcessorConfiguration config = i.next();
+ if ( config.match(processorContext) ) {
+ try {
+ found = this.processorManager.getProcessor(config, processorContext);
+ this.request.getRequestProgressTracker().log("Found processor for post processing {0}", config);
+ } catch (final SlingException se) {
+ // if an exception occurs during setup of the pipeline and we are currently
+ // already processing an error, we ignore this!
+ if ( processorContext.getRequest().getAttribute("javax.servlet.error.status_code") != null ) {
+ this.request.getRequestProgressTracker().log("Ignoring found processor for post processing {0}" +
+ " as an error occured ({1}) during setup while processing another error.", config, se);
+ } else {
+ throw se;
+ }
+ }
+ }
+ }
+ return found;
+ }
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/impl/ServletProcessingContext.java b/transformer/src/main/java/org/apache/sling/transformer/impl/ServletProcessingContext.java
new file mode 100644
index 0000000..30c933d
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/ServletProcessingContext.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.transformer.impl;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.transformer.ProcessingContext;
+
+/**
+ * An implementation of a processing context for a servlet.
+ */
+public class ServletProcessingContext implements ProcessingContext {
+
+ /** The current request. */
+ private final SlingHttpServletRequest request;
+
+ /** The current response. */
+ private final SlingHttpServletResponse response;
+
+ /** The original response. */
+ private final SlingHttpServletResponse originalResponse;
+
+ /** response content type */
+ private final String contentType;
+
+ /**
+ * Initializes a new instance.
+ */
+ public ServletProcessingContext(SlingHttpServletRequest request,
+ SlingHttpServletResponse response,
+ SlingHttpServletResponse originalResponse,
+ String contentType) {
+ this.request = request;
+ this.response = response;
+ this.originalResponse = originalResponse;
+ this.contentType = contentType;
+ }
+
+ /**
+ * @see org.apache.sling.transformer.ProcessingContext#getContentType()
+ */
+ public String getContentType() {
+ return this.contentType;
+ }
+
+ /**
+ * @see org.apache.sling.transformer.ProcessingContext#getRequest()
+ */
+ public SlingHttpServletRequest getRequest() {
+ return this.request;
+ }
+
+ /**
+ * @see org.apache.sling.transformer.ProcessingContext#getResponse()
+ */
+ public SlingHttpServletResponse getResponse() {
+ return this.response;
+ }
+
+ /**
+ * @see org.apache.sling.transformer.ProcessingContext#getWriter()
+ */
+ public PrintWriter getWriter() throws IOException {
+ return this.originalResponse.getWriter();
+ }
+
+ /**
+ * @see org.apache.sling.transformer.ProcessingContext#getOutputStream()
+ */
+ public OutputStream getOutputStream() throws IOException {
+ return this.originalResponse.getOutputStream();
+ }
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationFilter.java b/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationFilter.java
new file mode 100644
index 0000000..e8bd178
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationFilter.java
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.transformer.impl;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.transformer.ProcessorManager;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+
+/**
+ * This filter activates the rewriter for the output.
+ *
+ */
+@Component(
+ service = Filter.class,
+ property = {
+ Constants.SERVICE_VENDOR + "=The Apache Software Foundation",
+ "sling.filter.scope=request",
+ "sling.filter.scope=error",
+ Constants.SERVICE_RANKING + ":Integer=2500"
+ }
+)
+public class TransformationFilter implements Filter {
+
+ @Reference
+ private ProcessorManager pipelineManager;
+
+ /**
+ * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
+ */
+ @Override
+ public void init(FilterConfig config) throws ServletException {
+ // nothing to do
+ }
+
+ /**
+ * @see javax.servlet.Filter#destroy()
+ */
+ @Override
+ public void destroy() {
+ // nothing to do
+ }
+
+ /**
+ * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
+ */
+ @Override
+ public void doFilter(ServletRequest request,
+ ServletResponse response,
+ FilterChain chain)
+ throws IOException, ServletException {
+ if ( ! (request instanceof SlingHttpServletRequest)) {
+ throw new ServletException("Request is not a Apache Sling HTTP request.");
+ }
+ final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
+ final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
+
+ final RewriterResponse rewriterResponse =
+ new RewriterResponse(slingRequest, slingResponse, this.pipelineManager);
+ boolean errorOccured = true;
+ try {
+ chain.doFilter(request, rewriterResponse);
+ errorOccured = false;
+ } finally {
+ rewriterResponse.finished(errorOccured);
+ }
+ }
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationStepWrapper.java b/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationStepWrapper.java
new file mode 100644
index 0000000..d0e750a
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationStepWrapper.java
@@ -0,0 +1,25 @@
+package org.apache.sling.transformer.impl;
+
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+import org.apache.sling.commons.html.HtmlElement;
+import org.apache.sling.transformer.TransformationStep;
+
+public class TransformationStepWrapper implements Function<HtmlElement, Stream<HtmlElement>> {
+
+ private TransformationStep processStep;
+ private Process process;
+
+ public TransformationStepWrapper(TransformationStep processStep,Process process) {
+ this.processStep = processStep;
+ this.process = process;
+ }
+
+ @Override
+ public Stream<HtmlElement> apply(HtmlElement element) {
+ processStep.handle(element, process);
+ return process.getElements();
+ }
+
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/impl/WebConsoleConfigPrinter.java b/transformer/src/main/java/org/apache/sling/transformer/impl/WebConsoleConfigPrinter.java
new file mode 100644
index 0000000..69897f0
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/WebConsoleConfigPrinter.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.transformer.impl;
+
+import java.io.PrintWriter;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * This is a configuration printer for the web console which
+ * prints out the currently configured processors/pipelines.
+ *
+ */
+public class WebConsoleConfigPrinter {
+
+ final ProcessorManagerImpl manager;
+
+ private static ServiceRegistration REG;
+
+ public WebConsoleConfigPrinter(final ProcessorManagerImpl manager) {
+ this.manager = manager;
+ }
+
+ public static void register(final BundleContext bundleContext,
+ final ProcessorManagerImpl manager) {
+ final WebConsoleConfigPrinter printer = new WebConsoleConfigPrinter(manager);
+ final Dictionary<String, String> serviceProps = new Hashtable<String, String>();
+ serviceProps.put(Constants.SERVICE_DESCRIPTION,
+ "Apache Sling Rewriter Configuration Printer");
+ serviceProps.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+ serviceProps.put("felix.webconsole.label", "slingrewriter");
+ serviceProps.put("felix.webconsole.title", "Sling Rewriter");
+ serviceProps.put("felix.webconsole.configprinter.modes", "always");
+
+ REG = bundleContext.registerService(WebConsoleConfigPrinter.class.getName(),
+ printer,
+ serviceProps);
+ }
+
+ public static void unregister() {
+ if ( REG != null) {
+ REG.unregister();
+ REG = null;
+ }
+ }
+
+ /**
+ * Print out the rewriter configs.
+ * See org.apache.felix.webconsole.ConfigurationPrinter#printConfiguration(java.io.PrintWriter).
+ */
+ public void printConfiguration(PrintWriter pw) {
+ this.manager.printConfiguration(pw);
+ }
+}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/package-info.java b/transformer/src/main/java/org/apache/sling/transformer/package-info.java
new file mode 100644
index 0000000..7d911d9
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/transformer/package-info.java
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+@Version("1.0.0")
+package org.apache.sling.transformer;
+
+import org.osgi.annotation.versioning.Version;
+
diff --git a/transformer/src/test/java/org/apache/sling/rewriter/it/tests/EmojiIT.java b/transformer/src/test/java/org/apache/sling/rewriter/it/tests/EmojiIT.java
new file mode 100644
index 0000000..f24a299
--- /dev/null
+++ b/transformer/src/test/java/org/apache/sling/rewriter/it/tests/EmojiIT.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.rewriter.it.tests;
+
+import java.io.IOException;
+
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerClass.class)
+public class EmojiIT extends RewriterTestSupport {
+
+ private Document document;
+
+ @Before
+ public void setup() throws IOException {
+ final String url = String.format("http://localhost:%s/rewriter/emoji.html", httpPort());
+ document = Jsoup.connect(url).get();
+ }
+
+ @Test
+ public void testTitle() throws IOException {
+ final String title = document.title();
+ assertThat(title, is("emoji test"));
+ }
+
+ @Test
+ public void testContent() throws IOException {
+ final Element content = document.getElementById("content");
+ assertThat(content.text(), is("\u2602\uD83D\uDE03 \uD83C\uDF40 \uD83C\uDF40 \uD83C\uDF40 \uD83D\uDE01 \uD83C\uDF40\uD83C\uDF40"));
+ }
+
+}
diff --git a/transformer/src/test/java/org/apache/sling/rewriter/it/tests/RewriterTestSupport.java b/transformer/src/test/java/org/apache/sling/rewriter/it/tests/RewriterTestSupport.java
new file mode 100644
index 0000000..ba2c4d3
--- /dev/null
+++ b/transformer/src/test/java/org/apache/sling/rewriter/it/tests/RewriterTestSupport.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.rewriter.it.tests;
+
+import javax.inject.Inject;
+
+import org.apache.sling.api.servlets.ServletResolver;
+import org.apache.sling.resource.presence.ResourcePresence;
+import org.apache.sling.testing.paxexam.TestSupport;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.ProbeBuilder;
+import org.ops4j.pax.exam.TestProbeBuilder;
+import org.ops4j.pax.exam.util.Filter;
+import org.osgi.service.http.HttpService;
+
+import static org.apache.sling.testing.paxexam.SlingOptions.slingCommonsHtml;
+import static org.apache.sling.testing.paxexam.SlingOptions.slingQuickstartOakTar;
+import static org.apache.sling.testing.paxexam.SlingOptions.slingResourcePresence;
+import static org.apache.sling.testing.paxexam.SlingOptions.slingScriptingJavascript;
+import static org.ops4j.pax.exam.CoreOptions.composite;
+import static org.ops4j.pax.exam.CoreOptions.junitBundles;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.cm.ConfigurationAdminOptions.factoryConfiguration;
+
+public abstract class RewriterTestSupport extends TestSupport {
+
+ @Inject
+ protected ServletResolver servletResolver;
+
+ @Inject
+ protected HttpService httpService;
+
+ @Inject
+ @Filter(value = "(path=/content/rewriter)")
+ private ResourcePresence resourcePresence;
+
+ @Configuration
+ public Option[] configuration() {
+ return new Option[]{
+ baseConfiguration(),
+ quickstart(),
+ // Sling Rewriter
+ testBundle("bundle.filename"),
+ slingCommonsHtml(),
+ factoryConfiguration("org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended")
+ .put("user.mapping", new String[]{"org.apache.sling.rewriter=sling-readall"})
+ .asOption(),
+ factoryConfiguration("org.apache.sling.resource.presence.internal.ResourcePresenter")
+ .put("path", "/content/rewriter")
+ .asOption(),
+ // testing
+ mavenBundle().groupId("org.jsoup").artifactId("jsoup").versionAsInProject(),
+ mavenBundle().groupId("org.apache.servicemix.bundles").artifactId("org.apache.servicemix.bundles.hamcrest").versionAsInProject(),
+ slingResourcePresence(),
+ junitBundles()
+ };
+ }
+
+ @ProbeBuilder
+ public TestProbeBuilder probeConfiguration(final TestProbeBuilder testProbeBuilder) {
+ testProbeBuilder.setHeader("Sling-Initial-Content",
+ "apps/esp;path:=/apps/esp;overwrite:=true;uninstall:=true" + "," +
+ "content;path:=/content;overwrite:=true;uninstall:=true"
+ );
+ return testProbeBuilder;
+ }
+
+ protected Option quickstart() {
+ final int httpPort = findFreePort();
+ final String workingDirectory = workingDirectory();
+ return composite(
+ slingQuickstartOakTar(workingDirectory, httpPort),
+ slingScriptingJavascript()
+ );
+ }
+
+}
diff --git a/transformer/src/test/java/org/apache/sling/transformer/impl/ProcessorConfigurationImplTest.java b/transformer/src/test/java/org/apache/sling/transformer/impl/ProcessorConfigurationImplTest.java
new file mode 100644
index 0000000..ab22665
--- /dev/null
+++ b/transformer/src/test/java/org/apache/sling/transformer/impl/ProcessorConfigurationImplTest.java
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.sling.transformer.impl;
+
+import static org.apache.sling.transformer.impl.ProcessorConfigurationImpl.PROPERTY_CONTENT_TYPES;
+import static org.apache.sling.transformer.impl.ProcessorConfigurationImpl.PROPERTY_EXTENSIONS;
+import static org.apache.sling.transformer.impl.ProcessorConfigurationImpl.PROPERTY_PATHS;
+import static org.apache.sling.transformer.impl.ProcessorConfigurationImpl.PROPERTY_RESOURCE_TYPES;
+import static org.apache.sling.transformer.impl.ProcessorConfigurationImpl.PROPERTY_SELECTORS;
+import static org.apache.sling.transformer.impl.ProcessorConfigurationImpl.PROPERTY_UNWRAP_RESOURCES;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.when;
+
+import java.util.Map;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceWrapper;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.apache.sling.transformer.ProcessingContext;
+import org.apache.sling.transformer.impl.ProcessorConfigurationImpl;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import com.google.common.collect.ImmutableMap;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ProcessorConfigurationImplTest {
+
+ @Rule
+ public SlingContext context = new SlingContext();
+
+ @Mock
+ private ProcessingContext processingContext;
+
+ @Before
+ public void setup() {
+ when(processingContext.getContentType()).thenReturn("text/html");
+ when(processingContext.getRequest()).thenReturn(context.request());
+ when(processingContext.getResponse()).thenReturn(context.response());
+ }
+
+ private ProcessorConfigurationImpl buildConfig(Map<String,Object> configProps) {
+ Resource configResoruce = context.create().resource("/apps/myapp/rewriter/config", configProps);
+ return new ProcessorConfigurationImpl(configResoruce);
+ }
+
+ private void assertMatch(Map<String,Object> configProps) {
+ assertTrue(buildConfig(configProps).match(processingContext));
+ }
+
+ private void assertNoMatch(Map<String,Object> configProps) {
+ assertFalse(buildConfig(configProps).match(processingContext));
+ }
+
+ @Test
+ public void testMatchContentTypeMismatch() {
+ assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_CONTENT_TYPES, new String[] {"text/xml", "text/plain"}));
+ }
+
+ @Test
+ public void testMatchAtLeastOneContentType() {
+ assertMatch(ImmutableMap.<String,Object>of(PROPERTY_CONTENT_TYPES, new String[] {"text/html", "text/xml"}));
+ }
+
+ @Test
+ public void testMatchAnyContentType() {
+ assertMatch(ImmutableMap.<String,Object>of(PROPERTY_CONTENT_TYPES, new String[] {"text/xml", "*"}));
+ }
+
+ @Test
+ public void testMatchExtensionMismatch() {
+ context.requestPathInfo().setExtension("html");
+ assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_EXTENSIONS, new String[] {"xml","txt"}));
+ }
+
+ @Test
+ public void testMatchAtLeastOneExtension() {
+ context.requestPathInfo().setExtension("html");
+ assertMatch(ImmutableMap.<String,Object>of(PROPERTY_EXTENSIONS, new String[] {"xml","html"}));
+ }
+
+ @Test
+ public void testMatchResourceTypeMismatch() {
+ context.currentResource(context.create().resource("/content/test",
+ ImmutableMap.<String, Object>of("sling:resourceType", "type/1")));
+ assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_RESOURCE_TYPES, new String[] {"type/2","type/3"}));
+ }
+
+ @Test
+ public void testMatchAtLeastOneResourceType() {
+ context.currentResource(context.create().resource("/content/test",
+ ImmutableMap.<String, Object>of("sling:resourceType", "type/1")));
+ assertMatch(ImmutableMap.<String,Object>of(PROPERTY_RESOURCE_TYPES, new String[] {"type/1","type/2"}));
+ }
+
+ @Test
+ public void testMatchAtLeastOneResourceTypeWithResourceWrapper_UnwrapDisabled() {
+ Resource resource = context.create().resource("/content/test", ImmutableMap.<String, Object>of("sling:resourceType", "type/1"));
+
+ // overwrite resource type via ResourceWrapper
+ Resource resourceWrapper = new ResourceWrapper(resource) {
+ @Override
+ public String getResourceType() { return "/type/override/1"; }
+ };
+ context.currentResource(resourceWrapper);
+
+ assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_RESOURCE_TYPES, new String[] {"type/1","type/2"}));
+ }
+
+ @Test
+ public void testMatchAtLeastOneResourceTypeWithResourceWrapper_UnwrapEnabled() {
+ Resource resource = context.create().resource("/content/test", ImmutableMap.<String, Object>of("sling:resourceType", "type/1"));
+
+ // overwrite resource type via ResourceWrapper
+ Resource resourceWrapper = new ResourceWrapper(resource) {
+ @Override
+ public String getResourceType() { return "/type/override/1"; }
+ };
+ context.currentResource(resourceWrapper);
+
+ assertMatch(ImmutableMap.<String,Object>of(PROPERTY_RESOURCE_TYPES, new String[] {"type/1","type/2"},
+ PROPERTY_UNWRAP_RESOURCES, true));
+ }
+
+ @Test
+ public void testMatchPathMismatch() {
+ context.requestPathInfo().setResourcePath("/content/test");
+ assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_PATHS, new String[] {"/apps","/var"}));
+ }
+
+ @Test
+ public void testMatchAtLeastOnePath() {
+ context.requestPathInfo().setResourcePath("/content/test");
+ assertMatch(ImmutableMap.<String,Object>of(PROPERTY_PATHS, new String[] {"/apps","/content"}));
+ }
+
+ @Test
+ public void testMatchAnyPath() {
+ context.requestPathInfo().setResourcePath("/content/test");
+ assertMatch(ImmutableMap.<String,Object>of(PROPERTY_PATHS, new String[] {"/apps","*"}));
+ }
+
+ @Test
+ public void testMatchSelectorRequired() {
+ assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_SELECTORS, new String[] {"sel"}));
+ }
+
+ @Test
+ public void testMatchSelectorMismatch() {
+ context.requestPathInfo().setSelectorString("sel1.sel2");
+ assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_SELECTORS, new String[] {"sel3"}));
+ }
+
+ @Test
+ public void testMatchAtLeastOneSelector() {
+ context.requestPathInfo().setSelectorString("sel1.sel2");
+ assertMatch(ImmutableMap.<String,Object>of(PROPERTY_SELECTORS, new String[] {"sel1"}));
+ }
+
+ @Test
+ public void testNoMatchRequestAttribute() {
+ context.request().setAttribute(ProcessorConfigurationImpl.ATTR_PIPELINE, "config2");
+ context.requestPathInfo().setResourcePath("/content/test");
+ assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_PATHS, new String[] {"/apps","/content"}));
+ }
+
+ @Test
+ public void testNoMatchRequestAttributeIfConditionsDoNotMatch() {
+ context.request().setAttribute(ProcessorConfigurationImpl.ATTR_PIPELINE, "config");
+ context.requestPathInfo().setResourcePath("/content/test");
+ assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_PATHS, new String[] {"/apps"}));
+ }
+
+}
diff --git a/transformer/src/test/resources/apps/esp/page/emoji/html.esp b/transformer/src/test/resources/apps/esp/page/emoji/html.esp
new file mode 100644
index 0000000..38762c8
--- /dev/null
+++ b/transformer/src/test/resources/apps/esp/page/emoji/html.esp
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<html>
+<head>
+ <title><%= currentNode.title %></title>
+</head>
+<body>
+ <h1><%= currentNode.title %></h1>
+ <p id="content"><%= currentNode.content %></p>
+</body>
+</html>
diff --git a/transformer/src/test/resources/content/rewriter.json b/transformer/src/test/resources/content/rewriter.json
new file mode 100644
index 0000000..b1a076b
--- /dev/null
+++ b/transformer/src/test/resources/content/rewriter.json
@@ -0,0 +1,11 @@
+{
+ "jcr:primaryType": "nt:unstructured",
+ "sling:resourceType": "sling/servlet/default",
+ "title": "Apache Sling Rewriter",
+ "emoji": {
+ "jcr:primaryType": "nt:unstructured",
+ "sling:resourceType": "esp/page/emoji",
+ "title": "emoji test",
+ "content" : "\u2602\uD83D\uDE03 \uD83C\uDF40 \uD83C\uDF40 \uD83C\uDF40 \uD83D\uDE01 \uD83C\uDF40\uD83C\uDF40"
+ }
+}