From 6026bd6b613afb08cbe1c4a692ac63e4e1a92218 Mon Sep 17 00:00:00 2001 From: Christoph John Date: Sat, 12 Aug 2023 08:40:32 +0200 Subject: [PATCH] Correct `Resource not found` in `FileUtilTest` (#654) (#662) * update FileUtil to resolve new 403 error for requests with User-Agent header * Updated User-Agent value for FileUtil * Added check for HttpUrlConnection to changes done in #460 --------- Co-authored-by: david-gibbs-ig --- .../src/main/java/quickfix/FileUtil.java | 16 +++++++++++++++- .../src/test/java/quickfix/FileUtilTest.java | 9 +++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/quickfixj-core/src/main/java/quickfix/FileUtil.java b/quickfixj-core/src/main/java/quickfix/FileUtil.java index c8159f7788..9bf228e137 100644 --- a/quickfixj-core/src/main/java/quickfix/FileUtil.java +++ b/quickfixj-core/src/main/java/quickfix/FileUtil.java @@ -24,7 +24,10 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.net.HttpURLConnection; import java.net.URL; +import java.net.URLConnection; + public class FileUtil { public static String fileAppendPath(String pathPrefix, String pathSuffix) { @@ -141,7 +144,18 @@ public static InputStream open(Class clazz, String name, Location... location break; case URL: try { - in = new URL(name).openStream(); + URL url = new URL(name); + 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() {