From 48c19777e566dd8cd5e496ea364de8aea447abbf Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Mon, 8 Oct 2012 13:17:07 +0300 Subject: 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 --- server/src/com/vaadin/ui/LoginForm.java | 203 +++++++++++++------------------- 1 file changed, 84 insertions(+), 119 deletions(-) (limited to 'server/src/com/vaadin/ui/LoginForm.java') 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("Login form handled." - + ""); - - Map parameters = request.getParameterMap(); - - HashMap params = new HashMap(); - // expecting single params - for (Iterator 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 parameters = VaadinService.getCurrentRequest() + .getParameterMap(); + + HashMap params = new HashMap(); + // expecting single params + for (Iterator 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 "Login form handled." + + ""; + } 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 ("\n" + "" - + "" - + "" - + "
" - + "" - + "
" - + "
" - + usernameCaption - + "
" - + "
" - + "
" - + passwordCaption - + "
" - + "
" - + "
" - + loginButtonCaption - + "
" + "") - .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 "\n" + + "" + + "" + + "" + + "
" + + "" + + "
" + + "
" + + usernameCaption + + "
" + + "
" + + "
" + + passwordCaption + + "
" + + "
" + + "
" + + loginButtonCaption + + "
" + ""; } /** -- cgit v1.2.3