KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > psibt > framework > net > UserDialogRequestHandler


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /**
17  * Title: PSI Java Framework: UserDialogRequestHandler<p>
18  * Copyright: PSI-BT AG<p>
19  * History:
20  * Date Author What's new
21  * 16.04.2001 VMentzner Created
22  */

23
24 package com.psibt.framework.net;
25 /**
26  * This class implements a RequestHandler for the path "/userdialog/" in the PluggableHTTPServer.
27  * A simple input form is presented in the browser where you can enter a message. This message will be sent
28  * to the PluggableHTTPServer and shown in a JOptionPane MessageDialog.
29  *
30  * @author <a HREF="mailto:V.Mentzner@psi-bt.de">Volker Mentzner</a>
31  */

32 public class UserDialogRequestHandler extends RootRequestHandler {
33
34   private Component parentComponent;
35
36  /**
37    * Creates a new UserDialogRequestHandler object
38    */

39   public UserDialogRequestHandler() {
40     this(null);
41   }
42
43  /**
44    * Creates a new UserDialogRequestHandler object with a parentComponent reference
45    */

46   public UserDialogRequestHandler(Component parentComponent) {
47     this.setTitle("user dialog");
48     this.setDescription("show user dialog");
49     this.setHandledPath("/userdialog/");
50     this.parentComponent = parentComponent;
51   }
52
53  /**
54    * Handles the given request and writes the reply to the given out-stream.
55    *
56    * @param request - client browser request
57    * @param out - Out stream for sending data to client browser
58    * @return if the request was handled by this handler : true, else : false
59    */

60   public boolean handleRequest(String JavaDoc request, Writer out) {
61     String JavaDoc path = "";
62     String JavaDoc query = null;
63     try {
64       URL url = new URL("http://localhost"+request);
65       path = url.getPath();
66       query = url.getQuery();
67       if (path.startsWith(this.getHandledPath()) == false) {
68         return false;
69       }
70
71       out.write("HTTP/1.0 200 OK\r\n");
72       out.write("Content-type: text/html\r\n\r\n");
73       out.write("<HTML><HEAD><TITLE>" + this.getTitle() + "</TITLE></HEAD>\r\n");
74       out.write("<BODY><H1>" + this.getDescription() + "</H1>\r\n");
75       if ((query != null) && (query.length() >= 0)) {
76         int idx = query.indexOf("=");
77         String JavaDoc message = query.substring(idx+1, query.length());
78         // replace '+' by space
79
message = message.replace('+', ' ');
80         // replace hex strings starting with '%' by their values
81
idx = message.indexOf("%");
82         while (idx >= 0) {
83           String JavaDoc sl = message.substring(0, idx);
84           String JavaDoc sm = message.substring(idx+1, idx+3);
85           String JavaDoc sr = message.substring(idx+3, message.length());
86           try {
87             int i = Integer.parseInt(sm, 16);
88             sm = String.valueOf((char)i);
89           }
90           catch (Exception JavaDoc ex) {
91             sm = "";
92           }
93           message = sl + sm + sr;
94           idx = message.indexOf("%");
95         }
96         // show message in a new thread
97
if ((message != null) && (message.length() > 0)) {
98           Thread JavaDoc t = new Thread JavaDoc(new DialogThread(parentComponent, message));
99           t.start();
100         }
101       }
102       out.write("<form name=\"Formular\" ACTION=\""+this.getHandledPath()+"+\" METHOD=\"PUT\">");
103       out.write("<table>\r\n");
104       out.write(" <tr><td>Send message to user</td></tr>\r\n");
105       out.write(" <tr><td><textarea name=\"message\" rows=10 cols=50></textarea></td></tr>\r\n");
106       out.write("</table>\r\n");
107       out.write("<input type=submit value=\"Submit\">");
108       out.write("</form>");
109       out.write("</BODY></HTML>\r\n");
110       out.flush();
111       return true;
112     } catch (Exception JavaDoc ex) {
113       return false;
114     }
115   }
116
117  /**
118    * Internal class to start the user dialog in a new thread. This makes the RequestHandler return
119    * immediatly
120    */

121   class DialogThread implements Runnable JavaDoc {
122     private Component parentComponent;
123     private String JavaDoc message;
124
125     public DialogThread(Component parentComponent, String JavaDoc message) {
126       this.parentComponent = parentComponent;
127       this.message = message;
128     }
129
130     public void run() {
131       JOptionPane.showMessageDialog(parentComponent, message);
132     }
133   }
134 }
Popular Tags