From d1617b5aecf072a54b4fdb0e04991aeee9876667 Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Wed, 6 Jun 2007 12:15:28 +0000 Subject: [PATCH] still cleaning json svn changeset:1621/svn branch:trunk --- .../terminal/web/AjaxApplicationManager.java | 153 ++++++++++-------- .../terminal/web/AjaxJsonPaintTarget.java | 66 ++++---- 2 files changed, 113 insertions(+), 106 deletions(-) diff --git a/src/com/itmill/toolkit/terminal/web/AjaxApplicationManager.java b/src/com/itmill/toolkit/terminal/web/AjaxApplicationManager.java index 11b2ea7c16..8e80913e80 100644 --- a/src/com/itmill/toolkit/terminal/web/AjaxApplicationManager.java +++ b/src/com/itmill/toolkit/terminal/web/AjaxApplicationManager.java @@ -28,6 +28,7 @@ package com.itmill.toolkit.terminal.web; +import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -159,6 +160,11 @@ public class AjaxApplicationManager implements || request.getSession().isNew(); OutputStream out = response.getOutputStream(); + PrintWriter outWriter = new PrintWriter(new BufferedWriter( + new OutputStreamWriter(out, "UTF-8"))); + + outWriter.print(")/*{"); // some dirt to prevent cross site scripting vulnerabilities + try { // Is this a download request from application @@ -201,33 +207,12 @@ public class AjaxApplicationManager implements if (window == null) return; - if(isJson) { - // Sets the response type - response.setContentType("application/json; charset=UTF-8"); - paintTarget = new AjaxJsonPaintTarget(getVariableMap(), - this, out); - } else { - response.setContentType("application/xml; charset=UTF-8"); - paintTarget = new AjaxXmlPaintTarget(getVariableMap(), - this, out); - } - - // Render the removed windows - Set removed = new HashSet(getRemovedWindows()); - if (removed.size() > 0) { - for (Iterator i = removed.iterator(); i.hasNext();) { - Window w = (Window) i.next(); - paintTarget.startTag("change"); - paintTarget.addAttribute("format", "uidl"); - String pid = getPaintableId(w); - paintTarget.addAttribute("pid", pid); - paintTarget.addAttribute("windowname", w.getName()); - paintTarget.addAttribute("visible", false); - paintTarget.endTag("change"); - removedWindowNotified(w); - - } - } + // Sets the response type + response.setContentType("application/json; charset=UTF-8"); + outWriter.print("\"changes\":["); + + paintTarget = new AjaxJsonPaintTarget(getVariableMap(), + this, outWriter); // Paints components Set paintables; @@ -321,56 +306,82 @@ public class AjaxApplicationManager implements } } + ((AjaxPaintTarget) paintTarget).close(); + outWriter.print("]"); // close changes + + + // Render the removed windows + // TODO refactor commented area to send some meta instructions to close window +// Set removed = new HashSet(getRemovedWindows()); +// if (removed.size() > 0) { +// for (Iterator i = removed.iterator(); i.hasNext();) { +// Window w = (Window) i.next(); +// paintTarget.startTag("change"); +// paintTarget.addAttribute("format", "uidl"); +// String pid = getPaintableId(w); +// paintTarget.addAttribute("pid", pid); +// paintTarget.addAttribute("windowname", w.getName()); +// paintTarget.addAttribute("visible", false); +// paintTarget.endTag("change"); +// removedWindowNotified(w); +// +// } +// } + + + + outWriter.print(", meta : {"); + boolean metaOpen = false; + + + // .. or initializion (first uidl-request) + if(application.ajaxInit()) { + outWriter.print("\"appInit\":true"); + } // add meta instruction for client to set focus if it is set Paintable f = (Paintable) application.consumeFocus(); - // .. or initializion (first uidl-request) - boolean init = application.ajaxInit(); - if(init || f != null) { - paintTarget.startTag("meta"); - if(init) - paintTarget.addAttribute("appInit", true); - if(f != null) { - paintTarget.startTag("focus"); - paintTarget.addAttribute("pid", getPaintableId(f)); - paintTarget.endTag("focus"); - } - paintTarget.endTag("meta"); + if(f != null) { + if(metaOpen) + outWriter.append(","); + outWriter.write("\"focus\":\""+ getPaintableId(f) +"\""); } + outWriter.print("}"); // Precache custom layouts + // TODO Rewrite this to print to outWriter a custom json attribute. Don't use paintTarget !! // TODO Does not support theme-get param or different themes in different windows -> Allways preload layouts with the theme specified by the applications - String themeName = application.getTheme() != null ? application.getTheme() : ApplicationServlet.DEFAULT_THEME; - // TODO We should only precache the layouts that are not cached already - for (Iterator i=((AjaxPaintTarget) paintTarget).getPreCachedResources().iterator(); i.hasNext();) { - String resource = (String) i.next(); - InputStream is = null; - try { - is = themeSource.getResource(themeName + "/" + resource); - } catch (ThemeSource.ThemeException e) { - Log.info(e.getMessage()); - } - if (is != null) { - paintTarget.startTag("precache"); - paintTarget.addAttribute("resource", resource); - StringBuffer layout = new StringBuffer(); - - try { - InputStreamReader r = new InputStreamReader(is); - char[] buffer = new char[20000]; - int charsRead = 0; - while ((charsRead = r.read(buffer)) > 0) - layout.append(buffer, 0, charsRead); - r.close(); - } catch (java.io.IOException e) { - Log.info("Resource transfer failed: " + request.getRequestURI() - + ". (" + e.getMessage() + ")"); - } - paintTarget.addCharacterData(layout.toString()); - paintTarget.endTag("precache"); - } - } - - ((AjaxPaintTarget) paintTarget).close(); +// String themeName = application.getTheme() != null ? application.getTheme() : ApplicationServlet.DEFAULT_THEME; +// // TODO We should only precache the layouts that are not cached already +// for (Iterator i=((AjaxPaintTarget) paintTarget).getPreCachedResources().iterator(); i.hasNext();) { +// String resource = (String) i.next(); +// InputStream is = null; +// try { +// is = themeSource.getResource(themeName + "/" + resource); +// } catch (ThemeSource.ThemeException e) { +// Log.info(e.getMessage()); +// } +// if (is != null) { +// paintTarget.startTag("precache"); +// paintTarget.addAttribute("resource", resource); +// StringBuffer layout = new StringBuffer(); +// +// try { +// InputStreamReader r = new InputStreamReader(is); +// char[] buffer = new char[20000]; +// int charsRead = 0; +// while ((charsRead = r.read(buffer)) > 0) +// layout.append(buffer, 0, charsRead); +// r.close(); +// } catch (java.io.IOException e) { +// Log.info("Resource transfer failed: " + request.getRequestURI() +// + ". (" + e.getMessage() + ")"); +// } +// paintTarget.addCharacterData(layout.toString()); +// paintTarget.endTag("precache"); +// } +// } + outWriter.flush(); + outWriter.close(); out.flush(); } else { diff --git a/src/com/itmill/toolkit/terminal/web/AjaxJsonPaintTarget.java b/src/com/itmill/toolkit/terminal/web/AjaxJsonPaintTarget.java index c58b644a22..28fd82b77f 100644 --- a/src/com/itmill/toolkit/terminal/web/AjaxJsonPaintTarget.java +++ b/src/com/itmill/toolkit/terminal/web/AjaxJsonPaintTarget.java @@ -100,13 +100,13 @@ public class AjaxJsonPaintTarget implements PaintTarget, AjaxPaintTarget { * * @param variableMap * @param manager - * @param output + * @param outWriter * A character-output stream. * @throws PaintException * if the paint operation failed. */ public AjaxJsonPaintTarget(AjaxVariableMap variableMap, - AjaxApplicationManager manager, OutputStream output) + AjaxApplicationManager manager, PrintWriter outWriter) throws PaintException { this.manager = manager; @@ -115,12 +115,7 @@ public class AjaxJsonPaintTarget implements PaintTarget, AjaxPaintTarget { // Sets the target for UIDL writing - try { - this.uidlBuffer = new PrintWriter(new BufferedWriter( - new OutputStreamWriter(output, "UTF-8"))); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("Internal error"); - } + this.uidlBuffer = outWriter; // Initialize tag-writing mOpenTags = new Stack(); @@ -130,7 +125,6 @@ public class AjaxJsonPaintTarget implements PaintTarget, AjaxPaintTarget { // Adds document declaration // Adds UIDL start tag and its attributes - tag = new JsonTag("changes"); } public void startTag(String tagName) throws PaintException { @@ -166,15 +160,15 @@ public class AjaxJsonPaintTarget implements PaintTarget, AjaxPaintTarget { if (this.closed) throw new PaintException( "Attempted to write to a closed PaintTarget."); - - tagName = tag.postfixChildtag(tagName, true); + if(tag != null) { + openJsonTags.push(tag); + } + // Checks tagName and attributes here mOpenTags.push(tagName); - openJsonTags.push(tag); tag = new JsonTag(tagName); - mTagArgumentListOpen = true; @@ -203,20 +197,25 @@ public class AjaxJsonPaintTarget implements PaintTarget, AjaxPaintTarget { throw new PaintException( "Attempted to write to a closed PaintTarget."); - JsonTag parent = (JsonTag) openJsonTags.pop(); - if(parent != null) - tagName = parent.postfixChildtag(tagName, false); - - String lastTag = ""; - - lastTag = (String) mOpenTags.pop(); - if (!tagName.equalsIgnoreCase(lastTag)) - throw new PaintException("Invalid UIDL: wrong ending tag: '" - + tagName + "' expected: '" + lastTag + "'."); - - parent.addData(tag.getJSON()); - - tag = parent; + if(openJsonTags.size() > 0) { + JsonTag parent = (JsonTag) openJsonTags.pop(); + if(parent != null) + tagName = parent.postfixChildtag(tagName, false); + + String lastTag = ""; + + lastTag = (String) mOpenTags.pop(); + if (!tagName.equalsIgnoreCase(lastTag)) + throw new PaintException("Invalid UIDL: wrong ending tag: '" + + tagName + "' expected: '" + lastTag + "'."); + + parent.addData(tag.getJSON()); + + tag = parent; + } else { + this.uidlBuffer.print(tag.getJSON()); + tag = null; + } } /** @@ -648,13 +647,10 @@ public class AjaxJsonPaintTarget implements PaintTarget, AjaxPaintTarget { * if the paint operation failed. */ public void close() throws PaintException { - if (!this.closed) { + if(tag != null) uidlBuffer.append(tag.getJSON()); - flush(); - // Close all - this.uidlBuffer.close(); - this.closed = true; - } + flush(); + this.closed = true; } /** @@ -949,12 +945,12 @@ public class AjaxJsonPaintTarget implements PaintTarget, AjaxPaintTarget { } public String getJsonPresentation() { - String pres = "\""+name +"\":["; + String pres = "\""+name +"\":[\""; for (int i = 0; i < value.length;) { pres += value[i]; i++; if(i < value.length) - pres += ","; + pres += "\","; } pres += "]"; return pres; -- 2.39.5