Add some null checks to the RestfulServer

This commit is contained in:
James Agnew 2018-10-05 08:33:00 -04:00
parent 9c27e8e6dd
commit ca54409ce3
1 changed files with 28 additions and 16 deletions

View File

@ -1333,15 +1333,17 @@ public class RestfulServer extends HttpServlet implements IRestfulServer<Servlet
} }
/** /**
* Register a group of providers. These could be Resource Providers, "plain" providers or a mixture of the two. * Register a group of theProviders. These could be Resource Providers, "plain" theProviders or a mixture of the two.
* *
* @param providers a {@code Collection} of providers. The parameter could be null or an empty {@code Collection} * @param theProviders a {@code Collection} of theProviders. The parameter could be null or an empty {@code Collection}
*/ */
public void registerProviders(Collection<?> providers) { public void registerProviders(Collection<?> theProviders) {
Validate.noNullElements(theProviders, "theProviders must not contain any null elements");
myProviderRegistrationMutex.lock(); myProviderRegistrationMutex.lock();
try { try {
if (!myStarted) { if (!myStarted) {
for (Object provider : providers) { for (Object provider : theProviders) {
ourLog.info("Registration of provider [" + provider.getClass().getName() + "] will be delayed until FHIR server startup"); ourLog.info("Registration of provider [" + provider.getClass().getName() + "] will be delayed until FHIR server startup");
if (provider instanceof IResourceProvider) { if (provider instanceof IResourceProvider) {
myResourceProviders.add((IResourceProvider) provider); myResourceProviders.add((IResourceProvider) provider);
@ -1354,19 +1356,21 @@ public class RestfulServer extends HttpServlet implements IRestfulServer<Servlet
} finally { } finally {
myProviderRegistrationMutex.unlock(); myProviderRegistrationMutex.unlock();
} }
registerProviders(providers, false); registerProviders(theProviders, false);
} }
/* /*
* Inner method to actually register providers * Inner method to actually register theProviders
*/ */
protected void registerProviders(Collection<?> providers, boolean inInit) { protected void registerProviders(Collection<?> theProviders, boolean inInit) {
Validate.noNullElements(theProviders, "theProviders must not contain any null elements");
List<IResourceProvider> newResourceProviders = new ArrayList<>(); List<IResourceProvider> newResourceProviders = new ArrayList<>();
List<Object> newPlainProviders = new ArrayList<>(); List<Object> newPlainProviders = new ArrayList<>();
ProvidedResourceScanner providedResourceScanner = new ProvidedResourceScanner(getFhirContext()); ProvidedResourceScanner providedResourceScanner = new ProvidedResourceScanner(getFhirContext());
if (providers != null) { if (theProviders != null) {
for (Object provider : providers) { for (Object provider : theProviders) {
if (provider instanceof IResourceProvider) { if (provider instanceof IResourceProvider) {
IResourceProvider rsrcProvider = (IResourceProvider) provider; IResourceProvider rsrcProvider = (IResourceProvider) provider;
Class<? extends IBaseResource> resourceType = rsrcProvider.getResourceType(); Class<? extends IBaseResource> resourceType = rsrcProvider.getResourceType();
@ -1520,12 +1524,14 @@ public class RestfulServer extends HttpServlet implements IRestfulServer<Servlet
/** /**
* Sets (or clears) the list of interceptors * Sets (or clears) the list of interceptors
* *
* @param theList The list of interceptors (may be null) * @param theInterceptors The list of interceptors (may be null)
*/ */
public void setInterceptors(IServerInterceptor... theList) { public void setInterceptors(IServerInterceptor... theInterceptors) {
Validate.noNullElements(theInterceptors, "theInterceptors must not contain any null elements");
myInterceptors.clear(); myInterceptors.clear();
if (theList != null) { if (theInterceptors != null) {
myInterceptors.addAll(Arrays.asList(theList)); myInterceptors.addAll(Arrays.asList(theInterceptors));
} }
} }
@ -1535,6 +1541,8 @@ public class RestfulServer extends HttpServlet implements IRestfulServer<Servlet
* @see #setResourceProviders(Collection) * @see #setResourceProviders(Collection)
*/ */
public void setPlainProviders(Collection<Object> theProviders) { public void setPlainProviders(Collection<Object> theProviders) {
Validate.noNullElements(theProviders, "theProviders must not contain any null elements");
myPlainProviders.clear(); myPlainProviders.clear();
if (theProviders != null) { if (theProviders != null) {
myPlainProviders.addAll(theProviders); myPlainProviders.addAll(theProviders);
@ -1547,6 +1555,8 @@ public class RestfulServer extends HttpServlet implements IRestfulServer<Servlet
* @see #setResourceProviders(Collection) * @see #setResourceProviders(Collection)
*/ */
public void setProviders(Object... theProviders) { public void setProviders(Object... theProviders) {
Validate.noNullElements(theProviders, "theProviders must not contain any null elements");
myPlainProviders.clear(); myPlainProviders.clear();
if (theProviders != null) { if (theProviders != null) {
myPlainProviders.addAll(Arrays.asList(theProviders)); myPlainProviders.addAll(Arrays.asList(theProviders));
@ -1556,10 +1566,12 @@ public class RestfulServer extends HttpServlet implements IRestfulServer<Servlet
/** /**
* Sets the resource providers for this server * Sets the resource providers for this server
*/ */
public void setResourceProviders(Collection<IResourceProvider> theResourceProviders) { public void setResourceProviders(Collection<IResourceProvider> theProviders) {
Validate.noNullElements(theProviders, "theProviders must not contain any null elements");
myResourceProviders.clear(); myResourceProviders.clear();
if (theResourceProviders != null) { if (theProviders != null) {
myResourceProviders.addAll(theResourceProviders); myResourceProviders.addAll(theProviders);
} }
} }