001: // SeeOtherFrame.java
002: // $Id: SeeOtherFrame.java,v 1.6 2000/08/16 21:37:40 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.frames;
007:
008: import java.net.MalformedURLException;
009: import java.net.URL;
010:
011: import org.w3c.tools.resources.Attribute;
012: import org.w3c.tools.resources.AttributeRegistry;
013: import org.w3c.tools.resources.ProtocolException;
014: import org.w3c.tools.resources.Resource;
015: import org.w3c.tools.resources.StringAttribute;
016:
017: import org.w3c.www.http.HTTP;
018: import org.w3c.www.http.HttpReplyMessage;
019:
020: import org.w3c.jigsaw.http.Reply;
021: import org.w3c.jigsaw.http.Request;
022:
023: import org.w3c.jigsaw.forms.URLDecoder;
024: import org.w3c.jigsaw.forms.URLDecoderException;
025: import org.w3c.jigsaw.html.HtmlGenerator;
026:
027: import org.w3c.tools.resources.ProtocolException;
028: import org.w3c.tools.resources.ResourceException;
029:
030: /**
031: * generates a 303 See Other reply on a POST
032: * client should do the redirect with a GET
033: */
034:
035: public class SeeOtherFrame extends PostableFrame {
036:
037: protected static int ATTR_TARGET_URL = -1;
038:
039: static {
040: Attribute a = null;
041: Class cls = null;
042: try {
043: cls = Class.forName("org.w3c.jigsaw.frames.SeeOtherFrame");
044: } catch (Exception ex) {
045: ex.printStackTrace();
046: System.exit(1);
047: }
048: // The override attribute:
049: a = new StringAttribute("target-url", null, Attribute.EDITABLE);
050: ATTR_TARGET_URL = AttributeRegistry.registerAttribute(cls, a);
051: }
052:
053: /**
054: * Handle the form submission, after posted data parsing.
055: * <p>This method ought to be abstract, but for reasonable reason, it
056: * will just dump (parsed) the form content back to the client, so that it
057: * can be used for debugging.
058: * @param request The request proper.
059: * @param data The parsed data content.
060: * @exception ProtocolException If form data processing failed.
061: * @see org.w3c.jigsaw.forms.URLDecoder
062: */
063:
064: public Reply handle(Request request, URLDecoder data)
065: throws ProtocolException {
066: Reply reply = request.makeReply(HTTP.SEE_OTHER);
067: URL loc = null;
068: String target = (String) getValue(ATTR_TARGET_URL, null);
069: if (target == null) {
070: Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
071: HtmlGenerator g = new HtmlGenerator("Error");
072: g.append("The target RelocateResource doesn't define the"
073: + " relocation location. The server is "
074: + " misconfigured.");
075: error.setStream(g);
076: return error;
077: }
078: try {
079: loc = new URL(getURL(request), target);
080: } catch (MalformedURLException ex) {
081: // still not well configured :)
082: Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
083: HtmlGenerator g = new HtmlGenerator("Error");
084: g.append("The target RelocateResource doesn't define the"
085: + " relocation location. The server is "
086: + " misconfigured.");
087: error.setStream(g);
088: return error;
089: }
090: reply.setLocation(loc);
091: HtmlGenerator g = new HtmlGenerator("Moved");
092: g.append("<P>You should see the following resource, with a GET"
093: + ", click on the link if your"
094: + " browser doesn't support automatic redirection<BR>"
095: + "<A HREF=\"" + loc.toExternalForm() + "\">"
096: + loc.toExternalForm() + "</A>");
097: reply.setStream(g);
098: return reply;
099: }
100: }
|