Skip to content

Commit

Permalink
DummyPageContext: Wrap existing servlet response writer for provided …
Browse files Browse the repository at this point in the history
…JspWriter (#10)
  • Loading branch information
stefanseifert authored Mar 20, 2024
1 parent dbb6ae9 commit 46e8663
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 171 deletions.
6 changes: 6 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/plugins/maven-changes-plugin/xsd/changes-1.0.0.xsd">
<body>

<release version="1.10.4" date="not released">
<action type="fix" dev="sseifert" issue="10">
DummyPageContext: Wrap existing servlet response writer for provided JspWriter.
</action>
</release>

<release version="1.10.2" date="2024-01-26">
<action type="update" dev="sseifert">
Explicitly define folder type for JSON files with "nt:unstructured" as root node to circumvent SLING-12197.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* no direct pass-in of SlingHttpServletRequest.
*/
@SuppressWarnings("deprecation")
public class DummyPageContext extends PageContext {
public final class DummyPageContext extends PageContext {

private final SlingHttpServletRequest slingRequest;
private final SlingHttpServletResponse slingResponse;
Expand All @@ -56,7 +56,12 @@ public class DummyPageContext extends PageContext {
public DummyPageContext(SlingHttpServletRequest slingRequest, SlingHttpServletResponse slingResponse) {
this.slingRequest = slingRequest;
this.slingResponse = slingResponse;
this.jspWriter = new DummyJspWriter();
try {
this.jspWriter = new JspWriterWrapper(slingResponse.getWriter());
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to get output writer.", ex);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2019 wcm.io
* %%
* 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.
* #L%
*/
package io.wcm.wcm.ui.granite.pathfield.impl.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

import javax.servlet.jsp.JspWriter;

import org.jetbrains.annotations.NotNull;

/**
* Provide JspWriter wrapper because {@link com.adobe.granite.ui.components.ComponentHelper} tries
* to access the JspWriter from the dummy page context.
*/
class JspWriterWrapper extends JspWriter {

private final PrintWriter writer;

JspWriterWrapper(@NotNull Writer writer) {
super(JspWriter.NO_BUFFER, true);
this.writer = new PrintWriter(writer);
}

@Override
public void print(boolean value) throws IOException {
writer.print(value);
}

@Override
public void print(char value) throws IOException {
writer.print(value);
}

@Override
public void print(int value) throws IOException {
writer.print(value);
}

@Override
public void print(long value) throws IOException {
writer.print(value);
}

@Override
public void print(float value) throws IOException {
writer.print(value);
}

@Override
public void print(double value) throws IOException {
writer.print(value);
}

@Override
public void print(char[] value) throws IOException {
writer.print(value);
}

@Override
public void print(String value) throws IOException {
writer.print(value);
}

@Override
public void print(Object value) throws IOException {
writer.print(value);
}

@Override
public void println() throws IOException {
writer.println();
}

@Override
public void println(boolean value) throws IOException {
writer.println(value);
}

@Override
public void println(char value) throws IOException {
writer.println(value);
}

@Override
public void println(int value) throws IOException {
writer.println(value);
}

@Override
public void println(long value) throws IOException {
writer.println(value);
}

@Override
public void println(float value) throws IOException {
writer.println(value);
}

@Override
public void println(double value) throws IOException {
writer.println(value);
}

@Override
public void println(char[] value) throws IOException {
writer.println(value);
}

@Override
public void println(String value) throws IOException {
writer.println(value);
}

@Override
public void println(Object value) throws IOException {
writer.println(value);
}

@Override
public void flush() throws IOException {
writer.flush();
throw new UnsupportedOperationException();
}

@Override
public void close() throws IOException {
writer.close();
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
writer.write(cbuf, off, len);
}

// --- unsupported methods ---

@Override
public void newLine() throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void clear() throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void clearBuffer() throws IOException {
throw new UnsupportedOperationException();
}

@Override
public int getRemaining() {
throw new UnsupportedOperationException();
}

}
Loading

0 comments on commit 46e8663

Please sign in to comment.