public AttachmentContainer createAttachmentContainer() { AttachmentMeta meta = new AttachmentMeta(); AttachmentMetaAccessor accessor = new AttachmentMetaAccessor(); accessor.save(meta); File containerFolder = getContainerFileById(meta.getId()); containerFolder.mkdirs(); AttachmentContainer container = new AttachmentContainer(); container.setMeta(meta); container.setContainer(containerFolder); return container; }
public AttachmentMeta createAttachmentWithoutQuotaCheck(byte[] content, String filename) { AttachmentContainer container = attachmentManager.createAttachmentContainer(); try { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(container.getContainer().getAbsoluteFile()+"/"+filename))); bos.write(content); bos.close(); } catch (IOException ex) { logger.error("Unable to write exception.log", ex); } AttachmentMeta attachment = container.getMeta(); attachment.setName(filename); return attachment; }
public File resolve(String path) { File file; if(path.startsWith(ATTACHMENT_PREFIX)) { file = attachmentManager.getFileById(path.replace(ATTACHMENT_PREFIX, "")); } else { file = new File(path); } return file; } }
public ExportStatus createExportTask(String exportId, ExportRunnable runnable) { ExportStatus status = new ExportStatus(exportId); exportStatusMap.put(exportId, status); AttachmentContainer container = attachmentManager.createAttachmentContainer(); status.setAttachmentID(container.getMeta().getId().toString()); runnable.setStatus(status); runnable.setContainer(container.getContainer()); exportExecutor.submit(new Runnable() { @Override public void run() { try { runnable.runExport(); } catch(Exception e) { } finally { status.ready = true; } } }); return status; }
protected Context(Configuration configuration, AbstractFunctionType<?> functionType, Map<String, String> properties) { super(); token = new AgentTokenWrapper(); if(properties!=null) { token.setProperties(properties); } Grid grid = new Grid(0); GridClientImpl client = new GridClientImpl(grid, grid); FunctionTypeRegistry functionTypeRegistry = new FunctionTypeRegistryImpl(new FileResolver(new AttachmentManager(configuration)), grid); functionTypeRegistry.registerFunctionType(functionType); functionExecutionService = new FunctionExecutionServiceImpl(client, functionAccessor, functionTypeRegistry, new DynamicBeanResolver(new DynamicValueResolver(new ExpressionHandler()))); }
protected void registerFile(DynamicValue<String> dynamicValue, String properyName, Map<String, String> props) { if(dynamicValue!=null) { String filepath = dynamicValue.get(); if(filepath!=null && filepath.trim().length()>0) { File file = fileResolver.resolve(filepath); registerFile(file, properyName, props); } } }
@POST @Path("/grid/file") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public FileVersionId registerFile(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) throws FileManagerException { if (uploadedInputStream == null || fileDetail == null) throw new RuntimeException("Invalid arguments"); GridClient grid = controller.getContext().get(GridClient.class); AttachmentContainer container = controller.getContext().getAttachmentManager().createAttachmentContainer(); File file = new File(container.getContainer()+"/"+fileDetail.getFileName()); try { Files.copy(uploadedInputStream, file.toPath()); } catch (IOException e) { throw new RuntimeException("Error while saving file."); } FileVersion fileVersion = grid.registerFile(file); return fileVersion.getVersionId(); } }
@Deprecated public File getFileById(ObjectId id) { File folder = getContainerFileById(id); String[] list = folder.list(); if(list!=null && list.length>0) { File file = new File(folder+"/"+list[0]); return file; } else { return null; } }
@DELETE @Secured @Path("/{id}") public void deleteFile(@PathParam("id") String id) { attachmentManager.deleteContainer(id); }
@POST @Secured @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public UploadResponse uploadFile(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) { if (uploadedInputStream == null || fileDetail == null) throw new RuntimeException("Invalid arguments"); AttachmentContainer container = attachmentManager.createAttachmentContainer(); String uploadedFileLocation = container.getContainer() + "/" + fileDetail.getFileName(); try { Files.copy(uploadedInputStream, Paths.get(uploadedFileLocation)); } catch (IOException e) { logger.error("Error while saving file "+uploadedFileLocation, e); throw new RuntimeException("Error while saving file."); } UploadResponse response = new UploadResponse(); response.setAttachmentId(container.getMeta().getId().toString()); return response; }
protected Context(Configuration configuration, AbstractFunctionType<?> functionType, Map<String, String> properties) { super(); this.properties = properties; this.fileManagerDirectory = FileHelper.createTempFolder(); grid = new Grid(fileManagerDirectory, 0); client = new GridClientImpl(grid); FunctionTypeRegistry functionTypeRegistry = new FunctionTypeRegistryImpl(new FileResolver(new AttachmentManager(configuration)), client); functionTypeRegistry.registerFunctionType(functionType); try { functionExecutionService = new FunctionExecutionServiceImpl(client, functionTypeRegistry, new DynamicBeanResolver(new DynamicValueResolver(new ExpressionHandler()))); } catch (FunctionExecutionServiceException e) { throw new RuntimeException("Error while creating function execution service", e); } }
public File getFileById(String id) { return getFileById(new ObjectId(id)); }
protected void registerFile(DynamicValue<String> dynamicValue, String properyName, Map<String, String> props) { if(dynamicValue!=null) { String filepath = dynamicValue.get(); if(filepath!=null && filepath.trim().length()>0) { File file = fileResolver.resolve(filepath); registerFile(file, properyName, props); } } }
public void deleteContainer(String id) { File folder = getContainerFileById(new ObjectId(id)); FileHelper.deleteFolder(folder); } }
@POST @Path("/context/{id}/destroy") @Consumes(MediaType.APPLICATION_JSON) @Secured(right="plan-write") public void destroy(@PathParam("id") String id) { StagingContext context = stagingContextAccessor.get(id); context.getAttachments().forEach(attachmentId->{ controller.getContext().getAttachmentManager().deleteContainer(attachmentId); }); } }
@POST @Path("/context/{id}/file") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public String uploadFile(@PathParam("id") String id, @FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) { StagingContext context = stagingContextAccessor.get(id); if (uploadedInputStream == null || fileDetail == null) throw new RuntimeException("Invalid arguments"); AttachmentContainer container = controller.getContext().getAttachmentManager().createAttachmentContainer(); File file = new File(container.getContainer()+"/"+fileDetail.getFileName()); try { Files.copy(uploadedInputStream, file.toPath()); } catch (IOException e) { throw new RuntimeException("Error while saving file."); } context.addAttachment(container.getMeta().getId().toString()); stagingContextAccessor.save(context); return container.getMeta().getId().toString(); }
@GET @Secured @Path("/{id}/name") public String getFilename(@PathParam("id") String id) { File file = attachmentManager.getFileById(id); return file.getName(); }
@POST @Path("/container/{id}/artefact") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Secured(right="plan-write") public void importArtefact(@PathParam("id") String id) throws IOException { File file = attachmentManager.getFileById(id); importManager.importArtefacts(file); } }
@GET @Secured @Path("/{id}") public Response downloadFile(@PathParam("id") String id) { File file = attachmentManager.getFileById(id); StreamingOutput fileStream = new StreamingOutput() { @Override public void write(java.io.OutputStream output) throws IOException { java.nio.file.Path path = file.toPath(); byte[] data = Files.readAllBytes(path); output.write(data); output.flush(); } }; return Response.ok(fileStream, MediaType.APPLICATION_OCTET_STREAM) .header("content-disposition", "attachment; filename = "+file.getName()).build(); }
private void handleAttachments(AbstractArtefact artefact, ReportNode report) { List<ObjectId> attachments = artefact.getAttachments(); if(attachments!=null) { for(ObjectId attachmentId:attachments) { AttachmentManager attachmentManager = context.getAttachmentManager(); File file = attachmentManager.getFileById(attachmentId); context.getVariablesManager().putVariable(report, FILE_VARIABLE_PREFIX+file.getName(), file); } } }