return String.format(LINK_SHORT_API_URL_GET_FORMAT, shortLinkId);
}
private String resolveLink(String httpApplicationLink, int redirectCount) throws StorageException {
if (redirectCount >= LINK_HTTP_MAX_REDIRECT_COUNT) {
throw new StorageException("Max. redirect count of " + LINK_HTTP_MAX_REDIRECT_COUNT + " for URL reached. Canot find syncany:// link.");
}
try {
logger.log(Level.INFO, "- Retrieving HTTP HEAD for " + httpApplicationLink + " ...");
HttpHead headMethod = new HttpHead(httpApplicationLink);
HttpResponse httpResponse = createHttpClient().execute(headMethod);
// Find syncany:// link
Header locationHeader = httpResponse.getLastHeader("Location");
if (locationHeader == null) {
throw new Exception("Link does not redirect to a syncany:// link.");
}
String locationHeaderUrl = locationHeader.getValue();
Matcher locationHeaderMatcher = LINK_PATTERN.matcher(locationHeaderUrl);
boolean isApplicationLink = locationHeaderMatcher.find();
if (isApplicationLink) {
String applicationLink = locationHeaderMatcher.group(0);
logger.log(Level.INFO, "Resolved application link is: " + applicationLink);
return applicationLink;
}
else {
return resolveLink(locationHeaderUrl, ++redirectCount);
}
}
catch (Exception e) {
throw new StorageException(e.getMessage(), e);
}
}