Skip to content

Commit

Permalink
Tidy jdk package - final classes, getFirst, isEmpty, whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Nov 26, 2024
1 parent 0736537 commit 7050aa3
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 30 deletions.
3 changes: 1 addition & 2 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/BaseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.avaje.jex.Routing.Type;
import io.avaje.jex.routes.SpiRoutes;

class BaseHandler implements HttpHandler {
final class BaseHandler implements HttpHandler {

private final SpiRoutes routes;

Expand All @@ -23,7 +23,6 @@ void waitForIdle(long maxSeconds) {

@Override
public void handle(HttpExchange exchange) throws IOException {

JdkContext ctx = (JdkContext) exchange.getAttribute("JdkContext");
ExchangeHandler handlerConsumer =
(ExchangeHandler) exchange.getAttribute("SpiRoutes.Entry.Handler");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import com.sun.net.httpserver.HttpExchange;

class BufferedOutStream extends OutputStream {
final class BufferedOutStream extends OutputStream {

private static final long MAX = Long.getLong("jex.outputBuffer.max", 1024);
private static final int INITIAL =
Expand Down
8 changes: 4 additions & 4 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/CookieParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Parse cookies based on RFC6265 skipping parameters.
*/
class CookieParser {
final class CookieParser {

private static final String QUOTE = "\"";
private static final char[] QUOTE_CHARS = QUOTE.toCharArray();
Expand All @@ -30,7 +30,7 @@ private CookieParser() {
*
* @param rawHeader a value of '{@code Cookie:}' header.
*/
public static Map<String, String> parse(String rawHeader) {
static Map<String, String> parse(String rawHeader) {
if (rawHeader == null) {
return emptyMap();
}
Expand Down Expand Up @@ -102,7 +102,7 @@ static List<String> tokenize(char separator, String text) {
}
token.append(ch);
} else if (ch == separator) {
if (token.length() > 0) {
if (!token.isEmpty()) {
result.add(token.toString());
}
token.setLength(0);
Expand All @@ -117,7 +117,7 @@ static List<String> tokenize(char separator, String text) {
token.append(ch);
}
}
if (token.length() > 0) {
if (!token.isEmpty()) {
result.add(token.toString());
}
return result;
Expand Down
14 changes: 5 additions & 9 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/JdkContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import io.avaje.jex.security.Role;
import io.avaje.jex.spi.SpiContext;

class JdkContext implements Context, SpiContext {
final class JdkContext implements Context, SpiContext {

private static final String UTF8 = "UTF8";
private static final int SC_MOVED_TEMPORARILY = 302;
Expand Down Expand Up @@ -215,7 +215,7 @@ public String responseHeader(String key) {

private String header(Headers headers, String name) {
final List<String> values = headers.get(name);
return (values == null || values.isEmpty()) ? null : values.get(0);
return (values == null || values.isEmpty()) ? null : values.getFirst();
}

@Override
Expand All @@ -237,7 +237,7 @@ public String pathParam(String name) {
@Override
public String queryParam(String name) {
final List<String> vals = queryParams(name);
return vals == null || vals.isEmpty() ? null : vals.get(0);
return vals == null || vals.isEmpty() ? null : vals.getFirst();
}

private Map<String, List<String>> queryParams() {
Expand All @@ -263,7 +263,7 @@ public Map<String, String> queryParamMap() {
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
final List<String> value = entry.getValue();
if (value != null && !value.isEmpty()) {
single.put(entry.getKey(), value.get(0));
single.put(entry.getKey(), value.getFirst());
}
}
return single;
Expand Down Expand Up @@ -347,17 +347,14 @@ public Context html(String content) {

@Override
public Context write(String content) {

write(content.getBytes(StandardCharsets.UTF_8));
return this;
}

@Override
public Context write(byte[] bytes) {

try (var os = exchange.getResponseBody()) {
exchange.sendResponseHeaders(statusCode(), bytes.length == 0 ? -1 : bytes.length);

os.write(bytes);
os.flush();
} catch (IOException e) {
Expand All @@ -368,7 +365,6 @@ public Context write(byte[] bytes) {

@Override
public Context write(InputStream is) {

try (is; var os = outputStream()) {
is.transferTo(os);
} catch (IOException e) {
Expand All @@ -393,7 +389,7 @@ public Map<String, String> headerMap() {
for (Map.Entry<String, List<String>> entry : exchange.getRequestHeaders().entrySet()) {
final List<String> value = entry.getValue();
if (!value.isEmpty()) {
map.put(entry.getKey(), value.get(0));
map.put(entry.getKey(), value.getFirst());
}
}
return map;
Expand Down
2 changes: 0 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/JdkFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ public JdkFilter(HttpFilter handler) {

@Override
public void doFilter(HttpExchange exchange, Chain chain) throws IOException {

var ctx = (JdkContext) exchange.getAttribute("JdkContext");

handler.filter(ctx, () -> chain.doFilter(exchange));
}

Expand Down
2 changes: 1 addition & 1 deletion avaje-jex/src/main/java/io/avaje/jex/jdk/JdkJexServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import java.lang.System.Logger.Level;

class JdkJexServer implements Jex.Server {
final class JdkJexServer implements Jex.Server {

private static final System.Logger log = AppLog.getLogger("io.avaje.jex");

Expand Down
6 changes: 2 additions & 4 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@
import io.avaje.jex.core.SpiServiceManager;
import io.avaje.jex.routes.SpiRoutes;

public class JdkServerStart {
public final class JdkServerStart {

private static final System.Logger log = AppLog.getLogger("io.avaje.jex");

public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceManager) {

try {
final HttpServer server;

var port = new InetSocketAddress(jex.config().port());
final var sslContext = jex.config().sslContext();

final HttpServer server;
final String scheme;
if (sslContext != null) {
var httpsServer = HttpsServer.create(port, 0);
Expand Down
9 changes: 2 additions & 7 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/RoutingFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.avaje.jex.ExchangeHandler;
import io.avaje.jex.Routing;
import io.avaje.jex.Routing.Type;
import io.avaje.jex.http.HttpResponseException;
import io.avaje.jex.http.NotFoundException;
import io.avaje.jex.routes.SpiRoutes;
import io.avaje.jex.spi.SpiContext;
Expand All @@ -31,15 +30,14 @@ void waitForIdle(long maxSeconds) {

@Override
public void doFilter(HttpExchange exchange, Filter.Chain chain) {

final String uri = exchange.getRequestURI().getPath();
final Routing.Type routeType = mgr.lookupRoutingType(exchange.getRequestMethod());
final SpiRoutes.Entry route = routes.match(routeType, uri);

if (route == null) {
var ctx = new JdkContext(mgr, exchange, uri, Set.of());
routes.inc();
try {
try (exchange) {
processNoRoute(ctx, uri, routeType);
exchange.setAttribute("JdkContext", ctx);
chain.doFilter(exchange);
Expand All @@ -48,11 +46,10 @@ public void doFilter(HttpExchange exchange, Filter.Chain chain) {
handleException(ctx, e);
} finally {
routes.dec();
exchange.close();
}
} else {
route.inc();
try {
try (exchange) {
final Map<String, String> params = route.pathParams(uri);
JdkContext ctx = new JdkContext(mgr, exchange, route.matchPath(), params, route.roles());
try {
Expand All @@ -67,13 +64,11 @@ public void doFilter(HttpExchange exchange, Filter.Chain chain) {
}
} finally {
route.dec();
exchange.close();
}
}
}

private void handleNoResponse(HttpExchange exchange) throws IOException {

if (exchange.getResponseCode() == -1) {
exchange.sendResponseHeaders(204, -1);
}
Expand Down

0 comments on commit 7050aa3

Please sign in to comment.