package org.w3c.jigsaw.filters;
import java.io.*;
import org.w3c.tools.resources.*;
import org.w3c.www.http.*;
import org.w3c.jigsaw.http.*;
import org.w3c.jigsaw.resources.*;
public class PutFilter extends ResourceFilter {
protected static int ATTR_PUTLIST = -1;
static {
Class c = null;
Attribute a = null;
try {
c = Class.forName("org.w3c.jigsaw.filters.PutFilter");
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
a = new StringAttribute("put-list"
, null
, Attribute.EDITABLE|Attribute.MANDATORY);
ATTR_PUTLIST = AttributeRegistry.registerAttribute(c, a);
}
private ResourceReference list = null;
protected synchronized ResourceReference resolvePutListResource() {
ResourceReference rr_root = null;
rr_root = ((httpd) getServer()).getRootReference();
FramedResource root = null;
root = ((httpd) getServer()).getRoot();
String u = getPutListURL();
if ( u == null )
return null;
ResourceReference r_target = null;
try {
LookupState ls = new LookupState(u);
LookupResult lr = new LookupResult(rr_root);
root.lookup(ls, lr);
r_target = lr.getTarget();
} catch (Exception ex) {
r_target = null;
}
if (r_target != null) {
try {
Resource target = r_target.lock();
if (! (target instanceof PutListResource) )
r_target = null;
else
list = r_target;
} catch (InvalidResourceException ex) {
} finally {
r_target.unlock();
}
}
return r_target;
}
@return
public String getPutListURL() {
return getString(ATTR_PUTLIST, null);
}
@param idx@param value
public void setValue(int idx, Object value) {
super.setValue(idx, value);
if ( idx == ATTR_PUTLIST ) {
synchronized(this) {
list = null;
}
}
}
@param request
public ReplyInterface ingoingFilter(RequestInterface request) {
return null;
}
@param request@param reply@return
public ReplyInterface outgoingFilter(RequestInterface req,
ReplyInterface rep)
{
Request request = (Request) req;
Reply reply = (Reply) rep;
if (request.getMethod().equals("PUT")
&& ((reply.getStatus()/100) == 2)) {
ResourceReference rr = null;
PutListResource l = null;
boolean done = false;
synchronized (this) {
rr = resolvePutListResource();
if (rr != null) {
try {
l = (PutListResource) rr.lock();
if ( l != null ) {
l.registerRequest(request);
done = true;
}
} catch (InvalidResourceException ex) {
done = false;
} finally {
rr.unlock();
}
}
}
if ( done ) {
httpd s = (httpd) getServer();
s.errlog(getClass().getName()+
": unable to resolve companion PutListResource at "+
getPutListURL());
}
}
return null;
}
public void initialize(Object values[]) {
super.initialize(values);
}
}