Skip to content

Commit

Permalink
[UNDERTOW-1879] Utilize new Assert class for Null-Checks
Browse files Browse the repository at this point in the history
  • Loading branch information
boris-unckel committed Jun 13, 2021
1 parent 77c3b9b commit 19b529a
Show file tree
Hide file tree
Showing 20 changed files with 373 additions and 149 deletions.
9 changes: 9 additions & 0 deletions core/src/main/java/io/undertow/UndertowMessages.java
Expand Up @@ -618,4 +618,13 @@ public interface UndertowMessages {

@Message(id = 198, value = "Blocking write timed out after %s nanoseconds.")
WriteTimeoutException blockingWriteTimedOut(long timeoutNanoseconds);

@Message(id = 199, value = "Argument %s cannot be null")
NullPointerException argumentCannotBeNullNPE(final String argument);

@Message(id = 200, value = "Array index %d of parameter '%s' cannot be null")
IllegalArgumentException nullArrayParam(int index, String name);

@Message(id = 201, value = "Parameter '%s' must not be empty")
IllegalArgumentException emptyParam(String name);
}
Expand Up @@ -18,6 +18,7 @@
package io.undertow.security.impl;

import static io.undertow.UndertowMessages.MESSAGES;
import static io.undertow.util.Assert.checkNotNullParamWithNullPointerException;

import io.undertow.security.api.SessionNonceManager;
import io.undertow.server.HttpServerExchange;
Expand Down Expand Up @@ -429,10 +430,7 @@ private static class NonceHolder {
private final String nonce;

private NonceHolder(final String nonce) {
if (nonce == null) {
throw new NullPointerException("nonce must not be null.");
}
this.nonce = nonce;
this.nonce = checkNotNullParamWithNullPointerException("nonce", nonce);
}

@Override
Expand Down Expand Up @@ -511,10 +509,7 @@ private class InvalidNonceCleaner implements Runnable {
private final String nonce;

private InvalidNonceCleaner(final String nonce) {
if (nonce == null) {
throw new NullPointerException("nonce must not be null.");
}
this.nonce = nonce;
this.nonce = checkNotNullParamWithNullPointerException("nonce", nonce);
}

public void run() {
Expand All @@ -527,10 +522,7 @@ private class KnownNonceCleaner implements Runnable {
private final String nonce;

private KnownNonceCleaner(final String nonce) {
if (nonce == null) {
throw new NullPointerException("nonce must not be null.");
}
this.nonce = nonce;
this.nonce = checkNotNullParamWithNullPointerException("nonce", nonce);
}

public void run() {
Expand Down
Expand Up @@ -18,6 +18,9 @@

package io.undertow.server.handlers;

import static io.undertow.util.Assert.checkNotNullParam;
import static io.undertow.util.Assert.checkNotNullParamWithNullPointerException;

import io.undertow.UndertowLogger;
import io.undertow.UndertowMessages;
import io.undertow.server.ConduitWrapper;
Expand All @@ -37,7 +40,6 @@
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -215,20 +217,18 @@ public static final class Builder {
private Builder() {}

public Builder readTimeout(Duration readTimeout) {
this.readTimeout = Objects.requireNonNull(readTimeout, "A read timeout is required");
this.readTimeout = checkNotNullParamWithNullPointerException("readTimeout", readTimeout);
return this;
}

public Builder nextHandler(HttpHandler nextHandler) {
this.nextHandler = Objects.requireNonNull(nextHandler, "HttpHandler is required");
this.nextHandler = checkNotNullParamWithNullPointerException("nextHandler", nextHandler);
return this;
}

public HttpHandler build() {
HttpHandler next = Objects.requireNonNull(nextHandler, "HttpHandler is required");
if (readTimeout == null) {
throw new IllegalArgumentException("A read timeout is required");
}
HttpHandler next = checkNotNullParamWithNullPointerException("nextHandler", nextHandler);
checkNotNullParam("readTimeout", readTimeout);
if (readTimeout.isZero() || readTimeout.isNegative()) {
throw new IllegalArgumentException("Read timeout must be positive: " + readTimeout);
}
Expand Down
Expand Up @@ -18,6 +18,9 @@

package io.undertow.server.handlers;

import static io.undertow.util.Assert.checkNotNullParam;
import static io.undertow.util.Assert.checkNotNullParamWithNullPointerException;

import io.undertow.UndertowLogger;
import io.undertow.UndertowMessages;
import io.undertow.server.ConduitWrapper;
Expand All @@ -37,7 +40,6 @@
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -247,20 +249,18 @@ public static final class Builder {
private Builder() {}

public Builder writeTimeout(Duration writeTimeout) {
this.writeTimeout = Objects.requireNonNull(writeTimeout, "A write timeout is required");
this.writeTimeout = checkNotNullParamWithNullPointerException("writeTimeout", writeTimeout);
return this;
}

public Builder nextHandler(HttpHandler nextHandler) {
this.nextHandler = Objects.requireNonNull(nextHandler, "HttpHandler is required");
this.nextHandler = checkNotNullParamWithNullPointerException("nextHandler", nextHandler);
return this;
}

public HttpHandler build() {
HttpHandler next = Objects.requireNonNull(nextHandler, "HttpHandler is required");
if (writeTimeout == null) {
throw new IllegalArgumentException("A write timeout is required");
}
HttpHandler next = checkNotNullParamWithNullPointerException("nextHandler", nextHandler);
checkNotNullParam("writeTimeout", writeTimeout);
if (writeTimeout.isZero() || writeTimeout.isNegative()) {
throw new IllegalArgumentException("Write timeout must be positive: " + writeTimeout);
}
Expand Down
Expand Up @@ -18,6 +18,8 @@

package io.undertow.server.handlers;

import static io.undertow.util.Assert.checkNotEmptyParam;

import io.undertow.Handlers;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
Expand Down Expand Up @@ -66,11 +68,10 @@ public synchronized void addProtocol(String productString, HttpUpgradeListener o
}

private synchronized void addProtocol(String productString, HttpUpgradeListener openListener, final ChannelListener<? super StreamConnection> channelListener, final HttpUpgradeHandshake handshake) {
if (productString == null) {
throw new IllegalArgumentException("productString is null");
}
checkNotEmptyParam("productString", productString);

if (openListener == null && channelListener == null) {
throw new IllegalArgumentException("openListener is null");
throw new IllegalArgumentException("openListener and channelListener are null");
}
if(openListener == null) {
openListener = new HttpUpgradeListener() {
Expand Down
Expand Up @@ -17,6 +17,8 @@
*/
package io.undertow.server.handlers;

import static io.undertow.util.Assert.checkNotNullParam;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -58,9 +60,7 @@ public RequestLimitingHandler(int maximumConcurrentRequests, HttpHandler nextHan
* @param nextHandler the next handler
*/
public RequestLimitingHandler(int maximumConcurrentRequests, int queueSize, HttpHandler nextHandler) {
if (nextHandler == null) {
throw new IllegalArgumentException("nextHandler is null");
}
checkNotNullParam("nextHandler", nextHandler);
if (maximumConcurrentRequests < 1) {
throw new IllegalArgumentException("Maximum concurrent requests must be at least 1");
}
Expand All @@ -76,9 +76,7 @@ public RequestLimitingHandler(int maximumConcurrentRequests, int queueSize, Http
* @param nextHandler the next handler
*/
public RequestLimitingHandler(RequestLimit requestLimit, HttpHandler nextHandler) {
if (nextHandler == null) {
throw new IllegalArgumentException("nextHandler is null");
}
checkNotNullParam("nextHandler", nextHandler);
this.requestLimit = requestLimit;
this.nextHandler = nextHandler;
}
Expand Down
Expand Up @@ -18,6 +18,8 @@

package io.undertow.server.handlers.cache;

import static io.undertow.util.Assert.checkNotNullParam;

import org.xnio.BufferAllocator;

import java.nio.ByteBuffer;
Expand Down Expand Up @@ -64,7 +66,7 @@ public LimitedBufferSlicePool(final BufferAllocator<ByteBuffer> allocator, final
}
buffersPerRegion = maxRegionSize / bufferSize;
this.bufferSize = bufferSize;
this.allocator = allocator;
this.allocator = checkNotNullParam("allocator", allocator);
this.maxRegions = maxRegions;
}

Expand Down
Expand Up @@ -18,6 +18,8 @@

package io.undertow.server.handlers.proxy.mod_cluster;

import static io.undertow.util.Assert.checkNotNullParam;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -194,10 +196,9 @@ public synchronized void start() {
* @throws IOException
*/
public synchronized void advertise(MCMPConfig config) throws IOException {
final MCMPConfig.AdvertiseConfig advertiseConfig = config.getAdvertiseConfig();
if (advertiseConfig == null) {
throw new IllegalArgumentException("advertise not enabled");
}
checkNotNullParam("config", config);
final MCMPConfig.AdvertiseConfig advertiseConfig = checkNotNullParam("config.getAdvertiseConfig", config.getAdvertiseConfig());

MCMPAdvertiseTask.advertise(container, advertiseConfig, xnioWorker);
}

Expand Down

0 comments on commit 19b529a

Please sign in to comment.