private String getEncodedDownloadLocation(String projectName, String actualURL, int lastIndexUrl) { String encodedDownloadLocation; if (actualURL.substring(lastIndexUrl).contains(reference.getJarFileExt()) || actualURL.substring(lastIndexUrl).contains(reference.getZipFileExt())) { encodedDownloadLocation = actualURL.substring(lastIndexUrl); } else { encodedDownloadLocation = projectName + reference.getJarFileExt(); } return encodedDownloadLocation; }
public CurseFile getCurseForgeDownloadLocation(final CurseFile curseFile) throws IOException { val url = curseFile.getCurseForgeDownloadUrl(); val projectName = curseFile.getName(); String encodedDownloadLocation = URLHelper.encodeSpaces(projectName); if (!encodedDownloadLocation.contains(reference.getJarFileExt()) || !encodedDownloadLocation.contains(reference.getZipFileExt())) { val newUrl = url + reference.getCookieTest1(); HttpURLConnection conn = (HttpURLConnection) new URL(newUrl).openConnection(); conn.setInstanceFollowRedirects(false); conn.connect(); String actualURL = conn.getURL().toString(); int retryCount = 0; while (conn.getResponseCode() != 200 || !actualURL.contains(reference.getJarFileExt()) || !actualURL.contains(reference.getZipFileExt())) { val headerLocation = conn.getHeaderField("Location"); if (headerLocation != null) { actualURL = headerLocation; } else { actualURL = conn.getURL().toString(); } if (retryCount > reference.getRetryCounter()) { break; } conn = (HttpURLConnection) new URL(newUrl).openConnection(); retryCount++; } encodedDownloadLocation = getEncodedDownloadLocation(projectName, actualURL, actualURL.lastIndexOf(reference.getUrlDelimiter()) + 1); curseFile.setDownloadUrl(actualURL.replace("http:", "https:")); } curseFile.setFileName(URLHelper.decodeSpaces(encodedDownloadLocation)); return curseFile; }