Author: olli
Date: Fri Feb 13 12:41:58 2015
New Revision: 1659543

URL: http://svn.apache.org/r1659543
Log:
embed CaptureResponseWrapper from Scripting Core and use released version

Added:
    
sling/trunk/contrib/scripting/thymeleaf/src/main/java/org/apache/sling/scripting/core/
    
sling/trunk/contrib/scripting/thymeleaf/src/main/java/org/apache/sling/scripting/core/servlet/
    
sling/trunk/contrib/scripting/thymeleaf/src/main/java/org/apache/sling/scripting/core/servlet/CaptureResponseWrapper.java
Modified:
    sling/trunk/contrib/scripting/thymeleaf/pom.xml

Modified: sling/trunk/contrib/scripting/thymeleaf/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/thymeleaf/pom.xml?rev=1659543&r1=1659542&r2=1659543&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/thymeleaf/pom.xml (original)
+++ sling/trunk/contrib/scripting/thymeleaf/pom.xml Fri Feb 13 12:41:58 2015
@@ -88,7 +88,7 @@
     <dependency>
       <groupId>org.apache.sling</groupId>
       <artifactId>org.apache.sling.scripting.core</artifactId>
-      <version>2.0.29-SNAPSHOT</version>
+      <version>2.0.28</version>
       <scope>provided</scope>
     </dependency>
     <dependency>

Added: 
sling/trunk/contrib/scripting/thymeleaf/src/main/java/org/apache/sling/scripting/core/servlet/CaptureResponseWrapper.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/thymeleaf/src/main/java/org/apache/sling/scripting/core/servlet/CaptureResponseWrapper.java?rev=1659543&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/thymeleaf/src/main/java/org/apache/sling/scripting/core/servlet/CaptureResponseWrapper.java
 (added)
+++ 
sling/trunk/contrib/scripting/thymeleaf/src/main/java/org/apache/sling/scripting/core/servlet/CaptureResponseWrapper.java
 Fri Feb 13 12:41:58 2015
@@ -0,0 +1,112 @@
+/*
+ * 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.scripting.core.servlet;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+
+/**
+ * Extends the HttpServletResponse to wrap the response and capture the 
results.
+ */
+public final class CaptureResponseWrapper extends HttpServletResponseWrapper {
+
+    private boolean isBinaryResponse = false;
+
+    private PrintWriter writer;
+
+    private StringWriter stringWriter;
+
+    /**
+     * Construct a new CaptureResponseWrapper.
+     *
+     * @param response the response to wrap
+     */
+    public CaptureResponseWrapper(HttpServletResponse response) {
+        super(response);
+    }
+
+    /**
+     * Returns true if the response is binary.
+     *
+     * @return
+     */
+    public boolean isBinaryResponse() {
+        return isBinaryResponse;
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see javax.servlet.ServletResponseWrapper#flushBuffer()
+     */
+    @Override
+    public void flushBuffer() throws IOException {
+        if (isBinaryResponse()) {
+            getResponse().getOutputStream().flush();
+        } else {
+            writer.flush();
+        }
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see javax.servlet.ServletResponseWrapper#getOutputStream()
+     */
+    @Override
+    public ServletOutputStream getOutputStream() throws IOException {
+        if (writer != null) {
+            throw new IOException("'getWriter()' has already been invoked for 
a character data response.");
+        }
+        isBinaryResponse = true;
+        return getResponse().getOutputStream();
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see javax.servlet.ServletResponseWrapper#getWriter()
+     */
+    @Override
+    public PrintWriter getWriter() throws IOException {
+        if (writer != null) {
+            return writer;
+        }
+        if (isBinaryResponse) {
+            throw new IOException("'getOutputStream()' has already been 
invoked for a binary data response.");
+        }
+        stringWriter = new StringWriter();
+        writer = new PrintWriter(stringWriter);
+        return writer;
+    }
+
+    /**
+     *
+     * @return the captured character response data
+     * @throws IOException if no character response data captured
+     */
+    public String getCapturedCharacterResponse() throws IOException {
+        if (stringWriter == null) {
+            throw new IOException("no character response data captured");
+        }
+        writer.flush();
+        return stringWriter.toString();
+    }
+
+}


Reply via email to