use of com.synopsys.integration.rest.request.Request in project hub-alert by blackducksoftware.
the class ExternalAlertRequestUtility method executeRequest.
private String executeRequest(String path, HttpMethod httpMethod, String requestBody, String error) throws IntegrationException {
BodyContent requestBodyContent = new StringBodyContent(requestBody, BodyContentConverter.DEFAULT);
Request.Builder requestBuilder = createRequestBuilder(path);
requestBuilder.method(httpMethod);
if (null != requestBody) {
requestBuilder.bodyContent(requestBodyContent);
}
Request request = requestBuilder.build();
Response response = client.execute(request);
if (response.isStatusCodeError()) {
intLogger.error(error);
response.throwExceptionForError();
}
return response.getContentString();
}
use of com.synopsys.integration.rest.request.Request in project hub-alert by blackducksoftware.
the class ExternalAlertRequestUtility method loginToExternalAlert.
public void loginToExternalAlert() throws IntegrationException {
String loginBody = "{\"alertUsername\":\"sysadmin\",\"alertPassword\":\"blackduck\"}";
BodyContent requestBody = new StringBodyContent(loginBody, BodyContentConverter.DEFAULT);
Request.Builder requestBuilder = createRequestBuilder("/api/login");
requestBuilder.method(HttpMethod.POST);
requestBuilder.bodyContent(requestBody);
Request request = requestBuilder.build();
Response response = client.execute(request);
if (response.isStatusCodeError()) {
intLogger.error("Could not log into Alert.");
response.throwExceptionForError();
}
String csrfToken = response.getHeaderValue("X-CSRF-TOKEN");
String cookie = response.getHeaderValue("Set-Cookie");
client.addCommonRequestHeader("X-CSRF-TOKEN", csrfToken);
client.addCommonRequestHeader("Cookie", cookie);
intLogger.info("Logged into Alert.");
}
use of com.synopsys.integration.rest.request.Request in project hub-alert by blackducksoftware.
the class RestChannelUtilityTest method createPostMessageRequestParamsTest.
@Test
public void createPostMessageRequestParamsTest() {
String testParam1 = "param1";
String testParam2 = "param2";
String testParam3 = "param3";
Set<String> testValue = Set.of("test value");
HashMap<String, Set<String>> queryParams = new HashMap<>();
queryParams.put(testParam1, testValue);
queryParams.put(testParam2, testValue);
queryParams.put(testParam3, testValue);
RestChannelUtility restChannelUtility = new RestChannelUtility(null);
Request request = restChannelUtility.createPostMessageRequest("https://a-url", new HashMap<>(), queryParams, "{}");
Map<String, Set<String>> populatedQueryParameters = request.getPopulatedQueryParameters();
assertMapContains(populatedQueryParameters, testParam1);
assertMapContains(populatedQueryParameters, testParam2);
assertMapContains(populatedQueryParameters, testParam3);
assertEquals(testValue.size(), populatedQueryParameters.get(testParam1).size());
}
use of com.synopsys.integration.rest.request.Request in project hub-alert by blackducksoftware.
the class SlackChannelMessageSender method sendMessages.
@Override
public MessageResult sendMessages(SlackJobDetailsModel slackJobDetails, List<SlackChannelMessageModel> channelMessages) throws AlertException {
String webhook = slackJobDetails.getWebhook();
String channelName = slackJobDetails.getChannelName();
String channelUsername = Optional.ofNullable(slackJobDetails.getChannelUsername()).orElse(SLACK_DEFAULT_USERNAME);
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("Content-Type", "application/json");
IntHttpClient intHttpClient = connectionFactory.createIntHttpClient(webhook);
RestChannelUtility restChannelUtility = new RestChannelUtility(intHttpClient);
List<Request> requests = channelMessages.stream().map(channelMessage -> createJsonString(channelMessage.getMarkdownContent(), channelName, channelUsername)).map(jsonString -> restChannelUtility.createPostMessageRequest(webhook, requestHeaders, jsonString)).collect(Collectors.toList());
restChannelUtility.sendMessage(requests, slackChannelKey.getUniversalKey());
return new MessageResult(String.format("Successfully sent %d Slack message(s)", requests.size()));
}
use of com.synopsys.integration.rest.request.Request in project hub-alert by blackducksoftware.
the class ProxyTestService method pingHost.
public ConfigurationTestResult pingHost(String testUrl, SettingsProxyModel settingsProxyModel) {
ProxyInfo proxyInfo = proxyManager.createProxyInfo(settingsProxyModel);
IntHttpClient client = createIntHttpClient(proxyInfo);
try {
HttpUrl httpUrl = new HttpUrl(testUrl);
Request testRequest = new Request.Builder(httpUrl).build();
Response response = client.execute(testRequest);
if (RestConstants.OK_200 >= response.getStatusCode() && response.getStatusCode() < RestConstants.MULT_CHOICE_300) {
logger.info("Successfully pinged {}!", testUrl);
return ConfigurationTestResult.success();
} else {
return ConfigurationTestResult.failure(String.format("Could not ping: %s. Status Message: %s. Status code: %s", testUrl, response.getStatusMessage(), response.getStatusCode()));
}
} catch (IntegrationException e) {
logger.error(e.getMessage(), e);
return ConfigurationTestResult.failure(e.getMessage());
}
}
Aggregations