private void writeBootstrapPage(VaadinResponse response, String html)
throws IOException {
response.setContentType("text/html");
- BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
- writer.append(html);
- writer.close();
+ try (BufferedWriter writer = new BufferedWriter(
+ new OutputStreamWriter(response.getOutputStream(), "UTF-8"))) {
+ writer.append(html);
+ }
}
private void setupStandaloneDocument(BootstrapContext context,
*/
protected void writeBrowserTooOldPage(VaadinRequest request,
VaadinResponse response) throws IOException {
- Writer page = response.getWriter();
- WebBrowser b = VaadinSession.getCurrent().getBrowser();
-
- page.write(
+ try (Writer page = response.getWriter()) {
+ WebBrowser b = VaadinSession.getCurrent().getBrowser();
+
+ page.write(
"<html><body><h1>I'm sorry, but your browser is not supported</h1>"
- + "<p>The version (" + b.getBrowserMajorVersion() + "."
- + b.getBrowserMinorVersion()
- + ") of the browser you are using "
+ + "<p>The version (" + b.getBrowserMajorVersion() + "."
+ + b.getBrowserMinorVersion()
+ + ") of the browser you are using "
+ " is outdated and not supported.</p>"
+ "<p>You should <b>consider upgrading</b> to a more up-to-date browser.</p> "
+ "<p>The most popular browsers are <b>"
+ " <a href=\"https://www.google.com/chrome\">Chrome</a>,"
+ " <a href=\"http://www.mozilla.com/firefox\">Firefox</a>,"
- + (b.isWindows()
- ? " <a href=\"http://windows.microsoft.com/en-US/internet-explorer/downloads/ie\">Internet Explorer</a>,"
- : "")
- + " <a href=\"http://www.opera.com/browser\">Opera</a>"
+ + (b.isWindows()
+ ? " <a href=\"http://windows.microsoft.com/en-US/internet-explorer/downloads/ie\">Internet Explorer</a>,"
+ : "")
+ + " <a href=\"http://www.opera.com/browser\">Opera</a>"
+ " and <a href=\"http://www.apple.com/safari\">Safari</a>.</b><br/>"
+ "Upgrading to the latest version of one of these <b>will make the web safer, faster and better looking.</b></p>"
- + (b.isIE()
- ? "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js\"></script>"
- + "<p>If you can not upgrade your browser, please consider trying <a onclick=\"CFInstall.check({mode:'overlay'});return false;\" href=\"http://www.google.com/chromeframe\">Chrome Frame</a>.</p>"
- : "") //
- + "<p><sub><a onclick=\"document.cookie='"
- + FORCE_LOAD_COOKIE
- + "';window.location.reload();return false;\" href=\"#\">Continue without updating</a> (not recommended)</sub></p>"
+ + (b.isIE()
+ ? "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js\"></script>"
+ + "<p>If you can not upgrade your browser, please consider trying <a onclick=\"CFInstall.check({mode:'overlay'});return false;\" href=\"http://www.google.com/chromeframe\">Chrome Frame</a>.</p>"
+ : "") //
+ + "<p><sub><a onclick=\"document.cookie='"
+ + FORCE_LOAD_COOKIE
+ + "';window.location.reload();return false;\" href=\"#\">Continue without updating</a> (not recommended)</sub></p>"
+ "</body>\n" + "</html>");
-
- page.close();
+ }
}
}
response.setContentType(contentType);
final OutputStream out = response.getOutputStream();
- final PrintWriter outWriter = new PrintWriter(
- new BufferedWriter(new OutputStreamWriter(out, "UTF-8")));
- outWriter.print(reponseString);
- outWriter.close();
+ try (PrintWriter outWriter = new PrintWriter(
+ new BufferedWriter(new OutputStreamWriter(out, "UTF-8")))) {
+ outWriter.print(reponseString);
+ }
}
/**
String output) throws IOException {
response.setContentType(contentType);
final OutputStream out = response.getOutputStream();
- // Set the response type
- final PrintWriter outWriter = new PrintWriter(
- new BufferedWriter(new OutputStreamWriter(out, "UTF-8")));
- outWriter.print(output);
- outWriter.flush();
- outWriter.close();
+ try ( // Set the response type
+ PrintWriter outWriter = new PrintWriter(
+ new BufferedWriter(new OutputStreamWriter(out, "UTF-8")))) {
+ outWriter.print(output);
+ outWriter.flush();
+ }
}
/**
private static String readFile(File file, Charset charset)
throws IOException {
- InputStream in = new FileInputStream(file);
- try {
+ try (InputStream in = new FileInputStream(file)) {
// no point in reading files over 2GB to a String
byte[] b = new byte[(int) file.length()];
int len = b.length;
total += result;
}
return new String(b, charset);
- } finally {
- in.close();
}
}
private static void writeFile(String content, File file, Charset charset)
throws IOException {
- FileOutputStream fos = new FileOutputStream(file);
- try {
+ try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(content.getBytes(charset));
- } finally {
- fos.close();
}
}
protected void sendUploadResponse(VaadinRequest request,
VaadinResponse response) throws IOException {
response.setContentType("text/html");
- final OutputStream out = response.getOutputStream();
- final PrintWriter outWriter = new PrintWriter(
+ try (OutputStream out = response.getOutputStream()) {
+ final PrintWriter outWriter = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(out, "UTF-8")));
- outWriter.print("<html><body>download handled</body></html>");
- outWriter.flush();
- out.close();
+ outWriter.print("<html><body>download handled</body></html>");
+ outWriter.flush();
+ }
}
private void cleanStreamVariable(VaadinSession session, final UI ui,
final OutputStream out = ((ResourceResponse) response)
.getPortletOutputStream();
- final PrintWriter outWriter = new PrintWriter(
- new BufferedWriter(new OutputStreamWriter(out, "UTF-8")));
- outWriter.print("<html><body>dummy page</body></html>");
- outWriter.close();
+ try (PrintWriter outWriter = new PrintWriter(
+ new BufferedWriter(new OutputStreamWriter(out, "UTF-8")))) {
+ outWriter.print("<html><body>dummy page</body></html>");
+ }
return true;
}
import com.vaadin.server.LegacyCommunicationManager;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.UI;
+import java.nio.charset.StandardCharsets;
/**
* Serializes resources to JSON. Currently only used for {@link CustomLayout}
+ "\" : ");
final StringBuffer layout = new StringBuffer();
- try {
- final InputStreamReader r = new InputStreamReader(is,
- "UTF-8");
+ try (InputStreamReader r = new InputStreamReader(is,
+ StandardCharsets.UTF_8)) {
final char[] buffer = new char[20000];
int charsRead = 0;
while ((charsRead = r.read(buffer)) > 0) {
layout.append(buffer, 0, charsRead);
}
- r.close();
} catch (final java.io.IOException e) {
// FIXME: Handle exception
getLogger().log(Level.INFO, "Resource transfer failed", e);
*/
protected String getInitialUidl(VaadinRequest request, UI uI)
throws IOException {
- StringWriter writer = new StringWriter();
- try {
+ try (StringWriter writer = new StringWriter()) {
writer.write("{");
VaadinSession session = uI.getSession();
String initialUIDL = writer.toString();
getLogger().log(Level.FINE, "Initial UIDL:" + initialUIDL);
return initialUIDL;
- } finally {
- writer.close();
}
}
}
}
widgetsetFile.createNewFile();
- PrintStream printStream = new PrintStream(
- new FileOutputStream(widgetsetFile));
- printStream.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ try (PrintStream printStream = new PrintStream(
+ new FileOutputStream(widgetsetFile))) {
+ printStream.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<!DOCTYPE module PUBLIC \"-//Google Inc.//DTD Google Web Toolkit 2.5.1//EN\" \"http://google-web-toolkit.googlecode.com/svn/tags/2.5.1/distro-source/core/src/gwt-module.dtd\">\n");
- printStream.print("<module>\n");
- printStream.print(" <!--\n"
+ printStream.print("<module>\n");
+ printStream.print(" <!--\n"
+ " Uncomment the following to compile the widgetset for one browser only.\n\n"
+ " Multiple browsers can be specified as a comma separated list. The\n"
+ " supported user agents at the moment of writing were:\n"
+ " See https://vaadin.com/wiki/-/wiki/Main/Using%20SuperDevMode for more\n"
+ " information and instructions.\n" + " -->\n"
+ " <!-- <set-configuration-property name=\"devModeRedirectEnabled\" value=\"true\" /> -->\n\n");
- printStream.print("\n</module>\n");
- printStream.close();
+ printStream.print("\n</module>\n");
+ }
changed = true;
}
private static void commitChanges(File widgetsetFile, String content)
throws IOException {
- BufferedWriter bufferedWriter = new BufferedWriter(
- new OutputStreamWriter(new FileOutputStream(widgetsetFile)));
- bufferedWriter.write(content);
- bufferedWriter.close();
+ try (BufferedWriter bufferedWriter = new BufferedWriter(
+ new OutputStreamWriter(new FileOutputStream(widgetsetFile)))) {
+ bufferedWriter.write(content);
+ }
}
private static String addWidgetSet(String ws, String content) {
}
private static String readFile(File widgetsetFile) throws IOException {
- Reader fi = new FileReader(widgetsetFile);
- BufferedReader bufferedReader = new BufferedReader(fi);
- StringBuilder sb = new StringBuilder();
- String line;
- while ((line = bufferedReader.readLine()) != null) {
- sb.append(line);
- sb.append("\n");
+ StringBuilder sb;
+ try (Reader fi = new FileReader(widgetsetFile)) {
+ BufferedReader bufferedReader = new BufferedReader(fi);
+ sb = new StringBuilder();
+ String line;
+ while ((line = bufferedReader.readLine()) != null) {
+ sb.append(line);
+ sb.append("\n");
+ }
}
- fi.close();
return sb.toString();
}
NoSuchMethodException, SecurityException, InstantiationException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException, IOException, InterruptedException {
- URLClassLoader loader = new TestClassLoader();
- Class<?> clazz = loader.loadClass(Jsr303UnitTest.class.getName());
- UnitTest test = (UnitTest) clazz.newInstance();
- test.execute();
- loader.close();
+ try (URLClassLoader loader = new TestClassLoader()) {
+ Class<?> clazz = loader.loadClass(Jsr303UnitTest.class.getName());
+ UnitTest test = (UnitTest) clazz.newInstance();
+ test.execute();
+ }
}
}
int uiId = ui.getUIId();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
- ObjectOutputStream out = new ObjectOutputStream(bos);
- out.writeObject(session);
- out.close();
+ try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
+ out.writeObject(session);
+ }
session.unlock();