package org.glassfish.grizzly.servlet;
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.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import java.util.function.Supplier;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.glassfish.grizzly.ThreadCache;
import org.glassfish.grizzly.http.server.Response;
@SuppressWarnings("deprecation")
public class HttpServletResponseImpl implements HttpServletResponse, Holders.ResponseHolder {
private final ServletOutputStreamImpl outputStream;
private ServletWriterImpl writer;
protected boolean usingOutputStream = false;
protected boolean usingWriter = false;
private final class SetContentTypePrivilegedAction
implements PrivilegedAction {
private final String contentType;
public SetContentTypePrivilegedAction(String contentType){
this.contentType = contentType;
}
@Override
public Object run() {
response.setContentType(contentType);
return null;
}
}
private static final ThreadCache.CachedTypeIndex<HttpServletResponseImpl> CACHE_IDX =
ThreadCache.obtainIndex(HttpServletResponseImpl.class, 2);
public static HttpServletResponseImpl create() {
final HttpServletResponseImpl response =
ThreadCache.takeFromCache(CACHE_IDX);
if (response != null) {
return response;
}
return new HttpServletResponseImpl();
}
protected HttpServletResponseImpl() {
outputStream = new ServletOutputStreamImpl(this);
}
protected Response response = null;
protected HttpServletRequestImpl servletRequest;
public void initialize(final Response response,
final HttpServletRequestImpl servletRequest) throws IOException {
this.response = response;
this.servletRequest = servletRequest;
outputStream.initialize();
}
@Override
protected Object clone()
throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
public void finish() throws IOException {
if (response == null) {
throw new IllegalStateException("Null response object");
}
response.finish();
}
@Override
public String getCharacterEncoding() {
if (response == null) {
throw new IllegalStateException("Null response object");
}
return response.getCharacterEncoding();
}
@Override
public ServletOutputStream getOutputStream()
throws IOException {
if (usingWriter)
throw new IllegalStateException("Illegal attempt to call getOutputStream() after getWriter() has already been called.");
usingOutputStream = true;
return outputStream;
}
void recycle() {
response = null;
servletRequest = null;
writer = null;
outputStream.recycle();
usingOutputStream = false;
usingWriter = false;
}
@Override
public PrintWriter getWriter()
throws IOException {
if (usingOutputStream)
throw new IllegalStateException("Illegal attempt to call getWriter() after getOutputStream has already been called.");
usingWriter = true;
if (writer == null) {
writer = new ServletWriterImpl(response.getWriter());
}
return writer;
}
@Override
public void setContentLength(int len) {
if (isCommitted())
return;
response.setContentLength(len);
}
@Override
public void setContentLengthLong(long len) {
if (isCommitted())
return;
response.setContentLengthLong(len);
}
@Override
@SuppressWarnings("unchecked")
public void setContentType(String type) {
if (isCommitted())
return;
if (System.getSecurityManager() != null){
AccessController.doPrivileged(new SetContentTypePrivilegedAction(type));
} else {
response.setContentType(type);
}
}
@Override
public void setBufferSize(int size) {
if (isCommitted())
throw new IllegalStateException("Illegal attempt to adjust the buffer size after the response has already been committed.");
response.setBufferSize(size);
}
@Override
public int getBufferSize() {
if (response == null) {
throw new IllegalStateException("Null response object");
}
return response.getBufferSize();
}
@Override
@SuppressWarnings("unchecked")
public void flushBuffer()
throws IOException {
if (System.getSecurityManager() != null){
try{
AccessController.doPrivileged(new PrivilegedExceptionAction(){
@Override
public Object run() throws IOException{
response.flush();
return null;
}
});
} catch(PrivilegedActionException e){
Exception ex = e.getException();
if (ex instanceof IOException){
throw (IOException)ex;
}
}
} else {
response.flush();
}
}
@Override
public void resetBuffer() {
if (isCommitted())
throw new IllegalStateException("Illegal attempt to reset the buffer after the response has already been committed.");
response.resetBuffer();
}
@Override
public boolean isCommitted() {
if (response == null) {
throw new IllegalStateException("Null response object");
}
return response.isCommitted();
}
@Override
public void reset() {
if (isCommitted())
throw new IllegalStateException("Illegal attempt to reset the response after it has already been committed.");
response.reset();
}
@Override
public void setLocale(Locale loc) {
if (isCommitted())
return;
response.setLocale(loc);
}
@Override
public Locale getLocale() {
if (response == null) {
throw new IllegalStateException("Null response object");
}
return response.getLocale();
}
@Override
public void addCookie(Cookie cookie) {
if (isCommitted())
return;
CookieWrapper wrapper = new CookieWrapper(cookie.getName(),cookie.getValue());
wrapper.setWrappedCookie(cookie);
response.addCookie(wrapper);
}
@Override
public boolean (String name) {
if (response == null) {
throw new IllegalStateException("Null response object");
}
return response.containsHeader(name);
}
@Override
public String encodeURL(String url) {
if (response == null) {
throw new IllegalStateException("Null response object");
}
return response.encodeURL(url);
}
@Override
public String encodeRedirectURL(String url) {
if (response == null) {
throw new IllegalStateException("Null response object");
}
return response.encodeRedirectURL(url);
}
@Override
public String encodeUrl(String url) {
return encodeURL(url);
}
@Override
public String encodeRedirectUrl(String url) {
return encodeRedirectURL(url);
}
@Override
public void sendError(int sc, String msg)
throws IOException {
if (isCommitted())
throw new IllegalStateException("Illegal attempt to call sendError() after the response has been committed.");
response.sendError(sc, msg);
}
@Override
public void sendError(int sc)
throws IOException {
if (isCommitted())
throw new IllegalStateException("Illegal attempt to call sendError() after the response has already been committed.");
response.sendError(sc);
}
@Override
public void sendRedirect(String location)
throws IOException {
if (isCommitted())
throw new IllegalStateException("Illegal attempt to redirect the response after it has been committed.");
response.sendRedirect(location);
}
@Override
public String (String string) {
return response.getHeader(string);
}
@Override
public Collection<String> () {
return new ArrayList<>(Arrays.asList(response.getHeaderNames()));
}
@Override
public Collection<String> (String string) {
return new ArrayList<>(Arrays.asList(response.getHeaderValues(string)));
}
@Override
public void (String name, long date) {
if (isCommitted())
return;
response.setDateHeader(name, date);
}
@Override
public void (String name, long date) {
if (isCommitted())
return;
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 int getStatus() {
return response.getStatus();
}
public String getMessage() {
return response.getMessage();
}
public boolean isError() {
return response.isError();
}
@Override
public String getContentType() {
return response.getContentType();
}
@Override
public void setCharacterEncoding(String charEnc) {
response.setCharacterEncoding(charEnc);
}
public Response getResponse() {
return response;
}
@Override
public Response getInternalResponse() {
return response;
}
@Override
public void setTrailerFields(final Supplier<Map<String, String>> supplier) {
response.setTrailers(supplier);
}
@Override
public Supplier<Map<String, String>> getTrailerFields() {
return response.getTrailers();
}
}