use of de.tum.in.www1.artemis.exception.BambooException in project ArTEMiS by ls1intum.
the class BambooService method retrievArtifactPage.
/**
* Gets the content from a Bamboo artifact link
* Follows links on HTML directory pages, if necessary
*
* @param url
* @return
*/
private ResponseEntity retrievArtifactPage(String url) throws BambooException {
HttpHeaders headers = HeaderUtil.createAuthorization(BAMBOO_USER, BAMBOO_PASSWORD);
HttpEntity<?> entity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<byte[]> response;
try {
response = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class);
} catch (Exception e) {
log.error("HttpError while retrieving build artifact", e);
throw new BambooException("HttpError while retrieving build artifact");
}
if (response.getHeaders().containsKey("Content-Type") && response.getHeaders().get("Content-Type").get(0).equals("text/html")) {
// This is an "Index of" HTML page.
String html = new String(response.getBody(), StandardCharsets.UTF_8);
Pattern p = Pattern.compile("href=\"(.*?)\"", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(html);
if (m.find()) {
url = m.group(1);
// Recursively walk through the responses until we get the actual artifact.
return retrievArtifactPage(BAMBOO_SERVER_URL + url);
} else {
throw new BambooException("No artifact link found on artifact page");
}
} else {
// Actual artifact file
return response;
}
}
Aggregations