Search in sources :

Example 1 with StringBodyContent

use of com.synopsys.integration.rest.body.StringBodyContent in project hub-alert by blackducksoftware.

the class BlackDuckProviderIssueHandler method performRequest.

private void performRequest(HttpUrl httpUrl, HttpMethod httpMethod, IssueRequest issueRequest) throws IntegrationException {
    BlackDuckResponseRequest request = new BlackDuckRequestBuilder().method(httpMethod).bodyContent(new StringBodyContent(gson.toJson(issueRequest), BodyContentConverter.DEFAULT)).buildBlackDuckResponseRequest(httpUrl);
    blackDuckApiClient.execute(request);
}
Also used : StringBodyContent(com.synopsys.integration.rest.body.StringBodyContent) BlackDuckResponseRequest(com.synopsys.integration.blackduck.service.request.BlackDuckResponseRequest) BlackDuckRequestBuilder(com.synopsys.integration.blackduck.http.BlackDuckRequestBuilder)

Example 2 with StringBodyContent

use of com.synopsys.integration.rest.body.StringBodyContent 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 3 with StringBodyContent

use of com.synopsys.integration.rest.body.StringBodyContent 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 4 with StringBodyContent

use of com.synopsys.integration.rest.body.StringBodyContent 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();
}
Also used : BodyContent(com.synopsys.integration.rest.body.BodyContent) StringBodyContent(com.synopsys.integration.rest.body.StringBodyContent) StringBodyContent(com.synopsys.integration.rest.body.StringBodyContent) Response(com.synopsys.integration.rest.response.Response) Request(com.synopsys.integration.rest.request.Request)

Example 5 with StringBodyContent

use of com.synopsys.integration.rest.body.StringBodyContent 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.");
}
Also used : BodyContent(com.synopsys.integration.rest.body.BodyContent) StringBodyContent(com.synopsys.integration.rest.body.StringBodyContent) StringBodyContent(com.synopsys.integration.rest.body.StringBodyContent) Response(com.synopsys.integration.rest.response.Response) Request(com.synopsys.integration.rest.request.Request)

Aggregations

StringBodyContent (com.synopsys.integration.rest.body.StringBodyContent)5 Request (com.synopsys.integration.rest.request.Request)4 BodyContent (com.synopsys.integration.rest.body.BodyContent)2 Response (com.synopsys.integration.rest.response.Response)2 AlertRuntimeException (com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException)1 BlackDuckRequestBuilder (com.synopsys.integration.blackduck.http.BlackDuckRequestBuilder)1 BlackDuckResponseRequest (com.synopsys.integration.blackduck.service.request.BlackDuckResponseRequest)1 IntegrationException (com.synopsys.integration.exception.IntegrationException)1 HttpUrl (com.synopsys.integration.rest.HttpUrl)1 HashMap (java.util.HashMap)1 Test (org.junit.jupiter.api.Test)1