001: // LatestChildRedirectFrame.java
002: // $Id: LatestChildRedirectFrame.java,v 1.1 2000/11/13 16:43:45 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 2000.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.frames;
007:
008: import java.util.Enumeration;
009:
010: import org.w3c.jigsaw.http.Reply;
011: import org.w3c.jigsaw.http.Request;
012:
013: import org.w3c.jigsaw.html.HtmlGenerator;
014:
015: import org.w3c.tools.resources.ContainerResource;
016: import org.w3c.tools.resources.FramedResource;
017: import org.w3c.tools.resources.InvalidResourceException;
018: import org.w3c.tools.resources.ProtocolException;
019: import org.w3c.tools.resources.Resource;
020: import org.w3c.tools.resources.ResourceException;
021: import org.w3c.tools.resources.ResourceFrame;
022: import org.w3c.tools.resources.ResourceReference;
023:
024: import org.w3c.www.http.HTTP;
025:
026: /**
027: * do an automatic redirect to the most recently modified
028: * container child
029: */
030: public class LatestChildRedirectFrame extends HTTPFrame {
031:
032: /**
033: * The default GET method for other king of associated resource
034: * @param request The request to handle.
035: * @exception ProtocolException If processsing the request failed.
036: * @exception ResourceException If the resource got a fatal error.
037: */
038: protected Reply getOtherResource(Request request)
039: throws ProtocolException, ResourceException {
040: long lmbest = 0;
041: String best = null;
042:
043: ResourceReference rr = getResource().getParent();
044: if (rr != null) {
045: try {
046: FramedResource p = (FramedResource) rr.lock();
047: // if the father is a container (it should always be)
048: if (p instanceof ContainerResource) {
049: ContainerResource cr = (ContainerResource) p;
050: Enumeration res_enum;
051: res_enum = cr.enumerateResourceIdentifiers(false);
052: String childname;
053: ResourceReference childrr;
054: Class http_class = null;
055: ResourceReference framrr;
056: // get all the children, and find the container with
057: // the most recent last-modified.
058: while (res_enum.hasMoreElements()) {
059: childname = (String) res_enum.nextElement();
060: childrr = cr.lookup(childname);
061: try {
062: FramedResource cp = (FramedResource) childrr
063: .lock();
064: if (cp instanceof ContainerResource) {
065: long lm = cp.getLastModified();
066: if ((lmbest == 0) || (lm > lmbest)) {
067: lmbest = lm;
068: best = childname;
069: }
070: }
071: } catch (InvalidResourceException ex) {
072: // bad, but we won't choke on this
073: } finally {
074: childrr.unlock();
075: }
076: }
077: }
078: } catch (InvalidResourceException ex) {
079: throw new ResourceException("Invalid parent (latest):"
080: + ex.getMessage());
081: } finally {
082: rr.unlock();
083: }
084: if (lmbest != 0) {
085: // always temporary and no good support for 307 :(
086: Reply reply = request.makeReply(HTTP.FOUND);
087: reply.setLocation(best + "/");
088: HtmlGenerator g = new HtmlGenerator("Moved");
089: g.append("<P>The freshest child is available from "
090: + "<A HREF=\"" + best + "/\">" + best + "</A>");
091: reply.setStream(g);
092: return reply;
093: }
094: }
095: Reply reply = request.makeReply(HTTP.FOUND);
096: reply.setLocation("./");
097: HtmlGenerator g = new HtmlGenerator("Moved");
098: g.append("<P>No child available, see parent: "
099: + "<A HREF=\"./\">parent</A>");
100: reply.setStream(g);
101: return reply;
102: }
103: }
|