Search in sources :

Example 1 with Response

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.";
}
Also used : Response(com.blackducksoftware.integration.hub.request.Response) RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) HashMap(java.util.HashMap) Request(com.blackducksoftware.integration.hub.request.Request) ChannelRequestHelper(com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRequestHelper)

Example 2 with Response

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);
    }
}
Also used : Response(com.blackducksoftware.integration.hub.request.Response)

Example 3 with Response

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());
    }
}
Also used : Response(com.blackducksoftware.integration.hub.request.Response) HubService(com.blackducksoftware.integration.hub.service.HubService) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException)

Aggregations

Response (com.blackducksoftware.integration.hub.request.Response)3 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)2 ChannelRequestHelper (com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRequestHelper)1 AlertException (com.blackducksoftware.integration.hub.alert.exception.AlertException)1 Request (com.blackducksoftware.integration.hub.request.Request)1 RestConnection (com.blackducksoftware.integration.hub.rest.RestConnection)1 HubService (com.blackducksoftware.integration.hub.service.HubService)1 HashMap (java.util.HashMap)1