/* Reattore HTTP Server
Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: FallbackErrorInterceptor.java,v 1.5 2003/03/05 04:31:57 michaelh Exp $
*/
package juju.reattore.server.intercept.impl;
import java.text.MessageFormat;
import juju.reattore.io.impl.ByteSourceSink;
import juju.reattore.protocol.http.*;
import juju.reattore.server.intercept.Interceptor;
/** Last chance error handler that returns a simple HTML page.
@tag fallback
@group Interceptor
*/
public class FallbackErrorInterceptor
implements Interceptor {
private static final String DEFAULT =
"<html><head><title>Error {0}</title></head>"
+ "<body><h1>Error {0}</h1>"
+ "<p>Error {0} occured while fetching <tt>{1}</tt>"
+ "</body></html>";
private String msg;
/** Use the default message.
@see #FallbackErrorInterceptor
*/
public FallbackErrorInterceptor() {
this.msg = DEFAULT;
}
/** Set the message to return.
Create a new fallback handler with the given message/message
format. The parameters are:
<ul>
<li>{0} - The error code.</li>
<li>{1} - The path.</li>
</ul>
@param msg The new message.
*/
public void setMessage(String msg) {
this.msg = msg;
}
/** @see Interceptor */
public boolean process(HttpRequest req, HttpResponse resp) {
String out = MessageFormat.format(msg,
new Object[] {
new Integer(resp.getStatus()),
req.getPath()
});
resp.setBody(new ByteSourceSink(out.getBytes()));
resp.setHeader(HttpConstants.CONTENT_TYPE, HttpConstants.TEXT_HTML);
return true;
}
}