use of com.hp.octane.integrations.exceptions.TemporaryException in project octane-ci-java-sdk by MicroFocus.
the class SonarUtils method getDataFromSonar.
public static InputStream getDataFromSonar(String projectKey, String token, URIBuilder uriQuery) {
StringBuilder errorMessage = new StringBuilder().append("failed to get data from sonar for project key: ").append(projectKey);
try {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(uriQuery.build());
setTokenInHttpRequest(request, token);
HttpResponse httpResponse = httpClient.execute(request);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
return httpResponse.getEntity().getContent();
} else if (statusCode == HttpStatus.SC_BAD_REQUEST) {
errorMessage.append(" with status code: ").append(statusCode).append(" and response body: ").append(EntityUtils.toString(httpResponse.getEntity(), "UTF-8"));
throw new TemporaryException(errorMessage.toString());
} else {
errorMessage.append(" with status code: ").append(statusCode).append(" and response body: ").append(EntityUtils.toString(httpResponse.getEntity(), "UTF-8"));
throw new PermanentException(errorMessage.toString());
}
} catch (HttpHostConnectException e) {
throw new TemporaryException(errorMessage.toString(), e);
} catch (Exception e) {
throw new PermanentException(errorMessage.toString(), e);
}
}
use of com.hp.octane.integrations.exceptions.TemporaryException in project octane-ci-java-sdk by MicroFocus.
the class EventsServiceImpl method sendEventsData.
private void sendEventsData(CIEventsList eventsList, String correlationId) {
Map<String, String> headers = new HashMap<>();
headers.put(CONTENT_TYPE_HEADER, ContentType.APPLICATION_JSON.getMimeType());
headers.put(CORRELATION_ID_HEADER, correlationId);
OctaneRequest octaneRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.PUT).setUrl(configurer.octaneConfiguration.getUrl() + SHARED_SPACE_INTERNAL_API_PATH_PART + configurer.octaneConfiguration.getSharedSpace() + ANALYTICS_CI_PATH_PART + "events?ci_server_identity=" + configurer.octaneConfiguration.getInstanceId()).setHeaders(headers).setTimeoutSec(60).setBody(dtoFactory.dtoToJsonStream(eventsList));
OctaneResponse octaneResponse;
try {
octaneResponse = restService.obtainOctaneRestClient().execute(octaneRequest);
} catch (InterruptedIOException ie) {
String msg = "!!!!!!!!!!!!!!!!!!! request timeout" + ie.getClass().getCanonicalName() + " - " + ie.getMessage();
throw new RequestTimeoutException(msg);
} catch (IOException ioe) {
throw new TemporaryException(ioe);
}
if (octaneResponse.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || octaneResponse.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
throw new TemporaryException("PUT events failed with status " + octaneResponse.getStatus());
} else if (octaneResponse.getStatus() == HttpStatus.SC_UNAUTHORIZED || octaneResponse.getStatus() == HttpStatus.SC_FORBIDDEN) {
CIPluginSDKUtils.doWait(30000);
throw new PermanentException("PUT events failed with status " + octaneResponse.getStatus());
} else if (octaneResponse.getStatus() != HttpStatus.SC_OK) {
if (CIPluginSDKUtils.isServiceTemporaryUnavailable(octaneResponse.getBody())) {
throw new TemporaryException("Saas service is temporary unavailable.");
}
throw new PermanentException("PUT events failed with status " + octaneResponse.getStatus());
}
}
use of com.hp.octane.integrations.exceptions.TemporaryException in project octane-ci-java-sdk by MicroFocus.
the class LogsServiceImpl method preflightRequest.
private String[] preflightRequest(OctaneConfiguration octaneConfiguration, String encodedServerId, String encodedJobId, String encodedRootJobId, boolean base64) {
String[] result = new String[0];
OctaneResponse response;
// get result
String url = getAnalyticsContextPath(configurer.octaneConfiguration.getUrl(), octaneConfiguration.getSharedSpace()) + "servers/" + encodedServerId + "/jobs/" + encodedJobId + "/workspaceId";
if (encodedRootJobId != null && !encodedRootJobId.isEmpty()) {
url += "?rootJobId=" + encodedRootJobId;
}
if (base64) {
url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
}
try {
OctaneRequest preflightRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setUrl(url);
response = restService.obtainOctaneRestClient().execute(preflightRequest);
if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || response.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
throw new TemporaryException("preflight request failed with status " + response.getStatus());
} else if (response.getStatus() == HttpStatus.SC_UNAUTHORIZED || response.getStatus() == HttpStatus.SC_FORBIDDEN) {
CIPluginSDKUtils.doWait(30000);
throw new PermanentException("preflight request failed with status " + response.getStatus());
} else if (response.getStatus() != HttpStatus.SC_OK && response.getStatus() != HttpStatus.SC_NO_CONTENT) {
throw new PermanentException("preflight request failed with status " + response.getStatus() + ". JobId: '" + encodedJobId + "'. Request URL : " + url);
}
} catch (IOException ioe) {
throw new TemporaryException(ioe);
}
// parse result
if (response.getBody() != null && !response.getBody().isEmpty()) {
try {
result = CIPluginSDKUtils.getObjectMapper().readValue(response.getBody(), String[].class);
} catch (IOException ioe) {
if (CIPluginSDKUtils.isServiceTemporaryUnavailable(response.getBody())) {
throw new TemporaryException("Saas service is temporary unavailable.");
} else {
throw new PermanentException("failed to parse preflight response '" + response.getBody() + "' for '" + encodedJobId + "'");
}
}
}
return result;
}
use of com.hp.octane.integrations.exceptions.TemporaryException in project octane-ci-java-sdk by MicroFocus.
the class CoverageServiceImpl method isSonarReportRelevant.
@Override
public boolean isSonarReportRelevant(String jobId) {
if (jobId == null || jobId.isEmpty()) {
throw new IllegalArgumentException("job ID MUST NOT be null nor empty");
}
boolean result = false;
OctaneResponse response;
boolean base64 = isEncodeBase64();
String encodedJobId = base64 ? CIPluginSDKUtils.urlEncodeBase64(jobId) : CIPluginSDKUtils.urlEncodePathParam(jobId);
// get result
try {
String url = getAnalyticsContextPath(configurer.octaneConfiguration.getUrl(), configurer.octaneConfiguration.getSharedSpace()) + "servers/" + CIPluginSDKUtils.urlEncodePathParam(configurer.octaneConfiguration.getInstanceId()) + "/jobs/" + encodedJobId + "/workspaceId";
if (base64) {
url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
}
OctaneRequest preflightRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setUrl(url);
response = restService.obtainOctaneRestClient().execute(preflightRequest);
if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || response.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
throw new TemporaryException("preflight request failed with status " + response.getStatus());
} else if (response.getStatus() != HttpStatus.SC_OK && response.getStatus() != HttpStatus.SC_NO_CONTENT) {
throw new PermanentException("preflight request failed with status " + response.getStatus());
}
} catch (IOException ioe) {
throw new TemporaryException("failed to perform preflight request", ioe);
}
// parse result
if (response.getStatus() == HttpStatus.SC_OK && response.getBody() != null && !response.getBody().isEmpty()) {
try {
String[] wss = CIPluginSDKUtils.getObjectMapper().readValue(response.getBody(), String[].class);
if (wss.length > 0) {
logger.info(configurer.octaneConfiguration.getLocationForLog() + "coverage of " + jobId + " found " + wss.length + " interested workspace/s in Octane, dispatching the coverage");
result = true;
} else {
logger.info(configurer.octaneConfiguration.getLocationForLog() + "coverage of " + jobId + ", found no interested workspace in Octane");
}
} catch (IOException ioe) {
throw new PermanentException("failed to parse preflight response '" + response.getBody() + "' for '" + jobId + "'");
}
}
return result;
}
use of com.hp.octane.integrations.exceptions.TemporaryException in project octane-ci-java-sdk by MicroFocus.
the class CoverageServiceImpl method pushCoverage.
@Override
public OctaneResponse pushCoverage(String jobId, String buildId, CoverageReportType reportType, InputStream coverageReport) {
if (jobId == null || jobId.isEmpty()) {
throw new IllegalArgumentException("job ID MUST NOT be null nor empty");
}
if (buildId == null || buildId.isEmpty()) {
throw new IllegalArgumentException("build ID MUST NOT be null nor empty");
}
if (reportType == null) {
throw new IllegalArgumentException("report type MUST NOT be null");
}
if (coverageReport == null) {
throw new IllegalArgumentException("coverage report data MUST NOT be null");
}
boolean base64 = isEncodeBase64();
String tempJobId = jobId;
if (base64) {
tempJobId = CIPluginSDKUtils.urlEncodeBase64(jobId);
}
String url;
try {
url = new URIBuilder(getAnalyticsContextPath(configurer.octaneConfiguration.getUrl(), configurer.octaneConfiguration.getSharedSpace()) + "coverage").setParameter("ci-server-identity", configurer.octaneConfiguration.getInstanceId()).setParameter("ci-job-id", tempJobId).setParameter("ci-build-id", buildId).setParameter("file-type", reportType.name()).toString();
if (base64) {
url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
}
} catch (URISyntaxException urise) {
throw new PermanentException("failed to build URL to push coverage report", urise);
}
String correlationId = CIPluginSDKUtils.getNextCorrelationId();
Map<String, String> headers = new HashMap<>();
headers.put(CORRELATION_ID_HEADER, correlationId);
OctaneRequest pushCoverageRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.PUT).setUrl(url).setHeaders(headers).setBody(coverageReport);
try {
return restService.obtainOctaneRestClient().execute(pushCoverageRequest);
} catch (IOException ioe) {
throw new TemporaryException("failed to push coverage report", ioe);
}
}
Aggregations