| 1 16 package org.apache.axis.utils ; 17 18 19 import java.awt.BorderLayout ; 20 import java.awt.Color ; 21 import java.awt.Component ; 22 import java.awt.Dimension ; 23 import java.awt.GridBagConstraints ; 24 import java.awt.GridBagLayout ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.io.ByteArrayInputStream ; 28 import java.io.File ; 29 import java.io.FileOutputStream ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 import java.io.PrintWriter ; 33 import java.io.StringWriter ; 34 import java.net.ServerSocket ; 35 import java.net.Socket ; 36 import java.net.URL ; 37 import java.text.DateFormat ; 38 import java.text.SimpleDateFormat ; 39 import java.util.Date ; 40 import java.util.Iterator ; 41 import java.util.ResourceBundle ; 42 import java.util.Vector ; 43 44 import javax.swing.BorderFactory ; 45 import javax.swing.Box ; 46 import javax.swing.BoxLayout ; 47 import javax.swing.ButtonGroup ; 48 import javax.swing.JButton ; 49 import javax.swing.JCheckBox ; 50 import javax.swing.JFileChooser ; 51 import javax.swing.JFrame ; 52 import javax.swing.JLabel ; 53 import javax.swing.JPanel ; 54 import javax.swing.JRadioButton ; 55 import javax.swing.JScrollPane ; 56 import javax.swing.JSplitPane ; 57 import javax.swing.JTabbedPane ; 58 import javax.swing.JTable ; 59 import javax.swing.JTextArea ; 60 import javax.swing.JTextField ; 61 import javax.swing.ListSelectionModel ; 62 import javax.swing.SwingConstants ; 63 import javax.swing.UIManager ; 64 import javax.swing.UnsupportedLookAndFeelException ; 65 import javax.swing.border.TitledBorder ; 66 import javax.swing.event.ChangeEvent ; 67 import javax.swing.event.ListSelectionEvent ; 68 import javax.swing.event.ListSelectionListener ; 69 import javax.swing.plaf.basic.BasicButtonListener ; 70 import javax.swing.table.DefaultTableModel ; 71 import javax.swing.table.TableColumn ; 72 import javax.swing.table.TableModel ; 73 import javax.swing.text.AttributeSet ; 74 import javax.swing.text.BadLocationException ; 75 import javax.swing.text.Document ; 76 import javax.swing.text.PlainDocument ; 77 78 79 86 87 public class tcpmon extends JFrame { 88 private JTabbedPane notebook = null ; 89 90 private static final int STATE_COLUMN = 0 ; 91 private static final int TIME_COLUMN = 1 ; 92 private static final int INHOST_COLUMN = 2 ; 93 private static final int OUTHOST_COLUMN = 3 ; 94 private static final int REQ_COLUMN = 4 ; 95 96 97 private static final String DEFAULT_HOST="127.0.0.1"; 98 private static final int DEFAULT_PORT=8080; 99 100 103 class AdminPage extends JPanel { 104 public JRadioButton listenerButton, proxyButton ; 105 public JLabel hostLabel, tportLabel; 106 public NumberField port; 107 public HostnameField host; 108 public NumberField tport ; 109 public JTabbedPane noteb ; 110 public JCheckBox HTTPProxyBox ; 111 public HostnameField HTTPProxyHost; 112 public NumberField HTTPProxyPort ; 113 public JLabel HTTPProxyHostLabel, HTTPProxyPortLabel ; 114 public JLabel delayTimeLabel, delayBytesLabel; 115 public NumberField delayTime, delayBytes; 116 public JCheckBox delayBox; 117 118 public AdminPage( JTabbedPane notebook, String name ) { 119 JPanel mainPane = null ; 120 JButton addButton = null ; 121 122 this.setLayout( new BorderLayout () ); 123 noteb = notebook ; 124 125 GridBagLayout layout = new GridBagLayout (); 126 GridBagConstraints c = new GridBagConstraints (); 127 128 mainPane = new JPanel (layout); 129 130 c.anchor = GridBagConstraints.WEST ; 131 c.gridwidth = GridBagConstraints.REMAINDER; 132 mainPane.add( new JLabel (getMessage("newTCP00", "Create a new TCP/IP Monitor...") + " "), c ); 133 134 mainPane.add( Box.createRigidArea(new Dimension (1, 5)), c ); 136 137 JPanel tmpPanel = new JPanel (new GridBagLayout ()); 140 141 c.anchor = GridBagConstraints.WEST ; 142 c.gridwidth = 1 ; 143 tmpPanel.add( new JLabel (getMessage("listenPort00", "Listen Port #") + " "), c ); 144 145 c.anchor = GridBagConstraints.WEST ; 146 c.gridwidth = GridBagConstraints.REMAINDER ; 147 tmpPanel.add( port = new NumberField(4), c ); 148 149 mainPane.add( tmpPanel, c ); 150 151 mainPane.add( Box.createRigidArea(new Dimension (1, 5)), c ); 152 153 ButtonGroup btns = new ButtonGroup (); 155 156 c.anchor = GridBagConstraints.WEST ; 157 c.gridwidth = GridBagConstraints.REMAINDER ; 158 mainPane.add( new JLabel (getMessage("actAs00", "Act as a...") ), c ); 159 160 c.anchor = GridBagConstraints.WEST ; 163 c.gridwidth = GridBagConstraints.REMAINDER ; 164 165 final String listener = getMessage("listener00", "Listener"); 166 167 mainPane.add( listenerButton = new JRadioButton ( listener ), c ); 168 btns.add( listenerButton ); 169 listenerButton.setSelected( true ); 170 171 listenerButton.addActionListener( new ActionListener () { 172 public void actionPerformed(ActionEvent event) { 173 if (listener.equals(event.getActionCommand())) { 174 boolean state = listenerButton.isSelected(); 175 176 tport.setEnabled( state ); 177 host.setEnabled( state ); 178 hostLabel.setForeground(state ? Color.black : Color.gray); 179 tportLabel.setForeground(state ? Color.black : Color.gray); 180 } 181 } 182 } 183 ); 184 185 c.anchor = GridBagConstraints.WEST ; 186 c.gridwidth = 1 ; 187 mainPane.add( Box.createRigidArea(new Dimension (25, 0)) ); 188 mainPane.add( hostLabel = new JLabel (getMessage("targetHostname00", "Target Hostname") + " "), c ); 189 190 c.anchor = GridBagConstraints.WEST ; 191 c.gridwidth = GridBagConstraints.REMAINDER ; 192 host = new HostnameField(30); 193 mainPane.add( host, c ); 194 host.setText(DEFAULT_HOST); 195 196 c.anchor = GridBagConstraints.WEST ; 197 c.gridwidth = 1 ; 198 mainPane.add( Box.createRigidArea(new Dimension (25, 0)) ); 199 mainPane.add( tportLabel = new JLabel (getMessage("targetPort00", "Target Port #") + " "), c ); 200 201 c.anchor = GridBagConstraints.WEST ; 202 c.gridwidth = GridBagConstraints.REMAINDER ; 203 tport = new NumberField(4); 204 mainPane.add( tport, c ); 205 tport.setValue(DEFAULT_PORT); 206 207 c.anchor = GridBagConstraints.WEST ; 210 c.gridwidth = GridBagConstraints.REMAINDER ; 211 final String proxy = getMessage("proxy00", "Proxy"); 212 213 mainPane.add( proxyButton = new JRadioButton ( proxy ), c); 214 btns.add( proxyButton ); 215 216 proxyButton.addActionListener( new ActionListener () { 217 public void actionPerformed(ActionEvent event) { 218 if (proxy.equals(event.getActionCommand())) { 219 boolean state = proxyButton.isSelected(); 220 221 tport.setEnabled( !state ); 222 host.setEnabled( !state ); 223 hostLabel.setForeground(state ? Color.gray : Color.black); 224 tportLabel.setForeground(state ? Color.gray : Color.black); 225 } 226 } 227 } 228 ); 229 230 c.anchor = GridBagConstraints.WEST ; 233 c.gridwidth = GridBagConstraints.REMAINDER ; 234 mainPane.add( Box.createRigidArea(new Dimension (1, 10)), c ); 235 236 JPanel opts = new JPanel (new GridBagLayout ()); 239 240 opts.setBorder( new TitledBorder (getMessage("options00", "Options")) ); 241 c.anchor = GridBagConstraints.WEST ; 242 c.gridwidth = GridBagConstraints.REMAINDER ; 243 mainPane.add( opts, c ); 244 245 c.anchor = GridBagConstraints.WEST ; 248 c.gridwidth = GridBagConstraints.REMAINDER ; 249 final String proxySupport = getMessage("proxySupport00", "HTTP Proxy Support"); 250 251 opts.add(HTTPProxyBox = new JCheckBox (proxySupport), c); 252 253 c.anchor = GridBagConstraints.WEST ; 254 c.gridwidth = 1 ; 255 opts.add( HTTPProxyHostLabel = new JLabel (getMessage("hostname00", "Hostname") + " "), c ); 256 HTTPProxyHostLabel.setForeground( Color.gray ); 257 258 c.anchor = GridBagConstraints.WEST ; 259 c.gridwidth = GridBagConstraints.REMAINDER ; 260 opts.add( HTTPProxyHost = new HostnameField(30), c ); 261 HTTPProxyHost.setEnabled( false ); 262 263 c.anchor = GridBagConstraints.WEST ; 264 c.gridwidth = 1 ; 265 opts.add( HTTPProxyPortLabel = new JLabel (getMessage("port00", "Port #") + " "), c ); 266 HTTPProxyPortLabel.setForeground( Color.gray ); 267 268 c.anchor = GridBagConstraints.WEST ; 269 c.gridwidth = GridBagConstraints.REMAINDER ; 270 opts.add( HTTPProxyPort = new NumberField(4), c ); 271 HTTPProxyPort.setEnabled( false ); 272 273 HTTPProxyBox.addActionListener( new ActionListener () { 274 public void actionPerformed(ActionEvent event) { 275 if (proxySupport.equals(event.getActionCommand())) { 276 boolean b = HTTPProxyBox.isSelected(); 277 Color color = b ? Color.black : Color.gray ; 278 279 HTTPProxyHost.setEnabled( b ); 280 HTTPProxyPort.setEnabled( b ); 281 HTTPProxyHostLabel.setForeground( color ); 282 HTTPProxyPortLabel.setForeground( color ); 283 } 284 } 285 } 286 ); 287 288 String tmp = System.getProperty( "http.proxyHost" ); 290 291 if ( tmp != null && tmp.equals("") ) { 292 tmp = null ; 293 } 294 295 HTTPProxyBox.setSelected( tmp != null ); 296 HTTPProxyHost.setEnabled( tmp != null ); 297 HTTPProxyPort.setEnabled( tmp != null ); 298 HTTPProxyHostLabel.setForeground( tmp != null ? Color.black : Color.gray); 299 HTTPProxyPortLabel.setForeground( tmp != null ? Color.black : Color.gray); 300 301 if ( tmp != null ) { 302 HTTPProxyBox.setSelected( true ); 303 HTTPProxyHost.setText( tmp ); 304 tmp = System.getProperty( "http.proxyPort" ); 305 if ( tmp != null && tmp.equals("") ) { 306 tmp = null ; 307 } 308 if ( tmp == null ) { 309 tmp = "80" ; 310 } 311 HTTPProxyPort.setText( tmp ); 312 } 313 314 opts.add(Box.createRigidArea(new Dimension (1, 10)), c); 316 c.anchor = GridBagConstraints.WEST; 317 c.gridwidth = GridBagConstraints.REMAINDER; 318 final String delaySupport = getMessage("delay00", "Simulate Slow Connection"); 319 opts.add(delayBox = new JCheckBox (delaySupport), c); 320 321 c.anchor = GridBagConstraints.WEST; 323 c.gridwidth = 1; 324 delayBytesLabel=new JLabel (getMessage("delay01", "Bytes per Pause")); 325 opts.add(delayBytesLabel, c); 326 delayBytesLabel.setForeground(Color.gray); 327 c.anchor = GridBagConstraints.WEST; 328 c.gridwidth = GridBagConstraints.REMAINDER; 329 opts.add(delayBytes = new NumberField(6), c); 330 delayBytes.setEnabled(false); 331 332 c.anchor = GridBagConstraints.WEST; 334 c.gridwidth = 1; 335 delayTimeLabel = new JLabel (getMessage("delay02", "Delay in Milliseconds")); 336 opts.add(delayTimeLabel, c); 337 delayTimeLabel.setForeground(Color.gray); 338 c.anchor = GridBagConstraints.WEST; 339 c.gridwidth = GridBagConstraints.REMAINDER; 340 opts.add(delayTime = new NumberField(6), c); 341 delayTime.setEnabled(false); 342 343 delayBox.addActionListener(new ActionListener () { 345 public void actionPerformed(ActionEvent event) { 346 if (delaySupport.equals(event.getActionCommand())) { 347 boolean b = delayBox.isSelected(); 348 Color color = b ? Color.black : Color.gray; 349 350 delayBytes.setEnabled(b); 351 delayTime.setEnabled(b); 352 delayBytesLabel.setForeground(color); 353 delayTimeLabel.setForeground(color); 354 } 355 } 356 } 357 ); 358 359 mainPane.add( Box.createRigidArea(new Dimension (1, 10)), c ); 362 363 c.anchor = GridBagConstraints.WEST ; 366 c.gridwidth = GridBagConstraints.REMAINDER ; 367 final String add = getMessage("add00", "Add"); 368 369 mainPane.add( addButton = new JButton ( add ), c ); 370 371 372 this.add( new JScrollPane ( mainPane ), BorderLayout.CENTER ); 373 374 addButton.addActionListener( new ActionListener () { 376 public void actionPerformed(ActionEvent event) { 377 if ( add.equals(event.getActionCommand()) ) { 378 String text ; 379 Listener l = null ; 380 int lPort; 381 lPort=port.getValue(0); 382 if(lPort==0) { 383 return; 385 } 386 String tHost = host.getText(); 387 int tPort = 0 ; 388 tPort=tport.getValue(0); 389 SlowLinkSimulator slowLink=null; 390 if(delayBox.isSelected()) { 391 int bytes= delayBytes.getValue(0); 392 int time = delayTime.getValue(0); 393 slowLink=new SlowLinkSimulator(bytes,time); 394 } 395 try { 396 l = new Listener ( noteb, null, lPort, tHost, tPort, 397 proxyButton.isSelected(), slowLink); 398 } catch (Exception e){ 399 e.printStackTrace(); 400 } 401 text = HTTPProxyHost.getText(); 404 if ( "".equals(text) ) { 405 text = null ; 406 } 407 l.HTTPProxyHost = text ; 408 text = HTTPProxyPort.getText(); 409 int proxyPort=HTTPProxyPort.getValue(-1); 410 if(proxyPort!=-1) { 411 l.HTTPProxyPort = Integer.parseInt(text); 412 } 413 port.setText(null); 415 416 421 } 422 } 423 } 424 ); 425 426 notebook.addTab( name, this ); 427 notebook.repaint(); 428 notebook.setSelectedIndex( notebook.getTabCount() - 1 ); 429 } 430 431 432 } 433 434 438 class SocketWaiter extends Thread { 439 ServerSocket sSocket = null ; 440 Listener listener ; 441 int port ; 442 boolean pleaseStop = false ; 443 444 public SocketWaiter(Listener l, int p) { 445 listener = l ; 446 port = p ; 447 start(); 448 } 449 450 public void run() { 451 try { 452 listener.setLeft( new JLabel (getMessage("wait00", " Waiting for Connection...") ) ); 453 listener.repaint(); 454 sSocket = new ServerSocket ( port ); 455 for (; ; ) { 456 Socket inSocket = sSocket.accept(); 457 458 if ( pleaseStop ) { 459 break ; 460 } 461 new Connection( listener, inSocket ); 462 inSocket = null ; 463 } 464 } catch ( Exception exp ) { 465 if ( !"socket closed".equals(exp.getMessage()) ) { 466 JLabel tmp = new JLabel ( exp.toString() ); 467 468 tmp.setForeground( Color.red ); 469 listener.setLeft( tmp ); 470 listener.setRight( new JLabel ("") ); 471 listener.stop(); 472 } 473 } 474 } 475 476 479 public void halt() { 480 try { 481 pleaseStop = true ; 482 new Socket ( "127.0.0.1", port ); 483 if ( sSocket != null ) { 484 sSocket.close(); 485 } 486 } catch ( Exception e ) { 487 e.printStackTrace(); 488 } 489 } 490 } 491 492 493 496 static class SlowLinkSimulator { 497 private int delayBytes; 498 private int delayTime; 499 private int currentBytes; 500 private int totalBytes; 501 502 507 public SlowLinkSimulator(int delayBytes, int delayTime) { 508 this.delayBytes = delayBytes; 509 this.delayTime = delayTime; 510 } 511 512 517 public SlowLinkSimulator(SlowLinkSimulator that) { 518 this.delayBytes=that.delayBytes; 519 this.delayTime=that.delayTime; 520 } 521 522 526 public int getTotalBytes() { 527 return totalBytes; 528 } 529 530 535 public void pump(int bytes) { 536 totalBytes+=bytes; 537 if(delayBytes==0) { 538 return; 540 } 541 currentBytes += bytes; 542 if(currentBytes>delayBytes) { 543 int delaysize=currentBytes/delayBytes; 545 long delay=delaysize*(long)delayTime; 546 currentBytes=currentBytes%delayBytes; 548 try { 550 Thread.sleep(delay); 551 } catch (InterruptedException e) { 552 ; } 554 } 555 } 556 557 561 public int getCurrentBytes() { 562 return currentBytes; 563 } 564 565 569 public void setCurrentBytes(int currentBytes) { 570 this.currentBytes = currentBytes; 571 } 572 573 } 574 575 579 class SocketRR extends Thread { 580 Socket inSocket = null ; 581 Socket outSocket = null ; 582 JTextArea textArea ; 583 InputStream in = null ; 584 OutputStream out = null ; 585 boolean xmlFormat ; 586 boolean numericEnc ; 587 volatile boolean done = false ; 588 TableModel tmodel = null ; 589 int tableIndex = 0 ; 590 String type = null; 591 Connection myConnection = null; 592 SlowLinkSimulator slowLink; 593 594 public SocketRR(Connection c, Socket inputSocket, InputStream inputStream, 595 Socket outputSocket, OutputStream outputStream, 596 JTextArea _textArea, boolean format, boolean numeric, 597 TableModel tModel, int index, final String type, SlowLinkSimulator slowLink) { 598 inSocket = inputSocket ; 599 in = inputStream ; 600 outSocket = outputSocket ; 601 out = outputStream ; 602 textArea = _textArea ; 603 xmlFormat = format ; 604 numericEnc= numeric ; 605 tmodel = tModel ; 606 tableIndex = index ; 607 this.type = type; 608 myConnection = c; 609 this.slowLink= slowLink; 610 start(); 611 } 612 613 public boolean isDone() { 614 return ( done ); 615 } 616 617 public void run() { 618 try { 619 byte[] buffer = new byte[4096]; 620 byte[] tmpbuffer = new byte[8192]; 621 String message = null; 622 int saved = 0 ; 623 int len ; 624 int i1, i2 ; 625 int i ; 626 int reqSaved = 0 ; 627 int tabWidth = 3 ; 628 boolean atMargin = true ; 629 int thisIndent = -1, 630 nextIndent = -1, 631 previousIndent = -1; 632 633 636 if ( tmodel != null ) { 637 String tmpStr = (String ) tmodel.getValueAt(tableIndex, 638 REQ_COLUMN); 639 640 if ( !"".equals(tmpStr) ) { 641 reqSaved = tmpStr.length(); 642 } 643 } 644 645 a: 646 for ( ; ; ) { 647 if ( done ) { 648 break; 649 } 650 len = buffer.length ; 654 if ( len == 0 ) { 657 len = buffer.length; 658 } 659 if ( saved + len > buffer.length) { 660 len = buffer.length - saved ; 661 } 662 int len1 = 0; 663
|