Home » apache-log4j-1.2.15 » com.psibt.framework » net » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    * 
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   /**
   18    * Title:        PSI Java Framework: UserDialogRequestHandler<p>
   19    * Copyright:    PSI-BT AG<p>
   20    * History:
   21    *   Date        Author        What's new
   22    *   16.04.2001  VMentzner     Created
   23    */
   24   
   25   package com.psibt.framework.net;
   26   /**
   27    * This class implements a RequestHandler for the path "/userdialog/" in the PluggableHTTPServer.
   28    * A simple input form is presented in the browser where you can enter a message. This message will be sent
   29    * to the PluggableHTTPServer and shown in a JOptionPane MessageDialog.
   30    *
   31    * @author <a HREF="mailto:V.Mentzner@psi-bt.de">Volker Mentzner</a>
   32    */
   33   public class UserDialogRequestHandler extends RootRequestHandler {
   34   
   35     private Component parentComponent;
   36   
   37    /**
   38      * Creates a new UserDialogRequestHandler object
   39      */
   40     public UserDialogRequestHandler() {
   41       this(null);
   42     }
   43   
   44    /**
   45      * Creates a new UserDialogRequestHandler object with a parentComponent reference
   46      */
   47     public UserDialogRequestHandler(Component parentComponent) {
   48       this.setTitle("user dialog");
   49       this.setDescription("show user dialog");
   50       this.setHandledPath("/userdialog/");
   51       this.parentComponent = parentComponent;
   52     }
   53   
   54    /**
   55      * Handles the given request and writes the reply to the given out-stream.
   56      *
   57      * @param request - client browser request
   58      * @param out - Out stream for sending data to client browser
   59      * @return if the request was handled by this handler : true, else : false
   60      */
   61     public boolean handleRequest(String request, Writer out) {
   62       String path = "";
   63       String query = null;
   64       try {
   65         URL url = new URL("http://localhost"+request);
   66         path = url.getPath();
   67         query = url.getQuery();
   68         if (path.startsWith(this.getHandledPath()) == false) {
   69           return false;
   70         }
   71   
   72         out.write("HTTP/1.0 200 OK\r\n");
   73         out.write("Content-type: text/html\r\n\r\n");
   74         out.write("<HTML><HEAD><TITLE>" + this.getTitle() + "</TITLE></HEAD>\r\n");
   75         out.write("<BODY><H1>" + this.getDescription() + "</H1>\r\n");
   76         if ((query != null) && (query.length() >= 0)) {
   77           int idx = query.indexOf("=");
   78           String message = query.substring(idx+1, query.length());
   79           // replace '+' by space
   80           message = message.replace('+', ' ');
   81           // replace hex strings starting with '%' by their values
   82           idx = message.indexOf("%");
   83           while (idx >= 0) {
   84             String sl = message.substring(0, idx);
   85             String sm = message.substring(idx+1, idx+3);
   86             String sr = message.substring(idx+3, message.length());
   87             try {
   88               int i = Integer.parseInt(sm, 16);
   89               sm = String.valueOf((char)i);
   90             }
   91             catch (Exception ex) {
   92               sm = "";
   93             }
   94             message = sl + sm + sr;
   95             idx = message.indexOf("%");
   96           }
   97           // show message in a new thread
   98           if ((message != null) && (message.length() > 0)) {
   99             Thread t = new Thread(new DialogThread(parentComponent, message));
  100             t.start();
  101           }
  102         }
  103         out.write("<form name=\"Formular\" ACTION=\""+this.getHandledPath()+"+\" METHOD=\"PUT\">");
  104         out.write("<table>\r\n");
  105         out.write(" <tr><td>Send message to user</td></tr>\r\n");
  106         out.write(" <tr><td><textarea name=\"message\" rows=10 cols=50></textarea></td></tr>\r\n");
  107         out.write("</table>\r\n");
  108         out.write("<input type=submit value=\"Submit\">");
  109         out.write("</form>");
  110         out.write("</BODY></HTML>\r\n");
  111         out.flush();
  112         return true;
  113       } catch (Exception ex) {
  114         return false;
  115       }
  116     }
  117   
  118    /**
  119      * Internal class to start the user dialog in a new thread. This makes the RequestHandler return
  120      * immediatly
  121      */
  122     class DialogThread implements Runnable {
  123       private Component parentComponent;
  124       private String message;
  125   
  126       public DialogThread(Component parentComponent, String message) {
  127         this.parentComponent = parentComponent;
  128         this.message = message;
  129       }
  130   
  131       public void run() {
  132         JOptionPane.showMessageDialog(parentComponent, message);
  133       }
  134     }
  135   }

Save This Page
Home » apache-log4j-1.2.15 » com.psibt.framework » net » [javadoc | source]