Search in sources :

Example 1 with ResponseBodyBuilder

use of com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder 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 ResponseBodyBuilder

use of com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder in project hub-alert by blackducksoftware.

the class CommonConfigHandler method postConfig.

public ResponseEntity<String> postConfig(final R restModel) {
    if (restModel == null) {
        return createResponse(HttpStatus.BAD_REQUEST, "", "Required request body is missing " + configRestModelClass.getSimpleName());
    }
    if (!configActions.doesConfigExist(restModel.getId())) {
        try {
            configActions.validateConfig(restModel);
            configActions.configurationChangeTriggers(restModel);
            try {
                final D updatedEntity = configActions.saveConfig(restModel);
                return createResponse(HttpStatus.CREATED, updatedEntity.getId(), "Created");
            } catch (final AlertException e) {
                logger.error(e.getMessage(), e);
                return createResponse(HttpStatus.INTERNAL_SERVER_ERROR, restModel.getId(), e.getMessage());
            }
        } catch (final AlertFieldException e) {
            final ResponseBodyBuilder responseBuilder = new ResponseBodyBuilder(configActions.getObjectTransformer().stringToLong(restModel.getId()), "There were errors with the configuration.");
            responseBuilder.putErrors(e.getFieldErrors());
            return new ResponseEntity<>(responseBuilder.build(), HttpStatus.BAD_REQUEST);
        }
    }
    return createResponse(HttpStatus.CONFLICT, restModel.getId(), "Provided id must not be in use. To update an existing configuration, use PUT.");
}
Also used : AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException) ResponseBodyBuilder(com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)

Example 3 with ResponseBodyBuilder

use of com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder in project hub-alert by blackducksoftware.

the class CommonConfigHandler method putConfig.

public ResponseEntity<String> putConfig(final R restModel) {
    if (restModel == null) {
        return createResponse(HttpStatus.BAD_REQUEST, "", "Required request body is missing " + configRestModelClass.getSimpleName());
    }
    if (configActions.doesConfigExist(restModel.getId())) {
        try {
            configActions.validateConfig(restModel);
            configActions.configurationChangeTriggers(restModel);
            try {
                final D updatedEntity = configActions.saveNewConfigUpdateFromSavedConfig(restModel);
                return createResponse(HttpStatus.ACCEPTED, updatedEntity.getId(), "Updated");
            } catch (final AlertException e) {
                logger.error(e.getMessage(), e);
                return createResponse(HttpStatus.INTERNAL_SERVER_ERROR, restModel.getId(), e.getMessage());
            }
        } catch (final AlertFieldException e) {
            final ResponseBodyBuilder responseBuilder = new ResponseBodyBuilder(configActions.getObjectTransformer().stringToLong(restModel.getId()), "There were errors with the configuration.");
            responseBuilder.putErrors(e.getFieldErrors());
            return new ResponseEntity<>(responseBuilder.build(), HttpStatus.BAD_REQUEST);
        }
    }
    return createResponse(HttpStatus.BAD_REQUEST, restModel.getId(), "No configuration with the specified id.");
}
Also used : AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException) ResponseBodyBuilder(com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)

Aggregations

AlertFieldException (com.blackducksoftware.integration.hub.alert.exception.AlertFieldException)3 ResponseBodyBuilder (com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)3 AlertException (com.blackducksoftware.integration.hub.alert.exception.AlertException)2 IntegrationRestException (com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException)1 IntLogger (com.blackducksoftware.integration.log.IntLogger)1 PrintStreamIntLogger (com.blackducksoftware.integration.log.PrintStreamIntLogger)1 CsrfToken (org.springframework.security.web.csrf.CsrfToken)1