My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package com.google.clientlogin.example;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import com.google.clientlogin.CaptchaRequiredException;
import com.google.clientlogin.ClientLoginException;
import com.google.clientlogin.GoogleClientLogin;

public class ClientLoginDialog extends JDialog {

private final JPanel contentPanel = new JPanel();
private JTextField textField;
private JPasswordField passwordField;
private JTextField captchaTextField;
private JLabel lblCaptchaimage;
private JLabel lblEnterKey;
private GoogleClientLogin client = new GoogleClientLogin("ndev");

/**
* Launch the application.
*/
public static void main(String[] args) {
try {
ClientLoginDialog dialog = new ClientLoginDialog();
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Create the dialog.
*/
public ClientLoginDialog() {
setTitle("Google ClientLogin");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
GridBagLayout gbl_contentPanel = new GridBagLayout();
gbl_contentPanel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0,
0 };
gbl_contentPanel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
gbl_contentPanel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE };
gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0, 0.0,
Double.MIN_VALUE };
contentPanel.setLayout(gbl_contentPanel);
{
JLabel lblUsername = new JLabel("username");
GridBagConstraints gbc_lblUsername = new GridBagConstraints();
gbc_lblUsername.fill = GridBagConstraints.VERTICAL;
gbc_lblUsername.gridwidth = 6;
gbc_lblUsername.anchor = GridBagConstraints.WEST;
gbc_lblUsername.insets = new Insets(0, 0, 5, 5);
gbc_lblUsername.gridx = 2;
gbc_lblUsername.gridy = 1;
contentPanel.add(lblUsername, gbc_lblUsername);
}
{
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 8;
gbc_textField.gridy = 1;
contentPanel.add(textField, gbc_textField);
textField.setColumns(10);
}
{
JLabel lblPassword = new JLabel("password");
GridBagConstraints gbc_lblPassword = new GridBagConstraints();
gbc_lblPassword.anchor = GridBagConstraints.WEST;
gbc_lblPassword.gridwidth = 6;
gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
gbc_lblPassword.gridx = 2;
gbc_lblPassword.gridy = 2;
contentPanel.add(lblPassword, gbc_lblPassword);
}
{
passwordField = new JPasswordField();
GridBagConstraints gbc_passwordField = new GridBagConstraints();
gbc_passwordField.insets = new Insets(0, 0, 5, 0);
gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
gbc_passwordField.gridx = 8;
gbc_passwordField.gridy = 2;
contentPanel.add(passwordField, gbc_passwordField);
}
{
lblCaptchaimage = new JLabel("");
GridBagConstraints gbc_lblCaptchaimage = new GridBagConstraints();
gbc_lblCaptchaimage.insets = new Insets(0, 0, 5, 0);
gbc_lblCaptchaimage.gridx = 8;
gbc_lblCaptchaimage.gridy = 3;
contentPanel.add(lblCaptchaimage, gbc_lblCaptchaimage);
}
{
lblEnterKey = new JLabel("Enter key");
lblEnterKey.setVisible(false);
GridBagConstraints gbc_lblEnterKey = new GridBagConstraints();
gbc_lblEnterKey.anchor = GridBagConstraints.WEST;
gbc_lblEnterKey.gridwidth = 5;
gbc_lblEnterKey.insets = new Insets(0, 0, 0, 5);
gbc_lblEnterKey.gridx = 2;
gbc_lblEnterKey.gridy = 4;
contentPanel.add(lblEnterKey, gbc_lblEnterKey);
}
{
captchaTextField = new JTextField();
captchaTextField.setVisible(false);
GridBagConstraints gbc_captchaTextField = new GridBagConstraints();
gbc_captchaTextField.fill = GridBagConstraints.HORIZONTAL;
gbc_captchaTextField.gridx = 8;
gbc_captchaTextField.gridy = 4;
contentPanel.add(captchaTextField, gbc_captchaTextField);
captchaTextField.setColumns(10);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (isCaptcha()) {
try {
client.authenticate(textField.getText().trim(),
passwordField.getText().trim(),
captchaTextField.getText().trim());
showMessage("Auth Token: \n"+client.getAuthToken());

} catch (CaptchaRequiredException e1) {
showMessage("Incorrect captcha key.\nPlease try again");
showCaptcha(true);

} catch (ClientLoginException e1) {
showMessage(e1.getMessage());
}

} else {
try {
client.authenticate(textField.getText().trim(),
passwordField.getText().trim());
showMessage("Auth Token: \n"+client.getAuthToken());
} catch (CaptchaRequiredException e1) {
showCaptcha(true);
//
} catch (ClientLoginException e1) {
showMessage(e1.getMessage());
}
}
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton captchaTestButton = new JButton("Test Captcha");
captchaTestButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ExecutorService executor = Executors.newFixedThreadPool(10);
executor.execute(new ForceCaptchaRunnable());
}
});
captchaTestButton.setActionCommand("Captcha");
buttonPane.add(captchaTestButton);
}
}
}

private void showCaptcha(boolean b) {

captchaTextField.setVisible(b);
lblEnterKey.setVisible(b);
if (b) {
BufferedImage image = client.getCaptchaImage();
lblCaptchaimage.setIcon(new ImageIcon(image));
} else {
lblCaptchaimage.setIcon(null);
captchaTextField.setText("");
}
this.repaint();
this.validate();

}

private void showMessage(String msg) {
showCaptcha(false);
JTextArea txt =new JTextArea(msg);
txt.setPreferredSize(new Dimension(350, 200));
txt.setLineWrap(true);
txt.setEditable(false);
JScrollPane sp= new JScrollPane(txt,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setPreferredSize(new Dimension(300, 200));
sp.validate();
JOptionPane.showMessageDialog(this, sp);
}

private boolean isCaptcha() {
return (captchaTextField.isVisible() && lblEnterKey.isVisible());
}

private class ForceCaptchaRunnable implements Runnable{

public void run() {
Random r = new Random();
boolean isCaptcha = false;
while (!isCaptcha) {
try {
client.authenticate(
textField.getText().trim(),
passwordField.getText().trim()
+ r.nextInt(100));
showMessage("Auth Token: "+client.getAuthToken());
} catch (CaptchaRequiredException e1) {

isCaptcha = true;
showCaptcha(true);

} catch (ClientLoginException e1) {

}
}

}

}
}

Change log

r3 by othmanelmoulat on Sep 6, 2011   Diff
Google ClientLogin wrapper demo
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 8434 bytes, 257 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting