Search in sources :

Example 1 with Request

use of com.synopsys.integration.rest.request.Request in project hub-alert by blackducksoftware.

the class DockerTagRetriever method getTagResponseModel.

private DockerTagsResponseModel getTagResponseModel(String pageUrl) {
    HttpUrl httpUrl;
    try {
        httpUrl = new HttpUrl(pageUrl);
    } catch (IntegrationException e) {
        logger.warn("Invalid url: " + pageUrl);
        return DockerTagsResponseModel.EMPTY;
    }
    Request dockerTagsRequest = new Request.Builder(httpUrl).build();
    try (Response tagsResponse = intHttpClient.execute(dockerTagsRequest)) {
        tagsResponse.throwExceptionForError();
        return gson.fromJson(tagsResponse.getContentString(), DockerTagsResponseModel.class);
    } catch (IOException | IntegrationException e) {
        logger.warn("Could not get docker tags from {}: {}", pageUrl, e.getMessage());
    }
    return DockerTagsResponseModel.EMPTY;
}
Also used : Response(com.synopsys.integration.rest.response.Response) IntegrationException(com.synopsys.integration.exception.IntegrationException) Request(com.synopsys.integration.rest.request.Request) IOException(java.io.IOException) HttpUrl(com.synopsys.integration.rest.HttpUrl)

Example 2 with Request

use of com.synopsys.integration.rest.request.Request in project hub-alert by blackducksoftware.

the class MSTeamsChannelMessageSender method sendMessages.

@Override
public MessageResult sendMessages(MSTeamsJobDetailsModel msTeamsJobDetailsModel, List<MSTeamsChannelMessageModel> channelMessages) throws AlertException {
    String webhook = msTeamsJobDetailsModel.getWebhook();
    Map<String, String> requestHeaders = new HashMap<>();
    requestHeaders.put("Content-Type", "application/json");
    IntHttpClient intHttpClient = connectionFactory.createIntHttpClient(webhook);
    RestChannelUtility restChannelUtility = new RestChannelUtility(intHttpClient);
    List<Request> messageRequests = channelMessages.stream().map(this::createJsonString).map(jsonString -> restChannelUtility.createPostMessageRequest(webhook, requestHeaders, jsonString)).collect(Collectors.toList());
    restChannelUtility.sendMessage(messageRequests, msTeamsKey.getUniversalKey());
    return new MessageResult(String.format("Successfully sent %d MSTeams message(s)", channelMessages.size()));
}
Also used : JsonObject(com.google.gson.JsonObject) Request(com.synopsys.integration.rest.request.Request) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult) IntHttpClient(com.synopsys.integration.rest.client.IntHttpClient) RestChannelUtility(com.synopsys.integration.alert.api.channel.rest.RestChannelUtility) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ChannelRestConnectionFactory(com.synopsys.integration.alert.api.channel.rest.ChannelRestConnectionFactory) Collectors(java.util.stream.Collectors) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) MSTeamsJobDetailsModel(com.synopsys.integration.alert.common.persistence.model.job.details.MSTeamsJobDetailsModel) ChannelMessageSender(com.synopsys.integration.alert.api.channel.ChannelMessageSender) List(java.util.List) Component(org.springframework.stereotype.Component) JsonArray(com.google.gson.JsonArray) Map(java.util.Map) MsTeamsKey(com.synopsys.integration.alert.descriptor.api.MsTeamsKey) HashMap(java.util.HashMap) IntHttpClient(com.synopsys.integration.rest.client.IntHttpClient) RestChannelUtility(com.synopsys.integration.alert.api.channel.rest.RestChannelUtility) Request(com.synopsys.integration.rest.request.Request) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult)

Example 3 with Request

use of com.synopsys.integration.rest.request.Request in project hub-alert by blackducksoftware.

the class RestChannelUtility method createPostMessageRequest.

public Request createPostMessageRequest(String url, Map<String, String> headers, @Nullable Map<String, Set<String>> queryParameters, String jsonString) {
    HttpUrl httpUrl;
    try {
        httpUrl = new HttpUrl(url);
    } catch (IntegrationException e) {
        throw new AlertRuntimeException(e);
    }
    Request.Builder requestBuilder = new Request.Builder().method(HttpMethod.POST).url(httpUrl);
    requestBuilder.getHeaders().putAll(headers);
    requestBuilder.bodyContent(new StringBodyContent(jsonString, BodyContentConverter.DEFAULT));
    if (queryParameters != null && !queryParameters.isEmpty()) {
        requestBuilder.queryParameters(queryParameters);
    }
    return requestBuilder.build();
}
Also used : StringBodyContent(com.synopsys.integration.rest.body.StringBodyContent) IntegrationException(com.synopsys.integration.exception.IntegrationException) Request(com.synopsys.integration.rest.request.Request) AlertRuntimeException(com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException) HttpUrl(com.synopsys.integration.rest.HttpUrl)

Example 4 with Request

use of com.synopsys.integration.rest.request.Request in project hub-alert by blackducksoftware.

the class RestChannelUtilityTest method createPostMessageRequestTest.

@Test
public void createPostMessageRequestTest() {
    String testUrl = "https://a-url";
    String testJson = "{\"testField\": \"test value\"}";
    Map<String, String> testHeaders = new HashMap<>();
    String testHeaderKey = "Test Header";
    testHeaders.put(testHeaderKey, "header value");
    RestChannelUtility restChannelUtility = new RestChannelUtility(null);
    Request request = restChannelUtility.createPostMessageRequest(testUrl, testHeaders, testJson);
    assertEquals(testUrl, request.getUrl().string());
    assertEquals(HttpMethod.POST, request.getMethod());
    assertTrue(request.getHeaders().containsKey(testHeaderKey));
    assertTrue(request.getQueryParameters().isEmpty());
    assertReflectionEquals(new StringBodyContent(testJson, BodyContentConverter.DEFAULT), request.getBodyContent());
}
Also used : StringBodyContent(com.synopsys.integration.rest.body.StringBodyContent) HashMap(java.util.HashMap) Request(com.synopsys.integration.rest.request.Request) Test(org.junit.jupiter.api.Test)

Example 5 with Request

use of com.synopsys.integration.rest.request.Request in project hub-alert by blackducksoftware.

the class DockerTagRetrieverTest method getTagsModelTestIT.

@Test
@Tags({ @Tag(TestTags.DEFAULT_INTEGRATION), @Tag(TestTags.CUSTOM_EXTERNAL_CONNECTION) })
public void getTagsModelTestIT() throws IntegrationException {
    IntLogger intLogger = new PrintStreamIntLogger(System.out, LogLevel.INFO);
    IntHttpClient intHttpClient = new IntHttpClient(intLogger, gson, 10, true, ProxyInfo.NO_PROXY_INFO);
    HttpUrl httpUrl = new HttpUrl("https://google.com");
    Request testRequest = new Request.Builder(httpUrl).build();
    try (Response googleResponse = intHttpClient.execute(testRequest)) {
        googleResponse.throwExceptionForError();
    } catch (IntegrationException | IOException e) {
        assumeTrue(null == e, "Could not connect. Skipping this test...");
    }
    DockerTagRetriever dockerTagRetriever = new DockerTagRetriever(gson, intHttpClient);
    DockerTagsResponseModel tagsModel = dockerTagRetriever.getTagsModel();
    assertFalse(tagsModel.isEmpty(), "Expected tags from the docker repo to exist");
}
Also used : Response(com.synopsys.integration.rest.response.Response) DockerTagsResponseModel(com.synopsys.integration.alert.update.model.DockerTagsResponseModel) PrintStreamIntLogger(com.synopsys.integration.log.PrintStreamIntLogger) IntegrationException(com.synopsys.integration.exception.IntegrationException) IntHttpClient(com.synopsys.integration.rest.client.IntHttpClient) Request(com.synopsys.integration.rest.request.Request) IntLogger(com.synopsys.integration.log.IntLogger) PrintStreamIntLogger(com.synopsys.integration.log.PrintStreamIntLogger) IOException(java.io.IOException) HttpUrl(com.synopsys.integration.rest.HttpUrl) Test(org.junit.jupiter.api.Test) TestTags(com.synopsys.integration.alert.test.common.TestTags) Tags(org.junit.jupiter.api.Tags)

Aggregations

Request (com.synopsys.integration.rest.request.Request)10 Response (com.synopsys.integration.rest.response.Response)5 IntegrationException (com.synopsys.integration.exception.IntegrationException)4 HttpUrl (com.synopsys.integration.rest.HttpUrl)4 StringBodyContent (com.synopsys.integration.rest.body.StringBodyContent)4 IntHttpClient (com.synopsys.integration.rest.client.IntHttpClient)4 HashMap (java.util.HashMap)4 Test (org.junit.jupiter.api.Test)3 JsonObject (com.google.gson.JsonObject)2 ChannelMessageSender (com.synopsys.integration.alert.api.channel.ChannelMessageSender)2 ChannelRestConnectionFactory (com.synopsys.integration.alert.api.channel.rest.ChannelRestConnectionFactory)2 RestChannelUtility (com.synopsys.integration.alert.api.channel.rest.RestChannelUtility)2 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)2 MessageResult (com.synopsys.integration.alert.common.message.model.MessageResult)2 BodyContent (com.synopsys.integration.rest.body.BodyContent)2 IOException (java.io.IOException)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2