use of com.blackducksoftware.integration.hub.request.Response in project hub-alert by blackducksoftware.
the class HipChatChannel method testGlobalConfig.
@Override
public String testGlobalConfig(final GlobalHipChatConfigEntity entity) throws IntegrationException {
if (entity == null) {
return "The provided entity was null.";
}
if (StringUtils.isBlank(entity.getApiKey())) {
throw new IntegrationException("Invalid API key: API key not provided");
}
final RestConnection restConnection = channelRestConnectionFactory.createUnauthenticatedRestConnection(HIP_CHAT_API);
if (restConnection != null) {
try {
final String url = HIP_CHAT_API + "/v2/room/*/notification";
final Map<String, String> queryParameters = new HashMap<>();
queryParameters.put("auth_test", "true");
final Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("Authorization", "Bearer " + entity.getApiKey());
requestHeaders.put("Content-Type", "application/json");
// The {"message":"test"} is required to avoid a BAD_REQUEST (OkHttp issue: #854)
final ChannelRequestHelper channelRequestHelper = new ChannelRequestHelper(restConnection);
final Request request = channelRequestHelper.createPostMessageRequest(url, requestHeaders, queryParameters, "{\"message\":\"test\"}");
final Response response = channelRequestHelper.sendGenericRequest(request);
if (response.getStatusCode() >= 200 && response.getStatusCode() < 400) {
return "API key is valid.";
}
return "Invalid API key: " + response.getStatusMessage();
} catch (final IntegrationException e) {
restConnection.logger.error("Unable to create a response", e);
throw new IntegrationException("Invalid API key: " + e.getMessage());
}
}
return "Connection error: see logs for more information.";
}
use of com.blackducksoftware.integration.hub.request.Response in project hub-alert by blackducksoftware.
the class ChannelRequestHelper method sendMessageRequest.
public void sendMessageRequest(final Request request, final String messageType) throws IntegrationException {
logger.info("Attempting to send a {} message...", messageType);
final Response response = sendGenericRequest(request);
if (response.getStatusCode() >= 200 && response.getStatusCode() < 400) {
logger.info("Successfully sent a {} message!", messageType);
}
}
use of com.blackducksoftware.integration.hub.request.Response in project hub-alert by blackducksoftware.
the class ChannelRequestHelper method sendGenericRequest.
public Response sendGenericRequest(final Request request) throws IntegrationException {
Response response = null;
try {
final HubService service = hubServicesFactory.createHubService();
response = service.executeRequest(request);
logger.trace("Response: " + response.toString());
return response;
} catch (final Exception generalException) {
logger.error("Error sending request", generalException);
throw new AlertException(generalException.getMessage());
}
}
Aggregations