001: /*
002: * DialogHandler.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 1998-2000 Sun Microsystems, Inc.
007: *
008: * Sun Public License Notice
009: *
010: * The contents of this file are subject to the Sun Public License Version
011: * 1.0 (the "License"). You may not use this file except in compliance with
012: * the License. A copy of the License is included as the file "license.terms",
013: * and also available at http://www.sun.com/
014: *
015: * The Original Code is from:
016: * Brazil project web application Framework release 1.1.
017: * The Initial Developer of the Original Code is: suhler.
018: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): cstevens, suhler.
022: *
023: * Version: 1.9
024: * Created by suhler on 98/12/04
025: * Last modified by suhler on 00/12/11 13:28:02
026: */
027:
028: package sunlabs.brazil.handler;
029:
030: import java.awt.Button;
031: import java.awt.Dimension;
032: import java.awt.Event;
033: import java.awt.Font;
034: import java.awt.Frame;
035: import java.awt.GridBagConstraints;
036: import java.awt.GridBagLayout;
037: import java.awt.Insets;
038: import java.awt.Label;
039: import java.awt.Panel;
040: import java.io.IOException;
041: import sunlabs.brazil.server.Handler;
042: import sunlabs.brazil.server.Request;
043: import sunlabs.brazil.server.Server;
044:
045: /**
046: * Sample handler for popping up a dialog box on the server.
047: * This is used for interactive authentication of web pages, allowing
048: * an <i>operator</i> on the server's computer to allow or deny access
049: * to pages on a per request basis.
050: * Input parameters examined in the request properties:
051: * <dl class=props>
052: * <dt>prefix<dd>The required URL prefix.
053: * <dt>default<dd>The message to appear in the dialog box.
054: * Defaults to <b>Request from Client</b>.
055: * <dt>denied<dd>The message to appear in the "permission denied" spot.
056: * </dl>
057: * If query data is present, it is used as the message.
058: * <p>
059: * Note: This is the only class in the entire system that requires AWT.
060: *
061: * @author Stephen Uhler
062: * @version 1.9, 00/12/11
063: */
064:
065: public class DialogHandler extends java.applet.Applet implements
066: Handler {
067:
068: /*
069: * The user interface components
070: */
071:
072: /**
073: * @serial
074: */
075: public Panel frame_1;
076:
077: /**
078: * @serial
079: */
080: public Button ok;
081:
082: /**
083: * @serial
084: */
085: public Label title;
086:
087: /**
088: * @serial
089: */
090: public Button cancel;
091:
092: /**
093: * @serial
094: */
095: public String prefix;
096:
097: /**
098: * @serial
099: */
100: Frame f; // The dialog box
101:
102: /**
103: * @serial
104: */
105: Dimension screen; // The screen dimentions - so we can do centering
106:
107: /**
108: * @serial
109: */
110: boolean abort; // True if the user hit "cancel"
111:
112: /**
113: * @serial
114: */
115: String message = "User Request for URL"; // Default message
116:
117: /**
118: * Do one time initialization.
119: */
120: public boolean init(Server server, String prefix) {
121: this .prefix = prefix;
122: f = new Frame("Message from Client");
123: screen = f.getToolkit().getScreenSize();
124: init();
125: f.add("Center", this );
126: f.pack();
127: return true;
128: }
129:
130: /**
131: * Pop up a dialog box on the server machine.
132: * Allow the operator to select <b>yes</b> or <b>no</b>.
133: * If the request is allowed, it is passed on to the next handler.
134: */
135:
136: public boolean respond(Request request) throws IOException {
137: String urlPrefix = request.props.getProperty(prefix + "prefix",
138: "/");
139: if (!request.url.startsWith(urlPrefix)) {
140: return false;
141: }
142: message = request.props.getProperty(prefix + "default",
143: "Request from Client");
144: String denied = request.props.getProperty(prefix + "denied",
145: "<title>denied!</title>Requested URL denied by server");
146: if (request.query.length() == 0) {
147: title.setText(message);
148: } else {
149: title.setText(request.query);
150: }
151: Dimension size = f.getSize();
152: try {
153: synchronized (f) {
154: f.setVisible(true);
155: f.setLocation((screen.width - size.width) / 2,
156: (screen.height - size.height) / 2);
157: while (f.isVisible()) {
158: f.wait();
159: }
160: }
161: } catch (InterruptedException e) {
162: request.log(Server.LOG_WARNING, "Got: " + e);
163: }
164: if (abort) {
165: request.sendResponse(denied, "text/html", 404);
166: return true;
167: } else {
168: return false;
169: }
170: }
171:
172: public void dismiss(boolean how) {
173: synchronized (f) {
174: abort = !how;
175: f.setVisible(false);
176: f.notifyAll();
177: }
178: }
179:
180: /**
181: * Machine generated code.
182: * Everything after here was automatically generated
183: * SpecTcl generated class Dialog, version 1.0
184: */
185:
186: public void init() {
187: // main panel
188: GridBagLayout grid = new GridBagLayout();
189: int rowHeights[] = { 0, 30, 20 };
190: int columnWidths[] = { 0, 30 };
191: double rowWeights[] = { 0.0, 0.0, 0.0 };
192: double columnWeights[] = { 0.0, 0.0 };
193: grid.rowHeights = rowHeights;
194: grid.columnWidths = columnWidths;
195: grid.rowWeights = rowWeights;
196: grid.columnWeights = columnWeights;
197:
198: // container frame_1 in this.
199: GridBagLayout frame_1_grid = new GridBagLayout();
200: int frame_1_rowHeights[] = { 0, 14 };
201: int frame_1_columnWidths[] = { 0, 30, 30 };
202: double frame_1_rowWeights[] = { 0.0, 0.0 };
203: double frame_1_columnWeights[] = { 0.0, 0.0, 0.0 };
204: frame_1_grid.rowHeights = frame_1_rowHeights;
205: frame_1_grid.columnWidths = frame_1_columnWidths;
206: frame_1_grid.rowWeights = frame_1_rowWeights;
207: frame_1_grid.columnWeights = frame_1_columnWeights;
208:
209: frame_1 = new Panel();
210: this .add(frame_1);
211:
212: ok = new Button();
213: ok.setLabel("OK");
214: frame_1.add(ok);
215:
216: title = new Label();
217: title
218: .setFont(new Font("Helvetica", Font.PLAIN + Font.BOLD,
219: 18));
220: title.setText(message);
221: title.setAlignment(Label.CENTER);
222: this .add(title);
223:
224: cancel = new Button();
225: cancel.setLabel("Cancel");
226: frame_1.add(cancel);
227:
228: // Geometry management
229: GridBagConstraints con = new GridBagConstraints();
230: reset(con);
231: con.gridx = 1;
232: con.gridy = 2;
233: con.anchor = GridBagConstraints.CENTER;
234: con.fill = GridBagConstraints.NONE;
235: grid.setConstraints(frame_1, con);
236:
237: reset(con);
238: con.gridx = 1;
239: con.gridy = 1;
240: con.anchor = GridBagConstraints.CENTER;
241: con.fill = GridBagConstraints.NONE;
242: frame_1_grid.setConstraints(ok, con);
243:
244: reset(con);
245: con.gridx = 1;
246: con.gridy = 1;
247: con.anchor = GridBagConstraints.CENTER;
248: con.fill = GridBagConstraints.NONE;
249: grid.setConstraints(title, con);
250:
251: reset(con);
252: con.gridx = 2;
253: con.gridy = 1;
254: con.anchor = GridBagConstraints.CENTER;
255: con.fill = GridBagConstraints.NONE;
256: frame_1_grid.setConstraints(cancel, con);
257:
258: // Resize behavior management and parent heirarchy
259: setLayout(grid);
260: frame_1.setLayout(frame_1_grid);
261: }
262:
263: public boolean handleEvent(Event event) {
264: if (event.target == ok && event.id == event.ACTION_EVENT) {
265: dismiss(true);
266: } else if (event.target == cancel
267: && event.id == event.ACTION_EVENT) {
268: dismiss(false);
269: } else {
270: return super .handleEvent(event);
271: }
272: return true;
273: }
274:
275: private void reset(GridBagConstraints con) {
276: con.gridx = GridBagConstraints.RELATIVE;
277: con.gridy = GridBagConstraints.RELATIVE;
278: con.gridwidth = 1;
279: con.gridheight = 1;
280:
281: con.weightx = 0;
282: con.weighty = 0;
283: con.anchor = GridBagConstraints.CENTER;
284: con.fill = GridBagConstraints.NONE;
285:
286: con.insets = new Insets(0, 0, 0, 0);
287: con.ipadx = 0;
288: con.ipady = 0;
289: }
290: }
|