Skip to content

Commit 7a26257

Browse files
authored
Merge pull request #780 from sbliven/bugfixes-4.2
Fix NonstandardProteinCompoundTest failure
2 parents 211276d + 99fda9d commit 7a26257

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

biojava-core/src/main/java/org/biojava/nbio/core/sequence/loader/UniprotProxySequenceReader.java

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public class UniprotProxySequenceReader<C extends Compound> implements ProxySequ
8888
private static final String TREMBLID_PATTERN = "[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}";
8989
public static final Pattern UP_AC_PATTERN = Pattern.compile("(" + SPID_PATTERN + "|" + TREMBLID_PATTERN + ")");
9090

91-
private static String uniprotbaseURL = "http://www.uniprot.org"; //"http://pir.uniprot.org";
91+
private static String uniprotbaseURL = "https://www.uniprot.org"; //"http://pir.uniprot.org";
9292
private static String uniprotDirectoryCache = null;
9393
private String sequence;
9494
private CompoundSet<C> compoundSet;
@@ -414,6 +414,61 @@ private void writeCache(StringBuilder sb, String accession) throws IOException {
414414
fw.write(sb.toString());
415415
fw.close();
416416
}
417+
418+
/**
419+
* Open a URL connection.
420+
*
421+
* Follows redirects.
422+
* @param url
423+
* @throws IOException
424+
*/
425+
private static HttpURLConnection openURLConnection(URL url) throws IOException {
426+
// This method should be moved to a utility class in BioJava 5.0
427+
428+
final int timeout = 5000;
429+
final String useragent = "BioJava";
430+
431+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
432+
conn.setRequestProperty("User-Agent", useragent);
433+
conn.setInstanceFollowRedirects(true);
434+
conn.setConnectTimeout(timeout);
435+
conn.setReadTimeout(timeout);
436+
437+
int status = conn.getResponseCode();
438+
while (status == HttpURLConnection.HTTP_MOVED_TEMP
439+
|| status == HttpURLConnection.HTTP_MOVED_PERM
440+
|| status == HttpURLConnection.HTTP_SEE_OTHER) {
441+
// Redirect!
442+
String newUrl = conn.getHeaderField("Location");
443+
444+
if(newUrl.equals(url.toString())) {
445+
throw new IOException("Cyclic redirect detected at "+newUrl);
446+
}
447+
448+
// Preserve cookies
449+
String cookies = conn.getHeaderField("Set-Cookie");
450+
451+
// open the new connection again
452+
url = new URL(newUrl);
453+
conn.disconnect();
454+
conn = (HttpURLConnection) url.openConnection();
455+
if(cookies != null) {
456+
conn.setRequestProperty("Cookie", cookies);
457+
}
458+
conn.addRequestProperty("User-Agent", useragent);
459+
conn.setInstanceFollowRedirects(true);
460+
conn.setConnectTimeout(timeout);
461+
conn.setReadTimeout(timeout);
462+
conn.connect();
463+
464+
status = conn.getResponseCode();
465+
466+
logger.info("Redirecting from {} to {}", url, newUrl);
467+
}
468+
conn.connect();
469+
470+
return conn;
471+
}
417472

418473
private StringBuilder fetchUniprotXML(String uniprotURL)
419474
throws IOException, CompoundNotFoundException {
@@ -423,11 +478,9 @@ private StringBuilder fetchUniprotXML(String uniprotURL)
423478
int attempt = 5;
424479
List<String> errorCodes = new ArrayList<String>();
425480
while(attempt > 0) {
426-
HttpURLConnection uniprotConnection = (HttpURLConnection) uniprot.openConnection();
427-
uniprotConnection.setRequestProperty("User-Agent", "BioJava");
428-
uniprotConnection.connect();
481+
HttpURLConnection uniprotConnection = openURLConnection(uniprot);
429482
int statusCode = uniprotConnection.getResponseCode();
430-
if (statusCode == 200) {
483+
if (statusCode == HttpURLConnection.HTTP_OK) {
431484
BufferedReader in = new BufferedReader(
432485
new InputStreamReader(
433486
uniprotConnection.getInputStream()));

0 commit comments

Comments
 (0)