package com.vaadin.server.communication;
-import java.io.BufferedWriter;
import java.io.IOException;
-import java.io.OutputStream;
import java.io.OutputStreamWriter;
-import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
return false;
}
- // NOTE! GateIn requires, for some weird reason, getOutputStream
- // to be used instead of getWriter() (it seems to interpret
- // application/json as a binary content type)
- final OutputStream out = response.getOutputStream();
- final PrintWriter outWriter = new PrintWriter(new BufferedWriter(
- new OutputStreamWriter(out, "UTF-8")));
+ StringWriter stringWriter = new StringWriter();
try {
assert UI.getCurrent() == null;
// Set browser information from the request
session.getBrowser().updateRequestDetails(request);
- response.setContentType("application/json; charset=UTF-8");
-
UI uI = getBrowserDetailsUI(request, session);
session.getCommunicationManager().repaintAll(uI);
String initialUIDL = getInitialUidl(request, uI);
params.put("uidl", initialUIDL);
- outWriter.write(params.toString());
- // NOTE GateIn requires the buffers to be flushed to work
- outWriter.flush();
- out.flush();
+ stringWriter.write(params.toString());
} catch (JSONException e) {
- // TODO PUSH handle
- e.printStackTrace();
+ throw new IOException("Error producing initial UIDL", e);
+ } finally {
+ stringWriter.close();
+ }
+
+ return commitJsonResponse(request, response, stringWriter.toString());
+ }
+
+ /**
+ * Commit the JSON response. We can't write immediately to the output stream
+ * as we want to write only a critical notification if something goes wrong
+ * during the response handling.
+ *
+ * @param request
+ * The request that resulted in this response
+ * @param response
+ * The response to write to
+ * @param json
+ * The JSON to write
+ * @return true if the JSON was written successfully, false otherwise
+ * @throws IOException
+ * If there was an exception while writing to the output
+ */
+ static boolean commitJsonResponse(VaadinRequest request,
+ VaadinResponse response, String json) throws IOException {
+ // The response was produced without errors so write it to the client
+ response.setContentType("application/json; charset=UTF-8");
+
+ // Ensure that the browser does not cache UIDL responses.
+ // iOS 6 Safari requires this (#9732)
+ response.setHeader("Cache-Control", "no-cache");
+
+ // NOTE! GateIn requires, for some weird reason, getOutputStream
+ // to be used instead of getWriter() (it seems to interpret
+ // application/json as a binary content type)
+ OutputStreamWriter outputWriter = new OutputStreamWriter(
+ response.getOutputStream(), "UTF-8");
+ try {
+ outputWriter.write(json);
+ // NOTE GateIn requires the buffers to be flushed to work
+ outputWriter.flush();
} finally {
- outWriter.close();
+ outputWriter.close();
}
return true;
package com.vaadin.server.communication;
-import java.io.BufferedWriter;
import java.io.IOException;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
+import java.io.StringWriter;
import java.io.Writer;
import java.util.LinkedList;
import java.util.logging.Level;
ClientConnector highlightedConnector;
// repaint requested or session has timed out and new one is created
boolean repaintAll;
- final OutputStream out = response.getOutputStream();
// TODO PUSH repaintAll, analyzeLayouts, highlightConnector should be
// part of the message payload to make the functionality transport
}
}
- final Writer outWriter = new BufferedWriter(new OutputStreamWriter(out,
- "UTF-8"));
+ StringWriter stringWriter = new StringWriter();
try {
rpcHandler.handleRpc(uI, request.getReader(), request);
session.getCommunicationManager().repaintAll(uI);
}
- writeUidl(request, response, uI, outWriter, repaintAll,
+ writeUidl(request, response, uI, stringWriter, repaintAll,
analyzeLayouts);
postHandleRequest(uI);
} catch (JSONException e) {
// Refresh on client side
criticalNotifier.criticalNotification(request, response, null,
null, null, null);
+ return true;
} catch (InvalidUIDLSecurityKeyException e) {
getLogger().log(Level.WARNING,
"Invalid security key received from {}",
// Refresh on client side
criticalNotifier.criticalNotification(request, response, null,
null, null, null);
+ return true;
} finally {
- outWriter.close();
+ stringWriter.close();
requestThemeName = null;
}
- // Ensure that the browser does not cache UIDL responses.
- // iOS 6 Safari requires this (#9732)
- response.setHeader("Cache-Control", "no-cache");
-
- return true;
+ return UIInitHandler.commitJsonResponse(request, response,
+ stringWriter.toString());
}
/**
*/
protected void openJsonMessage(Writer outWriter, VaadinResponse response)
throws IOException {
- // Sets the response type
- response.setContentType("application/json; charset=UTF-8");
// some dirt to prevent cross site scripting
outWriter.write("for(;;);[{");
}