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/23 14:29:41 UTC
[sling-whiteboard] branch master updated: renaming classes for
consistency
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
The following commit(s) were added to refs/heads/master by this push:
new 34c0459 renaming classes for consistency
34c0459 is described below
commit 34c0459836f21e5f21c5229bbf4bd8dc642834f8
Author: JE Bailey <je...@apache.org>
AuthorDate: Fri Aug 23 09:30:35 2019 -0400
renaming classes for consistency
---
transformer/pom.xml | 2 +-
.../apache/sling/transformer/ProcessManager.java | 11 ---
.../ProcessingComponentConfiguration.java | 41 ----------
...singContext.java => TransformationContext.java} | 8 +-
.../sling/transformer/TransformationStep.java | 3 +-
...uration.java => TransformationStepManager.java} | 71 +++++++----------
.../org/apache/sling/transformer/Transformer.java | 50 ------------
.../sling/transformer/TransformerFactory.java | 51 ------------
.../sling/transformer/impl/LinkTransformer.java | 24 ++++--
.../sling/transformer/impl/ParserWriter.java | 42 ++++++++--
.../sling/transformer/impl/ProcessManager.java | 81 -------------------
.../transformer/impl/ServletProcessingContext.java | 91 ----------------------
...Process.java => TransformationContextImpl.java} | 18 ++++-
.../transformer/impl/TransformationFilter.java | 17 ++--
...mpl.java => TransformationStepManagerImpl.java} | 4 +-
.../impl/TransformationStepWrapper.java | 19 +++--
.../transformer/impl/TransformerResponse.java | 39 ++--------
17 files changed, 130 insertions(+), 442 deletions(-)
diff --git a/transformer/pom.xml b/transformer/pom.xml
index c3a0971..8dac08f 100644
--- a/transformer/pom.xml
+++ b/transformer/pom.xml
@@ -67,7 +67,7 @@
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.commons.html</artifactId>
<scope>provided</scope>
- <version>1.1.0</version>
+ <version>1.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
diff --git a/transformer/src/main/java/org/apache/sling/transformer/ProcessManager.java b/transformer/src/main/java/org/apache/sling/transformer/ProcessManager.java
deleted file mode 100644
index d55ce3d..0000000
--- a/transformer/src/main/java/org/apache/sling/transformer/ProcessManager.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package org.apache.sling.transformer;
-
-import java.util.List;
-
-import org.apache.sling.api.SlingHttpServletRequest;
-
-public interface ProcessManager {
-
- List<TransformationStep> getSteps(SlingHttpServletRequest request);
-
-}
\ No newline at end of file
diff --git a/transformer/src/main/java/org/apache/sling/transformer/ProcessingComponentConfiguration.java b/transformer/src/main/java/org/apache/sling/transformer/ProcessingComponentConfiguration.java
deleted file mode 100644
index 06a7e5c..0000000
--- a/transformer/src/main/java/org/apache/sling/transformer/ProcessingComponentConfiguration.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * 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/TransformationContext.java
similarity index 88%
rename from transformer/src/main/java/org/apache/sling/transformer/ProcessingContext.java
rename to transformer/src/main/java/org/apache/sling/transformer/TransformationContext.java
index c0ce18d..9aa3110 100644
--- a/transformer/src/main/java/org/apache/sling/transformer/ProcessingContext.java
+++ b/transformer/src/main/java/org/apache/sling/transformer/TransformationContext.java
@@ -19,14 +19,16 @@ package org.apache.sling.transformer;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
+import java.util.Map;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.commons.html.HtmlElement;
/**
* The context for a processor invocation.
*/
-public interface ProcessingContext {
+public interface TransformationContext {
/**
* The current request.
@@ -47,4 +49,8 @@ public interface ProcessingContext {
* The output stream.
*/
OutputStream getOutputStream() throws IOException;
+
+ Map<String, Object> getContext();
+
+ void next(HtmlElement... elements);
}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/TransformationStep.java b/transformer/src/main/java/org/apache/sling/transformer/TransformationStep.java
index 364abb7..3a6e2d3 100644
--- a/transformer/src/main/java/org/apache/sling/transformer/TransformationStep.java
+++ b/transformer/src/main/java/org/apache/sling/transformer/TransformationStep.java
@@ -17,10 +17,9 @@
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);
+ public void handle(HtmlElement element, TransformationContext process);
}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/ProcessorConfiguration.java b/transformer/src/main/java/org/apache/sling/transformer/TransformationStepManager.java
similarity index 58%
rename from transformer/src/main/java/org/apache/sling/transformer/ProcessorConfiguration.java
rename to transformer/src/main/java/org/apache/sling/transformer/TransformationStepManager.java
index fb133a6..9da9f46 100644
--- a/transformer/src/main/java/org/apache/sling/transformer/ProcessorConfiguration.java
+++ b/transformer/src/main/java/org/apache/sling/transformer/TransformationStepManager.java
@@ -1,44 +1,27 @@
-/*
- * 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);
-}
+/*
+ * 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;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+
+public interface TransformationStepManager {
+
+ List<TransformationStep> getSteps(SlingHttpServletRequest request);
+
+}
\ No newline at end of file
diff --git a/transformer/src/main/java/org/apache/sling/transformer/Transformer.java b/transformer/src/main/java/org/apache/sling/transformer/Transformer.java
deleted file mode 100644
index 6a83092..0000000
--- a/transformer/src/main/java/org/apache/sling/transformer/Transformer.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * 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
deleted file mode 100644
index 681dd9b..0000000
--- a/transformer/src/main/java/org/apache/sling/transformer/TransformerFactory.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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
index 415efb2..a54ca25 100644
--- a/transformer/src/main/java/org/apache/sling/transformer/impl/LinkTransformer.java
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/LinkTransformer.java
@@ -22,6 +22,9 @@ import java.util.Base64;
import java.util.Map;
import org.apache.sling.commons.html.HtmlElement;
+import org.apache.sling.commons.html.HtmlElementType;
+import org.apache.sling.commons.html.util.ElementFactory;
+import org.apache.sling.transformer.TransformationContext;
import org.apache.sling.transformer.TransformationStep;
import org.osgi.service.component.annotations.Component;
@@ -32,12 +35,12 @@ public class LinkTransformer implements TransformationStep {
IN, OUT
}
- public void handle(HtmlElement element, Process process) {
+ public void handle(HtmlElement element, TransformationContext process) {
- Map<String,Object> context = process.getContext();
+ Map<String, Object> context = process.getContext();
State current = (State) context.getOrDefault("currentState", State.OUT);
-
- MessageDigest d = (MessageDigest) context.computeIfAbsent("hash", (value)->{
+
+ MessageDigest d = (MessageDigest) context.computeIfAbsent("hash", (value) -> {
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("SHA-256");
@@ -46,7 +49,7 @@ public class LinkTransformer implements TransformationStep {
}
return digest;
});
-
+
switch (current) {
case IN:
switch (element.getType()) {
@@ -68,9 +71,14 @@ public class LinkTransformer implements TransformationStep {
context.put("currentState", State.IN);
}
break;
- case EOF:
- String headerValue = Base64.getEncoder().encodeToString(d.digest());
- process.getResponse().addHeader("X-Sucks", headerValue);
+ case END_TAG:
+ if (element.getValue().equalsIgnoreCase("body")) {
+ String headerValue = Base64.getEncoder().encodeToString(d.digest());
+ process.getResponse().addHeader("X-Sucks", headerValue);
+ HtmlElement br = ElementFactory.create(HtmlElementType.START_TAG, "br");
+ br.setAttribute("data-hash",headerValue );
+ process.next(br);
+ }
break;
default:
break;
diff --git a/transformer/src/main/java/org/apache/sling/transformer/impl/ParserWriter.java b/transformer/src/main/java/org/apache/sling/transformer/impl/ParserWriter.java
index f798d14..7f4cb7b 100644
--- a/transformer/src/main/java/org/apache/sling/transformer/impl/ParserWriter.java
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/ParserWriter.java
@@ -1,26 +1,56 @@
+/*
+ * 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.Writer;
+import java.util.stream.Collectors;
+
+import org.apache.sling.commons.html.Html;
+import org.apache.sling.commons.html.util.HtmlElements;
public class ParserWriter extends Writer {
+ private Writer originalWriter;
+
+ private TransformationStepWrapper wrapper;
+
+ public ParserWriter(TransformationContextImpl process) throws IOException {
+ this.originalWriter = process.getWriter();
+ this.wrapper = new TransformationStepWrapper(new LinkTransformer(), process);
+ }
+
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
- // TODO Auto-generated method stub
-
+ String foo = Html.stream(String.valueOf(cbuf, off, len))
+ .flatMap(wrapper)
+ .map(HtmlElements.TO_HTML)
+ .collect(Collectors.joining());
+ originalWriter.write(foo.toCharArray(), 0, foo.toCharArray().length);
}
@Override
public void flush() throws IOException {
- // TODO Auto-generated method stub
-
+ originalWriter.flush();
}
@Override
public void close() throws IOException {
- // TODO Auto-generated method stub
-
+ originalWriter.close();
}
}
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
deleted file mode 100644
index 44ffa5c..0000000
--- a/transformer/src/main/java/org/apache/sling/transformer/impl/ProcessManager.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * 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.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
-
-import org.apache.sling.api.SlingHttpServletRequest;
-import org.apache.sling.transformer.TransformationStep;
-import org.osgi.framework.Constants;
-import org.osgi.service.component.annotations.Component;
-import org.osgi.service.component.annotations.Reference;
-import org.osgi.service.component.annotations.ReferenceCardinality;
-import org.osgi.service.component.annotations.ReferencePolicy;
-import org.osgi.service.component.annotations.ReferencePolicyOption;
-
-@Component
-public class ProcessManager {
-
- // comparison in the same manner of a filter, where the highest ranking goes
- // first
- // in a situation where ranking is identical, then the lowest service id goes
- // first
- private Map<Map<String, Object>, TransformationStep> mapping = new TreeMap<>((map1, map2) -> {
- Integer value1 = (Integer) map1.getOrDefault(Constants.SERVICE_RANKING, (Integer) 0);
- Integer value2 = (Integer) map2.getOrDefault(Constants.SERVICE_RANKING, (Integer) 0);
- if (value1 - value2 == 0) {
- value1 = (Integer) map1.get(Constants.SERVICE_ID);
- value2 = (Integer) map2.get(Constants.SERVICE_ID);
- return value2.compareTo(value1);
- }
- return value1.compareTo(value2);
- });
-
- public List<TransformationStep> getSteps(SlingHttpServletRequest request) {
- List<TransformationStep> steps = new ArrayList<>();
- mapping.forEach((properties, step) -> {
- if (doStep(properties, request)) {
- steps.add(step);
- }
- });
- return steps;
- }
-
- private boolean doStep(Map<String, Object> properties, SlingHttpServletRequest request) {
- properties.keySet().forEach(key -> {
-
- });
- return true;
- }
-
- @Reference(service = TransformationStep.class, cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
- void bindTransformationStep(Map<String, Object> properties, TransformationStep step) {
- mapping.put(properties, step);
- }
-
- void updateTransformationStep(Map<String, Object> properties, TransformationStep step) {
- mapping.put(properties, step);
- }
-
- void unbindTransformationStep(Map<String, Object> properties) {
- mapping.remove(properties);
- }
-
-}
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
deleted file mode 100644
index 30c933d..0000000
--- a/transformer/src/main/java/org/apache/sling/transformer/impl/ServletProcessingContext.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * 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/Process.java b/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationContextImpl.java
similarity index 81%
rename from transformer/src/main/java/org/apache/sling/transformer/impl/Process.java
rename to transformer/src/main/java/org/apache/sling/transformer/impl/TransformationContextImpl.java
index 399950e..116aa43 100644
--- a/transformer/src/main/java/org/apache/sling/transformer/impl/Process.java
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationContextImpl.java
@@ -27,7 +27,7 @@ import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.commons.html.Html;
import org.apache.sling.commons.html.HtmlElement;
-import org.apache.sling.transformer.ProcessingContext;
+import org.apache.sling.transformer.TransformationContext;
import org.osgi.annotation.versioning.ProviderType;
/**
@@ -40,14 +40,15 @@ import org.osgi.annotation.versioning.ProviderType;
*
*/
@ProviderType
-public class Process implements ProcessingContext {
+public class TransformationContextImpl implements TransformationContext {
private List<HtmlElement> list = new ArrayList<>();
private Map<String, Object> context = new HashMap<>();
private SlingHttpServletRequest request;
private SlingHttpServletResponse response;
+ private boolean reset;
- private Process(SlingHttpServletRequest request, SlingHttpServletResponse response) {
+ public TransformationContextImpl(SlingHttpServletRequest request, SlingHttpServletResponse response) {
this.request = request;
this.response = response;
}
@@ -58,14 +59,23 @@ public class Process implements ProcessingContext {
* process.
*/
public void next(HtmlElement... elements) {
+ if (reset) {
+ list.clear();
+ reset = false;
+ }
Collections.addAll(list, elements);
}
public void next(String html) {
+ if (reset) {
+ list.clear();
+ reset = false;
+ }
Collections.addAll(list, Html.stream(html).toArray(HtmlElement[]::new));
}
public Stream<HtmlElement> getElements() {
+ reset = true;
return list.stream();
}
@@ -82,7 +92,7 @@ public class Process implements ProcessingContext {
}
@Override
- public PrintWriter getWriter() throws IOException {
+ public PrintWriter getWriter() throws IOException {
return response.getWriter();
}
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
index 905d57b..9d2ba07 100644
--- a/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationFilter.java
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationFilter.java
@@ -37,7 +37,7 @@ import org.osgi.service.component.annotations.Reference;
*
*/
@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" })
+ "sling.filter.scope=request", "sling.filter.scope=error", Constants.SERVICE_RANKING + ":Integer="+Integer.MIN_VALUE })
public class TransformationFilter implements Filter {
@Reference
@@ -69,17 +69,16 @@ public class TransformationFilter implements Filter {
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;
+
+ if (slingRequest.getRequestURI().endsWith(".html")){
+ TransformationContextImpl stepProcess = new TransformationContextImpl(slingRequest, slingResponse);
+ response = new TransformerResponse(stepProcess);
+ }
- final TransformerResponse rewriterResponse = new TransformerResponse(slingRequest, slingResponse);
+ chain.doFilter(request, response);
- 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/ProcessManagerImpl.java b/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationStepManagerImpl.java
similarity index 93%
rename from transformer/src/main/java/org/apache/sling/transformer/impl/ProcessManagerImpl.java
rename to transformer/src/main/java/org/apache/sling/transformer/impl/TransformationStepManagerImpl.java
index 8b3efea..ca5e1b8 100644
--- a/transformer/src/main/java/org/apache/sling/transformer/impl/ProcessManagerImpl.java
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationStepManagerImpl.java
@@ -22,7 +22,7 @@ import java.util.Map;
import java.util.TreeMap;
import org.apache.sling.api.SlingHttpServletRequest;
-import org.apache.sling.transformer.ProcessManager;
+import org.apache.sling.transformer.TransformationStepManager;
import org.apache.sling.transformer.TransformationStep;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
@@ -32,7 +32,7 @@ import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ReferencePolicyOption;
@Component
-public class ProcessManagerImpl implements ProcessManager {
+public class TransformationStepManagerImpl implements TransformationStepManager {
// comparison in the same manner of a filter, where the highest ranking goes
// first
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
index 3f7641f..3fe406d 100644
--- a/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationStepWrapper.java
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/TransformationStepWrapper.java
@@ -22,20 +22,25 @@ import java.util.stream.Stream;
import org.apache.sling.commons.html.HtmlElement;
import org.apache.sling.transformer.TransformationStep;
+/**
+ * Class that is used to convert a TransformationStep class into a Function object
+ * that will be used to Map an HtmlElement to a Stream<HtmlElement>
+ *
+ */
public class TransformationStepWrapper implements Function<HtmlElement, Stream<HtmlElement>> {
- private TransformationStep processStep;
- private Process process;
+ private TransformationStep tStep;
+ private TransformationContextImpl transformationContext;
- public TransformationStepWrapper(TransformationStep processStep,Process process) {
- this.processStep = processStep;
- this.process = process;
+ public TransformationStepWrapper(TransformationStep step,TransformationContextImpl context) {
+ this.tStep = step;
+ this.transformationContext = context;
}
@Override
public Stream<HtmlElement> apply(HtmlElement element) {
- processStep.handle(element, process);
- return process.getElements();
+ tStep.handle(element, transformationContext);
+ return transformationContext.getElements();
}
}
diff --git a/transformer/src/main/java/org/apache/sling/transformer/impl/TransformerResponse.java b/transformer/src/main/java/org/apache/sling/transformer/impl/TransformerResponse.java
index d3b5c63..e4bd312 100644
--- a/transformer/src/main/java/org/apache/sling/transformer/impl/TransformerResponse.java
+++ b/transformer/src/main/java/org/apache/sling/transformer/impl/TransformerResponse.java
@@ -18,42 +18,22 @@ package org.apache.sling.transformer.impl;
import java.io.IOException;
import java.io.PrintWriter;
-import java.io.Writer;
-import org.apache.sling.api.SlingHttpServletRequest;
-import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper;
class TransformerResponse
extends SlingHttpServletResponseWrapper {
- /** The current request. */
- private final SlingHttpServletRequest request;
-
-
/** wrapped rewriter/servlet writer */
private PrintWriter writer;
- /** response content type */
- private String contentType;
- /**
- * Initializes a new instance.
- * @param request The sling request.
- * @param delegatee The SlingHttpServletResponse wrapped by this instance.
- */
- public TransformerResponse(SlingHttpServletRequest request,
- SlingHttpServletResponse delegatee) {
- super(delegatee);
- this.request = request;
- }
+ private TransformationContextImpl process;
- /**
- * @see javax.servlet.ServletResponseWrapper#setContentType(java.lang.String)
- */
- public void setContentType(String type) {
- this.contentType = type;
- super.setContentType(type);
+
+ public TransformerResponse(TransformationContextImpl process) {
+ super(process.getResponse());
+ this.process = process;
}
/**
@@ -63,7 +43,7 @@ class TransformerResponse
*/
public PrintWriter getWriter() throws IOException {
if ( this.writer == null ) {
- this.writer = super.getWriter();
+ this.writer = new PrintWriter(new ParserWriter(process));
}
return writer;
}
@@ -79,12 +59,5 @@ class TransformerResponse
}
}
- /**
- * Inform this response that the request processing is finished.
- * @throws IOException
- */
- public void finished(final boolean errorOccured) throws IOException {
-
- }
}