More safety checks on null/empty/blank input params

This commit is contained in:
Joakim Erdfelt 2022-10-13 09:50:43 -05:00
parent c66d64f761
commit 454ec0640e
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
1 changed files with 22 additions and 0 deletions

View File

@ -22,6 +22,7 @@ import java.util.Objects;
import org.eclipse.jetty.util.FileID; import org.eclipse.jetty.util.FileID;
import org.eclipse.jetty.util.Loader; import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil; import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.component.Container; import org.eclipse.jetty.util.component.Container;
import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.Dumpable;
@ -49,6 +50,9 @@ public interface ResourceFactory
*/ */
default Resource newSystemResource(String resource) default Resource newSystemResource(String resource)
{ {
if (StringUtil.isBlank(resource))
throw new IllegalArgumentException("Resource String is invalid: " + resource);
URL url = null; URL url = null;
// Try to format as a URL? // Try to format as a URL?
ClassLoader loader = Thread.currentThread().getContextClassLoader(); ClassLoader loader = Thread.currentThread().getContextClassLoader();
@ -110,6 +114,9 @@ public interface ResourceFactory
*/ */
default Resource newClassPathResource(String resource) default Resource newClassPathResource(String resource)
{ {
if (StringUtil.isBlank(resource))
throw new IllegalArgumentException("Resource String is invalid: " + resource);
URL url = ResourceFactory.class.getResource(resource); URL url = ResourceFactory.class.getResource(resource);
if (url == null) if (url == null)
@ -135,6 +142,9 @@ public interface ResourceFactory
*/ */
default Resource newMemoryResource(URL url) default Resource newMemoryResource(URL url)
{ {
if (url == null)
throw new IllegalArgumentException("URL is null");
return new MemoryResource(url); return new MemoryResource(url);
} }
@ -146,6 +156,9 @@ public interface ResourceFactory
*/ */
default Resource newResource(String resource) default Resource newResource(String resource)
{ {
if (StringUtil.isBlank(resource))
throw new IllegalArgumentException("Resource String is invalid: " + resource);
return newResource(URIUtil.toURI(resource)); return newResource(URIUtil.toURI(resource));
} }
@ -157,6 +170,9 @@ public interface ResourceFactory
*/ */
default Resource newResource(Path path) default Resource newResource(Path path)
{ {
if (path == null)
throw new IllegalArgumentException("Path is null");
return newResource(path.toUri()); return newResource(path.toUri());
} }
@ -168,11 +184,17 @@ public interface ResourceFactory
*/ */
default ResourceCollection newResource(List<URI> uris) default ResourceCollection newResource(List<URI> uris)
{ {
if ((uris == null) || (uris.isEmpty()))
throw new IllegalArgumentException("List of URIs is invalid");
return Resource.combine(uris.stream().map(this::newResource).toList()); return Resource.combine(uris.stream().map(this::newResource).toList());
} }
default Resource newResource(URL url) default Resource newResource(URL url)
{ {
if (url == null)
throw new IllegalArgumentException("URL is null");
try try
{ {
return newResource(url.toURI()); return newResource(url.toURI());