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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.ui;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.vaadin.server.ConnectorResource;
import com.vaadin.server.ExternalResource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinResponse;
import com.vaadin.server.VaadinService;
import com.vaadin.server.VaadinServletService;
import com.vaadin.shared.ApplicationConstants;
/**
* LoginForm is a Vaadin component to handle common problem among Ajax
* applications: browsers password managers don't fill dynamically created forms
* like all those UI elements created by Vaadin.
* <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.
*
* @since 5.3
* @deprecated As of 7.0. This component no longer fulfills its duty reliably in
* the supported browsers and a {@link VerticalLayout} with two
* {@link TextField}s can be used instead.
*/
@Deprecated
public class LoginForm extends CustomComponent {
private String usernameCaption = "Username";
private String passwordCaption = "Password";
private String loginButtonCaption = "Login";
private Embedded iframe = new Embedded();
@Override
public boolean handleConnectorRequest(final VaadinRequest request,
final VaadinResponse response, String path) throws IOException {
if (!path.equals("login")) {
return super.handleConnectorRequest(request, response, path);
}
final StringBuilder responseBuilder = new StringBuilder();
getUI().accessSynchronously(new Runnable() {
@Override
public void run() {
String method = VaadinServletService.getCurrentServletRequest()
.getMethod();
if (method.equalsIgnoreCase("post")) {
responseBuilder.append(handleLogin(request));
} else {
responseBuilder.append(getLoginHTML());
}
}
});
if (responseBuilder.length() > 0) {
response.setContentType("text/html; charset=utf-8");
response.setCacheTime(-1);
response.getWriter().write(responseBuilder.toString());
return true;
} else {
return false;
}
}
private String handleLogin(VaadinRequest request) {
// Ensure UI.getCurrent() works in listeners
Map<String, String[]> parameters = VaadinService.getCurrentRequest()
.getParameterMap();
HashMap<String, String> params = new HashMap<String, String>();
// expecting single params
for (Iterator<String> it = parameters.keySet().iterator(); it.hasNext();) {
String key = it.next();
String value = (parameters.get(key))[0];
params.put(key, value);
}
LoginEvent event = new LoginEvent(LoginForm.this, params);
fireEvent(event);
return "<html><body>Login form handled."
+ "<script type='text/javascript'>parent.parent.vaadin.forceSync();"
+ "</script></body></html>";
}
public LoginForm() {
iframe.setType(Embedded.TYPE_BROWSER);
iframe.setSizeFull();
setSizeFull();
setCompositionRoot(iframe);
addStyleName("v-loginform");
}
@Override
public void beforeClientResponse(boolean initial) {
// Generate magic URL now when UI id and connectorId are known
iframe.setSource(new ExternalResource(
ApplicationConstants.APP_PROTOCOL_PREFIX
+ ApplicationConstants.APP_PATH + '/'
+ ConnectorResource.CONNECTOR_PATH + '/'
+ getUI().getUIId() + '/' + getConnectorId() + "/login"));
super.beforeClientResponse(initial);
}
/**
* 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 String getLoginHTML() {
return "<!DOCTYPE html>\n"
+ "<html>"
+ "<head><script type='text/javascript'>"
+ "var setTarget = function() {"
+ "var uri = window.location;"
+ "var f = document.getElementById('loginf');"
+ "document.forms[0].action = uri;document.forms[0].username.focus();};"
+ ""
+ "var styles = window.parent.document.styleSheets;"
+ "for(var j = 0; j < styles.length; j++) {\n"
+ "if(styles[j].href) {"
+ "var stylesheet = document.createElement('link');\n"
+ "stylesheet.setAttribute('rel', 'stylesheet');\n"
+ "stylesheet.setAttribute('type', 'text/css');\n"
+ "stylesheet.setAttribute('href', styles[j].href);\n"
+ "document.getElementsByTagName('head')[0].appendChild(stylesheet);\n"
+ "}"
+ "}\n"
+ "function submitOnEnter(e) { var keycode = e.keyCode || e.which;"
+ " if (keycode == 13) {document.forms[0].submit();} } \n"
+ "</script>"
+ "</head><body onload='setTarget();' style='margin:0;padding:0; background:transparent;' class=\""
+ ApplicationConstants.GENERATED_BODY_CLASSNAME
+ "\">"
+ "<div class='v-app v-app-loginpage "
+ getUIThemeClassName()
+ "' style=\"background:transparent;\">"
+ "<iframe name='logintarget' style='width:0;height:0;"
+ "border:0;margin:0;padding:0;display:block'></iframe>"
+ "<form id='loginf' target='logintarget' onkeypress=\"submitOnEnter(event)\" method=\"post\">"
+ "<div>"
+ usernameCaption
+ "</div><div >"
+ "<input class='v-textfield v-widget' style='display:block;' type='text' name='username'></div>"
+ "<div>"
+ passwordCaption
+ "</div>"
+ "<div><input class='v-textfield v-widget' style='display:block;' type='password' name='password'></div>"
+ "<div><div onclick=\"document.forms[0].submit();\" tabindex=\"0\" class=\"v-button\" role=\"button\" ><span class=\"v-button-wrap\"><span class=\"v-button-caption\">"
+ loginButtonCaption
+ "</span></span></div></div></form></div>" + "</body></html>";
}
private String getUIThemeClassName() {
if (getUI() != null) {
return getUI().getTheme();
}
return "";
}
/**
* This event is sent when login form is submitted.
*/
public static class LoginEvent extends Event {
private Map<String, String> params;
private LoginEvent(Component source, Map<String, String> params) {
super(source);
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 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;
private static final String UNDEFINED_HEIGHT = "140px";
private static final String UNDEFINED_WIDTH = "200px";
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 addLoginListener(LoginListener listener) {
addListener(LoginEvent.class, listener, ON_LOGIN_METHOD);
}
/**
* @deprecated As of 7.0, replaced by
* {@link #addLoginListener(LoginListener)}
**/
@Deprecated
public void addListener(LoginListener listener) {
addLoginListener(listener);
}
/**
* Removes LoginListener
*
* @param listener
*/
public void removeLoginListener(LoginListener listener) {
removeListener(LoginEvent.class, listener, ON_LOGIN_METHOD);
}
/**
* @deprecated As of 7.0, replaced by
* {@link #removeLoginListener(LoginListener)}
**/
@Deprecated
public void removeListener(LoginListener listener) {
removeLoginListener(listener);
}
@Override
public void setWidth(float width, Unit unit) {
super.setWidth(width, unit);
if (iframe != null) {
if (width < 0) {
iframe.setWidth(UNDEFINED_WIDTH);
} else {
iframe.setWidth("100%");
}
}
}
@Override
public void setHeight(float height, Unit unit) {
super.setHeight(height, unit);
if (iframe != null) {
if (height < 0) {
iframe.setHeight(UNDEFINED_HEIGHT);
} else {
iframe.setHeight("100%");
}
}
}
/**
* Returns the caption for the user name field.
*
* @return String
*/
public String getUsernameCaption() {
return usernameCaption;
}
/**
* Sets the caption to show for the user name field. The caption cannot be
* changed after the form has been shown to the user.
*
* @param usernameCaption
*/
public void setUsernameCaption(String usernameCaption) {
this.usernameCaption = usernameCaption;
}
/**
* Returns the caption for the password field.
*
* @return String
*/
public String getPasswordCaption() {
return passwordCaption;
}
/**
* Sets the caption to show for the password field. The caption cannot be
* changed after the form has been shown to the user.
*
* @param passwordCaption
*/
public void setPasswordCaption(String passwordCaption) {
this.passwordCaption = passwordCaption;
}
/**
* Returns the caption for the login button.
*
* @return String
*/
public String getLoginButtonCaption() {
return loginButtonCaption;
}
/**
* Sets the caption (button text) to show for the login button. The caption
* cannot be changed after the form has been shown to the user.
*
* @param loginButtonCaption
*/
public void setLoginButtonCaption(String loginButtonCaption) {
this.loginButtonCaption = loginButtonCaption;
}
}
|