diff --git a/src/main/java/robaho/net/httpserver/AuthFilter.java b/src/main/java/robaho/net/httpserver/AuthFilter.java index 9e40d4f..9c5a48a 100644 --- a/src/main/java/robaho/net/httpserver/AuthFilter.java +++ b/src/main/java/robaho/net/httpserver/AuthFilter.java @@ -29,7 +29,7 @@ import java.io.*; -public class AuthFilter extends Filter { +public final class AuthFilter extends Filter { private Authenticator authenticator; @@ -37,6 +37,7 @@ public AuthFilter(Authenticator authenticator) { this.authenticator = authenticator; } + @Override public String description() { return "Authentication filter"; } @@ -56,6 +57,7 @@ public void consumeInput(HttpExchange t) throws IOException { /** * The filter's implementation, which is invoked by the server */ + @Override public void doFilter(HttpExchange t, Filter.Chain chain) throws IOException { if (authenticator != null) { Authenticator.Result r = authenticator.authenticate(t); diff --git a/src/main/java/robaho/net/httpserver/Code.java b/src/main/java/robaho/net/httpserver/Code.java index a32a5cc..edb842e 100644 --- a/src/main/java/robaho/net/httpserver/Code.java +++ b/src/main/java/robaho/net/httpserver/Code.java @@ -25,7 +25,7 @@ package robaho.net.httpserver; -public class Code { +public final class Code { public static final int HTTP_CONTINUE = 100; public static final int HTTP_OK = 200; diff --git a/src/main/java/robaho/net/httpserver/ContextList.java b/src/main/java/robaho/net/httpserver/ContextList.java index 494724c..ec0ef68 100644 --- a/src/main/java/robaho/net/httpserver/ContextList.java +++ b/src/main/java/robaho/net/httpserver/ContextList.java @@ -29,13 +29,13 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; -class ContextList { +final class ContextList { private final List list = new CopyOnWriteArrayList<>(); private final Map cache = new ConcurrentHashMap<>(); private record CacheKey(String protocol,String path){} - public synchronized void add(HttpContextImpl ctx) { + synchronized void add(HttpContextImpl ctx) { assert ctx.getPath() != null; if (contains(ctx)) { throw new IllegalArgumentException("cannot add context to list"); @@ -47,7 +47,7 @@ boolean contains(HttpContextImpl ctx) { return findContext(ctx.getProtocol(), ctx.getPath(), true) != null; } - public int size() { + int size() { return list.size(); } @@ -90,7 +90,7 @@ HttpContextImpl findContext(String protocol, String path, boolean exact) { return lc; } - public synchronized void remove(String protocol, String path) + synchronized void remove(String protocol, String path) throws IllegalArgumentException { HttpContextImpl ctx = findContext(protocol, path, true); if (ctx == null) { @@ -99,7 +99,7 @@ public synchronized void remove(String protocol, String path) list.remove(ctx); } - public synchronized void remove(HttpContextImpl context) + synchronized void remove(HttpContextImpl context) throws IllegalArgumentException { for (HttpContextImpl ctx : list) { if (ctx.equals(context)) { diff --git a/src/main/java/robaho/net/httpserver/DefaultHttpServerProvider.java b/src/main/java/robaho/net/httpserver/DefaultHttpServerProvider.java index 6fd1feb..4c0de2b 100644 --- a/src/main/java/robaho/net/httpserver/DefaultHttpServerProvider.java +++ b/src/main/java/robaho/net/httpserver/DefaultHttpServerProvider.java @@ -30,11 +30,14 @@ import com.sun.net.httpserver.*; import com.sun.net.httpserver.spi.*; -public class DefaultHttpServerProvider extends HttpServerProvider { +public final class DefaultHttpServerProvider extends HttpServerProvider { + + @Override public HttpServer createHttpServer(InetSocketAddress addr, int backlog) throws IOException { return new HttpServerImpl(addr, backlog); } + @Override public HttpsServer createHttpsServer(InetSocketAddress addr, int backlog) throws IOException { return new HttpsServerImpl(addr, backlog); } diff --git a/src/main/java/robaho/net/httpserver/DelegatingHttpExchange.java b/src/main/java/robaho/net/httpserver/DelegatingHttpExchange.java index b36ed2b..6d554c9 100644 --- a/src/main/java/robaho/net/httpserver/DelegatingHttpExchange.java +++ b/src/main/java/robaho/net/httpserver/DelegatingHttpExchange.java @@ -44,64 +44,81 @@ public DelegatingHttpExchange(HttpExchange ex) { this.exchange = ex; } + @Override public abstract Headers getRequestHeaders(); + @Override public abstract String getRequestMethod(); + @Override public abstract URI getRequestURI(); + @Override public Headers getResponseHeaders() { return exchange.getResponseHeaders(); } + @Override public HttpContext getHttpContext() { return exchange.getHttpContext(); } + @Override public void close() { exchange.close(); } + @Override public InputStream getRequestBody() { return exchange.getRequestBody(); } + @Override public int getResponseCode() { return exchange.getResponseCode(); } + @Override public OutputStream getResponseBody() { return exchange.getResponseBody(); } + @Override public void sendResponseHeaders(int rCode, long contentLen) throws IOException { exchange.sendResponseHeaders(rCode, contentLen); } + @Override public InetSocketAddress getRemoteAddress() { return exchange.getRemoteAddress(); } + @Override public InetSocketAddress getLocalAddress() { return exchange.getLocalAddress(); } + @Override public String getProtocol() { return exchange.getProtocol(); } + @Override public Object getAttribute(String name) { return exchange.getAttribute(name); } + @Override public void setAttribute(String name, Object value) { exchange.setAttribute(name, value); } + @Override public void setStreams(InputStream i, OutputStream o) { exchange.setStreams(i, o); } + @Override public HttpPrincipal getPrincipal() { return exchange.getPrincipal(); } diff --git a/src/main/java/robaho/net/httpserver/ExchangeImpl.java b/src/main/java/robaho/net/httpserver/ExchangeImpl.java index 956838b..6385646 100644 --- a/src/main/java/robaho/net/httpserver/ExchangeImpl.java +++ b/src/main/java/robaho/net/httpserver/ExchangeImpl.java @@ -42,7 +42,7 @@ import robaho.net.httpserver.websockets.WebSocketHandler; -class ExchangeImpl { +final class ExchangeImpl { Headers reqHdrs, rspHdrs; Request req; @@ -108,23 +108,23 @@ class ExchangeImpl { this.ris = req.inputStream(); } - public Headers getRequestHeaders() { + Headers getRequestHeaders() { return reqHdrs; } - public Headers getResponseHeaders() { + Headers getResponseHeaders() { return rspHdrs; } - public URI getRequestURI() { + URI getRequestURI() { return uri; } - public String getRequestMethod() { + String getRequestMethod() { return method; } - public HttpContextImpl getHttpContext() { + HttpContextImpl getHttpContext() { return connection.getHttpContext(); } @@ -136,7 +136,7 @@ private boolean isConnectRequest() { return CONNECT.equals(getRequestMethod()); } - public void close() { + void close() { if (closed) { return; } @@ -166,7 +166,7 @@ public void close() { } } - public InputStream getRequestBody() { + InputStream getRequestBody() { if (uis != null) { return uis; } @@ -187,11 +187,11 @@ LeftOverInputStream getOriginalInputStream() { return uis_orig; } - public int getResponseCode() { + int getResponseCode() { return rcode; } - public OutputStream getResponseBody() { + OutputStream getResponseBody() { /* * TODO. Change spec to remove restriction below. Filters * cannot work with this restriction @@ -219,7 +219,7 @@ PlaceholderOutputStream getPlaceholderResponseBody() { // hard-coded formatting for Date header, rather than using slower DateFormatter - public void sendResponseHeaders(int rCode, long contentLen) + void sendResponseHeaders(int rCode, long contentLen) throws IOException { final Logger logger = getServerImpl().getLogger(); if (sentHeaders) { @@ -353,31 +353,31 @@ void writeHeaders(Headers map, OutputStream os) throws IOException { outputAscii(CRNL,os); } - public InetSocketAddress getRemoteAddress() { + InetSocketAddress getRemoteAddress() { Socket s = connection.getSocket(); InetAddress ia = s.getInetAddress(); int port = s.getPort(); return new InetSocketAddress(ia, port); } - public InetSocketAddress getLocalAddress() { + InetSocketAddress getLocalAddress() { Socket s = connection.getSocket(); InetAddress ia = s.getLocalAddress(); int port = s.getLocalPort(); return new InetSocketAddress(ia, port); } - public String getProtocol() { + String getProtocol() { String reqline = req.requestLine(); int index = reqline.lastIndexOf(" "); return reqline.substring(index + 1); } - public SSLSession getSSLSession() { + SSLSession getSSLSession() { return connection.getSSLSession(); } - public Object getAttribute(String name) { + Object getAttribute(String name) { if (name == null) { throw new NullPointerException("null name parameter"); } @@ -387,7 +387,7 @@ public Object getAttribute(String name) { return attributes.get(name); } - public void setAttribute(String name, Object value) { + void setAttribute(String name, Object value) { if (name == null) { throw new NullPointerException("null name parameter"); } @@ -401,7 +401,7 @@ public void setAttribute(String name, Object value) { } } - public void setStreams(InputStream i, OutputStream o) { + void setStreams(InputStream i, OutputStream o) { assert uis != null; if (i != null) { uis = i; @@ -422,7 +422,7 @@ ServerImpl getServerImpl() { return getHttpContext().getServerImpl(); } - public HttpPrincipal getPrincipal() { + HttpPrincipal getPrincipal() { return principal; } @@ -447,7 +447,7 @@ static ExchangeImpl get(HttpExchange t) { * the wrapped stream has been provided, then an IOException will * be thrown. */ -class PlaceholderOutputStream extends java.io.OutputStream { +final class PlaceholderOutputStream extends java.io.OutputStream { OutputStream wrapped; @@ -469,26 +469,31 @@ private void checkWrap() throws IOException { } } + @Override public void write(int b) throws IOException { checkWrap(); wrapped.write(b); } + @Override public void write(byte b[]) throws IOException { checkWrap(); wrapped.write(b); } + @Override public void write(byte b[], int off, int len) throws IOException { checkWrap(); wrapped.write(b, off, len); } + @Override public void flush() throws IOException { checkWrap(); wrapped.flush(); } + @Override public void close() throws IOException { checkWrap(); wrapped.close(); diff --git a/src/main/java/robaho/net/httpserver/FixedLengthInputStream.java b/src/main/java/robaho/net/httpserver/FixedLengthInputStream.java index d3f22ea..b263da9 100644 --- a/src/main/java/robaho/net/httpserver/FixedLengthInputStream.java +++ b/src/main/java/robaho/net/httpserver/FixedLengthInputStream.java @@ -33,7 +33,7 @@ * close() does not close the underlying stream */ -class FixedLengthInputStream extends LeftOverInputStream { +final class FixedLengthInputStream extends LeftOverInputStream { private long remaining; FixedLengthInputStream(ExchangeImpl t, InputStream src, long len) { @@ -44,6 +44,7 @@ class FixedLengthInputStream extends LeftOverInputStream { this.remaining = len; } + @Override protected int readImpl(byte[] b, int off, int len) throws IOException { eof = (remaining == 0L); @@ -62,6 +63,7 @@ protected int readImpl(byte[] b, int off, int len) throws IOException { return n; } + @Override public int available() throws IOException { if (eof) { return 0; @@ -70,13 +72,16 @@ public int available() throws IOException { return n < remaining ? n : (int) remaining; } + @Override public boolean markSupported() { return false; } + @Override public void mark(int l) { } + @Override public void reset() throws IOException { throw new IOException("mark/reset not supported"); } diff --git a/src/main/java/robaho/net/httpserver/FixedLengthOutputStream.java b/src/main/java/robaho/net/httpserver/FixedLengthOutputStream.java index 85c00b3..5f2663b 100644 --- a/src/main/java/robaho/net/httpserver/FixedLengthOutputStream.java +++ b/src/main/java/robaho/net/httpserver/FixedLengthOutputStream.java @@ -36,10 +36,10 @@ * normal close() does not close the underlying stream */ -class FixedLengthOutputStream extends FilterOutputStream { +final class FixedLengthOutputStream extends FilterOutputStream { private long remaining; private boolean closed = false; - ExchangeImpl t; + private final ExchangeImpl t; FixedLengthOutputStream(ExchangeImpl t, OutputStream src, long len) { super(src); @@ -50,6 +50,7 @@ class FixedLengthOutputStream extends FilterOutputStream { this.remaining = len; } + @Override public void write(int b) throws IOException { if (closed) { throw new IOException("stream closed"); @@ -61,6 +62,7 @@ public void write(int b) throws IOException { remaining--; } + @Override public void write(byte[] b, int off, int len) throws IOException { Objects.checkFromIndexSize(off, len, b.length); if (len == 0) { @@ -77,6 +79,7 @@ public void write(byte[] b, int off, int len) throws IOException { remaining -= len; } + @Override public void close() throws IOException { if (closed) { return; diff --git a/src/main/java/robaho/net/httpserver/HttpConnection.java b/src/main/java/robaho/net/httpserver/HttpConnection.java index 4375a8a..9edaf07 100644 --- a/src/main/java/robaho/net/httpserver/HttpConnection.java +++ b/src/main/java/robaho/net/httpserver/HttpConnection.java @@ -42,14 +42,14 @@ * one of these is hung from the selector attachment and is used to locate * everything from that. */ -class HttpConnection { +final class HttpConnection { private static final Logger logger = System.getLogger("robaho.net.httpserver"); HttpContextImpl context; /* low level stream that sits directly over channel */ - InputStream is; - OutputStream os; + final InputStream is; + final OutputStream os; final Socket socket; volatile boolean closed = false; @@ -139,7 +139,7 @@ HttpContextImpl getHttpContext() { return context; } - private class ActivityTimerInputStream extends FilterInputStream { + private final class ActivityTimerInputStream extends FilterInputStream { private ActivityTimerInputStream(InputStream inputStream) { super(inputStream); @@ -175,7 +175,7 @@ public int read(byte b[], int off, int len) throws IOException { } - private class ActivityTimerOutputStream extends FilterOutputStream { + private final class ActivityTimerOutputStream extends FilterOutputStream { private ActivityTimerOutputStream(OutputStream outputStream) { super(outputStream); diff --git a/src/main/java/robaho/net/httpserver/HttpContextImpl.java b/src/main/java/robaho/net/httpserver/HttpContextImpl.java index 013d6dc..2a85b32 100644 --- a/src/main/java/robaho/net/httpserver/HttpContextImpl.java +++ b/src/main/java/robaho/net/httpserver/HttpContextImpl.java @@ -41,7 +41,7 @@ * {@link HttpServer#createContext(String,String,HttpHandler,Object)} *

*/ -class HttpContextImpl extends HttpContext { +final class HttpContextImpl extends HttpContext { private final String path; private final String protocol; @@ -78,10 +78,12 @@ class HttpContextImpl extends HttpContext { * * @return the HttpHandler for this context */ + @Override public HttpHandler getHandler() { return handler; } + @Override public void setHandler(HttpHandler h) { if (h == null) { throw new NullPointerException("Null handler parameter"); @@ -97,6 +99,7 @@ public void setHandler(HttpHandler h) { * * @return this context's path */ + @Override public String getPath() { return path; } @@ -106,6 +109,7 @@ public String getPath() { * * @return this context's server */ + @Override public HttpServer getServer() { return server.getWrapper(); } @@ -119,7 +123,7 @@ ServerImpl getServerImpl() { * * @return this context's path */ - public String getProtocol() { + String getProtocol() { return protocol; } @@ -131,10 +135,12 @@ public String getProtocol() { * Every attribute stored in this Map will be visible to * every HttpExchange processed by this context */ + @Override public Map getAttributes() { return attributes; } + @Override public List getFilters() { return ufilters; } @@ -143,6 +149,7 @@ List getSystemFilters() { return sfilters; } + @Override public Authenticator setAuthenticator(Authenticator auth) { Authenticator old = authenticator; authenticator = auth; @@ -150,6 +157,7 @@ public Authenticator setAuthenticator(Authenticator auth) { return old; } + @Override public Authenticator getAuthenticator() { return authenticator; } diff --git a/src/main/java/robaho/net/httpserver/HttpExchangeImpl.java b/src/main/java/robaho/net/httpserver/HttpExchangeImpl.java index c7e6138..de3b32f 100644 --- a/src/main/java/robaho/net/httpserver/HttpExchangeImpl.java +++ b/src/main/java/robaho/net/httpserver/HttpExchangeImpl.java @@ -29,79 +29,96 @@ import java.net.*; import com.sun.net.httpserver.*; -class HttpExchangeImpl extends HttpExchange { +final class HttpExchangeImpl extends HttpExchange { - ExchangeImpl impl; + private final ExchangeImpl impl; HttpExchangeImpl(ExchangeImpl impl) { this.impl = impl; } + @Override public Headers getRequestHeaders() { return impl.getRequestHeaders(); } + @Override public Headers getResponseHeaders() { return impl.getResponseHeaders(); } + @Override public URI getRequestURI() { return impl.getRequestURI(); } + @Override public String getRequestMethod() { return impl.getRequestMethod(); } + @Override public HttpContextImpl getHttpContext() { return impl.getHttpContext(); } + @Override public void close() { impl.close(); } + @Override public InputStream getRequestBody() { return impl.getRequestBody(); } + @Override public int getResponseCode() { return impl.getResponseCode(); } + @Override public OutputStream getResponseBody() { return impl.getResponseBody(); } + @Override public void sendResponseHeaders(int rCode, long contentLen) throws IOException { impl.sendResponseHeaders(rCode, contentLen); } + @Override public InetSocketAddress getRemoteAddress() { return impl.getRemoteAddress(); } + @Override public InetSocketAddress getLocalAddress() { return impl.getLocalAddress(); } + @Override public String getProtocol() { return impl.getProtocol(); } + @Override public Object getAttribute(String name) { return impl.getAttribute(name); } + @Override public void setAttribute(String name, Object value) { impl.setAttribute(name, value); } + @Override public void setStreams(InputStream i, OutputStream o) { impl.setStreams(i, o); } + @Override public HttpPrincipal getPrincipal() { return impl.getPrincipal(); } diff --git a/src/main/java/robaho/net/httpserver/HttpServerImpl.java b/src/main/java/robaho/net/httpserver/HttpServerImpl.java index 6f7843e..f1815ec 100644 --- a/src/main/java/robaho/net/httpserver/HttpServerImpl.java +++ b/src/main/java/robaho/net/httpserver/HttpServerImpl.java @@ -30,9 +30,9 @@ import java.util.concurrent.*; import com.sun.net.httpserver.*; -public class HttpServerImpl extends HttpServer { +public final class HttpServerImpl extends HttpServer { - ServerImpl server; + private final ServerImpl server; HttpServerImpl() throws IOException { this(new InetSocketAddress(80), 0); @@ -43,42 +43,52 @@ public class HttpServerImpl extends HttpServer { server = new ServerImpl(this, "http", addr, backlog); } + @Override public void bind(InetSocketAddress addr, int backlog) throws IOException { server.bind(addr, backlog); } + @Override public void start() { server.start(); } + @Override public void setExecutor(Executor executor) { server.setExecutor(executor); } + @Override public Executor getExecutor() { return server.getExecutor(); } + @Override public void stop(int delay) { server.stop(delay); } - public HttpContextImpl createContext(String path, HttpHandler handler) { + @Override + public HttpContext createContext(String path, HttpHandler handler) { return server.createContext(path, handler); } - public HttpContextImpl createContext(String path) { + @Override + public HttpContext createContext(String path) { return server.createContext(path); } + @Override public void removeContext(String path) throws IllegalArgumentException { server.removeContext(path); } + @Override public void removeContext(HttpContext context) throws IllegalArgumentException { server.removeContext(context); } + @Override public InetSocketAddress getAddress() { return server.getAddress(); } diff --git a/src/main/java/robaho/net/httpserver/HttpsExchangeImpl.java b/src/main/java/robaho/net/httpserver/HttpsExchangeImpl.java index 49b5646..a674d16 100644 --- a/src/main/java/robaho/net/httpserver/HttpsExchangeImpl.java +++ b/src/main/java/robaho/net/httpserver/HttpsExchangeImpl.java @@ -37,83 +37,101 @@ import com.sun.net.httpserver.HttpPrincipal; import com.sun.net.httpserver.HttpsExchange; -class HttpsExchangeImpl extends HttpsExchange { +final class HttpsExchangeImpl extends HttpsExchange { - ExchangeImpl impl; + private final ExchangeImpl impl; - HttpsExchangeImpl(ExchangeImpl impl) throws IOException { + HttpsExchangeImpl(ExchangeImpl impl) { this.impl = impl; } + @Override public Headers getRequestHeaders() { return impl.getRequestHeaders(); } + @Override public Headers getResponseHeaders() { return impl.getResponseHeaders(); } + @Override public URI getRequestURI() { return impl.getRequestURI(); } + @Override public String getRequestMethod() { return impl.getRequestMethod(); } + @Override public HttpContextImpl getHttpContext() { return impl.getHttpContext(); } + @Override public void close() { impl.close(); } + @Override public InputStream getRequestBody() { return impl.getRequestBody(); } + @Override public int getResponseCode() { return impl.getResponseCode(); } + @Override public OutputStream getResponseBody() { return impl.getResponseBody(); } + @Override public void sendResponseHeaders(int rCode, long contentLen) throws IOException { impl.sendResponseHeaders(rCode, contentLen); } + @Override public InetSocketAddress getRemoteAddress() { return impl.getRemoteAddress(); } + @Override public InetSocketAddress getLocalAddress() { return impl.getLocalAddress(); } + @Override public String getProtocol() { return impl.getProtocol(); } + @Override public SSLSession getSSLSession() { return impl.getSSLSession(); } + @Override public Object getAttribute(String name) { return impl.getAttribute(name); } + @Override public void setAttribute(String name, Object value) { impl.setAttribute(name, value); } + @Override public void setStreams(InputStream i, OutputStream o) { impl.setStreams(i, o); } + @Override public HttpPrincipal getPrincipal() { return impl.getPrincipal(); } diff --git a/src/main/java/robaho/net/httpserver/HttpsServerImpl.java b/src/main/java/robaho/net/httpserver/HttpsServerImpl.java index fd75cef..cbffa15 100644 --- a/src/main/java/robaho/net/httpserver/HttpsServerImpl.java +++ b/src/main/java/robaho/net/httpserver/HttpsServerImpl.java @@ -34,9 +34,9 @@ import com.sun.net.httpserver.HttpsConfigurator; import com.sun.net.httpserver.HttpsServer; -public class HttpsServerImpl extends HttpsServer { +public final class HttpsServerImpl extends HttpsServer { - ServerImpl server; + private final ServerImpl server; HttpsServerImpl() throws IOException { this(new InetSocketAddress(443), 0); @@ -47,50 +47,62 @@ public class HttpsServerImpl extends HttpsServer { server = new ServerImpl(this, "https", addr, backlog); } + @Override public void setHttpsConfigurator(HttpsConfigurator config) { server.setHttpsConfigurator(config); } + @Override public HttpsConfigurator getHttpsConfigurator() { return server.getHttpsConfigurator(); } + @Override public void bind(InetSocketAddress addr, int backlog) throws IOException { server.bind(addr, backlog); } + @Override public void start() { server.start(); } + @Override public void setExecutor(Executor executor) { server.setExecutor(executor); } + @Override public Executor getExecutor() { return server.getExecutor(); } + @Override public void stop(int delay) { server.stop(delay); } - public HttpContextImpl createContext(String path, HttpHandler handler) { + @Override + public HttpContext createContext(String path, HttpHandler handler) { return server.createContext(path, handler); } - public HttpContextImpl createContext(String path) { + @Override + public HttpContext createContext(String path) { return server.createContext(path); } + @Override public void removeContext(String path) throws IllegalArgumentException { server.removeContext(path); } + @Override public void removeContext(HttpContext context) throws IllegalArgumentException { server.removeContext(context); } + @Override public InetSocketAddress getAddress() { return server.getAddress(); } diff --git a/src/main/java/robaho/net/httpserver/LeftOverInputStream.java b/src/main/java/robaho/net/httpserver/LeftOverInputStream.java index 0ce99f3..8b34ccb 100644 --- a/src/main/java/robaho/net/httpserver/LeftOverInputStream.java +++ b/src/main/java/robaho/net/httpserver/LeftOverInputStream.java @@ -48,12 +48,13 @@ abstract class LeftOverInputStream extends FilterInputStream { protected boolean eof = false; byte[] one = new byte[1]; - public LeftOverInputStream(ExchangeImpl t, InputStream src) { + protected LeftOverInputStream(ExchangeImpl t, InputStream src) { super(src); this.t = t; this.server = t.getServerImpl(); } + @Override public void close() throws IOException { if (closed) { return; @@ -64,12 +65,13 @@ public void close() throws IOException { } } - public boolean isClosed() { + protected boolean isClosed() { return closed; } protected abstract int readImpl(byte[] b, int off, int len) throws IOException; + @Override public synchronized int read() throws IOException { if (closed) { throw new EOFException("Stream is closed"); @@ -82,6 +84,7 @@ public synchronized int read() throws IOException { } } + @Override public synchronized int read(byte[] b, int off, int len) throws IOException { if (closed) { throw new EOFException("Stream is closed"); @@ -97,7 +100,7 @@ public synchronized int read(byte[] b, int off, int len) throws IOException { * is at eof (ie. all bytes were read) or false if not * (still bytes to be read) */ - public boolean drain(long l) throws IOException { + protected boolean drain(long l) throws IOException { while (l > 0) { if (server.isFinishing()) { @@ -113,7 +116,7 @@ public boolean drain(long l) throws IOException { } return false; } - public InputStream getRawInputStream() { + protected InputStream getRawInputStream() { return super.in; } } diff --git a/src/main/java/robaho/net/httpserver/Request.java b/src/main/java/robaho/net/httpserver/Request.java index a6967ce..cdf6887 100644 --- a/src/main/java/robaho/net/httpserver/Request.java +++ b/src/main/java/robaho/net/httpserver/Request.java @@ -35,7 +35,7 @@ /** */ -class Request { +final class Request { static final char CR = 13; static final char LF = 10; @@ -52,23 +52,23 @@ class Request { requestLine = readRequestLine(); } - public InputStream inputStream() { + InputStream inputStream() { return is; } - public OutputStream outputStream() { + OutputStream outputStream() { return os; } - static class PushbackStream { + static final class PushbackStream { private final InputStream is; private int pushback = -1; private boolean eof=false; - public PushbackStream(InputStream is) { + PushbackStream(InputStream is) { this.is = is; } - public int read() throws IOException { + int read() throws IOException { if(pushback!=-1) { try { return pushback; @@ -79,7 +79,7 @@ public int read() throws IOException { if(eof) return -1; return is.read(); } - public void skipWhitespace() throws IOException { + void skipWhitespace() throws IOException { int c; for(c=read();c==' ' || c=='\t';c=read()){} if(c==-1) eof=true; else pushback=c; @@ -143,7 +143,7 @@ private static String trimmed(BufferedBuilder bb) { /** * @returns the request line or the empty string if not found */ - public String requestLine() { + String requestLine() { return requestLine; } @@ -167,7 +167,7 @@ void append(char c) { } buffer[count++]=(byte)c; } - public String trimmed() { + String trimmed() { int start=0; while(start() { public InetSocketAddress run() { @@ -352,7 +352,7 @@ public InetSocketAddress run() { * The Dispatcher is responsible for accepting any connections and then * using those connections to process incoming requests. */ - class Dispatcher implements Runnable { + final class Dispatcher implements Runnable { public void run() { while (true) { try { @@ -419,7 +419,7 @@ private void closeConnection(HttpConnection conn) { } /* per exchange task */ - class Exchange implements Runnable { + final class Exchange implements Runnable { final HttpConnection connection; InputStream rawin; OutputStream rawout; @@ -647,7 +647,7 @@ private void runPerRequest() throws IOException { } /* used to link to 2 or more Filter.Chains together */ - class LinkHandler implements HttpHandler { + static final class LinkHandler implements HttpHandler { Filter.Chain nextChain; @@ -729,7 +729,7 @@ HttpServer getWrapper() { * Responsible for closing connections that have been idle or exceed other * limits */ - class ConnectionCleanerTask extends TimerTask { + final class ConnectionCleanerTask extends TimerTask { @Override public void run() { diff --git a/src/main/java/robaho/net/httpserver/StreamClosedException.java b/src/main/java/robaho/net/httpserver/StreamClosedException.java index e90e900..cec791f 100644 --- a/src/main/java/robaho/net/httpserver/StreamClosedException.java +++ b/src/main/java/robaho/net/httpserver/StreamClosedException.java @@ -27,6 +27,7 @@ import java.io.*; -class StreamClosedException extends IOException { +final class StreamClosedException extends IOException { + @Serial private static final long serialVersionUID = -4485921499356327937L; } diff --git a/src/main/java/robaho/net/httpserver/UndefLengthInputStream.java b/src/main/java/robaho/net/httpserver/UndefLengthInputStream.java index 0cf38f4..e906910 100644 --- a/src/main/java/robaho/net/httpserver/UndefLengthInputStream.java +++ b/src/main/java/robaho/net/httpserver/UndefLengthInputStream.java @@ -32,12 +32,13 @@ * close() does not close the underlying stream */ -class UndefLengthInputStream extends LeftOverInputStream { +final class UndefLengthInputStream extends LeftOverInputStream { UndefLengthInputStream(ExchangeImpl t, InputStream src) { super(t, src); } + @Override protected int readImpl(byte[] b, int off, int len) throws IOException { int n = in.read(b, off, len); @@ -46,17 +47,21 @@ protected int readImpl(byte[] b, int off, int len) throws IOException { return n; } + @Override public int available() throws IOException { return in.available(); } + @Override public boolean markSupported() { return false; } + @Override public void mark(int l) { } + @Override public void reset() throws IOException { throw new IOException("mark/reset not supported"); } diff --git a/src/main/java/robaho/net/httpserver/UndefLengthOutputStream.java b/src/main/java/robaho/net/httpserver/UndefLengthOutputStream.java index 5434db6..c1563b8 100644 --- a/src/main/java/robaho/net/httpserver/UndefLengthOutputStream.java +++ b/src/main/java/robaho/net/httpserver/UndefLengthOutputStream.java @@ -37,15 +37,16 @@ * The underlying connection needs to be closed afterwards. */ -class UndefLengthOutputStream extends FilterOutputStream { +final class UndefLengthOutputStream extends FilterOutputStream { private boolean closed = false; - ExchangeImpl t; + private final ExchangeImpl t; UndefLengthOutputStream(ExchangeImpl t, OutputStream src) { super(src); this.t = t; } + @Override public void write(int b) throws IOException { if (closed) { throw new IOException("stream closed"); @@ -53,6 +54,7 @@ public void write(int b) throws IOException { out.write(b); } + @Override public void write(byte[] b, int off, int len) throws IOException { Objects.checkFromIndexSize(off, len, b.length); if (len == 0) { @@ -64,6 +66,7 @@ public void write(byte[] b, int off, int len) throws IOException { out.write(b, off, len); } + @Override public void close() throws IOException { if (closed) { return; diff --git a/src/main/java/robaho/net/httpserver/Utils.java b/src/main/java/robaho/net/httpserver/Utils.java index 61d28b2..518a9f7 100644 --- a/src/main/java/robaho/net/httpserver/Utils.java +++ b/src/main/java/robaho/net/httpserver/Utils.java @@ -28,7 +28,7 @@ /** * Provides utility methods for checking header field names and quoted strings. */ -public class Utils { +public final class Utils { // ABNF primitives defined in RFC 7230 private static final boolean[] TCHAR = new boolean[256];