You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ol...@apache.org on 2015/01/12 18:32:31 UTC
svn commit: r1651147 - in /sling/trunk/bundles/scripting/core: ./
src/main/java/org/apache/sling/scripting/core/servlet/
Author: olli
Date: Mon Jan 12 17:32:30 2015
New Revision: 1651147
URL: http://svn.apache.org/r1651147
Log:
SLING-4298 make BufferedServletOutputStream and CaptureResponseWrapper available to other scripting implementations
* add new public package servlet
* add BufferedServletOutputStream and CaptureResponseWrapper from org.apache.sling.scripting.jsp.taglib.IncludeTagHandler
Added:
sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/servlet/
sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/servlet/BufferedServletOutputStream.java
sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/servlet/CaptureResponseWrapper.java
Modified:
sling/trunk/bundles/scripting/core/pom.xml
Modified: sling/trunk/bundles/scripting/core/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/pom.xml?rev=1651147&r1=1651146&r2=1651147&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/pom.xml (original)
+++ sling/trunk/bundles/scripting/core/pom.xml Mon Jan 12 17:32:30 2015
@@ -62,7 +62,8 @@
<configuration>
<instructions>
<Export-Package>
- org.apache.sling.scripting.core;version=2.0.8
+ org.apache.sling.scripting.core;version=2.0.8,
+ org.apache.sling.scripting.core.servlet;version=1.0.0
</Export-Package>
<Private-Package>
org.apache.sling.scripting.core.impl,
Added: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/servlet/BufferedServletOutputStream.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/servlet/BufferedServletOutputStream.java?rev=1651147&view=auto
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/servlet/BufferedServletOutputStream.java (added)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/servlet/BufferedServletOutputStream.java Mon Jan 12 17:32:30 2015
@@ -0,0 +1,68 @@
+/*
+ * 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.ByteArrayOutputStream;
+import java.io.IOException;
+
+import javax.servlet.ServletOutputStream;
+
+/**
+ * Extends the ServletOutputStream to capture the results into a byte array.
+ */
+public final class BufferedServletOutputStream extends ServletOutputStream {
+ private final ByteArrayOutputStream baops = new ByteArrayOutputStream();
+ private final String encoding;
+
+ /**
+ * Constructs a new BufferedServletOutputStream.
+ *
+ * @param encoding the encoding string
+ */
+ public BufferedServletOutputStream(String encoding) {
+ this.encoding = encoding;
+ }
+
+ /**
+ * Gets the byte buffer as a string.
+ *
+ * @return the byte buffer
+ * @throws java.io.IOException
+ */
+ public String getBuffer() throws IOException {
+ return baops.toString(encoding);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see java.io.OutputStream#close()
+ */
+ @Override
+ public void close() throws IOException {
+ baops.reset();
+ super.close();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see java.io.OutputStream#write(int)
+ */
+ @Override
+ public void write(int b) throws IOException {
+ baops.write(b);
+ }
+}
Added: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/servlet/CaptureResponseWrapper.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/servlet/CaptureResponseWrapper.java?rev=1651147&view=auto
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/servlet/CaptureResponseWrapper.java (added)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/servlet/CaptureResponseWrapper.java Mon Jan 12 17:32:30 2015
@@ -0,0 +1,103 @@
+/*
+ * 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.OutputStreamWriter;
+import java.io.PrintWriter;
+
+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 final String encoding;
+ private final ServletOutputStream ops;
+ private boolean isBinaryResponse = false;
+ private PrintWriter writer = null;
+
+ /**
+ * Construct a new CaptureResponseWrapper.
+ *
+ * @param response
+ * the response to wrap
+ * @param ops
+ * the output stream to write to
+ */
+ public CaptureResponseWrapper(HttpServletResponse response,
+ ServletOutputStream ops) {
+ super(response);
+ this.encoding = response.getCharacterEncoding();
+ this.ops = ops;
+ }
+
+ /**
+ * 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.");
+ }
+ writer = new PrintWriter(new OutputStreamWriter(ops, encoding));
+ return writer;
+ }
+
+}