Search in sources :

Example 1 with BodyContent

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

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

BodyContent (com.synopsys.integration.rest.body.BodyContent)2 StringBodyContent (com.synopsys.integration.rest.body.StringBodyContent)2 Request (com.synopsys.integration.rest.request.Request)2 Response (com.synopsys.integration.rest.response.Response)2