01: /*
02: * AddHeaderTemplate.java
03: *
04: * Brazil project web application Framework,
05: * export version: 1.1
06: * Copyright (c) 1999-2000 Sun Microsystems, Inc.
07: *
08: * Sun Public License Notice
09: *
10: * The contents of this file are subject to the Sun Public License Version
11: * 1.0 (the "License"). You may not use this file except in compliance with
12: * the License. A copy of the License is included as the file "license.terms",
13: * and also available at http://www.sun.com/
14: *
15: * The Original Code is from:
16: * Brazil project web application Framework release 1.1.
17: * The Initial Developer of the Original Code is: suhler.
18: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
19: * All Rights Reserved.
20: *
21: * Contributor(s): cstevens, suhler.
22: *
23: * Version: 1.9
24: * Created by suhler on 99/03/09
25: * Last modified by suhler on 00/11/21 13:24:11
26: */
27:
28: package sunlabs.brazil.template;
29:
30: import java.util.Enumeration;
31: import sunlabs.brazil.util.Format;
32: import sunlabs.brazil.server.Server;
33:
34: /**
35: * Template class for adding arbitrary mime headers to a reply.
36: * Add a header onto the response, removing the tag from the HTML.
37: * <code><addheader name1=value1 name2=value2 ...></code>
38: * where <code>name</code> is the name of an HTTP header, and
39: * <code>value</code> is its value. If no value is provided, then
40: * the header is removed. If multiple name/value pairs are provided,
41: * they are processed in arbitrary order.
42: * <p>
43: * If a "location" header is added, the status code is automatically
44: * set to "302". If the value doesn't start with "http...", then the
45: * server's url is prepended. Removing location headers is ill-advised.
46: * <p>
47: * The values are subject to ${...} substitutions.
48: * <br>Note: Setting invalid headers will lead to unpredictable results,
49: *
50: * @author Stephen Uhler
51: * @version %V% 00/11/21
52: */
53:
54: public class AddHeaderTemplate extends Template {
55: /**
56: * Process the special <code>addheader</code> tag.
57: */
58:
59: public void tag_addheader(RewriteContext hr) {
60: Enumeration e = hr.keys();
61: while (e.hasMoreElements()) {
62: String key = (String) e.nextElement();
63: String value = hr.get(key);
64: hr.request.log(Server.LOG_DIAGNOSTIC, hr.prefix
65: + "addheader: " + key + ": " + value);
66: if (value.equals("")) {
67: hr.request.responseHeaders.remove(key);
68: } else {
69: value = Format.subst(hr.request.props, value);
70: if (key.equals("location")) {
71: hr.request.setStatus(302);
72: if (!value.startsWith("http")) {
73: value = hr.request.serverUrl() + value;
74: }
75: hr.request.log(Server.LOG_DIAGNOSTIC, hr.prefix
76: + "redirecting to: " + value);
77: }
78: hr.request.addHeader(key, value);
79: }
80: }
81: hr.killToken();
82: }
83: }
|