use of com.hp.octane.integrations.dto.connectivity.OctaneRequest in project octane-ci-java-sdk by MicroFocus.
the class EntitiesServiceImpl method updateEntities.
@Override
public List<Entity> updateEntities(Long workspaceId, String entityCollectionName, String jsonData) {
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, null, null, null, null);
OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.PUT).setUrl(url).setBody(jsonData).setHeaders(headers);
OctaneResponse response = executeRequest(octaneRestClient, request);
ResponseEntityList responseEntityList = parseBody(HttpStatus.SC_OK, response);
return responseEntityList.getData();
}
use of com.hp.octane.integrations.dto.connectivity.OctaneRequest 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();
}
use of com.hp.octane.integrations.dto.connectivity.OctaneRequest 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.dto.connectivity.OctaneRequest in project octane-ci-java-sdk by MicroFocus.
the class LogsServiceImpl method pushBuildLog.
private void pushBuildLog(String serverId, BuildLogQueueItem queueItem) {
OctaneConfiguration octaneConfiguration = configurer.octaneConfiguration;
String encodedServerId = CIPluginSDKUtils.urlEncodePathParam(serverId);
String encodedBuildId = CIPluginSDKUtils.urlEncodePathParam(queueItem.buildId);
boolean base64 = isEncodeBase64();
String encodedJobId = base64 ? CIPluginSDKUtils.urlEncodeBase64(queueItem.jobId) : CIPluginSDKUtils.urlEncodePathParam(queueItem.jobId);
String encodedRootJobId = base64 ? CIPluginSDKUtils.urlEncodeBase64(queueItem.rootJobId) : CIPluginSDKUtils.urlEncodeQueryParam(queueItem.rootJobId);
// preflight
String[] workspaceIDs = preflightRequest(octaneConfiguration, encodedServerId, encodedJobId, encodedRootJobId, base64);
if (workspaceIDs.length == 0) {
logger.info(configurer.octaneConfiguration.getLocationForLog() + "log of " + queueItem + ", no interested workspace is found");
return;
} else {
logger.info(configurer.octaneConfiguration.getLocationForLog() + "log of " + queueItem + ", found " + workspaceIDs.length + " interested workspace/s");
}
// submit log for each workspace returned by the 'preflight' API
// [YG] TODO: the code below should and will be refactored to a simpler state once Octane's APIs will be adjusted
InputStream log;
OctaneRequest request;
OctaneResponse response;
for (String workspaceId : workspaceIDs) {
String url = octaneConfiguration.getUrl() + RestService.SHARED_SPACE_INTERNAL_API_PATH_PART + octaneConfiguration.getSharedSpace() + "/workspaces/" + workspaceId + RestService.ANALYTICS_CI_PATH_PART + encodedServerId + "/" + encodedJobId + "/" + encodedBuildId + "/logs";
if (base64) {
url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
}
String correlationId = CIPluginSDKUtils.getNextCorrelationId();
Map<String, String> headers = new HashMap<>();
headers.put(CORRELATION_ID_HEADER, correlationId);
request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.POST).setHeaders(headers).setUrl(url);
try {
log = configurer.pluginServices.getBuildLog(queueItem.jobId, queueItem.buildId);
if (log == null) {
logger.info(configurer.octaneConfiguration.getLocationForLog() + "no log for " + queueItem + " found, abandoning");
break;
}
request.setBody(log);
response = restService.obtainOctaneRestClient().execute(request);
if (response.getStatus() == HttpStatus.SC_OK) {
logger.info(configurer.octaneConfiguration.getLocationForLog() + "successfully pushed log of " + queueItem + " to WS " + workspaceId + ", correlation Id = " + correlationId);
} else {
logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to push log of " + queueItem + " to WS " + workspaceId + ", status: " + response.getStatus() + ", correlation Id = " + correlationId);
}
} catch (IOException ioe) {
logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to push log of " + queueItem + " to WS " + workspaceId + ", breathing " + TEMPORARY_ERROR_BREATHE_INTERVAL + "ms and retrying one more time due to IOException", ioe);
CIPluginSDKUtils.doWait(TEMPORARY_ERROR_BREATHE_INTERVAL);
log = configurer.pluginServices.getBuildLog(queueItem.jobId, queueItem.buildId);
if (log == null) {
logger.info(configurer.octaneConfiguration.getLocationForLog() + "no log for " + queueItem + " found, abandoning");
break;
}
request.setBody(log);
try {
response = restService.obtainOctaneRestClient().execute(request);
if (response.getStatus() == HttpStatus.SC_OK) {
logger.info(configurer.octaneConfiguration.getLocationForLog() + "successfully pushed log of " + queueItem + " to WS " + workspaceId);
} else {
logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to push log of " + queueItem + " to WS " + workspaceId + ", status: " + response.getStatus());
}
} catch (IOException ioem) {
logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to push log of " + queueItem + " to WS " + workspaceId + " for the second time, abandoning", ioem);
}
}
}
}
use of com.hp.octane.integrations.dto.connectivity.OctaneRequest 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;
}
Aggregations