diff options
11 files changed, 76 insertions, 86 deletions
diff --git a/server/src/main/java/com/vaadin/server/BootstrapHandler.java b/server/src/main/java/com/vaadin/server/BootstrapHandler.java index fd30d2891d..3cb1dc9097 100644 --- a/server/src/main/java/com/vaadin/server/BootstrapHandler.java +++ b/server/src/main/java/com/vaadin/server/BootstrapHandler.java @@ -353,10 +353,10 @@ public abstract class BootstrapHandler extends SynchronizedRequestHandler { 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, diff --git a/server/src/main/java/com/vaadin/server/UnsupportedBrowserHandler.java b/server/src/main/java/com/vaadin/server/UnsupportedBrowserHandler.java index 2165830101..fa1f48b790 100644 --- a/server/src/main/java/com/vaadin/server/UnsupportedBrowserHandler.java +++ b/server/src/main/java/com/vaadin/server/UnsupportedBrowserHandler.java @@ -61,34 +61,33 @@ public class UnsupportedBrowserHandler extends SynchronizedRequestHandler { */ 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(); + } } } diff --git a/server/src/main/java/com/vaadin/server/VaadinService.java b/server/src/main/java/com/vaadin/server/VaadinService.java index 922c7cfe52..3899965622 100644 --- a/server/src/main/java/com/vaadin/server/VaadinService.java +++ b/server/src/main/java/com/vaadin/server/VaadinService.java @@ -1499,10 +1499,10 @@ public abstract class VaadinService implements Serializable { 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); + } } /** diff --git a/server/src/main/java/com/vaadin/server/VaadinServlet.java b/server/src/main/java/com/vaadin/server/VaadinServlet.java index d43db5c303..13713dcc54 100644 --- a/server/src/main/java/com/vaadin/server/VaadinServlet.java +++ b/server/src/main/java/com/vaadin/server/VaadinServlet.java @@ -605,12 +605,12 @@ public class VaadinServlet extends HttpServlet implements Constants { 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(); + } } /** @@ -1405,8 +1405,7 @@ public class VaadinServlet extends HttpServlet implements Constants { 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; @@ -1420,18 +1419,13 @@ public class VaadinServlet extends HttpServlet implements Constants { 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(); } } diff --git a/server/src/main/java/com/vaadin/server/communication/FileUploadHandler.java b/server/src/main/java/com/vaadin/server/communication/FileUploadHandler.java index 19c3d45fa9..d5c047b0eb 100644 --- a/server/src/main/java/com/vaadin/server/communication/FileUploadHandler.java +++ b/server/src/main/java/com/vaadin/server/communication/FileUploadHandler.java @@ -676,12 +676,12 @@ public class FileUploadHandler implements RequestHandler { 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, diff --git a/server/src/main/java/com/vaadin/server/communication/PortletDummyRequestHandler.java b/server/src/main/java/com/vaadin/server/communication/PortletDummyRequestHandler.java index 04d0e8b669..7540fcb190 100644 --- a/server/src/main/java/com/vaadin/server/communication/PortletDummyRequestHandler.java +++ b/server/src/main/java/com/vaadin/server/communication/PortletDummyRequestHandler.java @@ -60,10 +60,10 @@ public class PortletDummyRequestHandler implements RequestHandler { 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; } diff --git a/server/src/main/java/com/vaadin/server/communication/ResourceWriter.java b/server/src/main/java/com/vaadin/server/communication/ResourceWriter.java index 2ca4b296bb..be83bf7185 100644 --- a/server/src/main/java/com/vaadin/server/communication/ResourceWriter.java +++ b/server/src/main/java/com/vaadin/server/communication/ResourceWriter.java @@ -29,6 +29,7 @@ import com.vaadin.server.JsonPaintTarget; 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} @@ -82,15 +83,13 @@ public class ResourceWriter implements Serializable { + "\" : "); 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); diff --git a/server/src/main/java/com/vaadin/server/communication/UIInitHandler.java b/server/src/main/java/com/vaadin/server/communication/UIInitHandler.java index 0050872aaa..09d2b0aa79 100644 --- a/server/src/main/java/com/vaadin/server/communication/UIInitHandler.java +++ b/server/src/main/java/com/vaadin/server/communication/UIInitHandler.java @@ -273,8 +273,7 @@ public abstract class UIInitHandler extends SynchronizedRequestHandler { */ 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(); @@ -287,8 +286,6 @@ public abstract class UIInitHandler extends SynchronizedRequestHandler { String initialUIDL = writer.toString(); getLogger().log(Level.FINE, "Initial UIDL:" + initialUIDL); return initialUIDL; - } finally { - writer.close(); } } diff --git a/server/src/main/java/com/vaadin/server/widgetsetutils/WidgetSetBuilder.java b/server/src/main/java/com/vaadin/server/widgetsetutils/WidgetSetBuilder.java index 951944d8ac..689ac21cd7 100644 --- a/server/src/main/java/com/vaadin/server/widgetsetutils/WidgetSetBuilder.java +++ b/server/src/main/java/com/vaadin/server/widgetsetutils/WidgetSetBuilder.java @@ -84,12 +84,12 @@ public class WidgetSetBuilder { } } 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" @@ -103,8 +103,8 @@ public class WidgetSetBuilder { + " 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; } @@ -155,10 +155,10 @@ public class WidgetSetBuilder { 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) { @@ -187,15 +187,16 @@ public class WidgetSetBuilder { } 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(); } diff --git a/server/src/test/java/com/vaadin/data/Jsr303Test.java b/server/src/test/java/com/vaadin/data/Jsr303Test.java index 74e97432ba..da30f56743 100644 --- a/server/src/test/java/com/vaadin/data/Jsr303Test.java +++ b/server/src/test/java/com/vaadin/data/Jsr303Test.java @@ -108,11 +108,11 @@ public class Jsr303Test { 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(); + } } } diff --git a/server/src/test/java/com/vaadin/server/VaadinSessionTest.java b/server/src/test/java/com/vaadin/server/VaadinSessionTest.java index 466f89df07..58fb0658c5 100644 --- a/server/src/test/java/com/vaadin/server/VaadinSessionTest.java +++ b/server/src/test/java/com/vaadin/server/VaadinSessionTest.java @@ -286,9 +286,9 @@ public class VaadinSessionTest implements Serializable { 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(); |