Skip to content

Commit

Permalink
Issue #5320 - do all exception handling in XmlBasedHttpClientProvider
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Oct 1, 2020
1 parent 5b96f6f commit 81c88cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
Expand Up @@ -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);
}
Expand Down
Expand Up @@ -22,26 +22,55 @@

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));
return (HttpClient)configuration.configure();
}
catch (Throwable t)
{
Log.getLogger(XmlBasedHttpClientProvider.class).warn("Unable to load: " + resource, t);
LOG.warn("Unable to load: {}", resource, t);
}

return null;
Expand Down

0 comments on commit 81c88cd

Please sign in to comment.