Search in sources :

Example 1 with IntegrationRestException

use of com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException in project hub-alert by blackducksoftware.

the class LoginHandler method authenticateUser.

public ResponseEntity<String> authenticateUser(final HttpServletRequest request, final HttpServletResponse response, final LoginRestModel loginRestModel) {
    final IntLogger logger = new PrintStreamIntLogger(System.out, LogLevel.INFO);
    try {
        if (loginActions.authenticateUser(loginRestModel, logger)) {
            final CsrfToken token = csrfTokenRepository.generateToken(request);
            csrfTokenRepository.saveToken(token, request, response);
            response.setHeader(token.getHeaderName(), token.getToken());
            return createResponse(HttpStatus.OK, "{\"message\":\"Success\"}");
        }
        return createResponse(HttpStatus.UNAUTHORIZED, "User not administrator");
    } catch (final IntegrationRestException e) {
        logger.error(e.getMessage(), e);
        return createResponse(HttpStatus.valueOf(e.getHttpStatusCode()), e.getHttpStatusMessage() + " : " + e.getMessage());
    } catch (final AlertFieldException e) {
        logger.error(e.getMessage(), e);
        final ResponseBodyBuilder responseBodyBuilder = new ResponseBodyBuilder(0L, e.getMessage());
        responseBodyBuilder.putErrors(e.getFieldErrors());
        final String responseBody = responseBodyBuilder.build();
        return createResponse(HttpStatus.BAD_REQUEST, responseBody);
    } catch (final Exception e) {
        logger.error(e.getMessage(), e);
        return createResponse(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) PrintStreamIntLogger(com.blackducksoftware.integration.log.PrintStreamIntLogger) IntLogger(com.blackducksoftware.integration.log.IntLogger) PrintStreamIntLogger(com.blackducksoftware.integration.log.PrintStreamIntLogger) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) CsrfToken(org.springframework.security.web.csrf.CsrfToken) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) ResponseBodyBuilder(com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)

Example 2 with IntegrationRestException

use of com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException in project hub-alert by blackducksoftware.

the class DistributionChannel method sendAuditedMessage.

public void sendAuditedMessage(final E event, final C config) throws IntegrationException {
    try {
        sendMessage(event, config);
        setAuditEntrySuccess(event.getAuditEntryId());
    } catch (final Exception e) {
        setAuditEntryFailure(event.getAuditEntryId(), e.getMessage(), e);
        if (e instanceof IntegrationRestException) {
            logger.error(((IntegrationRestException) e).getHttpStatusCode() + ":" + ((IntegrationRestException) e).getHttpStatusMessage());
        }
        logger.error(e.getMessage(), e);
        throw new AlertException(e.getMessage());
    }
}
Also used : IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException)

Example 3 with IntegrationRestException

use of com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException in project hub-alert by blackducksoftware.

the class CommonConfigHandlerTest method testConfigWithRestExceptionTest.

@Test
public void testConfigWithRestExceptionTest() throws Exception {
    final CommonDistributionConfigActions configActions = Mockito.mock(CommonDistributionConfigActions.class);
    final CommonConfigHandler<CommonDistributionConfigEntity, CommonDistributionConfigRestModel, CommonDistributionRepositoryWrapper> handler = new CommonConfigHandler<>(CommonDistributionConfigEntity.class, CommonDistributionConfigRestModel.class, configActions, objectTransformer);
    final int responseCode = HttpStatus.BAD_GATEWAY.value();
    Mockito.when(configActions.testConfig(Mockito.any())).thenThrow(new IntegrationRestException(responseCode, "", ""));
    Mockito.when(configActions.getObjectTransformer()).thenReturn(objectTransformer);
    final CommonDistributionConfigRestModel restModel = mockCommonDistributionRestModel.createRestModel();
    final ResponseEntity<String> response = handler.testConfig(restModel);
    assertEquals(HttpStatus.BAD_GATEWAY, response.getStatusCode());
}
Also used : CommonDistributionConfigActions(com.blackducksoftware.integration.hub.alert.web.actions.distribution.CommonDistributionConfigActions) CommonDistributionRepositoryWrapper(com.blackducksoftware.integration.hub.alert.datasource.entity.repository.CommonDistributionRepositoryWrapper) IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) CommonDistributionConfigRestModel(com.blackducksoftware.integration.hub.alert.web.model.distribution.CommonDistributionConfigRestModel) CommonDistributionConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity) Test(org.junit.Test)

Example 4 with IntegrationRestException

use of com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException in project hub-alert by blackducksoftware.

the class HubDataHandlerTest method testGetHubGroupsThrowIntegrationRestException.

@Test
public void testGetHubGroupsThrowIntegrationRestException() throws Exception {
    final ObjectTransformer objectTransformer = new ObjectTransformer();
    final Gson gson = new Gson();
    final HubDataActions hubDataActions = Mockito.mock(HubDataActions.class);
    Mockito.when(hubDataActions.getHubGroups()).thenThrow(new IntegrationRestException(402, "StatusMessage", "ErrorMessage"));
    final HubDataHandler hubDataHandler = new HubDataHandler(objectTransformer, gson, hubDataActions);
    final ResponseEntity<String> responseEntity = hubDataHandler.getHubGroups();
    assertEquals(HttpStatus.PAYMENT_REQUIRED, responseEntity.getStatusCode());
    assertEquals("{\"id\":-1,\"message\":\"StatusMessage : ErrorMessage: 402: StatusMessage\"}", responseEntity.getBody());
}
Also used : IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) ObjectTransformer(com.blackducksoftware.integration.hub.alert.web.ObjectTransformer) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 5 with IntegrationRestException

use of com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException in project hub-alert by blackducksoftware.

the class HubDataHandlerTest method testGetHubProjectsThrowIntegrationRestException.

@Test
public void testGetHubProjectsThrowIntegrationRestException() throws Exception {
    final ObjectTransformer objectTransformer = new ObjectTransformer();
    final Gson gson = new Gson();
    final HubDataActions hubDataActions = Mockito.mock(HubDataActions.class);
    Mockito.when(hubDataActions.getHubProjects()).thenThrow(new IntegrationRestException(402, "StatusMessage", "ErrorMessage"));
    final HubDataHandler hubDataHandler = new HubDataHandler(objectTransformer, gson, hubDataActions);
    final ResponseEntity<String> responseEntity = hubDataHandler.getHubProjects();
    assertEquals(HttpStatus.PAYMENT_REQUIRED, responseEntity.getStatusCode());
    assertEquals("{\"id\":-1,\"message\":\"StatusMessage : ErrorMessage: 402: StatusMessage\"}", responseEntity.getBody());
}
Also used : IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) ObjectTransformer(com.blackducksoftware.integration.hub.alert.web.ObjectTransformer) Gson(com.google.gson.Gson) Test(org.junit.Test)

Aggregations

IntegrationRestException (com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException)7 Test (org.junit.Test)4 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)2 ObjectTransformer (com.blackducksoftware.integration.hub.alert.web.ObjectTransformer)2 Gson (com.google.gson.Gson)2 CommonDistributionConfigEntity (com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity)1 CommonDistributionRepositoryWrapper (com.blackducksoftware.integration.hub.alert.datasource.entity.repository.CommonDistributionRepositoryWrapper)1 AlertException (com.blackducksoftware.integration.hub.alert.exception.AlertException)1 AlertFieldException (com.blackducksoftware.integration.hub.alert.exception.AlertFieldException)1 LoginActions (com.blackducksoftware.integration.hub.alert.web.actions.LoginActions)1 CommonDistributionConfigActions (com.blackducksoftware.integration.hub.alert.web.actions.distribution.CommonDistributionConfigActions)1 ResponseBodyBuilder (com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)1 CommonDistributionConfigRestModel (com.blackducksoftware.integration.hub.alert.web.model.distribution.CommonDistributionConfigRestModel)1 DetectUserFriendlyException (com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException)1 DoesNotExistException (com.blackducksoftware.integration.hub.exception.DoesNotExistException)1 HubTimeoutExceededException (com.blackducksoftware.integration.hub.exception.HubTimeoutExceededException)1 HubService (com.blackducksoftware.integration.hub.service.HubService)1 ProjectService (com.blackducksoftware.integration.hub.service.ProjectService)1 ReportService (com.blackducksoftware.integration.hub.service.ReportService)1 ScanStatusService (com.blackducksoftware.integration.hub.service.ScanStatusService)1