// headers because when we have to rely on the 'sendError' method,
// the Servlet containers are expected to commit their response.
if (Status.isError(getStatusCode()) && (response.getEntity() == null)) {
try {
// Add the response headers
Parameter header;
for (final Iterator<Parameter> iter = getResponseHeaders()
.iterator(); iter.hasNext();) {
header = iter.next();
// We don't need to set the content length, especially
// because it could send the response too early on some
// containers (ex: Tomcat 5.0).
if (!header.getName().equals(
HttpConstants.HEADER_CONTENT_LENGTH)) {
getResponse().addHeader(header.getName(),
header.getValue());
}
}
getResponse().sendError(getStatusCode(), getReasonPhrase());
} catch (IOException ioe) {
getLogger().log(Level.WARNING,
"Unable to set the response error status", ioe);
}
} else {
// Send the response entity
getResponse().setStatus(getStatusCode());
// Add the response headers after setting the status because
// otherwise some containers (ex: Tomcat 5.0) immediately send
// the response if a "Content-Length: 0" header is found.
Parameter header;
Parameter contentLengthHeader = null;
for (final Iterator<Parameter> iter = getResponseHeaders()
.iterator(); iter.hasNext();) {
header = iter.next();
if (header.getName()
.equals(HttpConstants.HEADER_CONTENT_LENGTH)) {
contentLengthHeader = header;
} else {
getResponse()
.addHeader(header.getName(), header.getValue());
}
}
if (contentLengthHeader != null) {
getResponse().addHeader(contentLengthHeader.getName(),
contentLengthHeader.getValue());
}
super.sendResponse(response);
}
}