Search in sources :

Example 6 with OctaneResponse

use of com.hp.octane.integrations.dto.connectivity.OctaneResponse in project octane-ci-java-sdk by MicroFocus.

the class VulnerabilitiesServiceImpl method vulnerabilitiesPreflightRequest.

private Date vulnerabilitiesPreflightRequest(String jobId, String buildId) throws IOException {
    OctaneResponse response = getBaselineDateFromOctane(jobId, buildId);
    if (response.getStatus() == HttpStatus.SC_OK) {
        if (response.getBody() == null || "".equals(response.getBody())) {
            logger.info(configurer.octaneConfiguration.getLocationForLog() + "vulnerabilities data of " + jobId + " #" + buildId + " is not relevant to Octane");
            return null;
        } else {
            logger.info(configurer.octaneConfiguration.getLocationForLog() + "vulnerabilities data of " + jobId + " #" + buildId + " found to be relevant to Octane");
            boolean forTest = false;
            // backward compatibility with Octane
            if ("true".equals(response.getBody()) || forTest) {
                return DateUtils.getDateFromUTCString("2000-01-01", "yyyy-MM-dd");
            }
            return DateUtils.getDateFromUTCString(response.getBody(), DateUtils.octaneFormat);
        }
    }
    if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || response.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
        throw new TemporaryException("vulnerabilities preflight request FAILED, service unavailable");
    } else {
        throw new PermanentException("vulnerabilities preflight request FAILED with " + response.getStatus() + "");
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse)

Example 7 with OctaneResponse

use of com.hp.octane.integrations.dto.connectivity.OctaneResponse in project octane-ci-java-sdk by MicroFocus.

the class GeneralRestClient method createResponse.

private OctaneResponse createResponse(HttpResponse response) throws IOException {
    OctaneResponse octaneResponse = DTOFactory.getInstance().newDTO(OctaneResponse.class).setStatus(response.getStatusLine().getStatusCode());
    // set body
    if (response.getEntity() != null) {
        octaneResponse.setBody(CIPluginSDKUtils.inputStreamToUTF8String(response.getEntity().getContent()));
    }
    // set headers
    if (response.getAllHeaders() != null && response.getAllHeaders().length > 0) {
        Map<String, String> mapHeaders = new HashMap<>();
        for (Header header : response.getAllHeaders()) {
            mapHeaders.put(header.getName(), header.getValue());
        }
        octaneResponse.setHeaders(mapHeaders);
    }
    return octaneResponse;
}
Also used : Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HashMap(java.util.HashMap) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse)

Example 8 with OctaneResponse

use of com.hp.octane.integrations.dto.connectivity.OctaneResponse in project octane-ci-java-sdk by MicroFocus.

the class GeneralRestClient method executeRequest.

public OctaneResponse executeRequest(OctaneRequest request) throws IOException {
    if (authentication.isAuthenticationNeeded()) {
        authentication.authenticate(httpClient, false);
    }
    OctaneResponse result;
    HttpClientContext context;
    HttpResponse httpResponse = null;
    try {
        // we are running this loop either once or twice: once - regular flow, twice - when retrying after re-login attempt
        for (int i = 0; i < 2; i++) {
            HttpUriRequest uriRequest = createHttpRequest(request);
            context = createHttpContext(request.getUrl(), request.getTimeoutSec());
            httpResponse = httpClient.execute(uriRequest, context);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (i == 0 && HttpStatus.SC_UNAUTHORIZED == statusCode && authentication.supportAuthenticationOnUnauthorizedException()) {
                EntityUtils.consumeQuietly(httpResponse.getEntity());
                HttpClientUtils.closeQuietly(httpResponse);
                if (!authentication.authenticate(httpClient, true)) {
                    break;
                }
            } else {
                authentication.onResponse(httpResponse, context);
                break;
            }
        }
        result = createResponse(httpResponse);
    } finally {
        if (httpResponse != null) {
            EntityUtils.consumeQuietly(httpResponse.getEntity());
            HttpClientUtils.closeQuietly(httpResponse);
        }
    }
    return result;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpResponse(org.apache.http.HttpResponse) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext)

Example 9 with OctaneResponse

use of com.hp.octane.integrations.dto.connectivity.OctaneResponse in project octane-ci-java-sdk by MicroFocus.

the class OctaneRestClientImpl method login.

private OctaneResponse login(OctaneConfiguration config) throws IOException {
    OctaneResponse result;
    HttpResponse response = null;
    try {
        HttpUriRequest loginRequest = buildLoginRequest(config);
        HttpClientContext context = createHttpContext(loginRequest.getURI().toString(), 0, true);
        response = httpClient.execute(loginRequest, context);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            refreshSecurityToken(context, true);
        } else {
            logger.warn(configurer.octaneConfiguration.getLocationForLog() + "failed to login; response status: " + response.getStatusLine().getStatusCode());
        }
        result = createNGAResponse(null, response);
    } catch (IOException ioe) {
        logger.debug(configurer.octaneConfiguration.getLocationForLog() + "failed to login", ioe);
        throw ioe;
    } finally {
        if (response != null) {
            EntityUtils.consumeQuietly(response.getEntity());
            HttpClientUtils.closeQuietly(response);
        }
    }
    return result;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpResponse(org.apache.http.HttpResponse) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) IOException(java.io.IOException)

Example 10 with OctaneResponse

use of com.hp.octane.integrations.dto.connectivity.OctaneResponse in project octane-ci-java-sdk by MicroFocus.

the class PipelineContextServiceImpl method updatePipeline.

@Override
public PipelineContext updatePipeline(String serverIdentity, String jobName, PipelineContext pipelineContext) throws IOException {
    String url = getConfigurationUrl(serverIdentity, jobName);
    validateReleaseAndMilestone(pipelineContext);
    OctaneRestClient octaneRestClient = restService.obtainOctaneRestClient();
    Map<String, String> headers = new HashMap<>();
    headers.put(ACCEPT_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    headers.put(CONTENT_TYPE_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    PipelineContextList list = dtoFactory.newDTO(PipelineContextList.class).setData(Arrays.asList(pipelineContext));
    String jsonData = dtoFactory.dtoToJson(list);
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.PUT).setUrl(url).setBody(jsonData).setHeaders(headers);
    OctaneResponse response = octaneRestClient.execute(request);
    validateResponse(HttpStatus.SC_OK, response);
    PipelineContextList resultList = dtoFactory.dtoFromJson(response.getBody(), PipelineContextList.class);
    // we might receive several pipeline context from other workspaces.
    // find updated context by id
    PipelineContext result = resultList.getData().stream().filter(p -> p.getContextEntityId() == pipelineContext.getContextEntityId()).findFirst().get();
    return result;
}
Also used : HashMap(java.util.HashMap) PipelineContext(com.hp.octane.integrations.dto.pipelines.PipelineContext) PipelineContextList(com.hp.octane.integrations.dto.pipelines.PipelineContextList) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) OctaneRestClient(com.hp.octane.integrations.services.rest.OctaneRestClient)

Aggregations

OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)36 OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)27 IOException (java.io.IOException)14 PermanentException (com.hp.octane.integrations.exceptions.PermanentException)10 TemporaryException (com.hp.octane.integrations.exceptions.TemporaryException)10 OctaneRestClient (com.hp.octane.integrations.services.rest.OctaneRestClient)10 HashMap (java.util.HashMap)7 InputStream (java.io.InputStream)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 PipelineContextList (com.hp.octane.integrations.dto.pipelines.PipelineContextList)3 RequestTimeoutException (com.hp.octane.integrations.exceptions.RequestTimeoutException)3 ResourceNotFoundException (com.hp.octane.integrations.exceptions.ResourceNotFoundException)3 InterruptedIOException (java.io.InterruptedIOException)3 HttpResponse (org.apache.http.HttpResponse)3 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)3 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)3 OctaneConfiguration (com.hp.octane.integrations.OctaneConfiguration)2 PipelineContext (com.hp.octane.integrations.dto.pipelines.PipelineContext)2 OctaneConnectivityException (com.hp.octane.integrations.exceptions.OctaneConnectivityException)2 Header (org.apache.http.Header)2