Search in sources :

Example 56 with ActionResponse

use of com.synopsys.integration.alert.common.action.ActionResponse in project blackduck-alert by blackducksoftware.

the class AbstractUploadAction method deleteFile.

public ActionResponse<Void> deleteFile() {
    if (isTargetUndefined()) {
        return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, META_DATA_MISSING);
    }
    if (!authorizationManager.hasUploadDeletePermission(target.getContext(), target.getDescriptorKey())) {
        return new ActionResponse<>(HttpStatus.FORBIDDEN, ActionResponse.FORBIDDEN_MESSAGE);
    }
    try {
        String targetFilename = target.getFilename();
        File fileToValidate = filePersistenceUtil.createUploadsFile(targetFilename);
        filePersistenceUtil.delete(fileToValidate);
    } catch (IOException ex) {
        logger.error("Error deleting file - file: {}, context: {}, descriptor: {} ", target.getFilename(), target.getContext(), target.getDescriptorKey().getUniversalKey());
        logger.error("Error deleting file caused by: ", ex);
        return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, "Error deleting uploaded file from server.");
    }
    return new ActionResponse<>(HttpStatus.NO_CONTENT);
}
Also used : IOException(java.io.IOException) File(java.io.File) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse)

Example 57 with ActionResponse

use of com.synopsys.integration.alert.common.action.ActionResponse in project blackduck-alert by blackducksoftware.

the class AbstractUploadAction method writeFile.

private ActionResponse<Void> writeFile(Resource fileResource) {
    try {
        String targetFilename = target.getFilename();
        String tempFilename = "temp_" + targetFilename;
        Optional<UploadValidationFunction> validationFunction = target.getValidationFunction();
        if (validationFunction.isPresent()) {
            writeFile(tempFilename, fileResource);
            File tempFileToValidate = filePersistenceUtil.createUploadsFile(tempFilename);
            ValidationResult validationResult = validationFunction.get().apply(tempFileToValidate);
            filePersistenceUtil.delete(tempFileToValidate);
            if (validationResult.hasErrors()) {
                return new ActionResponse<>(HttpStatus.BAD_REQUEST, validationResult.combineErrorMessages());
            }
        }
        writeFile(targetFilename, fileResource);
    } catch (IOException ex) {
        logger.error("Error uploading file - file: {}, context: {}, descriptor: {} ", target.getFilename(), target.getContext(), target.getDescriptorKey().getUniversalKey());
        logger.error("Error uploading file caused by: ", ex);
        return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, "Error uploading file to server.");
    }
    return new ActionResponse<>(HttpStatus.NO_CONTENT);
}
Also used : UploadValidationFunction(com.synopsys.integration.alert.common.descriptor.config.field.validation.UploadValidationFunction) IOException(java.io.IOException) ValidationResult(com.synopsys.integration.alert.common.descriptor.config.field.validation.ValidationResult) File(java.io.File) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse)

Example 58 with ActionResponse

use of com.synopsys.integration.alert.common.action.ActionResponse in project blackduck-alert by blackducksoftware.

the class AzureBoardsCustomFunctionAction method createActionResponse.

@Override
public ActionResponse<OAuthEndpointResponse> createActionResponse(FieldModel fieldModel, HttpServletContentWrapper servletContentWrapper) {
    try {
        Optional<FieldModel> savedFieldModel = saveIfValid(fieldModel);
        if (!savedFieldModel.isPresent()) {
            return createErrorResponse("The configuration is invalid. Please test the configuration.");
        }
        FieldUtility fieldUtility = createFieldAccessor(savedFieldModel.get());
        Optional<String> clientId = fieldUtility.getString(AzureBoardsDescriptor.KEY_CLIENT_ID);
        if (!clientId.isPresent()) {
            return createErrorResponse("App ID not found.");
        }
        Optional<String> alertServerUrl = alertWebServerUrlManager.getServerUrl();
        if (!alertServerUrl.isPresent()) {
            return createErrorResponse("Could not determine the alert server url for the callback.");
        }
        String requestKey = oAuthRequestValidator.generateRequestKey();
        // since we have only one OAuth channel now remove any other requests.
        // if we have more OAuth clients then the "remove requests" will have to be removed from here.
        // beginning authentication process create the request id at the start.
        oAuthRequestValidator.removeRequestsOlderThan5MinutesAgo();
        oAuthRequestValidator.addAuthorizationRequest(requestKey);
        logger.info("OAuth authorization request created: {}", requestKey);
        String authUrl = createAuthURL(clientId.get(), requestKey);
        logger.debug("Authenticating Azure OAuth URL: {}", authUrl);
        return new ActionResponse<>(HttpStatus.OK, new OAuthEndpointResponse(isAuthenticated(fieldUtility), authUrl, "Authenticating..."));
    } catch (Exception ex) {
        logger.error("Error activating Azure Boards", ex);
        return createErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, "Error activating azure oauth.");
    }
}
Also used : OAuthEndpointResponse(com.synopsys.integration.alert.common.descriptor.config.field.endpoint.oauth.OAuthEndpointResponse) ConfigurationFieldModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel) FieldModel(com.synopsys.integration.alert.common.rest.model.FieldModel) FieldUtility(com.synopsys.integration.alert.common.persistence.accessor.FieldUtility) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse)

Example 59 with ActionResponse

use of com.synopsys.integration.alert.common.action.ActionResponse in project blackduck-alert by blackducksoftware.

the class AzureBoardsCustomFunctionAction method createErrorResponse.

private ActionResponse<OAuthEndpointResponse> createErrorResponse(HttpStatus httpStatus, String errorMessage) {
    oAuthRequestValidator.removeAllRequests();
    OAuthEndpointResponse oAuthEndpointResponse = new OAuthEndpointResponse(false, "", errorMessage);
    return new ActionResponse<>(httpStatus, errorMessage, oAuthEndpointResponse);
}
Also used : OAuthEndpointResponse(com.synopsys.integration.alert.common.descriptor.config.field.endpoint.oauth.OAuthEndpointResponse) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse)

Example 60 with ActionResponse

use of com.synopsys.integration.alert.common.action.ActionResponse in project blackduck-alert by blackducksoftware.

the class CertificateActions method updateWithoutChecks.

@Override
protected ActionResponse<CertificateModel> updateWithoutChecks(Long id, CertificateModel resource) {
    try {
        Optional<CustomCertificateModel> existingCertificate = certificateAccessor.getCertificate(id);
        String logableId = escapeUtil.replaceWithUnderscore(resource.getId());
        String loggableAlias = escapeUtil.replaceWithUnderscore(resource.getAlias());
        logger.info("Updating certificate with id: {} and alias: {}", logableId, loggableAlias);
        if (existingCertificate.isPresent()) {
            CertificateModel certificateModel = importCertificate(resource);
            return new ActionResponse<>(HttpStatus.NO_CONTENT, certificateModel);
        }
        logger.error("Certificate with id: {} missing.", logableId);
        return new ActionResponse<>(HttpStatus.NOT_FOUND, "Certificate not found.");
    } catch (AlertException ex) {
        logger.error("Error occurred updating certificate", ex);
        return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
    }
}
Also used : CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Aggregations

ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)132 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)46 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)40 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)38 PermissionMatrixModel (com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel)34 PermissionKey (com.synopsys.integration.alert.common.persistence.model.PermissionKey)32 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)30 ChannelKey (com.synopsys.integration.alert.descriptor.api.model.ChannelKey)30 AuthenticationTestUtils (com.synopsys.integration.alert.test.common.AuthenticationTestUtils)30 Test (org.junit.jupiter.api.Test)30 FieldModel (com.synopsys.integration.alert.common.rest.model.FieldModel)24 ConfigurationFieldModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel)16 JobFieldModel (com.synopsys.integration.alert.common.rest.model.JobFieldModel)16 DistributionJobModel (com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel)12 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)10 Collection (java.util.Collection)10 Optional (java.util.Optional)10 Set (java.util.Set)10 Collectors (java.util.stream.Collectors)10 Autowired (org.springframework.beans.factory.annotation.Autowired)10