diff options
author | Leif Åstrand <leif@vaadin.com> | 2012-10-08 13:17:07 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2012-10-08 10:43:19 +0000 |
commit | 48c19777e566dd8cd5e496ea364de8aea447abbf (patch) | |
tree | e6f5ff65c2d4d249230e92e62a52db537e6042a7 /server/src/com/vaadin/ui/LoginForm.java | |
parent | 13d5b3e98954c2ade382305f8d044b2b49fdbd0b (diff) | |
download | vaadin-framework-48c19777e566dd8cd5e496ea364de8aea447abbf.tar.gz vaadin-framework-48c19777e566dd8cd5e496ea364de8aea447abbf.zip |
Remove VaadinServiceSession.getURL (#9884)
* Give an URL to LegacyApplication when initializing
* Update LoginForm to use DynamicConnectorResource instead of
RequestHandler
* Make CustomUIClassLoader work again (including previous issues not
caused by this change)
* Update some other tests to use more sensible URLs
Change-Id: I53ed5e9be3b44ed1b62f9762507b0007d53f15b7
Diffstat (limited to 'server/src/com/vaadin/ui/LoginForm.java')
-rw-r--r-- | server/src/com/vaadin/ui/LoginForm.java | 203 |
1 files changed, 84 insertions, 119 deletions
diff --git a/server/src/com/vaadin/ui/LoginForm.java b/server/src/com/vaadin/ui/LoginForm.java index 76cec66f27..b1e4741450 100644 --- a/server/src/com/vaadin/ui/LoginForm.java +++ b/server/src/com/vaadin/ui/LoginForm.java @@ -15,21 +15,18 @@ */ package com.vaadin.ui; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.Serializable; -import java.io.UnsupportedEncodingException; 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.DownloadStream; -import com.vaadin.server.RequestHandler; +import com.vaadin.server.DynamicConnectorResource; import com.vaadin.server.VaadinRequest; import com.vaadin.server.VaadinResponse; -import com.vaadin.server.VaadinServiceSession; +import com.vaadin.server.VaadinService; +import com.vaadin.server.VaadinServletService; import com.vaadin.shared.ApplicationConstants; /** @@ -62,67 +59,56 @@ public class LoginForm extends CustomComponent { private Embedded iframe = new Embedded(); - private ConnectorResource loginPage = new ConnectorResource() { - @Override - public String getFilename() { - return "login"; + @Override + public boolean handleConnectorRequest(VaadinRequest request, + VaadinResponse response, String path) throws IOException { + String method = VaadinServletService.getCurrentServletRequest() + .getMethod(); + if (!path.equals("login")) { + return super.handleConnectorRequest(request, response, path); } - - @Override - public DownloadStream getStream() { - byte[] loginHTML = getLoginHTML(); - DownloadStream downloadStream = new DownloadStream( - new ByteArrayInputStream(loginHTML), getMIMEType(), - getFilename()); - downloadStream.setBufferSize(loginHTML.length); - downloadStream.setCacheTime(-1); - return downloadStream; + String responseString = null; + if (method.equalsIgnoreCase("post")) { + responseString = handleLogin(request); + } else { + responseString = getLoginHTML(); } - @Override - public String getMIMEType() { - return "text/html; charset=utf-8"; - } - }; - - private final RequestHandler requestHandler = new RequestHandler() { - @Override - public boolean handleRequest(VaadinServiceSession session, - VaadinRequest request, VaadinResponse response) - throws IOException { - String requestPathInfo = request.getRequestPathInfo(); - if ("/loginHandler".equals(requestPathInfo)) { - // Ensure UI.getCurrent() works in listeners - UI.setCurrent(getUI()); - - response.setCacheTime(-1); - response.setContentType("text/html; charset=utf-8"); - response.getWriter() - .write("<html><body>Login form handled." - + "<script type='text/javascript'>parent.parent.vaadin.forceSync();" - + "</script></body></html>"); - - Map<String, String[]> parameters = request.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 true; - } + if (responseString != null) { + response.setContentType("text/html; charset=utf-8"); + response.setCacheTime(-1); + response.getWriter().write(responseString); + 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(); + iframe.setSource(new DynamicConnectorResource(this, "login")); setSizeFull(); setCompositionRoot(iframe); addStyleName("v-loginform"); @@ -135,67 +121,46 @@ public class LoginForm extends CustomComponent { * * @return byte array containing login page html */ - protected byte[] getLoginHTML() { - String appUri = getSession().getURL().toString(); - - try { - return ("<!DOCTYPE html>\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();};" - + "" - + "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' 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>") - .getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("UTF-8 encoding not avalable", e); - } - } - - @Override - public void attach() { - super.attach(); - getSession().addRequestHandler(requestHandler); - iframe.setSource(loginPage); - } - - @Override - public void detach() { - getSession().removeRequestHandler(requestHandler); - - super.detach(); + 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' 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>"; } /** |