/*
* Copyright 2009-2010 Markus KARG
*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* For a copy of the GNU Lesser General Public License
* see .
*/
package eu.headcrashing.javax.servlet;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
* Implementation of {@link HttpServletResponseWrapper} allowing external
* inspection of the wrapped response.
*
* @deprecated Since Servlet API 3.0, this class is not needed anymore, as
* {@link HttpServletResponseWrapper} 3.0 is already readable
* itself.
* @author Markus KARG (markus@headcrashing.eu)
*/
@Deprecated
public final class ReadableHttpServletResponseWrapper extends HttpServletResponseWrapper {
private int status;
private Map> headers = new HashMap>();
public ReadableHttpServletResponseWrapper(final HttpServletResponse httpServletResponse) {
super(httpServletResponse);
}
@Override
public final void addDateHeader(final String name, final long date) {
super.addDateHeader(name, date);
final String value = new Date(date).toString();
if (this.containsHeader(name))
this.headers.get(name).add(value);
else
this.headers.put(name, new LinkedList(Collections.singleton(value)));
}
@Override
public final void addHeader(final String name, final String value) {
super.addHeader(name, value);
if (this.headers.containsKey(name))
this.headers.get(name).add(value);
else
this.headers.put(name, new LinkedList(Collections.singleton(value)));
}
@Override
public final void addIntHeader(String name, int integer) {
super.addIntHeader(name, integer);
final String value = Integer.toString(integer);
if (this.headers.containsKey(name))
this.headers.get(name).add(value);
else
this.headers.put(name, new LinkedList(Collections.singleton(value)));
}
@Override
public final void setDateHeader(final String name, final long date) {
super.setDateHeader(name, date);
this.headers.put(name, new LinkedList(Collections.singleton(new Date(date).toString())));
}
@Override
public final void setHeader(final String name, final String value) {
super.setHeader(name, value);
this.headers.put(name, new LinkedList(Collections.singleton(value)));
}
@Override
public final void setIntHeader(final String name, final int value) {
super.setIntHeader(name, value);
this.headers.put(name, new LinkedList(Collections.singleton(Integer.toString(value))));
}
@Override
public final void setStatus(final int statusCode, final String statusMessage) {
super.setStatus(statusCode, statusMessage);
this.status = statusCode;
}
@Override
public final void setStatus(final int statusCode) {
super.setStatus(statusCode);
this.status = statusCode;
}
@Override
public final void sendError(final int statusCode) throws IOException {
super.sendError(statusCode);
this.status = statusCode;
}
@Override
public final void sendError(final int statusCode, final String statusMessage) throws IOException {
super.sendError(statusCode, statusMessage);
this.status = statusCode;
}
/**
* @return the current status code of the wrapped response
*/
public final int getStatus() {
return this.status;
}
/**
* @return a (possibly empty) Collection of the names of the response
* headers
*/
public final Collection getHeaderNames() {
return this.headers.keySet();
}
/**
* @param name
* - the name of the response header whose value to return
* @return the value of the response header with the given name, or null if
* no header with the given name has been set on the wrapped
* response
*/
public final Collection getHeaders(final String name) {
return this.headers.get(name);
}
}