Handle hostnames with upper-case letters
[webmin.git] / itsecur-firewall / LogViewer.java
1 import java.awt.*;
2 import java.net.*;
3 import java.io.*;
4 import java.util.*;
5 import java.applet.*;
6
7 public class LogViewer extends Applet implements Runnable,CbButtonCallback
8 {
9         TextArea log;
10         StringBuffer logbuffer = new StringBuffer();
11         LineInputStream is;
12         Thread th;
13         CbButton pause, button;
14         boolean paused = false;
15
16         public void init()
17         {
18         // Create the UI
19         setLayout(new BorderLayout());
20         add("Center", log = new TextArea());
21         log.setEditable(false);
22         Util.setFont(new Font("TimesRoman", Font.PLAIN, 12));
23         Panel bot = new Panel();
24         bot.setBackground(Color.white);
25         bot.setForeground(Color.white);
26         bot.setLayout(new FlowLayout(FlowLayout.RIGHT));
27         if (getParameter("pause") != null) {
28                 // Add button to pause display
29                 bot.add(pause = new CbButton("  Pause  ", this));
30                 }
31         if (getParameter("buttonname") != null) {
32                 // Add button for some other purpose
33                 bot.add(button = new CbButton(getParameter("buttonname"),this));
34                 }
35         add("South", bot);
36         }
37
38         public void start()
39         {
40         // Start download thread
41         log.setText("");
42         th = new Thread(this);
43         th.start();
44         }
45
46         public void stop()
47         {
48         // Stop download
49         try {
50                 String killurl = getParameter("killurl");
51                 if (killurl != null) {
52                         // Call this CGI at stop time
53                         try {
54                                 URL u = new URL(getDocumentBase(), killurl);
55                                 URLConnection uc = u.openConnection();
56                                 String session = getParameter("session");
57                                 if (session != null)
58                                     uc.setRequestProperty("Cookie", session);
59                                 uc.getInputStream().close();
60                                 }
61                         catch(Exception e2) { }
62                         }
63                 if (is != null) is.close();
64                 if (th != null) th.stop();
65                 }
66         catch(Exception e) {
67                 // ignore it
68                 e.printStackTrace();
69                 }
70         }
71
72         public void run()
73         {
74         try {
75                 URL u = new URL(getDocumentBase(), getParameter("url"));
76                 URLConnection uc = u.openConnection();
77                 String session = getParameter("session");
78                 if (session != null)
79                         uc.setRequestProperty("Cookie", session);
80                 is = new LineInputStream(uc.getInputStream());
81                 while(true) {
82                         String l = is.gets();
83                         append(l);
84                         }
85                 }
86         catch(EOFException e) {
87                 // end of file ..
88                 }
89         catch(IOException e) {
90                 // shouldn't happen!
91                 e.printStackTrace();
92                 append("IO error : "+e.getMessage());
93                 }
94         }
95
96         int len = 0, oldlen = 0;
97
98         void append(String str) {
99                 if (!paused) {
100                         log.append((len == 0 ? "" : "\n")+str);
101                         }
102                 logbuffer.append((len == 0 ? "" : "\n")+str);
103                 oldlen = len;
104                 len += str.length()+1;
105                 if (!paused) {
106                         log.select(oldlen, oldlen);
107                         }
108         }
109
110         public void click(CbButton b) {
111                 if (b == pause) {
112                         if (paused) {
113                                 // Resume display, and append missing text
114                                 pause.setText("  Pause  ");
115                                 log.setText(logbuffer.toString());
116                                 log.select(oldlen, oldlen);
117                         } else {
118                                 // Stop display
119                                 pause.setText("Resume");
120                         }
121                         paused = !paused;
122                 } else if (b == button) {
123                         // Open some page
124                         try {
125                                 URL u = new URL(getDocumentBase(),
126                                                 getParameter("buttonlink"));
127                                 getAppletContext().showDocument(u);
128                                 }
129                         catch(Exception e) { }
130                 }
131         }
132 }
133