From 615cae3800434ba4bab12f7732f3a357d47b8e78 Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Wed, 30 May 2007 12:11:02 +0000 Subject: [PATCH] fixes #756, #757 svn changeset:1531/svn branch:trunk --- .../terminal/web/ApplicationServlet.java | 169 ++++++++++++------ 1 file changed, 117 insertions(+), 52 deletions(-) diff --git a/src/com/itmill/toolkit/terminal/web/ApplicationServlet.java b/src/com/itmill/toolkit/terminal/web/ApplicationServlet.java index 38e25ba7e1..8aa0960ace 100644 --- a/src/com/itmill/toolkit/terminal/web/ApplicationServlet.java +++ b/src/com/itmill/toolkit/terminal/web/ApplicationServlet.java @@ -78,6 +78,7 @@ import com.itmill.toolkit.service.License.LicenseViolation; import com.itmill.toolkit.terminal.DownloadStream; import com.itmill.toolkit.terminal.Paintable; import com.itmill.toolkit.terminal.ParameterHandler; +import com.itmill.toolkit.terminal.StreamResource; import com.itmill.toolkit.terminal.ThemeResource; import com.itmill.toolkit.terminal.URIHandler; import com.itmill.toolkit.terminal.Paintable.RepaintRequestEvent; @@ -876,59 +877,28 @@ public class ApplicationServlet extends HttpServlet implements Theme theme) throws IOException, MalformedURLException { response.setContentType("text/html"); BufferedWriter page = new BufferedWriter(new OutputStreamWriter(out)); - + page .write("\n"); - + page .write("\n" + window.getCaption() + "\n"); page .write("\n"); - Theme t = theme; - Vector themes = new Vector(); - themes.add(t); - while (t.getParent() != null) { - String parentName = t.getParent(); - t = themeSource.getThemeByName(parentName); - themes.add(t); - } - for (int k = themes.size() - 1; k >= 0; k--) { - t = (Theme) themes.get(k); - Collection files = t.getFileNames(terminalType, Theme.MODE_AJAX); - for (Iterator i = files.iterator(); i.hasNext();) { - String file = (String) i.next(); - if (file.endsWith(".css")) - page.write("\n"); - else if (file.endsWith(".js")) { - page.write("\n"); - } - } - } + page.write("\n"); + page.write(""); page.write("\n"); - + page.write("
Loading...
\n"); - + page.write("
\n"); - + page.write("\n"); - + page.write("\n"); page.close(); } + /** + * Writes javascript for this theme and terminal + * + * @param response + * the HTTP response to write to. + * @param terminalType + * @param theme + * @throws IOException + * if the writing failed due to input/output error. + */ + private void writeJavascript(HttpServletResponse response, + WebBrowser terminalType, Theme theme) throws IOException, MalformedURLException { + response.setContentType("text/javascript"); + BufferedWriter page = new BufferedWriter(new OutputStreamWriter(response.getOutputStream())); + getThemeResourcesWithType(".js",terminalType, theme, page); + page.close(); + } + + /** + * Writes javascript for this theme and terminal + * + * @param response + * the HTTP response to write to. + * @param terminalType + * @param theme + * @throws IOException + * if the writing failed due to input/output error. + */ + private void writeCss(HttpServletResponse response, WebBrowser terminalType, + Theme theme) throws IOException, MalformedURLException { + response.setContentType("text/css"); + BufferedWriter page = new BufferedWriter(new OutputStreamWriter(response.getOutputStream())); + getThemeResourcesWithType(".css",terminalType, theme, page); + page.close(); + } + + /** + * Catenates all themes required files that which ends with type into a StringBuffer. + * Used to serve browser only a single javascript or css file + * + * @param terminalType + * @param theme + * @throws IOException + * if the writing failed due to input/output error. + * @return StringBuffer containing all themes javascript + */ + private void getThemeResourcesWithType(String type, WebBrowser terminalType, + Theme theme, BufferedWriter out) throws IOException { + Vector themes = new Vector(); + themes.add(theme); + while (theme.getParent() != null) { + String parentName = theme.getParent(); + theme = themeSource.getThemeByName(parentName); + themes.add(theme); + } + + for (int k = themes.size() - 1; k >= 0; k--) { + theme = (Theme) themes.get(k); + Collection files = theme.getFileNames(terminalType, Theme.MODE_AJAX); + for (Iterator i = files.iterator(); i.hasNext();) { + String file = (String) i.next(); + if (file.endsWith(type)) { + try { + InputStreamReader in = new InputStreamReader(themeSource.getResource(theme.getName() + "/" + file)); + + char[] b = new char[DEFAULT_BUFFER_SIZE]; + int read = 0; + while((read = in.read(b, 0, DEFAULT_BUFFER_SIZE)) > 0) + out.write(b,0,read); + } catch (ThemeException e) { + e.printStackTrace(); + } + } + } + } + } + + /** * Handles the requested URI. An application can add handlers to do special * processing, when a certain URI is requested. The handlers are invoked @@ -1191,6 +1235,27 @@ public class ApplicationServlet extends HttpServlet implements // Checks if this really is a resource request if (resourceId == null || !resourceId.startsWith(RESOURCE_URI)) return false; + + if (resourceId.endsWith("compiledstyle.css") || resourceId.endsWith("compiledjavascript.js")) { + String[] parts = resourceId.split("/"); + + Theme t = themeSource.getThemeByName(parts[2]); + try { + if(resourceId.endsWith("compiledstyle.css")) { + writeCss(response, WebBrowserProbe.getTerminalType(request + .getSession()), t); + } else { + writeJavascript(response, WebBrowserProbe.getTerminalType(request + .getSession()), t); + } + } catch (IOException e) { + e.printStackTrace(); + } + return true; + } + if(resourceId.endsWith("compiledjavascript.js")) { + return true; + } // Checks the resource type resourceId = resourceId.substring(RESOURCE_URI.length()); -- 2.39.5