KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ViaHTTP


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 import com.jcraft.jsch.*;
3 import java.awt.*;
4 import javax.swing.*;
5
6 public class ViaHTTP{
7   public static void main(String JavaDoc[] arg){
8
9     String JavaDoc proxy_host;
10     int proxy_port;
11
12     try{
13       JSch jsch=new JSch();
14
15       String JavaDoc host=JOptionPane.showInputDialog("Enter username@hostname",
16                           System.getProperty("user.name")+
17                           "@localhost");
18       String JavaDoc user=host.substring(0, host.indexOf('@'));
19       host=host.substring(host.indexOf('@')+1);
20
21       Session session=jsch.getSession(user, host, 22);
22
23       String JavaDoc proxy=JOptionPane.showInputDialog("Enter proxy server",
24                                                  "hostname:port");
25       proxy_host=proxy.substring(0, proxy.indexOf(':'));
26       proxy_port=Integer.parseInt(proxy.substring(proxy.indexOf(':')+1));
27
28       session.setProxy(new ProxyHTTP(proxy_host, proxy_port));
29
30       // username and password will be given via UserInfo interface.
31
UserInfo ui=new MyUserInfo();
32       session.setUserInfo(ui);
33
34       //java.util.Hashtable config=new java.util.Hashtable();
35
//config.put("compression.s2c", "zlib,non");
36
//config.put("compression.c2s", "zlib,none");
37
//session.setConfig(config);
38

39       session.connect();
40
41       Channel channel=session.openChannel("shell");
42
43       channel.setInputStream(System.in);
44       channel.setOutputStream(System.out);
45
46       channel.connect();
47     }
48     catch(Exception JavaDoc e){
49       System.out.println(e);
50     }
51   }
52
53   public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
54     public String JavaDoc getPassword(){ return passwd; }
55     public boolean promptYesNo(String JavaDoc str){
56       Object JavaDoc[] options={ "yes", "no" };
57       int foo=JOptionPane.showOptionDialog(null,
58              str,
59              "Warning",
60              JOptionPane.DEFAULT_OPTION,
61              JOptionPane.WARNING_MESSAGE,
62              null, options, options[0]);
63        return foo==0;
64     }
65   
66     String JavaDoc passwd;
67     JTextField passwordField=(JTextField)new JPasswordField(20);
68
69     public String JavaDoc getPassphrase(){ return null; }
70     public boolean promptPassphrase(String JavaDoc message){ return true; }
71     public boolean promptPassword(String JavaDoc message){
72       Object JavaDoc[] ob={passwordField};
73       int result=
74       JOptionPane.showConfirmDialog(null, ob, message,
75                     JOptionPane.OK_CANCEL_OPTION);
76       if(result==JOptionPane.OK_OPTION){
77     passwd=passwordField.getText();
78     return true;
79       }
80       else{ return false; }
81     }
82     public void showMessage(String JavaDoc message){
83       JOptionPane.showMessageDialog(null, message);
84     }
85     final GridBagConstraints gbc =
86       new GridBagConstraints(0,0,1,1,1,1,
87                              GridBagConstraints.NORTHWEST,
88                              GridBagConstraints.NONE,
89                              new Insets(0,0,0,0),0,0);
90     private Container panel;
91     public String JavaDoc[] promptKeyboardInteractive(String JavaDoc destination,
92                                               String JavaDoc name,
93                                               String JavaDoc instruction,
94                                               String JavaDoc[] prompt,
95                                               boolean[] echo){
96       panel = new JPanel();
97       panel.setLayout(new GridBagLayout());
98
99       gbc.weightx = 1.0;
100       gbc.gridwidth = GridBagConstraints.REMAINDER;
101       gbc.gridx = 0;
102       panel.add(new JLabel(instruction), gbc);
103       gbc.gridy++;
104
105       gbc.gridwidth = GridBagConstraints.RELATIVE;
106
107       JTextField[] texts=new JTextField[prompt.length];
108       for(int i=0; i<prompt.length; i++){
109         gbc.fill = GridBagConstraints.NONE;
110         gbc.gridx = 0;
111         gbc.weightx = 1;
112         panel.add(new JLabel(prompt[i]),gbc);
113
114         gbc.gridx = 1;
115         gbc.fill = GridBagConstraints.HORIZONTAL;
116         gbc.weighty = 1;
117         if(echo[i]){
118           texts[i]=new JTextField(20);
119         }
120         else{
121           texts[i]=new JPasswordField(20);
122         }
123         panel.add(texts[i], gbc);
124         gbc.gridy++;
125       }
126
127       if(JOptionPane.showConfirmDialog(null, panel,
128                                        destination+": "+name,
129                                        JOptionPane.OK_CANCEL_OPTION,
130                                        JOptionPane.QUESTION_MESSAGE)
131          ==JOptionPane.OK_OPTION){
132         String JavaDoc[] response=new String JavaDoc[prompt.length];
133         for(int i=0; i<prompt.length; i++){
134           response[i]=texts[i].getText();
135         }
136     return response;
137       }
138       else{
139         return null; // cancel
140
}
141     }
142   }
143
144 }
145
146
147
Popular Tags