Search in sources :

Example 26 with PermanentException

use of com.hp.octane.integrations.exceptions.PermanentException in project octane-ci-java-sdk by MicroFocus.

the class FODConnector method getAccessToken.

private void getAccessToken() {
    HttpPost post = new HttpPost(fodConfig.authURL);
    HttpEntity content = new StringEntity(fodConfig.getAuthBody(), ContentType.APPLICATION_FORM_URLENCODED);
    post.setEntity(content);
    CloseableHttpResponse response = null;
    try {
        logger.debug("inside getAccessToken():before Post ");
        response = httpClient.execute(post);
        logger.debug("inside getAccessToken():after Post, got status : " + response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() != 200 && response.getStatusLine().getStatusCode() != 201) {
            logger.error("inside getAccessToken():after Post, got status : " + response.getStatusLine().getStatusCode());
            throw new PermanentException("Cannot authenticate:" + response.getStatusLine().getReasonPhrase());
        }
        String secToken = isToString(response.getEntity().getContent());
        HashMap secTokeAsMap = new ObjectMapper().readValue(secToken, HashMap.class);
        access_token = secTokeAsMap.get("access_token").toString();
        accessTokenTime = new Date().getTime();
        logger.debug("inside getAccessToken():after Post ");
    } catch (IOException e) {
        logger.error("inside getAccessToken():After Post inside exception ", e);
        e.printStackTrace();
        if (response != null) {
            EntityUtils.consumeQuietly(response.getEntity());
            HttpClientUtils.closeQuietly(response);
        }
        throw new PermanentException("Cannot authenticate:" + e.getMessage());
    } finally {
        if (response != null) {
            EntityUtils.consumeQuietly(response.getEntity());
            HttpClientUtils.closeQuietly(response);
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpEntity(org.apache.http.HttpEntity) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 27 with PermanentException

use of com.hp.octane.integrations.exceptions.PermanentException in project octane-ci-java-sdk by MicroFocus.

the class FODConnector method getRawResponseFromFOD.

private String getRawResponseFromFOD(String url) {
    // URL encode
    HttpGet httpGet = new HttpGet(url);
    httpGet.setHeader("Authorization", "Bearer " + getUpdatedAccessToken());
    httpGet.setHeader("Accept", "application/json");
    httpGet.setHeader("Cookie", "__zlcmid=hTgaa94NtAdw5T; FoD=1725197834.36895.0000");
    httpGet.setHeader("Accept-Encoding", "gzip, deflate, br");
    CloseableHttpResponse response = null;
    try {
        logger.debug("sending the request : " + url);
        response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() > 300) {
            if (response.getStatusLine().getStatusCode() == 401) {
                throw new PermanentException("Cannot authenticate , the user is Unauthorized , make sure you are using the correct role for the user (): " + response.getStatusLine().getReasonPhrase());
            }
            if (response.getStatusLine().getStatusCode() == 503) {
                throw new TemporaryException("Service Unavailable ,  retry");
            }
            throw new PermanentException("unexpected return response : " + response.getStatusLine().getReasonPhrase());
        }
        return isToString(response.getEntity().getContent());
    } catch (IOException e) {
        throw new PermanentException(e);
    } finally {
        if (response != null) {
            EntityUtils.consumeQuietly(response.getEntity());
            HttpClientUtils.closeQuietly(response);
        }
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) HttpGet(org.apache.http.client.methods.HttpGet) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Example 28 with PermanentException

use of com.hp.octane.integrations.exceptions.PermanentException in project octane-ci-java-sdk by MicroFocus.

the class SonarVulnerabilitiesServiceImpl method getIssuesFromSecurityTool.

public List<SonarIssue> getIssuesFromSecurityTool(VulnerabilitiesQueueItem queueItem) {
    String projectKey = queueItem.getAdditionalProperties().get(PROJECT_KEY_KEY);
    String sonarToken = queueItem.getAdditionalProperties().get(SONAR_TOKEN_KEY);
    StringBuilder errorMessage = new StringBuilder().append("failed to get sonarqube vulnerability data for project key: ").append(projectKey).append(" with jobId: ").append(queueItem.getJobId()).append(" with buildId: ").append(queueItem.getBuildId());
    try {
        List<SonarIssue> sonarIssues = new ArrayList<>();
        // retrieve coverage report from Sonar
        Integer pageIndex = 0;
        JsonNode jsonReport;
        do {
            pageIndex++;
            URIBuilder vulnerabilityQuery = createQueryForSonarVulnerability(pageIndex, queueItem);
            InputStream reportStream = SonarUtils.getDataFromSonar(projectKey, sonarToken, vulnerabilityQuery);
            jsonReport = CIPluginSDKUtils.getObjectMapper().readTree(reportStream);
            sonarIssues.addAll(getSonarIssuesFromReport(jsonReport));
        } while (SonarUtils.sonarReportHasAnotherPage(pageIndex, jsonReport));
        return sonarIssues;
    } catch (IOException e) {
        logger.error(errorMessage, e);
        throw new PermanentException(e);
    }
}
Also used : InputStream(java.io.InputStream) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) SonarIssue(com.hp.octane.integrations.services.vulnerabilities.sonar.dto.SonarIssue) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 29 with PermanentException

use of com.hp.octane.integrations.exceptions.PermanentException in project octane-ci-java-sdk by MicroFocus.

the class SSCHandler method isScanProcessFinished.

public boolean isScanProcessFinished() {
    logger.debug("enter isScanProcessFinished");
    Artifacts artifacts = sscProjectConnector.getArtifactsOfProjectVersion(this.projectVersion.id, 10);
    Artifacts.Artifact closestArtifact = getClosestArtifact(artifacts);
    if (closestArtifact == null) {
        logger.debug("Cannot find artifact of the run");
        return false;
    }
    if (closestArtifact.status.equals(ARTIFACT_STATUS_COMPLETE)) {
        logger.debug("artifact of the run is in completed");
        return true;
    }
    if (closestArtifact.status.equals(ARTIFACT_ERROR_PROCESSING)) {
        throw new PermanentException("artifact of the run faced error, polling should stop");
    }
    // todo , if there are more cases need to handle separately
    logger.debug("artifact of the run is not complete, polling should continue");
    return false;
}
Also used : Artifacts(com.hp.octane.integrations.services.vulnerabilities.ssc.dto.Artifacts) PermanentException(com.hp.octane.integrations.exceptions.PermanentException)

Example 30 with PermanentException

use of com.hp.octane.integrations.exceptions.PermanentException in project octane-ci-java-sdk by MicroFocus.

the class SSCProjectConnector method readPagedEntities.

public <SSCArray extends SscBaseEntityArray> SSCArray readPagedEntities(String url, Class<SSCArray> type) {
    int startIndex = 0;
    try {
        boolean allFetched = false;
        SSCArray total = type.newInstance();
        while (!allFetched) {
            String pagedURL = getPagedURL(url, startIndex);
            String rawResponse = sendGetEntity(pagedURL);
            SSCArray page = stringToObject(rawResponse, type);
            if (total.getData() == null) {
                total.setData(page.getData());
            } else {
                total.getData().addAll(page.getData());
            }
            total.setCount(total.getData().size());
            allFetched = (total.getData().size() == page.getCount());
            startIndex = total.getCount();
        }
        return total;
    } catch (InstantiationException | IllegalAccessException e) {
        logger.error(e.getMessage());
        throw new PermanentException("Fail to fetch Issues", e);
    }
}
Also used : PermanentException(com.hp.octane.integrations.exceptions.PermanentException)

Aggregations

PermanentException (com.hp.octane.integrations.exceptions.PermanentException)31 TemporaryException (com.hp.octane.integrations.exceptions.TemporaryException)19 IOException (java.io.IOException)15 OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)10 OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)8 InputStream (java.io.InputStream)6 URISyntaxException (java.net.URISyntaxException)6 URIBuilder (org.apache.http.client.utils.URIBuilder)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 RequestTimeoutException (com.hp.octane.integrations.exceptions.RequestTimeoutException)5 HashMap (java.util.HashMap)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 InterruptedIOException (java.io.InterruptedIOException)4 HttpGet (org.apache.http.client.methods.HttpGet)4 OctaneRestClient (com.hp.octane.integrations.services.rest.OctaneRestClient)3 LinkedHashMap (java.util.LinkedHashMap)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 OctaneSDKGeneralException (com.hp.octane.integrations.exceptions.OctaneSDKGeneralException)2 List (java.util.List)2 HttpEntity (org.apache.http.HttpEntity)2