Skip to content

Commit e612ee6

Browse files
Correct Resource not found in FileUtilTest (#654)
* 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 <[email protected]>
1 parent 5db9c8f commit e612ee6

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

quickfixj-core/src/main/java/quickfix/FileUtil.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import java.io.FileNotFoundException;
2525
import java.io.IOException;
2626
import java.io.InputStream;
27+
import java.net.HttpURLConnection;
2728
import java.net.URL;
29+
import java.net.URLConnection;
30+
2831

2932
public class FileUtil {
3033
public static String fileAppendPath(String pathPrefix, String pathSuffix) {
@@ -141,7 +144,18 @@ public static InputStream open(Class<?> clazz, String name, Location... location
141144
break;
142145
case URL:
143146
try {
144-
in = new URL(name).openStream();
147+
URL url = new URL(name);
148+
URLConnection urlConnection = url.openConnection();
149+
if (urlConnection instanceof HttpURLConnection) {
150+
HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection;
151+
httpURLConnection.setRequestProperty("User-Agent", "Java-QuickFIXJ-FileUtil");
152+
httpURLConnection.connect();
153+
in = httpURLConnection.getInputStream();
154+
} else {
155+
if (urlConnection != null) {
156+
in = urlConnection.getInputStream();
157+
}
158+
}
145159
} catch (IOException e) {
146160
// ignore
147161
}

quickfixj-core/src/test/java/quickfix/FileUtilTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ public void testURLLocation() throws Exception {
6767
}
6868
}
6969

70+
@Test
71+
public void testJARURLLocation() throws Exception {
72+
// just test that we don't run into a ClassCastException
73+
InputStream in = FileUtil.open(Message.class, "jar:file:/foo.bar!/");
74+
if (in != null) {
75+
in.close();
76+
}
77+
}
78+
7079
@Test
7180
// QFJ-775
7281
public void testSessionIDFileName() {

0 commit comments

Comments
 (0)