From a6995708a8b928e810eab78012849b65854183b5 Mon Sep 17 00:00:00 2001 From: Christoph John Date: Thu, 27 Jul 2023 14:16:58 +0200 Subject: [PATCH] Added check for HttpUrlConnection to changes done in #460 --- .../src/main/java/quickfix/FileUtil.java | 16 ++++++++++++---- .../src/test/java/quickfix/FileUtilTest.java | 9 +++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/quickfixj-core/src/main/java/quickfix/FileUtil.java b/quickfixj-core/src/main/java/quickfix/FileUtil.java index b589980895..9bf228e137 100644 --- a/quickfixj-core/src/main/java/quickfix/FileUtil.java +++ b/quickfixj-core/src/main/java/quickfix/FileUtil.java @@ -26,6 +26,7 @@ import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.net.URLConnection; public class FileUtil { @@ -144,10 +145,17 @@ public static InputStream open(Class clazz, String name, Location... location case URL: try { URL url = new URL(name); - HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); - httpURLConnection.setRequestProperty("User-Agent", "Java-QuickFIXJ-FileUtil"); - httpURLConnection.connect(); - in = httpURLConnection.getInputStream(); + URLConnection urlConnection = url.openConnection(); + if (urlConnection instanceof HttpURLConnection) { + HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection; + httpURLConnection.setRequestProperty("User-Agent", "Java-QuickFIXJ-FileUtil"); + httpURLConnection.connect(); + in = httpURLConnection.getInputStream(); + } else { + if (urlConnection != null) { + in = urlConnection.getInputStream(); + } + } } catch (IOException e) { // ignore } diff --git a/quickfixj-core/src/test/java/quickfix/FileUtilTest.java b/quickfixj-core/src/test/java/quickfix/FileUtilTest.java index 16cffdc225..c50b4a21a4 100644 --- a/quickfixj-core/src/test/java/quickfix/FileUtilTest.java +++ b/quickfixj-core/src/test/java/quickfix/FileUtilTest.java @@ -67,6 +67,15 @@ public void testURLLocation() throws Exception { } } + @Test + public void testJARURLLocation() throws Exception { + // just test that we don't run into a ClassCastException + InputStream in = FileUtil.open(Message.class, "jar:file:/foo.bar!/"); + if (in != null) { + in.close(); + } + } + @Test // QFJ-775 public void testSessionIDFileName() {