package org.apache.catalina.connector;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import java.util.function.Supplier;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.catalina.Globals;
import org.apache.catalina.security.SecurityUtil;
import org.apache.tomcat.util.res.StringManager;
@SuppressWarnings("deprecation")
public class ResponseFacade implements HttpServletResponse {
private final class SetContentTypePrivilegedAction
implements PrivilegedAction<Void> {
private final String contentType;
public SetContentTypePrivilegedAction(String contentType){
this.contentType = contentType;
}
@Override
public Void run() {
response.setContentType(contentType);
return null;
}
}
private final class
implements PrivilegedAction<Void> {
private final String ;
private final long ;
private final boolean ;
(String name, long value, boolean add) {
this.name = name;
this.value = value;
this.add = add;
}
@Override
public Void () {
if(add) {
response.addDateHeader(name, value);
} else {
response.setDateHeader(name, value);
}
return null;
}
}
private static class FlushBufferPrivilegedAction implements PrivilegedExceptionAction<Void> {
private final Response response;
public FlushBufferPrivilegedAction(Response response) {
this.response = response;
}
@Override
public Void run() throws IOException {
response.setAppCommitted(true);
response.flushBuffer();
return null;
}
}
public ResponseFacade(Response response) {
this.response = response;
}
protected static final StringManager sm = StringManager.getManager(ResponseFacade.class);
protected Response response = null;
public void clear() {
response = null;
}
@Override
protected Object clone()
throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
public void finish() {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
response.setSuspended(true);
}
public boolean isFinished() {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
return response.isSuspended();
}
public long getContentWritten() {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
return response.getContentWritten();
}
@Override
public String getCharacterEncoding() {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
return response.getCharacterEncoding();
}
@Override
public ServletOutputStream getOutputStream()
throws IOException {
ServletOutputStream sos = response.getOutputStream();
if (isFinished()) {
response.setSuspended(true);
}
return sos;
}
@Override
public PrintWriter getWriter()
throws IOException {
PrintWriter writer = response.getWriter();
if (isFinished()) {
response.setSuspended(true);
}
return writer;
}
@Override
public void setContentLength(int len) {
if (isCommitted()) {
return;
}
response.setContentLength(len);
}
@Override
public void setContentLengthLong(long length) {
if (isCommitted()) {
return;
}
response.setContentLengthLong(length);
}
@Override
public void setContentType(String type) {
if (isCommitted()) {
return;
}
if (SecurityUtil.isPackageProtectionEnabled()){
AccessController.doPrivileged(new SetContentTypePrivilegedAction(type));
} else {
response.setContentType(type);
}
}
@Override
public void setBufferSize(int size) {
if (isCommitted()) {
throw new IllegalStateException
(sm.getString("coyoteResponse.setBufferSize.ise"));
}
response.setBufferSize(size);
}
@Override
public int getBufferSize() {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
return response.getBufferSize();
}
@Override
public void flushBuffer() throws IOException {
if (isFinished()) {
return;
}
if (SecurityUtil.isPackageProtectionEnabled()) {
try{
AccessController.doPrivileged(new FlushBufferPrivilegedAction(response));
} catch(PrivilegedActionException e) {
Exception ex = e.getException();
if (ex instanceof IOException) {
throw (IOException)ex;
}
}
} else {
response.setAppCommitted(true);
response.flushBuffer();
}
}
@Override
public void resetBuffer() {
if (isCommitted()) {
throw new IllegalStateException
(sm.getString("coyoteResponse.resetBuffer.ise"));
}
response.resetBuffer();
}
@Override
public boolean isCommitted() {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
return response.isAppCommitted();
}
@Override
public void reset() {
if (isCommitted()) {
throw new IllegalStateException
(sm.getString("coyoteResponse.reset.ise"));
}
response.reset();
}
@Override
public void setLocale(Locale loc) {
if (isCommitted()) {
return;
}
response.setLocale(loc);
}
@Override
public Locale getLocale() {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
return response.getLocale();
}
@Override
public void addCookie(Cookie cookie) {
if (isCommitted()) {
return;
}
response.addCookie(cookie);
}
@Override
public boolean (String name) {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
return response.containsHeader(name);
}
@Override
public String encodeURL(String url) {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
return response.encodeURL(url);
}
@Override
public String encodeRedirectURL(String url) {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
return response.encodeRedirectURL(url);
}
@Override
public String encodeUrl(String url) {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
return response.encodeURL(url);
}
@Override
public String encodeRedirectUrl(String url) {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
return response.encodeRedirectURL(url);
}
@Override
public void sendError(int sc, String msg)
throws IOException {
if (isCommitted()) {
throw new IllegalStateException
(sm.getString("coyoteResponse.sendError.ise"));
}
response.setAppCommitted(true);
response.sendError(sc, msg);
}
@Override
public void sendError(int sc)
throws IOException {
if (isCommitted()) {
throw new IllegalStateException
(sm.getString("coyoteResponse.sendError.ise"));
}
response.setAppCommitted(true);
response.sendError(sc);
}
@Override
public void sendRedirect(String location)
throws IOException {
if (isCommitted()) {
throw new IllegalStateException
(sm.getString("coyoteResponse.sendRedirect.ise"));
}
response.setAppCommitted(true);
response.sendRedirect(location);
}
@Override
public void (String name, long date) {
if (isCommitted()) {
return;
}
if(Globals.IS_SECURITY_ENABLED) {
AccessController.doPrivileged(new DateHeaderPrivilegedAction
(name, date, false));
} else {
response.setDateHeader(name, date);
}
}
@Override
public void (String name, long date) {
if (isCommitted()) {
return;
}
if(Globals.IS_SECURITY_ENABLED) {
AccessController.doPrivileged(new DateHeaderPrivilegedAction
(name, date, true));
} else {
response.addDateHeader(name, date);
}
}
@Override
public void (String name, String value) {
if (isCommitted()) {
return;
}
response.setHeader(name, value);
}
@Override
public void (String name, String value) {
if (isCommitted()) {
return;
}
response.addHeader(name, value);
}
@Override
public void (String name, int value) {
if (isCommitted()) {
return;
}
response.setIntHeader(name, value);
}
@Override
public void (String name, int value) {
if (isCommitted()) {
return;
}
response.addIntHeader(name, value);
}
@Override
public void setStatus(int sc) {
if (isCommitted()) {
return;
}
response.setStatus(sc);
}
@Override
public void setStatus(int sc, String sm) {
if (isCommitted()) {
return;
}
response.setStatus(sc, sm);
}
@Override
public String getContentType() {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
return response.getContentType();
}
@Override
public void setCharacterEncoding(String arg0) {
if (response == null) {
throw new IllegalStateException(
sm.getString("responseFacade.nullResponse"));
}
response.setCharacterEncoding(arg0);
}
@Override
public int getStatus() {
return response.getStatus();
}
@Override
public String (String name) {
return response.getHeader(name);
}
@Override
public Collection<String> () {
return response.getHeaderNames();
}
@Override
public Collection<String> (String name) {
return response.getHeaders(name);
}
@Override
public void setTrailerFields(Supplier<Map<String, String>> supplier) {
response.setTrailerFields(supplier);
}
@Override
public Supplier<Map<String, String>> getTrailerFields() {
return response.getTrailerFields();
}
}