Issue #5320 - do all exception handling in XmlBasedHttpClientProvider

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-10-01 14:03:25 +10:00
parent 5b96f6f984
commit 81c88cdde0
2 changed files with 33 additions and 26 deletions

View File

@ -19,37 +19,15 @@
package org.eclipse.jetty.websocket.client;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.webapp.WebAppClassLoader;
import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
public final class HttpClientProvider
{
public static HttpClient get(WebSocketContainerScope scope)
{
Logger logger = Log.getLogger(HttpClientProvider.class);
// Try to load a HttpClient from a jetty-websocket-httpclient.xml configuration file.
// If WebAppClassLoader run with server class access, otherwise run normally.
try
{
try
{
return WebAppClassLoader.runWithServerClassAccess(() -> XmlBasedHttpClientProvider.get(scope));
}
catch (NoClassDefFoundError | ClassNotFoundException e)
{
if (logger.isDebugEnabled())
logger.debug("Could not use WebAppClassLoader to run with Server class access", e);
return XmlBasedHttpClientProvider.get(scope);
}
}
catch (Throwable t)
{
if (logger.isDebugEnabled())
logger.debug("Failure to load HttpClient from XML", t);
}
HttpClient httpClient = XmlBasedHttpClientProvider.get(scope);
if (httpClient != null)
return httpClient;
return DefaultHttpClientProvider.newHttpClient(scope);
}

View File

@ -22,18 +22,47 @@ import java.net.URL;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppClassLoader;
import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
import org.eclipse.jetty.xml.XmlConfiguration;
class XmlBasedHttpClientProvider
{
public static final Logger LOG = Log.getLogger(XmlBasedHttpClientProvider.class);
public static HttpClient get(@SuppressWarnings("unused") WebSocketContainerScope scope)
{
URL resource = Thread.currentThread().getContextClassLoader().getResource("jetty-websocket-httpclient.xml");
if (resource == null)
return null;
// Try to load a HttpClient from a jetty-websocket-httpclient.xml configuration file.
// If WebAppClassLoader run with server class access, otherwise run normally.
try
{
try
{
return WebAppClassLoader.runWithServerClassAccess(() -> newHttpClient(resource));
}
catch (NoClassDefFoundError | ClassNotFoundException e)
{
if (LOG.isDebugEnabled())
LOG.debug("Could not use WebAppClassLoader to run with Server class access", e);
return newHttpClient(resource);
}
}
catch (Throwable t)
{
LOG.warn("Failure to load HttpClient from XML", t);
}
return null;
}
private static HttpClient newHttpClient(URL resource)
{
try
{
XmlConfiguration configuration = new XmlConfiguration(Resource.newResource(resource));
@ -41,7 +70,7 @@ class XmlBasedHttpClientProvider
}
catch (Throwable t)
{
Log.getLogger(XmlBasedHttpClientProvider.class).warn("Unable to load: " + resource, t);
LOG.warn("Unable to load: {}", resource, t);
}
return null;