001: // LabelBureauFrame.java
002: // $Id: LabelBureauFrame.java,v 1.6 2000/08/16 21:37:43 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.pics;
007:
008: import org.w3c.tools.resources.FramedResource;
009: import org.w3c.tools.resources.ProtocolException;
010:
011: import org.w3c.www.http.HTTP;
012:
013: import org.w3c.jigsaw.http.HTTPException;
014: import org.w3c.jigsaw.http.Reply;
015: import org.w3c.jigsaw.http.Request;
016:
017: import org.w3c.jigsaw.forms.URLDecoder;
018: import org.w3c.jigsaw.forms.URLDecoderException;
019:
020: import org.w3c.jigsaw.frames.HTTPFrame;
021: import org.w3c.jigsaw.frames.PostableFrame;
022:
023: /**
024: * @version $Revision: 1.6 $
025: * @author Benoît Mahé (bmahe@w3.org)
026: */
027: public class LabelBureauFrame extends PostableFrame {
028:
029: protected LabelBureauResource labelb = null;
030:
031: /**
032: * Register our resource. Must be an instance of ServletWrapper.
033: */
034: public void registerResource(FramedResource resource) {
035: super .registerOtherResource(resource);
036: if (resource instanceof LabelBureauResource)
037: labelb = (LabelBureauResource) resource;
038: }
039:
040: /**
041: * Parse the URLs given in the URLDecoder, as the <strong>u</strong> field.
042: * This method just unquote any quoted URLs, before sending them back.
043: * @param data The URLDecoder to get data from.
044: * @return An array of String, properly parsed.
045: */
046:
047: protected String[] parseURLs(String us[]) {
048: //FIXME .... no FIX IE!
049: if (us[0] == null)
050: return null;
051: String urls[] = new String[us.length];
052: for (int i = 0; i < us.length; i++) {
053: if (us[i].charAt(0) == '"') {
054: int ulen = us[i].length();
055: // unquote only if last char is a quote, otherwise, leave as is
056: // which will trigger an exception when trying to build the URL
057: // object out of it.
058: if (us[i].charAt(ulen - 1) == '"')
059: urls[i] = us[i].substring(1, ulen - 1);
060: } else {
061: urls[i] = us[i];
062: }
063: }
064: return urls;
065: }
066:
067: /**
068: * Get the integer code for the String based PICS format.
069: * @param request The request to be handled.
070: * @param format The string representation of the format.
071: * @return An LabelBureauInterface defined format code.
072: * @exception HTTPException if processing the request failed.
073: * @see org.w3c.jigsaw.pics.LabelBureauInterface
074: */
075:
076: protected int parseFormat(Request request, String format)
077: throws HTTPException {
078: if (format == null) {
079: return LabelBureauInterface.FMT_FULL;
080: } else if (format.equals("minimal")) {
081: return LabelBureauInterface.FMT_MINIMAL;
082: } else if (format.equals("short")) {
083: return LabelBureauInterface.FMT_SHORT;
084: } else if (format.equals("full")) {
085: return LabelBureauInterface.FMT_FULL;
086: } else if (format.equals("signed")) {
087: return LabelBureauInterface.FMT_SIGNED;
088: }
089: Reply error = request.makeReply(HTTP.BAD_REQUEST);
090: error.setContent("Unknown label format: " + format);
091: throw new HTTPException(error);
092: }
093:
094: /**
095: * Handle the request.
096: * @param request The request to be handled.
097: * @param data The URLDecoder
098: * @return A Reply instance.
099: * @exception ProtocolException if processing the request failed.
100: * @see org.w3c.jigsaw.pics.LabelBureauInterface
101: */
102: public Reply handle(Request request, URLDecoder data)
103: throws ProtocolException {
104: if (labelb == null) {
105: Reply reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
106: reply
107: .setContent("LabelBureauFrame not configured properly: "
108: + "must be attached to a LabelBureauResource.");
109: throw new HTTPException(reply);
110: }
111: // Get opt (only generic/normal/insert)
112: String opt = data.getValue("opt");
113: String urls[] = parseURLs(data.getMultipleValues("u"));
114: int iformat = parseFormat(request, data.getValue("format"));
115: String services[] = parseURLs(data.getMultipleValues("s"));
116: if (services == null)
117: services = labelb.getServices();
118: if ((services == null) || (services.length == 0))
119: return labelb.makePICSErrorReply(request,
120: "no-ratings \"unknown service\"");
121: // Perform request
122: if (labelb.getDebugFlag()) {
123: System.out.println("******** PICS REQUEST ********");
124: System.out.println("opt : " + opt);
125: System.out.println("format : " + data.getValue("format"));
126: System.out.print("services : ");
127: for (int i = 0; i < services.length; i++)
128: System.out.println(services[i]);
129: System.out.print("urls : ");
130: for (int i = 0; i < urls.length; i++)
131: System.out.println(urls[i]);
132: System.out.println("******************************");
133: }
134: if (opt.equals("generic")) {
135: return labelb.getGenericLabels(request, iformat, urls,
136: services, data);
137: } else if (opt.equals("normal")) {
138: return labelb.getNormalLabels(request, iformat, urls,
139: services, data);
140: } else if (opt.equals("tree")) {
141: return labelb.getTreeLabels(request, iformat, urls,
142: services, data);
143: } else if (opt.equals("generic+tree")) {
144: return labelb.getGenericTreeLabels(request, iformat, urls,
145: services, data);
146: } else {
147: Reply error = request.makeReply(HTTP.BAD_REQUEST);
148: error.setContent("Invalid label bureau query, bad option: "
149: + opt);
150: throw new HTTPException(error);
151: }
152: }
153: }
|