Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UNDERTOW-1879] Utilize o.w.common.Assert class for Null-Checks #1089

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -18,6 +18,7 @@
package io.undertow.security.impl;

import static io.undertow.UndertowMessages.MESSAGES;
import static org.wildfly.common.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 @@ -507,10 +505,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 @@ -523,10 +518,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 org.wildfly.common.Assert.checkNotNullParam;
import static org.wildfly.common.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 org.wildfly.common.Assert.checkNotNullParam;
import static org.wildfly.common.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 org.wildfly.common.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 org.wildfly.common.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 org.wildfly.common.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 org.wildfly.common.Assert.checkNotNullParam;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -191,10 +193,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
28 changes: 10 additions & 18 deletions core/src/main/java/io/undertow/util/FastConcurrentDirectDeque.java
Expand Up @@ -24,6 +24,8 @@

package io.undertow.util;

import static org.wildfly.common.Assert.checkNotNullParamWithNullPointerException;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.security.AccessController;
Expand Down Expand Up @@ -363,7 +365,7 @@ public Unsafe run() {
* Links e as first element.
*/
private Node linkFirst(E e) {
checkNotNull(e);
checkNotNullParamWithNullPointerException("e", e);
final Node<E> newNode = new Node<>(e);

restartFromHead:
Expand Down Expand Up @@ -396,7 +398,7 @@ else if (p.next == p) // PREV_TERMINATOR
* Links e as last element.
*/
private Node linkLast(E e) {
checkNotNull(e);
checkNotNullParamWithNullPointerException("e", e);
final Node<E> newNode = new Node<>(e);

restartFromTail:
Expand Down Expand Up @@ -784,16 +786,6 @@ else if (p == t

// Minor convenience utilities

/**
* Throws NullPointerException if argument is null.
*
* @param v the element
*/
private static void checkNotNull(Object v) {
if (v == null)
throw new NullPointerException();
}

/**
* Returns element unless it is null, in which case throws
* NoSuchElementException.
Expand Down Expand Up @@ -843,7 +835,7 @@ public FastConcurrentDirectDeque(Collection<? extends E> c) {
// Copy c into a private chain of Nodes
Node<E> h = null, t = null;
for (E e : c) {
checkNotNull(e);
checkNotNullParamWithNullPointerException("e", e);
Node<E> newNode = new Node<>(e);
if (h == null)
h = t = newNode;
Expand Down Expand Up @@ -1082,7 +1074,7 @@ public void push(E e) {
* @throws NullPointerException if the specified element is null
*/
public boolean removeFirstOccurrence(Object o) {
checkNotNull(o);
checkNotNullParamWithNullPointerException("o", o);
for (Node<E> p = first(); p != null; p = succ(p)) {
E item = p.item;
if (item != null && o.equals(item) && p.casItem(item, null)) {
Expand All @@ -1103,7 +1095,7 @@ public boolean removeFirstOccurrence(Object o) {
* @throws NullPointerException if the specified element is null
*/
public boolean removeLastOccurrence(Object o) {
checkNotNull(o);
checkNotNullParamWithNullPointerException("o", o);
for (Node<E> p = last(); p != null; p = pred(p)) {
E item = p.item;
if (item != null && o.equals(item) && p.casItem(item, null)) {
Expand Down Expand Up @@ -1199,7 +1191,7 @@ public boolean addAll(Collection<? extends E> c) {
// Copy c into a private chain of Nodes
Node<E> beginningOfTheEnd = null, last = null;
for (E e : c) {
checkNotNull(e);
checkNotNullParamWithNullPointerException("e", e);
Node<E> newNode = new Node<>(e);
if (beginningOfTheEnd == null)
beginningOfTheEnd = last = newNode;
Expand Down Expand Up @@ -1478,7 +1470,7 @@ public Spliterator<E> trySplit() {

public void forEachRemaining(Consumer<? super E> action) {
Node<E> p;
if (action == null) throw new NullPointerException();
checkNotNullParamWithNullPointerException("action", action);
final FastConcurrentDirectDeque<E> q = this.queue;
if (!exhausted &&
((p = current) != null || (p = q.first()) != null)) {
Expand All @@ -1495,7 +1487,7 @@ public void forEachRemaining(Consumer<? super E> action) {

public boolean tryAdvance(Consumer<? super E> action) {
Node<E> p;
if (action == null) throw new NullPointerException();
checkNotNullParamWithNullPointerException("action", action);
final FastConcurrentDirectDeque<E> q = this.queue;
if (!exhausted &&
((p = current) != null || (p = q.first()) != null)) {
Expand Down
26 changes: 8 additions & 18 deletions core/src/main/java/io/undertow/util/HeaderMap.java
Expand Up @@ -18,6 +18,8 @@

package io.undertow.util;

import static org.wildfly.common.Assert.checkNotNullParam;

import java.util.AbstractCollection;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -691,9 +693,7 @@ public HeaderMap add(HttpString headerName, String headerValue) {
}

public HeaderMap addFirst(final HttpString headerName, final String headerValue) {
if (headerName == null) {
throw new IllegalArgumentException("headerName is null");
}
checkNotNullParam("headerName", headerName);
if (headerValue == null) {
return this;
}
Expand All @@ -702,9 +702,7 @@ public HeaderMap addFirst(final HttpString headerName, final String headerValue)
}

public HeaderMap addLast(final HttpString headerName, final String headerValue) {
if (headerName == null) {
throw new IllegalArgumentException("headerName is null");
}
checkNotNullParam("headerName", headerName);
if (headerValue == null) {
return this;
}
Expand All @@ -719,9 +717,7 @@ public HeaderMap add(HttpString headerName, long headerValue) {


public HeaderMap addAll(HttpString headerName, Collection<String> headerValues) {
if (headerName == null) {
throw new IllegalArgumentException("headerName is null");
}
checkNotNullParam("headerName", headerName);
if (headerValues == null || headerValues.isEmpty()) {
return this;
}
Expand All @@ -732,9 +728,7 @@ public HeaderMap addAll(HttpString headerName, Collection<String> headerValues)
// put

public HeaderMap put(HttpString headerName, String headerValue) {
if (headerName == null) {
throw new IllegalArgumentException("headerName is null");
}
checkNotNullParam("headerName", headerName);
if (headerValue == null) {
remove(headerName);
return this;
Expand All @@ -746,19 +740,15 @@ public HeaderMap put(HttpString headerName, String headerValue) {
}

public HeaderMap put(HttpString headerName, long headerValue) {
if (headerName == null) {
throw new IllegalArgumentException("headerName is null");
}
checkNotNullParam("headerName", headerName);
final HeaderValues entry = getOrCreateEntry(headerName);
entry.clear();
entry.add(Long.toString(headerValue));
return this;
}

public HeaderMap putAll(HttpString headerName, Collection<String> headerValues) {
if (headerName == null) {
throw new IllegalArgumentException("headerName is null");
}
checkNotNullParam("headerName", headerName);
if (headerValues == null || headerValues.isEmpty()) {
remove(headerName);
return this;
Expand Down