001: // LabelBureauResource.java
002: // $Id: LabelBureauResource.java,v 1.13 2000/08/16 21:37:43 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.jigsaw.pics;
007:
008: import java.io.File;
009: import java.io.PrintStream;
010:
011: import java.net.MalformedURLException;
012: import java.net.URL;
013:
014: import org.w3c.tools.resources.Attribute;
015: import org.w3c.tools.resources.AttributeHolder;
016: import org.w3c.tools.resources.AttributeRegistry;
017: import org.w3c.tools.resources.BooleanAttribute;
018: import org.w3c.tools.resources.FileAttribute;
019: import org.w3c.tools.resources.FramedResource;
020: import org.w3c.tools.resources.Resource;
021: import org.w3c.tools.resources.StringArrayAttribute;
022:
023: import org.w3c.www.http.HTTP;
024: import org.w3c.www.http.HttpEntityMessage;
025: import org.w3c.www.http.HttpMessage;
026:
027: import org.w3c.jigsaw.forms.URLDecoder;
028: import org.w3c.jigsaw.forms.URLDecoderException;
029:
030: import org.w3c.jigsaw.http.HTTPException;
031: import org.w3c.jigsaw.http.Reply;
032: import org.w3c.jigsaw.http.Request;
033:
034: /**
035: * An HTTP interface to query a Label bureau.
036: * This conforms to the
037: * <a href="http://www.w3.org/hypertext/WWW/PICS/labels.html">PICS
038: * protocol specification</a>.
039: * <p>This entity defines the following parameter:</p>
040: * <table border>
041: * <caption>The list of parameters</caption>
042: * <tr>
043: * <th align=left>Parameter name</th>
044: * <th align=left>Semantics</th>
045: * <th align=left>Default value</th>
046: * <th align=left>Type</th>
047: * </tr>
048: * <tr>
049: * <th align=left>bureau</th>
050: * <th align=left>Name of the LabelBureau database</th>
051: * <th align=left><em>none</em></th>
052: * <th align=left>java.lang.String</th>
053: * </tr>
054: * </table>
055: * <p>This entity also inherits the parameters from the PostableResource.</p>
056: */
057:
058: public class LabelBureauResource extends FramedResource {
059:
060: /**
061: * Attribute index - Our label bureau identifier.
062: */
063: protected static int ATTR_BUREAU_IDENTIFIER = -1;
064:
065: /**
066: * Attribute index - Our label bureau identifier.
067: */
068: protected static int ATTR_BUREAU_SERVICES = -1;
069:
070: /**
071: * Attribute index - debug flag.
072: */
073: protected static int ATTR_BUREAU_DEBUG = -1;
074:
075: static {
076: Attribute a = null;
077: Class cls = null;
078:
079: try {
080: cls = Class
081: .forName("org.w3c.jigsaw.pics.LabelBureauResource");
082: } catch (Exception ex) {
083: ex.printStackTrace();
084: System.exit(1);
085: }
086: // The bureau identifier (a file attribute):
087: a = new FileAttribute("bureau", null, Attribute.EDITABLE
088: | Attribute.MANDATORY);
089: ATTR_BUREAU_IDENTIFIER = AttributeRegistry.registerAttribute(
090: cls, a);
091: // The services ... to fix a IE bug with PICS :(
092: a = new StringArrayAttribute("services", null,
093: Attribute.EDITABLE);
094: ATTR_BUREAU_SERVICES = AttributeRegistry.registerAttribute(cls,
095: a);
096: // The debug flag
097: a = new BooleanAttribute("debug", Boolean.FALSE,
098: Attribute.EDITABLE);
099: ATTR_BUREAU_DEBUG = AttributeRegistry.registerAttribute(cls, a);
100: }
101:
102: /**
103: * Our loaded label bureau:
104: */
105: protected LabelBureauInterface bureau = null;
106:
107: /**
108: * Get our label bureau identifier.
109: */
110:
111: public File getBureauIdentifier() {
112: return (File) getValue(ATTR_BUREAU_IDENTIFIER, null);
113: }
114:
115: /**
116: * Get the service list
117: */
118: public String[] getServices() {
119: return (String[]) getValue(ATTR_BUREAU_SERVICES, null);
120: }
121:
122: /**
123: *
124: */
125: public boolean getDebugFlag() {
126: return getBoolean(ATTR_BUREAU_DEBUG, false);
127: }
128:
129: /**
130: * Check that we have loaded our bureau, or load it.
131: */
132:
133: protected final void acquireBureau() {
134: if (bureau != null)
135: return;
136: File repository = getBureauIdentifier();
137: if (repository != null)
138: bureau = LabelBureauFactory
139: .getLabelBureau(getBureauIdentifier());
140: return;
141: }
142:
143: /**
144: * Build a PICS reply out of the given content.
145: * @param request The request to reply to.
146: * @param sb The StringBuffer containing the content to send back.
147: */
148:
149: protected Reply makePICSReply(Request request, StringBuffer buffer) {
150: String content = buffer.toString();
151: Reply reply = request.makeReply(HTTP.OK);
152: reply.setHeaderValue(reply.H_CONTENT_TYPE,
153: PICS.APPLICATION_PICSLABEL);
154: reply.setContent(content);
155: if (getDebugFlag()) {
156: System.out.println("********* PICS REPLY *********");
157: System.out.println(content);
158: System.out.println("******************************");
159: }
160: return reply;
161: }
162:
163: protected Reply makePICSErrorReply(Request request, String msg) {
164: Reply reply = request.makeReply(HTTP.OK);
165: reply.setHeaderValue(reply.H_CONTENT_TYPE,
166: PICS.APPLICATION_PICSLABEL);
167: String content = "(" + PICS.PICS_PROTOCOL_ID + " error " + "("
168: + msg + "))";
169: reply.setContent(content);
170: if (getDebugFlag()) {
171: System.out.println("********* PICS REPLY *********");
172: System.out.println(content);
173: System.out.println("******************************");
174: }
175: return reply;
176: }
177:
178: /**
179: * Get the generic labels for a set of service and a set of urls.
180: * @param request the incomminmg request
181: * @param format the pics format
182: * @param urls the urls to label
183: * @param services the services to ask
184: * @param data the URLDecoder
185: * @return A Reply instance
186: * @exception HTTPException if processing the request failed.
187: */
188: protected Reply getGenericLabels(Request request, int format,
189: String urls[], String services[], URLDecoder data)
190: throws HTTPException {
191: StringBuffer sb = new StringBuffer(128);
192:
193: sb.append("(" + PICS.PICS_PROTOCOL_ID);
194: // Go through each service:
195: sloop: for (int is = 0; is < services.length; is++) {
196: LabelServiceInterface s = bureau
197: .getLabelService(services[is]);
198: if (s == null) {
199: sb.append(" error "
200: + "(no-ratings \"unknown service\")");
201: continue sloop;
202: }
203: s.dump(sb, format);
204: sb.append(" labels ");
205: try {
206: uloop: for (int iu = 0; iu < urls.length; iu++) {
207: LabelInterface l = s.getGenericLabel(new URL(
208: urls[iu]));
209: if (l == null) {
210: sb.append(" error " + "(not-labeled \""
211: + urls[iu] + "\")");
212: continue uloop;
213: }
214: if (iu != 0)
215: sb.append(" ");
216: l.dump(sb, format);
217: }
218: } catch (MalformedURLException e) {
219: Reply error = request.makeReply(HTTP.BAD_REQUEST);
220: error.setContent("You are requesting an invalid URL,");
221: throw new HTTPException(error);
222: }
223: }
224: sb.append(")");
225: return makePICSReply(request, sb);
226: }
227:
228: /**
229: * Get the normal (generic or specific) labels.
230: * @param request the incomminmg request
231: * @param format the pics format
232: * @param urls the urls to label
233: * @param services the services to ask
234: * @param data the URLDecoder
235: * @return A Reply instance
236: * @exception HTTPException if processing the request failed.
237: */
238: protected Reply getNormalLabels(Request request, int format,
239: String urls[], String services[], URLDecoder data)
240: throws HTTPException {
241: StringBuffer sb = new StringBuffer(128);
242:
243: sb.append("(" + PICS.PICS_PROTOCOL_ID);
244: // Go through each service:
245: sloop: for (int is = 0; is < services.length; is++) {
246: LabelServiceInterface s = bureau
247: .getLabelService(services[is]);
248: if (s == null) {
249: sb.append(" error "
250: + "(no-ratings \"unknown service\")");
251: continue sloop;
252: }
253: s.dump(sb, format);
254: sb.append(" labels ");
255: try {
256: uloop: for (int iu = 0; iu < urls.length; iu++) {
257: URL u = new URL(urls[iu]);
258: LabelInterface l = s.getSpecificLabel(u);
259: if ((l == null)
260: && ((l = s.getGenericLabel(u)) == null)) {
261: sb.append(" error " + "(not-labeled \""
262: + urls[iu] + "\")");
263: continue uloop;
264: }
265: if (iu != 0)
266: sb.append(" ");
267: l.dump(sb, format);
268: }
269: } catch (MalformedURLException e) {
270: Reply error = request.makeReply(HTTP.BAD_REQUEST);
271: error.setContent("You are requesting an invalid URL.");
272: throw new HTTPException(error);
273: }
274: }
275: sb.append(")");
276: return makePICSReply(request, sb);
277: }
278:
279: /**
280: * Get tree labels.
281: * @param request the incomminmg request
282: * @param format the pics format
283: * @param urls the urls to label
284: * @param services the services to ask
285: * @param data the URLDecoder
286: * @return A Reply instance
287: * @exception HTTPException if processing the request failed.
288: */
289: protected Reply getTreeLabels(Request request, int format,
290: String urls[], String services[], URLDecoder data)
291: throws HTTPException {
292: StringBuffer sb = new StringBuffer(128);
293: sb.append("(" + PICS.PICS_PROTOCOL_ID);
294: // Go through each service:
295: sloop: for (int is = 0; is < services.length; is++) {
296: LabelServiceInterface s = bureau
297: .getLabelService(services[is]);
298: if (s == null) {
299: sb.append(" error "
300: + "(no-ratings \"unknown service\")");
301: continue sloop;
302: }
303: s.dump(sb, format);
304: sb.append(" labels ");
305: try {
306: uloop: for (int iu = 0; iu < urls.length; iu++) {
307: boolean spaced = false;
308: LabelInterface l[] = s.getTreeLabels(new URL(
309: urls[iu]));
310: if (l == null) {
311: sb.append(" error " + "(not-labeled \""
312: + urls[iu] + "\")");
313: continue uloop;
314: }
315: sb.append((iu == 0) ? "(" : " (");
316: for (int il = 0; il < l.length; il++) {
317: if (il != 0)
318: sb.append(" ");
319: l[il].dump(sb, format);
320: }
321: sb.append(")");
322: }
323: } catch (MalformedURLException e) {
324: Reply error = request.makeReply(HTTP.BAD_REQUEST);
325: error.setContent("You are requesting an invalid URL.");
326: throw new HTTPException(error);
327: }
328: }
329: sb.append(")");
330: return makePICSReply(request, sb);
331: }
332:
333: /**
334: * Get generic tree labels.
335: * @param request the incomminmg request
336: * @param format the pics format
337: * @param urls the urls to label
338: * @param services the services to ask
339: * @param data the URLDecoder
340: * @return A Reply instance
341: * @exception HTTPException if processing the request failed.
342: */
343:
344: protected Reply getGenericTreeLabels(Request request, int format,
345: String urls[], String services[], URLDecoder data)
346: throws HTTPException {
347: StringBuffer sb = new StringBuffer(128);
348: sb.append("(" + PICS.PICS_PROTOCOL_ID);
349: // Go through each service:
350: sloop: for (int is = 0; is < services.length; is++) {
351: LabelServiceInterface s = bureau
352: .getLabelService(services[is]);
353: if (s == null) {
354: sb.append(" error "
355: + "(no-ratings \"unknown service\")");
356: continue sloop;
357: }
358: s.dump(sb, format);
359: sb.append(" labels ");
360: try {
361: uloop: for (int iu = 0; iu < urls.length; iu++) {
362: URL u = new URL(urls[iu]);
363: LabelInterface l[] = s.getGenericTreeLabels(u);
364: if (l == null) {
365: sb.append(" error " + "(not-labeled \""
366: + urls[iu] + "\")");
367: continue uloop;
368: }
369: sb.append((iu == 0) ? "(" : " (");
370: for (int il = 0; il < l.length; il++) {
371: if (il != 0)
372: sb.append(" ");
373: l[il].dump(sb, format);
374: }
375: sb.append(")");
376: }
377: } catch (MalformedURLException e) {
378: Reply error = request.makeReply(HTTP.BAD_REQUEST);
379: error.setContent("You are requesting an invalid URL.");
380: throw new HTTPException(error);
381: }
382:
383: }
384: sb.append(")");
385: return makePICSReply(request, sb);
386: }
387:
388: public void setValue(int idx, Object value) {
389: super .setValue(idx, value);
390: if (idx == ATTR_BUREAU_DEBUG) {
391: PICS.setDebug(getDebugFlag());
392: }
393: }
394:
395: public void initialize(Object values[]) {
396: super .initialize(values);
397: acquireBureau();
398: try {
399: registerFrameIfNone("org.w3c.jigsaw.pics.LabelBureauFrame",
400: "label-bureau-frame");
401: } catch (Exception ex) {
402: ex.printStackTrace();
403: }
404: PICS.setDebug(getDebugFlag());
405: }
406:
407: }
|