Search in sources :

Example 36 with ConcordApplicationException

use of com.walmartlabs.concord.server.sdk.ConcordApplicationException in project concord by walmartlabs.

the class ProcessResource method downloadAttachment.

/**
 * Returns a process' attachment file.
 */
@GET
@ApiOperation(value = "Download a process' attachment", response = File.class)
@javax.ws.rs.Path("/{id}/attachment/{name:.*}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadAttachment(@ApiParam @PathParam("id") UUID instanceId, @PathParam("name") @NotNull @Size(min = 1) String attachmentName) {
    ProcessEntry processEntry = processManager.assertProcess(instanceId);
    assertProcessAccess(processEntry, "attachment");
    PartialProcessKey processKey = new ProcessKey(processEntry.instanceId(), processEntry.createdAt());
    // TODO replace with javax.validation
    if (attachmentName.endsWith("/")) {
        throw new ConcordApplicationException("Invalid attachment name: " + attachmentName, Status.BAD_REQUEST);
    }
    String resource = path(Constants.Files.JOB_ATTACHMENTS_DIR_NAME, attachmentName);
    Optional<Path> o = stateManager.get(processKey, resource, src -> {
        try {
            Path tmp = IOUtils.createTempFile("attachment", ".bin");
            try (OutputStream dst = Files.newOutputStream(tmp)) {
                IOUtils.copy(src, dst);
            }
            return Optional.of(tmp);
        } catch (IOException e) {
            throw new ConcordApplicationException("Error while exporting an attachment: " + attachmentName, e);
        }
    });
    if (!o.isPresent()) {
        return Response.status(Status.NOT_FOUND).build();
    }
    Path tmp = o.get();
    return Response.ok((StreamingOutput) out -> {
        try (InputStream in = Files.newInputStream(tmp)) {
            IOUtils.copy(in, out);
        } finally {
            Files.delete(tmp);
        }
    }).build();
}
Also used : Path(java.nio.file.Path) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) ApiOperation(io.swagger.annotations.ApiOperation)

Example 37 with ConcordApplicationException

use of com.walmartlabs.concord.server.sdk.ConcordApplicationException in project concord by walmartlabs.

the class RoleResource method delete.

@DELETE
@ApiOperation("Delete an existing role")
@Path("/{roleName}")
@Produces(MediaType.APPLICATION_JSON)
public GenericOperationResult delete(@ApiParam @PathParam("roleName") @ConcordKey String roleName) {
    assertAdmin();
    UUID id = roleDao.getId(roleName);
    if (id == null) {
        throw new ConcordApplicationException("Role not found: " + roleName, Status.NOT_FOUND);
    }
    roleDao.delete(id);
    auditLog.add(AuditObject.ROLE, AuditAction.DELETE).field("roleId", id).field("name", roleName).log();
    return new GenericOperationResult(OperationResult.DELETED);
}
Also used : GenericOperationResult(com.walmartlabs.concord.server.GenericOperationResult) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) UUID(java.util.UUID) ApiOperation(io.swagger.annotations.ApiOperation)

Example 38 with ConcordApplicationException

use of com.walmartlabs.concord.server.sdk.ConcordApplicationException in project concord by walmartlabs.

the class RoleResource method get.

@GET
@ApiOperation("Get a role's details")
@Path("/{roleName}")
@Produces(MediaType.APPLICATION_JSON)
@WithTimer
public RoleEntry get(@ApiParam @PathParam("roleName") String roleName) {
    assertAdmin();
    UUID id = roleDao.getId(roleName);
    if (id == null) {
        throw new ConcordApplicationException("Role not found: " + roleName, Status.NOT_FOUND);
    }
    return roleDao.get(id);
}
Also used : ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) UUID(java.util.UUID) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) ApiOperation(io.swagger.annotations.ApiOperation)

Example 39 with ConcordApplicationException

use of com.walmartlabs.concord.server.sdk.ConcordApplicationException in project concord by walmartlabs.

the class RepositoryRefresher method refresh.

public void refresh(String orgName, String projectName, String repositoryName, boolean sync) {
    UUID orgId = orgManager.assertAccess(orgName, true).getId();
    ProjectEntry projectEntry = assertProject(orgId, projectName, ResourceAccessLevel.READER, true);
    UUID projectId = projectEntry.getId();
    RepositoryEntry repositoryEntry = assertRepository(projectEntry, repositoryName);
    if (!sync) {
        Map<String, Object> event = new HashMap<>();
        event.put("event", "repositoryRefresh");
        event.put("org", orgName);
        event.put("project", projectName);
        event.put("repository", repositoryName);
        externalEventResource.event("concord", event);
        return;
    }
    try (TemporaryPath tmpRepoPath = IOUtils.tempDir("refreshRepo_")) {
        repositoryManager.withLock(repositoryEntry.getUrl(), () -> {
            Repository repo = repositoryManager.fetch(projectId, repositoryEntry);
            repo.export(tmpRepoPath.path());
            return null;
        });
        tx(tx -> {
            for (RepositoryRefreshListener l : listeners) {
                l.onRefresh(tx, repositoryEntry, tmpRepoPath.path());
            }
        });
    } catch (Exception e) {
        String errorMessage = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
        throw new ConcordApplicationException("Error while refreshing repository: \n" + errorMessage, e);
    }
}
Also used : RepositoryUtils.assertRepository(com.walmartlabs.concord.server.org.project.RepositoryUtils.assertRepository) Repository(com.walmartlabs.concord.repository.Repository) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) RepositoryRefreshListener(com.walmartlabs.concord.server.repository.listeners.RepositoryRefreshListener) TemporaryPath(com.walmartlabs.concord.common.TemporaryPath) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException)

Example 40 with ConcordApplicationException

use of com.walmartlabs.concord.server.sdk.ConcordApplicationException in project concord by walmartlabs.

the class ProcessLogResourceV2 method append.

/**
 * Appends a process' log.
 */
@POST
@Path("{id}/log/segment/{segmentId}/data")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@WithTimer
public void append(@ApiParam @PathParam("id") UUID instanceId, @ApiParam @PathParam("segmentId") long segmentId, InputStream data) {
    ProcessKey processKey = logAccessManager.assertLogAccess(instanceId);
    try {
        byte[] ab = IOUtils.toByteArray(data);
        int upper = logManager.log(processKey, segmentId, ab);
        int logSizeLimit = processCfg.getLogSizeLimit();
        if (upper >= logSizeLimit) {
            logManager.error(processKey, "Maximum log size reached: {}. Process cancelled.", logSizeLimit);
            processManager.kill(processKey);
        }
    } catch (IOException e) {
        throw new ConcordApplicationException("Error while appending a log: " + e.getMessage());
    }
}
Also used : ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) IOException(java.io.IOException) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Aggregations

ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)70 ApiOperation (io.swagger.annotations.ApiOperation)28 UUID (java.util.UUID)22 WithTimer (com.walmartlabs.concord.server.sdk.metrics.WithTimer)21 PartialProcessKey (com.walmartlabs.concord.server.sdk.PartialProcessKey)18 IOException (java.io.IOException)14 ValidationErrorsException (org.sonatype.siesta.ValidationErrorsException)12 GenericOperationResult (com.walmartlabs.concord.server.GenericOperationResult)11 ProcessKey (com.walmartlabs.concord.server.sdk.ProcessKey)11 Validate (org.sonatype.siesta.Validate)11 Path (java.nio.file.Path)10 UserPrincipal (com.walmartlabs.concord.server.security.UserPrincipal)9 OrganizationEntry (com.walmartlabs.concord.server.org.OrganizationEntry)8 EntryPoint (com.walmartlabs.concord.server.process.PayloadManager.EntryPoint)6 ProcessStatus (com.walmartlabs.concord.server.sdk.ProcessStatus)6 UnauthorizedException (org.apache.shiro.authz.UnauthorizedException)6 UserEntry (com.walmartlabs.concord.server.user.UserEntry)5 InputStream (java.io.InputStream)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Form (com.walmartlabs.concord.forms.Form)3