Skip to content

Commit

Permalink
Merge pull request #780 from sbliven/bugfixes-4.2
Browse files Browse the repository at this point in the history
Fix NonstandardProteinCompoundTest failure
  • Loading branch information
sbliven committed Jun 27, 2018
2 parents 211276d + 99fda9d commit 7a26257
Showing 1 changed file with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class UniprotProxySequenceReader<C extends Compound> implements ProxySequ
private static final String TREMBLID_PATTERN = "[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}";
public static final Pattern UP_AC_PATTERN = Pattern.compile("(" + SPID_PATTERN + "|" + TREMBLID_PATTERN + ")");

private static String uniprotbaseURL = "http://www.uniprot.org"; //"http://pir.uniprot.org";
private static String uniprotbaseURL = "https://www.uniprot.org"; //"http://pir.uniprot.org";
private static String uniprotDirectoryCache = null;
private String sequence;
private CompoundSet<C> compoundSet;
Expand Down Expand Up @@ -414,6 +414,61 @@ private void writeCache(StringBuilder sb, String accession) throws IOException {
fw.write(sb.toString());
fw.close();
}

/**
* Open a URL connection.
*
* Follows redirects.
* @param url
* @throws IOException
*/
private static HttpURLConnection openURLConnection(URL url) throws IOException {
// This method should be moved to a utility class in BioJava 5.0

final int timeout = 5000;
final String useragent = "BioJava";

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent", useragent);
conn.setInstanceFollowRedirects(true);
conn.setConnectTimeout(timeout);
conn.setReadTimeout(timeout);

int status = conn.getResponseCode();
while (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER) {
// Redirect!
String newUrl = conn.getHeaderField("Location");

if(newUrl.equals(url.toString())) {
throw new IOException("Cyclic redirect detected at "+newUrl);
}

// Preserve cookies
String cookies = conn.getHeaderField("Set-Cookie");

// open the new connection again
url = new URL(newUrl);
conn.disconnect();
conn = (HttpURLConnection) url.openConnection();
if(cookies != null) {
conn.setRequestProperty("Cookie", cookies);
}
conn.addRequestProperty("User-Agent", useragent);
conn.setInstanceFollowRedirects(true);
conn.setConnectTimeout(timeout);
conn.setReadTimeout(timeout);
conn.connect();

status = conn.getResponseCode();

logger.info("Redirecting from {} to {}", url, newUrl);
}
conn.connect();

return conn;
}

private StringBuilder fetchUniprotXML(String uniprotURL)
throws IOException, CompoundNotFoundException {
Expand All @@ -423,11 +478,9 @@ private StringBuilder fetchUniprotXML(String uniprotURL)
int attempt = 5;
List<String> errorCodes = new ArrayList<String>();
while(attempt > 0) {
HttpURLConnection uniprotConnection = (HttpURLConnection) uniprot.openConnection();
uniprotConnection.setRequestProperty("User-Agent", "BioJava");
uniprotConnection.connect();
HttpURLConnection uniprotConnection = openURLConnection(uniprot);
int statusCode = uniprotConnection.getResponseCode();
if (statusCode == 200) {
if (statusCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(
new InputStreamReader(
uniprotConnection.getInputStream()));
Expand Down

0 comments on commit 7a26257

Please sign in to comment.