package org.w3c.jigsaw.filters;
import org.w3c.tools.resources.*;
import org.w3c.jigsaw.http.*;
import org.w3c.jigsaw.resources.*;
import org.w3c.www.http.*;
public class ErrorFilter extends ResourceFilter {
protected static final String ERRED = "org.w3c.jigsaw.filters.ErrorFilter";
protected static int ATTR_BASEURL = -1;
protected static int ATTR_EXTENSION = -1;
static {
Attribute a = null;
Class cls = null;
try {
cls = Class.forName("org.w3c.jigsaw.filters.ErrorFilter");
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
a = new StringAttribute("base-url"
, "/errors"
, Attribute.EDITABLE);
ATTR_BASEURL = AttributeRegistry.registerAttribute(cls, a);
a = new StringAttribute("extension"
, "html"
, Attribute.EDITABLE);
ATTR_EXTENSION = AttributeRegistry.registerAttribute(cls, a);
}
@return
public String getBaseURL() {
return (String) getValue(ATTR_BASEURL, null);
}
@return
public String getExtension() {
return (String) getValue(ATTR_EXTENSION, null);
}
@return
public String getErrorResource(int status) {
String ext = getExtension() ;
if ( ext != null ) {
return getBaseURL()+"/"+Integer.toString(status)+"."+ext;
} else {
return getBaseURL()+"/"+Integer.toString(status);
}
}
@param request
public ReplyInterface ingoingFilter(RequestInterface request)
throws ProtocolException
{
return null;
}
@param request@param reply@return
public ReplyInterface outgoingFilter(RequestInterface req,
ReplyInterface rep)
throws ProtocolException
{
Request request = (Request) req;
Reply reply = (Reply) rep;
int status = reply.getStatus();
switch (status/100) {
case 1:
case 2:
case 3:
case 10:
return null;
}
if ( request.hasState(ERRED) )
return null;
Request ereq = (Request) request.getClone();
Reply erep = null;
try {
ereq.setState(ERRED, Boolean.TRUE);
ereq.setURLPath(getErrorResource(status));
ereq.setMethod("GET");
erep = (Reply) getServer().perform(ereq);
HeaderValue v = null;
erep.setStatus(reply.getStatus());
v = reply.getHeaderValue(reply.H_WWW_AUTHENTICATE);
erep.setHeaderValue(reply.H_WWW_AUTHENTICATE, v);
v = reply.getHeaderValue(reply.H_PROXY_AUTHENTICATE);
erep.setHeaderValue(reply.H_PROXY_AUTHENTICATE, v);
} catch (Exception ex) {
return reply;
}
return erep;
}
@param request@param ex@param filters@param idx
public ReplyInterface exceptionFilter(RequestInterface request,
ProtocolException ex,
FilterInterface filters[],
int idx)
{
Reply reply = (Reply) ex.getReply();
if ( reply != null ) {
try {
return outgoingFilter(request, reply, filters, idx);
} catch (ProtocolException exx) {
}
}
return null;
}
public void initialize(Object values[]) {
super.initialize(values);
}
}