diff options
author | Leif Åstrand <leif@vaadin.com> | 2011-12-14 17:24:10 +0200 |
---|---|---|
committer | Leif Åstrand <leif@vaadin.com> | 2011-12-14 17:24:10 +0200 |
commit | 453a65cbc49eaa45f3a812c67f0c34987f0a61b5 (patch) | |
tree | af91dbd91699a55d323c55cdb2034b9a7a82f9b3 /src/com | |
parent | 5c481e25354e23b56fcaa158c14774dc57827b2b (diff) | |
download | vaadin-framework-453a65cbc49eaa45f3a812c67f0c34987f0a61b5.tar.gz vaadin-framework-453a65cbc49eaa45f3a812c67f0c34987f0a61b5.zip |
Move the initialization of AjaxPageHandler to avoid null pointers
Diffstat (limited to 'src/com')
3 files changed, 160 insertions, 150 deletions
diff --git a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java index ec7d8eeb53..0651c79446 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java @@ -1910,6 +1910,8 @@ public abstract class AbstractCommunicationManager implements private final HashMap<Class<? extends Paintable>, Integer> typeToKey = new HashMap<Class<? extends Paintable>, Integer>(); private int nextTypeKey = 0; + private AjaxPageHandler ajaxPageHandler; + String getTagForType(Class<? extends Paintable> class1) { Integer object = typeToKey.get(class1); if (object == null) { @@ -1955,7 +1957,15 @@ public abstract class AbstractCommunicationManager implements * * @return the ajax page handler to use */ - protected abstract AjaxPageHandler getAjaxPageHandler(); + private AjaxPageHandler getAjaxPageHandler() { + if (ajaxPageHandler == null) { + ajaxPageHandler = createAjaxPageHandler(); + } + + return ajaxPageHandler; + } + + protected abstract AjaxPageHandler createAjaxPageHandler(); protected boolean handleApplicationRequest(WrappedRequest request, WrappedResponse response) throws IOException { @@ -1996,7 +2006,7 @@ public abstract class AbstractCommunicationManager implements root.getRootId()); if (sendUIDL) { // TODO maybe unify w/ AjaxPageHandler & writeUidlResponCe()? - this.makeAllPaintablesDirty(root); + makeAllPaintablesDirty(root); StringWriter sWriter = new StringWriter(); PrintWriter pWriter = new PrintWriter(sWriter); pWriter.print("{"); diff --git a/src/com/vaadin/terminal/gwt/server/CommunicationManager.java b/src/com/vaadin/terminal/gwt/server/CommunicationManager.java index e352d606c1..e660e14a8f 100644 --- a/src/com/vaadin/terminal/gwt/server/CommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/CommunicationManager.java @@ -34,57 +34,6 @@ import com.vaadin.ui.Component; @SuppressWarnings("serial") public class CommunicationManager extends AbstractCommunicationManager { - private final AjaxPageHandler ajaxPageHandler = new AjaxPageHandler() { - @Override - protected String getApplicationId(AjaxPageContext context) { - String appUrl = getAppUri(context); - - String appId = appUrl; - if ("".equals(appUrl)) { - appId = "ROOT"; - } - appId = appId.replaceAll("[^a-zA-Z0-9]", ""); - // Add hashCode to the end, so that it is still (sort of) - // predictable, but indicates that it should not be used in CSS and - // such: - int hashCode = appId.hashCode(); - if (hashCode < 0) { - hashCode = -hashCode; - } - appId = appId + "-" + hashCode; - return appId; - } - - @Override - protected String getAppUri(AjaxPageContext context) { - /* Fetch relative url to application */ - // don't use server and port in uri. It may cause problems with some - // virtual server configurations which lose the server name - Application application = context.getApplication(); - URL url = application.getURL(); - String appUrl = url.getPath(); - if (appUrl.endsWith("/")) { - appUrl = appUrl.substring(0, appUrl.length() - 1); - } - return appUrl; - } - - @Override - public String getThemeName(AjaxPageContext context) { - String themeName = context.getRequest().getParameter( - AbstractApplicationServlet.URL_PARAMETER_THEME); - if (themeName == null) { - themeName = super.getThemeName(context); - } - return themeName; - } - - @Override - protected AbstractCommunicationManager getCommunicationManager() { - return CommunicationManager.this; - } - }; - /** * @deprecated use {@link #CommunicationManager(Application)} instead * @param application @@ -232,7 +181,58 @@ public class CommunicationManager extends AbstractCommunicationManager { } @Override - protected AjaxPageHandler getAjaxPageHandler() { - return ajaxPageHandler; + protected AjaxPageHandler createAjaxPageHandler() { + return new AjaxPageHandler() { + @Override + protected String getApplicationId(AjaxPageContext context) { + String appUrl = getAppUri(context); + + String appId = appUrl; + if ("".equals(appUrl)) { + appId = "ROOT"; + } + appId = appId.replaceAll("[^a-zA-Z0-9]", ""); + // Add hashCode to the end, so that it is still (sort of) + // predictable, but indicates that it should not be used in CSS + // and + // such: + int hashCode = appId.hashCode(); + if (hashCode < 0) { + hashCode = -hashCode; + } + appId = appId + "-" + hashCode; + return appId; + } + + @Override + protected String getAppUri(AjaxPageContext context) { + /* Fetch relative url to application */ + // don't use server and port in uri. It may cause problems with + // some + // virtual server configurations which lose the server name + Application application = context.getApplication(); + URL url = application.getURL(); + String appUrl = url.getPath(); + if (appUrl.endsWith("/")) { + appUrl = appUrl.substring(0, appUrl.length() - 1); + } + return appUrl; + } + + @Override + public String getThemeName(AjaxPageContext context) { + String themeName = context.getRequest().getParameter( + AbstractApplicationServlet.URL_PARAMETER_THEME); + if (themeName == null) { + themeName = super.getThemeName(context); + } + return themeName; + } + + @Override + protected AbstractCommunicationManager getCommunicationManager() { + return CommunicationManager.this; + } + }; } } diff --git a/src/com/vaadin/terminal/gwt/server/PortletCommunicationManager.java b/src/com/vaadin/terminal/gwt/server/PortletCommunicationManager.java index d1398c14e1..f6fb1ac43a 100644 --- a/src/com/vaadin/terminal/gwt/server/PortletCommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/PortletCommunicationManager.java @@ -35,99 +35,6 @@ import com.vaadin.ui.Root; @SuppressWarnings("serial") public class PortletCommunicationManager extends AbstractCommunicationManager { - private final AjaxPageHandler ajaxPageHandler = new AjaxPageHandler() { - - @Override - public boolean handleRequest(Application application, - WrappedRequest request, WrappedResponse response) - throws IOException { - PortletRequest portletRequest = WrappedPortletRequest.cast(request) - .getPortletRequest(); - if (portletRequest instanceof RenderRequest) { - return super.handleRequest(application, request, response); - } else { - return false; - } - } - - @Override - protected String getApplicationId(AjaxPageContext context) { - PortletRequest portletRequest = WrappedPortletRequest.cast( - context.getRequest()).getPortletRequest(); - /* - * We need to generate a unique ID because some portals already - * create a DIV with the portlet's Window ID as the DOM ID. - */ - return "v-" + portletRequest.getWindowID(); - } - - @Override - protected String getAppUri(AjaxPageContext context) { - return getRenderResponse(context).createActionURL().toString(); - } - - private RenderResponse getRenderResponse(AjaxPageContext context) { - PortletResponse response = ((WrappedPortletResponse) context - .getResponse()).getPortletResponse(); - - RenderResponse renderResponse = (RenderResponse) response; - return renderResponse; - } - - @Override - protected JSONObject getDefaultParameters(AjaxPageContext context) - throws JSONException { - /* - * We need this in order to get uploads to work. TODO this is not - * needed for uploads anymore, check if this is needed for some - * other things - */ - JSONObject defaults = super.getDefaultParameters(context); - defaults.put("usePortletURLs", true); - - ResourceURL uidlUrlBase = getRenderResponse(context) - .createResourceURL(); - uidlUrlBase.setResourceID("UIDL"); - defaults.put("portletUidlURLBase", uidlUrlBase.toString()); - defaults.put("pathInfo", ""); - - return defaults; - } - - @Override - protected void writeMainScriptTagContents(AjaxPageContext context) - throws JSONException, IOException { - // fixed base theme to use - all portal pages with Vaadin - // applications will load this exactly once - String portalTheme = WrappedPortletRequest.cast( - context.getRequest()).getPortalProperty( - AbstractApplicationPortlet.PORTAL_PARAMETER_VAADIN_THEME); - if (portalTheme != null - && !portalTheme.equals(context.getThemeName())) { - String portalThemeUri = getThemeUri(context, portalTheme); - // XSS safe - originates from portal properties - context.getWriter().write( - "vaadin.loadTheme('" + portalThemeUri + "')"); - } - - super.writeMainScriptTagContents(context); - } - - @Override - protected String getMainDivStyle(AjaxPageContext context) { - DeploymentConfiguration deploymentConfiguration = context - .getRequest().getDeploymentConfiguration(); - return deploymentConfiguration.getApplicationOrSystemProperty( - AbstractApplicationPortlet.PORTLET_PARAMETER_STYLE, null); - } - - @Override - protected AbstractCommunicationManager getCommunicationManager() { - return PortletCommunicationManager.this; - } - - }; - private transient ResourceResponse currentUidlResponse; public PortletCommunicationManager(Application application) { @@ -207,8 +114,101 @@ public class PortletCommunicationManager extends AbstractCommunicationManager { } @Override - protected AjaxPageHandler getAjaxPageHandler() { - return ajaxPageHandler; + protected AjaxPageHandler createAjaxPageHandler() { + return new AjaxPageHandler() { + @Override + public boolean handleRequest(Application application, + WrappedRequest request, WrappedResponse response) + throws IOException { + PortletRequest portletRequest = WrappedPortletRequest.cast( + request).getPortletRequest(); + if (portletRequest instanceof RenderRequest) { + return super.handleRequest(application, request, response); + } else { + return false; + } + } + + @Override + protected String getApplicationId(AjaxPageContext context) { + PortletRequest portletRequest = WrappedPortletRequest.cast( + context.getRequest()).getPortletRequest(); + /* + * We need to generate a unique ID because some portals already + * create a DIV with the portlet's Window ID as the DOM ID. + */ + return "v-" + portletRequest.getWindowID(); + } + + @Override + protected String getAppUri(AjaxPageContext context) { + return getRenderResponse(context).createActionURL().toString(); + } + + private RenderResponse getRenderResponse(AjaxPageContext context) { + PortletResponse response = ((WrappedPortletResponse) context + .getResponse()).getPortletResponse(); + + RenderResponse renderResponse = (RenderResponse) response; + return renderResponse; + } + + @Override + protected JSONObject getDefaultParameters(AjaxPageContext context) + throws JSONException { + /* + * We need this in order to get uploads to work. TODO this is + * not needed for uploads anymore, check if this is needed for + * some other things + */ + JSONObject defaults = super.getDefaultParameters(context); + defaults.put("usePortletURLs", true); + + ResourceURL uidlUrlBase = getRenderResponse(context) + .createResourceURL(); + uidlUrlBase.setResourceID("UIDL"); + defaults.put("portletUidlURLBase", uidlUrlBase.toString()); + defaults.put("pathInfo", ""); + + return defaults; + } + + @Override + protected void writeMainScriptTagContents(AjaxPageContext context) + throws JSONException, IOException { + // fixed base theme to use - all portal pages with Vaadin + // applications will load this exactly once + String portalTheme = WrappedPortletRequest + .cast(context.getRequest()) + .getPortalProperty( + AbstractApplicationPortlet.PORTAL_PARAMETER_VAADIN_THEME); + if (portalTheme != null + && !portalTheme.equals(context.getThemeName())) { + String portalThemeUri = getThemeUri(context, portalTheme); + // XSS safe - originates from portal properties + context.getWriter().write( + "vaadin.loadTheme('" + portalThemeUri + "')"); + } + + super.writeMainScriptTagContents(context); + } + + @Override + protected String getMainDivStyle(AjaxPageContext context) { + DeploymentConfiguration deploymentConfiguration = context + .getRequest().getDeploymentConfiguration(); + return deploymentConfiguration.getApplicationOrSystemProperty( + AbstractApplicationPortlet.PORTLET_PARAMETER_STYLE, + null); + } + + @Override + protected AbstractCommunicationManager getCommunicationManager() { + return PortletCommunicationManager.this; + } + + }; + } } |