From 1ea209f48c861b4e8422b8e739d75f3367d95df6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Fri, 10 Aug 2012 16:01:09 +0300 Subject: [PATCH] Simplify BootstrapHandler now when extensibility isn't needed (#9274) --- .../terminal/gwt/server/BootstrapHandler.java | 281 ++++++------------ 1 file changed, 93 insertions(+), 188 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/server/BootstrapHandler.java b/src/com/vaadin/terminal/gwt/server/BootstrapHandler.java index 64d0e0bf4b..372ec694e6 100644 --- a/src/com/vaadin/terminal/gwt/server/BootstrapHandler.java +++ b/src/com/vaadin/terminal/gwt/server/BootstrapHandler.java @@ -124,7 +124,29 @@ public abstract class BootstrapHandler implements RequestHandler { } try { - writeBootstrapPage(request, response, application, rootId); + Map headers = new LinkedHashMap(); + BootstrapContext context = createContext(request, response, + application, rootId, headers); + BootstrapResponse bootstrapResponse = context + .getBootstrapResponse(); + + setupMainDiv(context); + + DeploymentConfiguration deploymentConfiguration = request + .getDeploymentConfiguration(); + boolean standalone = deploymentConfiguration.isStandalone(request); + if (standalone) { + setupStandaloneDocument(context); + deploymentConfiguration.getVaadinContext() + .fireModifyBootstrapEvent(bootstrapResponse); + sendBootstrapHeaders(response, headers); + writeBootstrapPage(response, bootstrapResponse.getDocument()); + } else { + deploymentConfiguration.getVaadinContext() + .fireModifyBootstrapEvent(bootstrapResponse); + writeBootstrapPage(response, + bootstrapResponse.getApplicationTag()); + } } catch (JSONException e) { writeError(response, e); } @@ -132,56 +154,78 @@ public abstract class BootstrapHandler implements RequestHandler { return true; } - protected final void writeBootstrapPage(WrappedRequest request, - WrappedResponse response, Application application, Integer rootId) - throws IOException, JSONException { + private void sendBootstrapHeaders(WrappedResponse response, + Map headers) { + Set> entrySet = headers.entrySet(); + for (Entry header : entrySet) { + Object value = header.getValue(); + if (value instanceof String) { + response.setHeader(header.getKey(), (String) value); + } else if (value instanceof Long) { + response.setDateHeader(header.getKey(), + ((Long) value).longValue()); + } else { + throw new RuntimeException("Unsupported header value: " + value); + } + } + } - Map headers = new LinkedHashMap(); - BootstrapContext context = createContext(request, response, - application, rootId, headers); + private void writeBootstrapPage(WrappedResponse response, Element dom) + throws IOException { + response.setContentType("text/html"); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( + response.getOutputStream(), "UTF-8")); + writer.append(dom.outerHtml()); + writer.close(); + } - DeploymentConfiguration deploymentConfiguration = request - .getDeploymentConfiguration(); + private void setupStandaloneDocument(BootstrapContext context) { + BootstrapResponse response = context.getBootstrapResponse(); + response.setHeader("Cache-Control", "no-cache"); + response.setHeader("Pragma", "no-cache"); + response.setDateHeader("Expires", 0); - boolean standalone = deploymentConfiguration.isStandalone(request); - if (standalone) { - setBootstrapPageHeaders(context); - setBasicHtml(context); - setBootstrapPageHtmlHeader(context); - setBodyTag(context); - } + Document document = response.getDocument(); - setBootstrapPageHtmlVaadinScripts(context); + DocumentType doctype = new DocumentType("html", + "-//W3C//DTD XHTML 1.0 Transitional//EN", + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", + document.baseUri()); + document.child(0).before(doctype); + document.body().parent().attr("xmlns", "http://www.w3.org/1999/xhtml"); - setMainDiv(context); + Element head = document.head(); + head.appendElement("meta").attr("http-equiv", "Content-Type") + .attr("content", "text/html; charset=utf-8"); - request.getDeploymentConfiguration().getVaadinContext() - .fireModifyBootstrapEvent(context.getBootstrapResponse()); + // Chrome frame in all versions of IE (only if Chrome frame is + // installed) + head.appendElement("meta").attr("http-equiv", "X-UA-Compatible") + .attr("content", "chrome=1"); - response.setContentType("text/html"); - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( - response.getOutputStream(), "UTF-8")); - if (standalone) { - Set> entrySet = headers.entrySet(); - for (Entry header : entrySet) { - Object value = header.getValue(); - if (value instanceof String) { - response.setHeader(header.getKey(), (String) value); - } else if (value instanceof Long) { - response.setDateHeader(header.getKey(), - ((Long) value).longValue()); - } else { - throw new RuntimeException("Unsupported header value: " - + value); - } - } - writer.append(context.getBootstrapResponse().getDocument() - .outerHtml()); - } else { - writer.append(context.getBootstrapResponse().getApplicationTag() - .outerHtml()); + Root root = context.getRoot(); + String title = ((root == null || root.getCaption() == null) ? "" : root + .getCaption()); + head.appendElement("title").appendText(title); + + head.appendElement("style").attr("type", "text/css") + .appendText("html, body {height:100%;margin:0;}"); + + // Add favicon links + String themeName = context.getThemeName(); + if (themeName != null) { + String themeUri = getThemeUri(context, themeName); + head.appendElement("link").attr("rel", "shortcut icon") + .attr("type", "image/vnd.microsoft.icon") + .attr("href", themeUri + "/favicon.ico"); + head.appendElement("link").attr("rel", "icon") + .attr("type", "image/vnd.microsoft.icon") + .attr("href", themeUri + "/favicon.ico"); } - writer.close(); + + Element body = document.body(); + body.attr("scroll", "auto"); + body.addClass(ApplicationConnection.GENERATED_BODY_CLASSNAME); } public BootstrapContext createContext(WrappedRequest request, @@ -243,8 +287,10 @@ public abstract class BootstrapHandler implements RequestHandler { * @param context * * @throws IOException + * @throws JSONException */ - protected void setMainDiv(BootstrapContext context) throws IOException { + private void setupMainDiv(BootstrapContext context) throws IOException, + JSONException { String style = getMainDivStyle(context); /*- Add classnames; @@ -256,7 +302,7 @@ public abstract class BootstrapHandler implements RequestHandler { */ String appClass = "v-app-" - + getApplicationCSSClassName(context.getApplication()); + + context.getApplication().getClass().getSimpleName(); String classNames = "v-app " + appClass; @@ -269,60 +315,9 @@ public abstract class BootstrapHandler implements RequestHandler { mainDiv.attr("style", style); } mainDiv.appendElement("div").addClass("v-app-loading"); - mainDiv.appendElement("noscript").append(getNoScriptMessage()); - } - - /** - * Returns a message printed for browsers without scripting support or if - * browsers scripting support is disabled. - */ - protected String getNoScriptMessage() { - return "You have to enable javascript in your browser to use an application built with Vaadin."; - } - - /** - * Returns the application class identifier for use in the application CSS - * class name in the root DIV. The application CSS class name is of form - * "v-app-"+getApplicationCSSClassName(). - * - * This method should normally not be overridden. - * - * @return The CSS class name to use in combination with "v-app-". - */ - protected String getApplicationCSSClassName(Application application) { - return application.getClass().getSimpleName(); - } - - /** - * - * Method to open the body tag of the html kickstart page. - *

- * This method is responsible for closing the head tag and opening the body - * tag. - *

- * Override this method if you want to add some custom html to the page. - * - * @throws IOException - */ - protected void setBodyTag(BootstrapContext context) throws IOException { - Element body = context.getBootstrapResponse().getDocument().body(); - body.attr("scroll", "auto"); - body.addClass(ApplicationConnection.GENERATED_BODY_CLASSNAME); - } + mainDiv.appendElement("noscript") + .append("You have to enable javascript in your browser to use an application built with Vaadin."); - /** - * Method to write the script part of the page which loads needed Vaadin - * scripts and themes. - *

- * Override this method if you want to add some custom html around scripts. - * - * @param context - * - * @throws IOException - * @throws JSONException - */ - protected void setBootstrapPageHtmlVaadinScripts(BootstrapContext context) - throws IOException, JSONException { WrappedRequest request = context.getRequest(); DeploymentConfiguration deploymentConfiguration = request @@ -330,9 +325,6 @@ public abstract class BootstrapHandler implements RequestHandler { String staticFileLocation = deploymentConfiguration .getStaticFileLocation(request); - Element applicationTag = context.getBootstrapResponse() - .getApplicationTag(); - applicationTag .appendElement("iframe") .attr("tabIndex", "-1") @@ -481,93 +473,6 @@ public abstract class BootstrapHandler implements RequestHandler { protected abstract String getAppUri(BootstrapContext context); - /** - * Method to write the contents of head element in html kickstart page. - *

- * Override this method if you want to add some custom html to the header of - * the page. - * - * @throws IOException - */ - protected void setBootstrapPageHtmlHeader(BootstrapContext context) - throws IOException { - String themeName = context.getThemeName(); - Element head = context.getBootstrapResponse().getDocument().head(); - head.appendElement("meta").attr("http-equiv", "Content-Type") - .attr("content", "text/html; charset=utf-8"); - - // Chrome frame in all versions of IE (only if Chrome frame is - // installed) - head.appendElement("meta").attr("http-equiv", "X-UA-Compatible") - .attr("content", "chrome=1"); - - head.appendElement("style").attr("type", "text/css") - .appendText("html, body {height:100%;margin:0;}"); - - // Add favicon links - if (themeName != null) { - String themeUri = getThemeUri(context, themeName); - head.appendElement("link").attr("rel", "shortcut icon") - .attr("type", "image/vnd.microsoft.icon") - .attr("href", themeUri + "/favicon.ico"); - head.appendElement("link").attr("rel", "icon") - .attr("type", "image/vnd.microsoft.icon") - .attr("href", themeUri + "/favicon.ico"); - } - - Root root = context.getRoot(); - String title = ((root == null || root.getCaption() == null) ? "" : root - .getCaption()); - - head.appendElement("title").appendText(title); - } - - /** - * Method to set http request headers for the Vaadin kickstart page. - *

- * Override this method if you need to customize http headers of the page. - * - * @param context - */ - protected void setBootstrapPageHeaders(BootstrapContext context) { - WrappedResponse response = context.getResponse(); - - // Window renders are not cacheable - response.setHeader("Cache-Control", "no-cache"); - response.setHeader("Pragma", "no-cache"); - response.setDateHeader("Expires", 0); - response.setContentType("text/html; charset=UTF-8"); - } - - /** - * Method to write the beginning of the html page. - *

- * This method is responsible for writing appropriate doc type declarations - * and to open html and head tags. - *

- * Override this method if you want to add some custom html to the very - * beginning of the page. - * - * @param context - * @throws IOException - */ - protected void setBasicHtml(BootstrapContext context) throws IOException { - - // write html header - // page.write("\n"); - Document document = context.getBootstrapResponse().getDocument(); - DocumentType doctype = new DocumentType("html", - "-//W3C//DTD XHTML 1.0 Transitional//EN", - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", - document.baseUri()); - document.child(0).before(doctype); - - document.body().parent().attr("xmlns", "http://www.w3.org/1999/xhtml"); - } - /** * Get the URI for the application theme. * -- 2.39.5