001: // PutFilter.java
002: // $Id: PutFilter.java,v 1.4 2000/08/16 21:37:32 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigedit.filters;
007:
008: import java.io.IOException;
009:
010: import org.w3c.tools.resources.Attribute;
011: import org.w3c.tools.resources.AttributeHolder;
012: import org.w3c.tools.resources.AttributeRegistry;
013: import org.w3c.tools.resources.FramedResource;
014: import org.w3c.tools.resources.InvalidResourceException;
015: import org.w3c.tools.resources.LookupResult;
016: import org.w3c.tools.resources.LookupState;
017: import org.w3c.tools.resources.ReplyInterface;
018: import org.w3c.tools.resources.RequestInterface;
019: import org.w3c.tools.resources.Resource;
020: import org.w3c.tools.resources.ResourceFilter;
021: import org.w3c.tools.resources.ResourceFrame;
022: import org.w3c.tools.resources.ResourceReference;
023: import org.w3c.tools.resources.StringAttribute;
024:
025: import org.w3c.www.http.HTTP;
026: import org.w3c.www.http.HttpReplyMessage;
027: import org.w3c.www.http.HttpRequestMessage;
028:
029: import org.w3c.jigsaw.http.Client;
030: import org.w3c.jigsaw.http.Reply;
031: import org.w3c.jigsaw.http.Request;
032: import org.w3c.jigsaw.http.httpd;
033:
034: import org.w3c.jigsaw.html.HtmlGenerator;
035:
036: public class PutFilter extends ResourceFilter {
037: /**
038: * Attribute index - The companion PutList resource's URL.
039: */
040: protected static int ATTR_PUTLIST = -1;
041:
042: static {
043: Class c = null;
044: Attribute a = null;
045:
046: try {
047: c = Class.forName("org.w3c.jigedit.filters.PutFilter");
048: } catch (Exception ex) {
049: ex.printStackTrace();
050: System.exit(1);
051: }
052: // Register the PutList URL attribute:
053: a = new StringAttribute("put-list", null, Attribute.EDITABLE
054: | Attribute.MANDATORY);
055: ATTR_PUTLIST = AttributeRegistry.registerAttribute(c, a);
056: }
057:
058: /**
059: * Resolve the companion PutList URL attribute into a resource:
060: */
061:
062: private ResourceReference list = null;
063:
064: protected synchronized ResourceReference resolvePutListResource() {
065: // Prepare for lookup:
066: ResourceReference rr_root = null;
067: rr_root = ((httpd) getServer()).getRootReference();
068: FramedResource root = null;
069: root = ((httpd) getServer()).getRoot();
070: String u = getPutListURL();
071: if (u == null)
072: return null;
073: // Do the lookup:
074: ResourceReference r_target = null;
075: try {
076: LookupState ls = new LookupState(u);
077: LookupResult lr = new LookupResult(rr_root);
078: root.lookup(ls, lr);
079: r_target = lr.getTarget();
080: } catch (Exception ex) {
081: r_target = null;
082: }
083: if (r_target != null) {
084: try {
085: Resource target = r_target.lock();
086: if (!(target instanceof PutListResource))
087: r_target = null;
088: else
089: list = r_target;
090: } catch (InvalidResourceException ex) {
091: // continue
092: } finally {
093: r_target.unlock();
094: }
095: }
096: return r_target;
097: }
098:
099: /**
100: * Get our companion PutListResource's URL.
101: * @return The URL encoded as a String, or <strong>null</strong> if
102: * undefined.
103: */
104:
105: public String getPutListURL() {
106: return getString(ATTR_PUTLIST, null);
107: }
108:
109: /**
110: * Catch PUTLIST assignments.
111: * @param idx The attribute being updated.
112: * @param value It's new value.
113: */
114:
115: public void setValue(int idx, Object value) {
116: super .setValue(idx, value);
117: if (idx == ATTR_PUTLIST) {
118: synchronized (this ) {
119: list = null;
120: }
121: }
122: }
123:
124: /**
125: * Nothing done in the ingoingFilter.
126: * We wait until the outgoigFilter.
127: * @param request The request that is about to be processsed.
128: */
129:
130: public ReplyInterface ingoingFilter(RequestInterface req) {
131: Request request = (Request) req;
132: String expect = request.getExpect();
133: if (expect != null) {
134: if (expect.startsWith("100")) { // expect 100?
135: Client client = request.getClient();
136: if (client != null) {
137: try {
138: client.sendContinue();
139: } catch (java.io.IOException ex) {
140: }
141: }
142: }
143: }
144: //register request, but this must be confirmed.
145: if (request.getMethod().equals("DELETE")
146: && (request.getTargetResource() != null)) {
147: boolean done = false;
148: synchronized (this ) {
149: ResourceReference rr = resolvePutListResource();
150: PutListResource l = null;
151: if (rr != null) {
152: try {
153: l = (PutListResource) rr.lock();
154: if (l != null) {
155: l.registerDeleteRequest(request);
156: done = true;
157: }
158: } catch (InvalidResourceException ex) {
159: done = false;
160: } finally {
161: rr.unlock();
162: }
163: }
164: }
165: if (!done) {
166: httpd s = (httpd) getServer();
167: s
168: .errlog(getClass().getName()
169: + ": unable to resolve companion PutListResource at "
170: + getPutListURL());
171: }
172: }
173: return null;
174: }
175:
176: protected HtmlGenerator getHtmlGenerator(String title) {
177: HtmlGenerator g = new HtmlGenerator(title);
178: g.addStyle("BODY {color: black; background: white; "
179: + "font-family: serif; margin-top: 35px }\n");
180: return g;
181: }
182:
183: /**
184: * Catch successfull PUTs, and keep track of them.
185: * @param request The original request.
186: * @param reply The original reply.
187: * @return Always <strong>null</strong>.
188: */
189:
190: public ReplyInterface outgoingFilter(RequestInterface req,
191: ReplyInterface rep) {
192: Request request = (Request) req;
193: Reply reply = (Reply) rep;
194: int status = PutListResource.FILE_UC;
195: boolean put = false;
196: // Is this a successfull PUT request ?
197: if (((put = request.getMethod().equals("PUT")) || request
198: .getMethod().equals("DELETE"))
199: && ((reply.getStatus() / 100) == 2)) {
200: // Cool, keep track of the modified file:
201: ResourceReference rr = null;
202: PutListResource l = null;
203: boolean done = false;
204: synchronized (this ) {
205: rr = resolvePutListResource();
206: if (rr != null) {
207: try {
208: l = (PutListResource) rr.lock();
209: if (l != null) {
210: if (put)
211: status = l.registerRequest(request);
212: else
213: status = l.confirmDelete(request);
214: done = true;
215: }
216: } catch (InvalidResourceException ex) {
217: done = false;
218: } finally {
219: rr.unlock();
220: }
221: }
222: }
223: // Make sure we did something:
224: if (!done) {
225: httpd s = (httpd) getServer();
226: s
227: .errlog(getClass().getName()
228: + ": unable to resolve companion PutListResource at "
229: + getPutListURL());
230: }
231:
232: switch (status) {
233: case PutListResource.FILE_UC:
234: case PutListResource.FILE_PB:
235: case PutListResource.FILE_DEL:
236: return null;
237: case PutListResource.FILE_MG:
238: Reply msg = request.makeReply(HTTP.CONFLICT);
239: HtmlGenerator g = getHtmlGenerator("Warning");
240: g
241: .append("<H1>Warning</H1> The file on publish space has "
242: + "been modified directly but attempting to merge "
243: + "has succeed.<p>"
244: + "You should update the file before editing "
245: + "it again.");
246: msg.setStream(g);
247: return msg;
248: case PutListResource.FILE_CF:
249: Reply error = request.makeReply(HTTP.CONFLICT);
250: HtmlGenerator gerr = getHtmlGenerator("Warning");
251: gerr
252: .append("<H1>Warning</H1> The file on publish space has "
253: + "been modified directly and attempting to merge"
254: + " has failed.<p>"
255: + "Ask your system administrator.");
256: error.setStream(gerr);
257: return error;
258: default:
259: return null;
260: }
261: }
262: return null;
263: }
264:
265: public void initialize(Object values[]) {
266: super.initialize(values);
267: }
268:
269: }
|