Skip to content

Commit 478c987

Browse files
committed
add user agent for browsing
1 parent 5f445fc commit 478c987

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/main/java/io/bioimage/modelrunner/bioimageio/download/DownloadModel.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import io.bioimage.modelrunner.bioimageio.description.weights.WeightFormat;
4949
import io.bioimage.modelrunner.engine.EngineInfo;
5050
import io.bioimage.modelrunner.engine.installation.FileDownloader;
51+
import io.bioimage.modelrunner.system.PlatformDetection;
52+
import io.bioimage.modelrunner.utils.CommonUtils;
5153
import io.bioimage.modelrunner.utils.Constants;
5254
import io.bioimage.modelrunner.utils.ZipUtils;
5355

@@ -592,6 +594,7 @@ public static long getFileSize(URL url) {
592594
HttpURLConnection conn = null;
593595
try {
594596
conn = (HttpURLConnection) url.openConnection();
597+
conn.setRequestProperty("User-Agent", CommonUtils.getJDLLUserAgent());
595598
if (conn.getResponseCode() >= 300 && conn.getResponseCode() <= 308)
596599
return getFileSize(redirectedURL(url));
597600
if (conn.getResponseCode() != 200)
@@ -676,6 +679,7 @@ public static URL redirectedURL(URL url) throws MalformedURLException, URISyntax
676679
HttpURLConnection conn;
677680
try {
678681
conn = (HttpURLConnection) url.openConnection();
682+
conn.setRequestProperty("User-Agent", CommonUtils.getJDLLUserAgent());
679683
statusCode = conn.getResponseCode();
680684
} catch (IOException ex) {
681685
return url;

src/main/java/io/bioimage/modelrunner/utils/CommonUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.text.SimpleDateFormat;
2626
import java.util.Calendar;
2727

28+
import io.bioimage.modelrunner.system.PlatformDetection;
2829
import net.imglib2.RandomAccessibleInterval;
2930
import net.imglib2.type.NativeType;
3031
import net.imglib2.type.numeric.RealType;
@@ -46,6 +47,8 @@
4647
*/
4748
public class CommonUtils {
4849

50+
private static String USER_AGENT;
51+
4952
public static Calendar cal;
5053

5154
/**
@@ -161,4 +164,23 @@ public static boolean int64Overflows(long[] arr) {
161164
public static void main(String[] args) {
162165
System.out.println(getTime());
163166
}
167+
168+
/**
169+
* Create a user agent for browsing
170+
* @return the user agent
171+
*/
172+
public static String getJDLLUserAgent() {
173+
if (USER_AGENT != null)
174+
return USER_AGENT;
175+
String os = "Unknown";
176+
if (PlatformDetection.isWindows()) {
177+
os = "Windows";
178+
} else if (PlatformDetection.isLinux()) {
179+
os = "Linux";
180+
} else if (PlatformDetection.isMacOS()) {
181+
os = "MacOS";
182+
}
183+
USER_AGENT = "JDLL/0.5.0(" + os + "; Java " + PlatformDetection.getJavaVersion();
184+
return USER_AGENT;
185+
}
164186
}

0 commit comments

Comments
 (0)