From 0c66f3a68e4bd83a8f32c06d44a33852d9b6f55b Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Thu, 17 Jul 2014 10:00:26 +1000 Subject: [PATCH] [FindBugs] Ensure resources are closed/freed properly Removing dumb connector.close during server.getURI() --- .../java/org/eclipse/jetty/server/Server.java | 1 + .../jetty/server/handler/DefaultHandler.java | 107 ++++++------- .../jetty/server/handler/ShutdownHandler.java | 1 + .../server/session/HashSessionManager.java | 4 +- .../org/eclipse/jetty/util/UrlEncoded.java | 150 +++++++++--------- 5 files changed, 134 insertions(+), 129 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java index 66919f42a7a..6e5050d0883 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java @@ -606,6 +606,7 @@ public class Server extends HandlerWrapper implements Attributes /** * @return The URI of the first {@link NetworkConnector} and first {@link ContextHandler}, or null */ + @SuppressWarnings("resource") public URI getURI() { NetworkConnector connector=null; diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/DefaultHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/DefaultHandler.java index f71720af41f..cf92003c0eb 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/DefaultHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/DefaultHandler.java @@ -117,62 +117,63 @@ public class DefaultHandler extends AbstractHandler response.setStatus(HttpServletResponse.SC_NOT_FOUND); response.setContentType(MimeTypes.Type.TEXT_HTML.toString()); - ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500); - - writer.write("\n\nError 404 - Not Found"); - writer.write("\n\n

Error 404 - Not Found.

\n"); - writer.write("No context on this server matched or handled this request.
"); - writer.write("Contexts known to this server are:
"); + writer.write(" "); + writer.write("Powered by Jetty:// Java Web Server
\n"); + + writer.write("\n\n\n"); + writer.flush(); + response.setContentLength(writer.size()); + try (OutputStream out=response.getOutputStream()) + { + writer.writeTo(out); + } + } } /* ------------------------------------------------------------ */ diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ShutdownHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ShutdownHandler.java index 93c9f86a571..9c948ae0e7d 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ShutdownHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ShutdownHandler.java @@ -133,6 +133,7 @@ public class ShutdownHandler extends HandlerWrapper } } + @SuppressWarnings("resource") private String getServerUrl() { NetworkConnector connector=null; diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/HashSessionManager.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/HashSessionManager.java index d1bd94efaf2..53ccd6398e1 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/HashSessionManager.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/HashSessionManager.java @@ -665,14 +665,14 @@ public class HashSessionManager extends AbstractSessionManager return session; } - + + @SuppressWarnings("resource") private void restoreSessionAttributes (InputStream is, int size, HashedSession session) throws Exception { if (size>0) { // input stream should not be closed here - @SuppressWarnings("resource") ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is); for (int i=0; i implements Cloneable int c; int totalLength = 0; - ByteArrayOutputStream2 output = new ByteArrayOutputStream2(); - int size=0; - - while ((c=in.read())>0) + try(ByteArrayOutputStream2 output = new ByteArrayOutputStream2();) { - switch ((char) c) + int size=0; + + while ((c=in.read())>0) { - case '&': - size=output.size(); - value = size==0?"":output.toString(charset); - output.setCount(0); - if (key != null) - { - map.add(key,value); - } - else if (value!=null&&value.length()>0) - { - map.add(value,""); - } - key = null; - value=null; - if (maxKeys>0 && map.size()>maxKeys) - throw new IllegalStateException("Form too many keys"); - break; - case '=': - if (key!=null) - { + switch ((char) c) + { + case '&': + size=output.size(); + value = size==0?"":output.toString(charset); + output.setCount(0); + if (key != null) + { + map.add(key,value); + } + else if (value!=null&&value.length()>0) + { + map.add(value,""); + } + key = null; + value=null; + if (maxKeys>0 && map.size()>maxKeys) + throw new IllegalStateException("Form too many keys"); + break; + case '=': + if (key!=null) + { + output.write(c); + break; + } + size=output.size(); + key = size==0?"":output.toString(charset); + output.setCount(0); + break; + case '+': + output.write(' '); + break; + case '%': + int code0=in.read(); + if ('u'==code0) + { + int code1=in.read(); + if (code1>=0) + { + int code2=in.read(); + if (code2>=0) + { + int code3=in.read(); + if (code3>=0) + output.write(new String(Character.toChars((convertHexDigit(code0)<<12)+(convertHexDigit(code1)<<8)+(convertHexDigit(code2)<<4)+convertHexDigit(code3))).getBytes(charset)); + } + } + + } + else if (code0>=0) + { + int code1=in.read(); + if (code1>=0) + output.write((convertHexDigit(code0)<<4)+convertHexDigit(code1)); + } + break; + default: output.write(c); break; - } - size=output.size(); - key = size==0?"":output.toString(charset); - output.setCount(0); - break; - case '+': - output.write(' '); - break; - case '%': - int code0=in.read(); - if ('u'==code0) - { - int code1=in.read(); - if (code1>=0) - { - int code2=in.read(); - if (code2>=0) - { - int code3=in.read(); - if (code3>=0) - output.write(new String(Character.toChars((convertHexDigit(code0)<<12)+(convertHexDigit(code1)<<8)+(convertHexDigit(code2)<<4)+convertHexDigit(code3))).getBytes(charset)); - } - } - - } - else if (code0>=0) - { - int code1=in.read(); - if (code1>=0) - output.write((convertHexDigit(code0)<<4)+convertHexDigit(code1)); - } - break; - default: - output.write(c); - break; - } - - totalLength++; - if (maxLength>=0 && totalLength > maxLength) - throw new IllegalStateException("Form too large"); - } + } - size=output.size(); - if (key != null) - { - value = size==0?"":output.toString(charset); - output.setCount(0); - map.add(key,value); + totalLength++; + if (maxLength>=0 && totalLength > maxLength) + throw new IllegalStateException("Form too large"); + } + + size=output.size(); + if (key != null) + { + value = size==0?"":output.toString(charset); + output.setCount(0); + map.add(key,value); + } + else if (size>0) + map.add(output.toString(charset),""); } - else if (size>0) - map.add(output.toString(charset),""); } }