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
258
259
260
261
262
263
|
package com.vaadin.ui;
import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.vaadin.Application;
import com.vaadin.terminal.ApplicationResource;
import com.vaadin.terminal.DownloadStream;
import com.vaadin.terminal.ParameterHandler;
import com.vaadin.terminal.URIHandler;
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
/**
* LoginForm is a Toolkit component to handle common problem among Ajax
* applications: browsers password managers don't fill dynamically created forms
* like all those UI elements created by IT Mill Toolkit.
* <p>
* For developer it is easy to use: add component to a desired place in you UI
* and add LoginListener to validate form input. Behind the curtain LoginForm
* creates an iframe with static html that browsers detect.
* <p>
* Login form is by default 100% width and height, so consider using it inside a
* sized {@link Panel} or {@link Window}.
* <p>
* Login page html can be overridden by replacing protected getLoginHTML method.
* As the login page is actually an iframe, styles must be handled manually. By
* default component tries to guess the right place for theme css.
* <p>
* Note, this is a new Ajax terminal specific component and is likely to change.
*
* @since 5.3
*/
@SuppressWarnings("serial")
public class LoginForm extends CustomComponent {
private Embedded iframe = new Embedded();
private ApplicationResource loginPage = new ApplicationResource() {
public Application getApplication() {
return LoginForm.this.getApplication();
}
public int getBufferSize() {
return getLoginHTML().length;
}
public long getCacheTime() {
return -1;
}
public String getFilename() {
return "login";
}
public DownloadStream getStream() {
return new DownloadStream(new ByteArrayInputStream(getLoginHTML()),
getMIMEType(), getFilename());
}
public String getMIMEType() {
return "text/html";
}
};
private ParameterHandler paramHandler = new ParameterHandler() {
public void handleParameters(Map parameters) {
if (parameters.containsKey("username")) {
getWindow().addURIHandler(uriHandler);
HashMap params = new HashMap();
// expecting single params
for (Iterator it = parameters.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
String value = ((String[]) parameters.get(key))[0];
params.put(key, value);
}
LoginEvent event = new LoginEvent(params);
fireEvent(event);
}
}
};
private URIHandler uriHandler = new URIHandler() {
private final String responce = "<html><body>Login form handeled."
+ "<script type='text/javascript'>top.vaadin.forceSync();"
+ "</script></body></html>";
public DownloadStream handleURI(URL context, String relativeUri) {
if (relativeUri != null && relativeUri.contains("loginHandler")) {
if (window != null) {
window.removeURIHandler(this);
}
DownloadStream downloadStream = new DownloadStream(
new ByteArrayInputStream(responce.getBytes()),
"text/html", "loginSuccesfull");
downloadStream.setCacheTime(-1);
return downloadStream;
} else {
return null;
}
}
};
private Window window;
public LoginForm() {
iframe.setType(Embedded.TYPE_BROWSER);
iframe.setSizeFull();
setSizeFull();
setCompositionRoot(iframe);
}
/**
* Returns byte array containing login page html. If you need to override
* the login html, use the default html as basis. Login page sets its target
* with javascript.
*
* @return byte array containing login page html
*/
protected byte[] getLoginHTML() {
String defaultThemeName = AbstractApplicationServlet.getDefaultTheme();
String theme = getApplication().getMainWindow().getTheme();
String guessedThemeUri = getApplication().getURL() + "VAADIN/themes/"
+ (theme == null ? defaultThemeName : theme) + "/styles.css";
String guessedThemeUri2 = getApplication().getURL()
+ "../VAADIN/themes/"
+ (theme == null ? defaultThemeName : theme) + "/styles.css";
String appUri = getApplication().getURL().toString();
return ("<!DOCTYPE html PUBLIC \"-//W3C//DTD "
+ "XHTML 1.0 Transitional//EN\" "
+ "\"http://www.w3.org/TR/xhtml1/"
+ "DTD/xhtml1-transitional.dtd\">\n" + "<html>"
+ "<head><script type='text/javascript'>"
+ "var setTarget = function() {" + "var uri = '"
+ appUri
+ "loginHandler"
+ "'; var f = document.getElementById('loginf');"
+ "document.forms[0].action = uri;document.forms[0].username.focus();};"
+ "</script>"
+ "<link rel='stylesheet' href='"
+ guessedThemeUri
+ "'/>"
+ "<link rel='stylesheet' href='"
+ guessedThemeUri2
+ "'/>"
+ "</head><body onload='setTarget();' style='margin:0;padding:0;'>"
+ "<div class='v-app v-app-loginpage'>"
+ "<iframe name='logintarget' style='width:0;height:0;"
+ "border:0;margin:0;padding:0;'></iframe>"
+ "<form id='loginf' target='logintarget'>"
+ "<div>Username</div><div >"
+ "<input class='v-textfield' style='display:block;' type='text' name='username'></div>"
+ "<div>Password</div>"
+ "<div><input class='v-textfield' style='display:block;' type='password' name='password'></div>"
+ "<div><input class='v-button' type='submit' value='Login'></div></form></div>" + "</body></html>")
.getBytes();
}
@Override
public void attach() {
super.attach();
getApplication().addResource(loginPage);
getWindow().addParameterHandler(paramHandler);
iframe.setSource(loginPage);
}
@Override
public void detach() {
getApplication().removeResource(loginPage);
getWindow().removeParameterHandler(paramHandler);
// store window temporary to properly remove uri handler once
// response is handled. (May happen if login handler removes login
// form
window = getWindow();
if (window.getParent() != null) {
window = (Window) window.getParent();
}
super.detach();
}
/**
* This event is sent when login form is submitted.
*/
public class LoginEvent extends Event {
private Map params;
private LoginEvent(Map params) {
super(LoginForm.this);
this.params = params;
}
/**
* Access method to form values by field names.
*
* @param name
* @return value in given field
*/
public String getLoginParameter(String name) {
if (params.containsKey(name)) {
return (String) params.get(name);
} else {
return null;
}
}
}
/**
* Login listener is a class capable to listen LoginEvents sent from
* LoginBox
*/
public interface LoginListener extends Serializable {
/**
* This method is fired on each login form post.
*
* @param event
*/
public void onLogin(LoginForm.LoginEvent event);
}
private static final Method ON_LOGIN_METHOD;
static {
try {
ON_LOGIN_METHOD = LoginListener.class.getDeclaredMethod("onLogin",
new Class[] { LoginEvent.class });
} catch (final java.lang.NoSuchMethodException e) {
// This should never happen
throw new java.lang.RuntimeException(
"Internal error finding methods in LoginForm");
}
}
/**
* Adds LoginListener to handle login logic
*
* @param listener
*/
public void addListener(LoginListener listener) {
addListener(LoginEvent.class, listener, ON_LOGIN_METHOD);
}
/**
* Removes LoginListener
*
* @param listener
*/
public void removeListener(LoginListener listener) {
removeListener(LoginEvent.class, listener, ON_LOGIN_METHOD);
}
}
|