Search in sources :

Example 6 with OctaneRestClient

use of com.hp.octane.integrations.services.rest.OctaneRestClient in project octane-ci-java-sdk by MicroFocus.

the class EntitiesServiceImpl method deleteEntities.

@Override
public List<Entity> deleteEntities(Long workspaceId, String entityCollectionName, Collection<String> conditions) {
    // SEND REQUEST
    OctaneRestClient octaneRestClient = restService.obtainOctaneRestClient();
    Map<String, String> headers = new HashMap<>();
    headers.put(ACCEPT_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    headers.put(OctaneRestClient.CLIENT_TYPE_HEADER, OctaneRestClient.CLIENT_TYPE_VALUE);
    String url = buildEntityUrl(workspaceId, entityCollectionName, conditions, null, null, null, null);
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.DELETE).setUrl(url).setHeaders(headers);
    OctaneResponse response = executeRequest(octaneRestClient, request);
    ResponseEntityList responseEntityList = parseBody(HttpStatus.SC_OK, response);
    return responseEntityList.getData();
}
Also used : OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) OctaneRestClient(com.hp.octane.integrations.services.rest.OctaneRestClient)

Example 7 with OctaneRestClient

use of com.hp.octane.integrations.services.rest.OctaneRestClient in project octane-ci-java-sdk by MicroFocus.

the class BridgeServiceImpl method getAbridgedTasks.

private String getAbridgedTasks(String selfIdentity, String selfType, String selfUrl, String pluginVersion, String octaneUser, String ciServerUser) {
    String responseBody = null;
    OctaneRestClient octaneRestClient = restService.obtainOctaneRestClient();
    Map<String, String> headers = new HashMap<>();
    headers.put(RestService.ACCEPT_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    OctaneRequest octaneRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setTimeoutSec(60).setUrl(configurer.octaneConfiguration.getUrl() + RestService.SHARED_SPACE_INTERNAL_API_PATH_PART + configurer.octaneConfiguration.getSharedSpace() + RestService.ANALYTICS_CI_PATH_PART + "servers/" + selfIdentity + "/tasks?self-type=" + CIPluginSDKUtils.urlEncodeQueryParam(selfType) + "&self-url=" + CIPluginSDKUtils.urlEncodeQueryParam(selfUrl) + "&api-version=" + OctaneSDK.API_VERSION + "&sdk-version=" + CIPluginSDKUtils.urlEncodeQueryParam(OctaneSDK.SDK_VERSION) + "&plugin-version=" + CIPluginSDKUtils.urlEncodeQueryParam(pluginVersion) + "&client-id=" + CIPluginSDKUtils.urlEncodeQueryParam(octaneUser) + "&ci-server-user=" + CIPluginSDKUtils.urlEncodeQueryParam(ciServerUser)).setHeaders(headers);
    try {
        changeServiceState(ServiceState.WaitingToOctane);
        OctaneResponse octaneResponse = octaneRestClient.execute(octaneRequest);
        changeServiceState(ServiceState.AfterWaitingToOctane);
        if (octaneResponse.getStatus() == HttpStatus.SC_OK) {
            responseBody = octaneResponse.getBody();
            if (CIPluginSDKUtils.isServiceTemporaryUnavailable(responseBody)) {
                breathingOnException("Saas service is temporary unavailable.", 60, null);
                responseBody = null;
            } else {
                setConnectionSuccessful();
            }
        } else {
            if (octaneResponse.getStatus() == HttpStatus.SC_NO_CONTENT) {
                logger.debug(configurer.octaneConfiguration.getLocationForLog() + "no tasks found on server");
                setConnectionSuccessful();
            } else if (octaneResponse.getStatus() == HttpStatus.SC_REQUEST_TIMEOUT) {
                logger.debug(configurer.octaneConfiguration.getLocationForLog() + "expected timeout disconnection on retrieval of abridged tasks, reconnecting immediately...");
                setConnectionSuccessful();
            } else if (octaneResponse.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || octaneResponse.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
                breathingOnException("Octane service is unavailable.", 30, null);
            } else if (octaneResponse.getStatus() == HttpStatus.SC_UNAUTHORIZED) {
                breathingOnException("Connection to Octane failed: authentication error.", 30, null);
            } else if (octaneResponse.getStatus() == HttpStatus.SC_FORBIDDEN) {
                breathingOnException("Connection to Octane failed: authorization error.", 30, null);
            } else if (octaneResponse.getStatus() == HttpStatus.SC_NOT_FOUND) {
                breathingOnException("Connection to Octane failed: 404, validate proxy settings, maybe missing 'No Proxy Host' setting?", 30, null);
            } else if (octaneResponse.getStatus() == HttpStatus.SC_TEMPORARY_REDIRECT) {
                breathingOnException("Task polling request is redirected. Possibly Octane service is unavailable now.", 30, null);
            } else {
                // don't print more that 2000 characters
                String output = octaneResponse.getBody() == null ? "" : octaneResponse.getBody().substring(0, Math.min(octaneResponse.getBody().length(), 2000));
                breathingOnException("Unexpected response from Octane; status: " + octaneResponse.getStatus() + ", content: " + output + ".", 20, null);
            }
        }
    } catch (InterruptedIOException ie) {
        requestTimeoutCount++;
        lastRequestTimeoutTime = System.currentTimeMillis();
        long timeout = (lastRequestTimeoutTime - stateStartTime) / 1000;
        breathingOnException("!!!!!!!!!!!!!!!!!!! request timeout after request timeout after " + timeout + " sec", 5, ie);
    } catch (SocketException | UnknownHostException e) {
        breathingOnException("Failed to retrieve abridged tasks. ALM Octane Server is not accessible", 30, e);
    } catch (IOException ioe) {
        breathingOnException("Failed to retrieve abridged tasks", 30, ioe);
    } catch (Throwable t) {
        breathingOnException("Unexpected error during retrieval of abridged tasks", 30, t);
    }
    return responseBody;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) OctaneRestClient(com.hp.octane.integrations.services.rest.OctaneRestClient)

Example 8 with OctaneRestClient

use of com.hp.octane.integrations.services.rest.OctaneRestClient in project octane-ci-java-sdk by MicroFocus.

the class EntitiesServiceImpl method getPagedEntities.

@Override
public ResponseEntityList getPagedEntities(String url) {
    OctaneRestClient octaneRestClient = restService.obtainOctaneRestClient();
    Map<String, String> headers = new HashMap<>();
    headers.put(ACCEPT_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    headers.put(OctaneRestClient.CLIENT_TYPE_HEADER, OctaneRestClient.CLIENT_TYPE_VALUE);
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setUrl(url).setHeaders(headers);
    OctaneResponse response = executeRequest(octaneRestClient, request);
    ResponseEntityList pagedEntityList = parseBody(HttpStatus.SC_OK, response);
    return pagedEntityList;
}
Also used : OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) OctaneRestClient(com.hp.octane.integrations.services.rest.OctaneRestClient)

Example 9 with OctaneRestClient

use of com.hp.octane.integrations.services.rest.OctaneRestClient in project octane-ci-java-sdk by MicroFocus.

the class EntitiesServiceImpl method postEntities.

@Override
public List<Entity> postEntities(Long workspaceId, String entityCollectionName, String jsonData, Collection<String> fields, Map<String, String> serviceArgument) {
    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());
    headers.put(OctaneRestClient.CLIENT_TYPE_HEADER, OctaneRestClient.CLIENT_TYPE_VALUE);
    String url = buildEntityUrl(workspaceId, entityCollectionName, null, fields, null, null, null, serviceArgument);
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.POST).setUrl(url).setBody(jsonData).setHeaders(headers);
    OctaneResponse response = executeRequest(octaneRestClient, request);
    ResponseEntityList responseEntityList = parseBody(HttpStatus.SC_CREATED, response);
    return responseEntityList.getData();
}
Also used : OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) OctaneRestClient(com.hp.octane.integrations.services.rest.OctaneRestClient)

Example 10 with OctaneRestClient

use of com.hp.octane.integrations.services.rest.OctaneRestClient in project octane-ci-java-sdk by MicroFocus.

the class PipelineContextServiceImpl method createPipeline.

@Override
public PipelineContext createPipeline(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());
    String jsonData = dtoFactory.dtoToJson(pipelineContext);
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.POST).setUrl(url).setBody(jsonData).setHeaders(headers);
    OctaneResponse response = octaneRestClient.execute(request);
    validateResponse(HttpStatus.SC_CREATED, response);
    PipelineContextList list = dtoFactory.dtoFromJson(response.getBody(), PipelineContextList.class);
    // we might receive several pipeline context from other workspaces.
    // find updated context by workspace id
    PipelineContext result = list.getData().stream().filter(p -> p.getWorkspaceId() == pipelineContext.getWorkspaceId()).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

OctaneRestClient (com.hp.octane.integrations.services.rest.OctaneRestClient)13 OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)11 OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)10 HashMap (java.util.HashMap)7 LinkedHashMap (java.util.LinkedHashMap)4 PipelineContextList (com.hp.octane.integrations.dto.pipelines.PipelineContextList)3 PermanentException (com.hp.octane.integrations.exceptions.PermanentException)3 InterruptedIOException (java.io.InterruptedIOException)3 PipelineContext (com.hp.octane.integrations.dto.pipelines.PipelineContext)2 TemporaryException (com.hp.octane.integrations.exceptions.TemporaryException)2 IOException (java.io.IOException)2 RequestTimeoutException (com.hp.octane.integrations.exceptions.RequestTimeoutException)1 SocketException (java.net.SocketException)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1