Search in sources :

Example 6 with BambooException

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;
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Pattern(java.util.regex.Pattern) BambooException(de.tum.in.www1.artemis.exception.BambooException) HttpEntity(org.springframework.http.HttpEntity) Matcher(java.util.regex.Matcher) RestTemplate(org.springframework.web.client.RestTemplate) GitException(de.tum.in.www1.artemis.exception.GitException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BambooException(de.tum.in.www1.artemis.exception.BambooException)

Aggregations

BambooException (de.tum.in.www1.artemis.exception.BambooException)6 IOException (java.io.IOException)2 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 GitException (de.tum.in.www1.artemis.exception.GitException)1 ExerciseRepository (de.tum.in.www1.artemis.repository.ExerciseRepository)1 MalformedURLException (java.net.MalformedURLException)1 Path (java.nio.file.Path)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 Transactional (org.springframework.transaction.annotation.Transactional)1 RestTemplate (org.springframework.web.client.RestTemplate)1 BambooClient (org.swift.bamboo.cli.BambooClient)1 CliClient (org.swift.common.cli.CliClient)1