@Override public FileItem read(String name) throws IOException { File file = new File(root, name); if (file.exists()) { FileData data = getFileData(file); FileInputStream stream = new FileInputStream(file); return new FileItem(data, stream); } return null; }
/** * Returns the latest zipped project. * * @param name a project name * @return a zipped project */ @GET @Path("project/{name}") @Produces("application/zip") public Response getLastProject(@PathParam("name") String name) throws WorkspaceException { try { if (!isGranted(Privileges.VIEW_PROJECTS)) { return Response.status(Status.FORBIDDEN).entity("Doesn't have VIEW privilege").build(); } FileItem fileItem = getRepository().read(getFileName(name)); if (fileItem == null) { throw new FileNotFoundException("File '" + name + "' not found."); } String zipFileName = String.format("%s-%s.zip", name, fileItem.getData().getVersion()); return Response.ok(fileItem.getStream()) .header("Content-Disposition", "attachment;filename=\"" + zipFileName + "\"") .build(); } catch (IOException ex) { return Response.status(Status.NOT_FOUND).entity(ex.getMessage()).build(); } }
private ZipInputStream getZipInputStream() throws IOException { FileItem fileItem = version == null ? delegate.read(zipPath) : delegate.readHistory(zipPath, version); return new ZipInputStream(fileItem.getStream()); }
/** * Returns a zipped project. * * @param name a project name * @param version a project version * @return a zipped project */ @GET @Path("project/{name}/{version}") @Produces("application/zip") public Response getProject(@PathParam("name") String name, @PathParam("version") String version) throws WorkspaceException { try { if (!isGranted(Privileges.VIEW_PROJECTS)) { return Response.status(Status.FORBIDDEN).entity("Doesn't have VIEW privilege").build(); } FileItem fileItem = getRepository().readHistory(getFileName(name), version); if (fileItem == null) { throw new FileNotFoundException("File '" + name + "' not found."); } String zipFileName = String.format("%s-%s.zip", name, version); return Response.ok(fileItem.getStream()) .header("Content-Disposition", "attachment;filename=\"" + zipFileName + "\"") .build(); } catch (IOException ex) { return Response.status(Status.NOT_FOUND).entity(ex.getMessage()).build(); } }
/** * Returns the latest zipped project. * * @param name a project name * @return a zipped project */ @GET @Path("project/{name}") @Produces("application/zip") public Response getLastProject(@PathParam("name") String name) throws WorkspaceException { try { if (!isGranted(Privileges.VIEW_PROJECTS)) { return Response.status(Status.FORBIDDEN).entity("Doesn't have VIEW privilege").build(); } FileItem fileItem = getRepository().read(getFileName(name)); if (fileItem == null) { throw new FileNotFoundException("File '" + name + "' not found."); } String zipFileName = String.format("%s-%s.zip", name, fileItem.getData().getVersion()); return Response.ok(fileItem.getStream()) .header("Content-Disposition", "attachment;filename=\"" + zipFileName + "\"") .build(); } catch (IOException ex) { return Response.status(Status.NOT_FOUND).entity(ex.getMessage()).build(); } }
private FileItem createFileItem(FolderAPI project, FileData fileData) throws IOException, ProjectException { ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zipOutputStream = new ZipOutputStream(out); writeFolderToZip(project, zipOutputStream, ""); zipOutputStream.close(); return new FileItem(fileData, new ByteArrayInputStream(out.toByteArray())); }
/** * Returns a zipped project. * * @param name a project name * @param version a project version * @return a zipped project */ @GET @Path("project/{name}/{version}") @Produces("application/zip") public Response getProject(@PathParam("name") String name, @PathParam("version") String version) throws WorkspaceException { try { if (!isGranted(Privileges.VIEW_PROJECTS)) { return Response.status(Status.FORBIDDEN).entity("Doesn't have VIEW privilege").build(); } FileItem fileItem = getRepository().readHistory(getFileName(name), version); if (fileItem == null) { throw new FileNotFoundException("File '" + name + "' not found."); } String zipFileName = String.format("%s-%s.zip", name, version); return Response.ok(fileItem.getStream()) .header("Content-Disposition", "attachment;filename=\"" + zipFileName + "\"") .build(); } catch (IOException ex) { return Response.status(Status.NOT_FOUND).entity(ex.getMessage()).build(); } }
FileItem fileItem = readHistory(fileFrom, data.getVersion()); String fileTo = destData.getName() + fileFrom.substring(srcName.length()); files.add(new FileChange(fileTo, fileItem.getStream())); fileItem = readHistory(srcName, version); destData.setSize(fileItem.getData().getSize()); return save(destData, fileItem.getStream()); } finally { if (fileItem != null) { IOUtils.closeQuietly(fileItem.getStream());
private FileItem createFileItem(FolderAPI project, FileData fileData) throws IOException, ProjectException { ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zipOutputStream = new ZipOutputStream(out); writeFolderToZip(project, zipOutputStream, ""); zipOutputStream.close(); return new FileItem(fileData, new ByteArrayInputStream(out.toByteArray())); }
@Override public FileChange next() { FileData file = files.get(fileIndex++); String srcFileName = file.getName(); String fileTo = deploymentPath + srcFileName.substring(rulesPath.length()); FileItem fileItem; try { fileItem = designRepo.readHistory(file.getName(), file.getVersion()); IOUtils.closeQuietly(openedStream); openedStream = fileItem.getStream(); return new FileChange(fileTo, openedStream); } catch (Exception e) { log.error(e.getMessage(), e); return null; } }
@Override public FileData copyHistory(String srcName, FileData destData, String version) throws IOException { FileItem fileItem = null; try { fileItem = readHistory(srcName, version); FileData copy = new FileData(); copy.setName(destData.getName()); copy.setComment(destData.getComment()); copy.setAuthor(destData.getAuthor()); copy.setSize(fileItem.getData().getSize()); return save(copy, fileItem.getStream()); } finally { if (fileItem != null) { IOUtils.closeQuietly(fileItem.getStream()); } } }
@Override public FileItem read(String name) throws IOException { File file = new File(root, name); if (file.exists()) { FileData data = getFileData(file); FileInputStream stream = new FileInputStream(file); return new FileItem(data, stream); } return null; }
private void archiveAndSave(FolderRepository designRepo, String projectPath, String version, Repository deployRepo, FileData dest) throws ProjectException { ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(out); List<FileData> files = designRepo.listFiles(projectPath, version); for (FileData file : files) { String internalPath = file.getName().substring(projectPath.length()); zipOutputStream.putNextEntry(new ZipEntry(internalPath)); FileItem fileItem = designRepo.readHistory(file.getName(), file.getVersion()); try (InputStream content = fileItem.getStream()) { IOUtils.copy(content, zipOutputStream); } zipOutputStream.closeEntry(); } zipOutputStream.finish(); dest.setSize(out.size()); deployRepo.save(dest, new ByteArrayInputStream(out.toByteArray())); } catch (IOException e) { throw new ProjectException(e.getMessage(), e); } finally { IOUtils.closeQuietly(zipOutputStream); } }
@Override public FileData copyHistory(String srcName, FileData destData, String version) throws IOException { FileItem fileItem = null; try { fileItem = readHistory(srcName, version); FileData copy = new FileData(); copy.setName(destData.getName()); copy.setComment(destData.getComment()); copy.setAuthor(destData.getAuthor()); copy.setSize(fileItem.getData().getSize()); return save(copy, fileItem.getStream()); } finally { if (fileItem != null) { IOUtils.closeQuietly(fileItem.getStream()); } } }
@Override public FileItem read(String name) throws IOException { String artefactName = name.substring(zipPath.length() + 1); ZipInputStream zipInputStream = getZipInputStream(); ZipEntry entry; while ((entry = zipInputStream.getNextEntry()) != null) { if (entry.getName().equals(artefactName)) { return new FileItem(createFileData(entry), zipInputStream); } } return null; }
private Iterator<FileChange> getProjectIterator(String projectName, String version) { try { if (designRepo instanceof FolderRepository) { // Project in design repository is stored as a folder String srcProjectPath = rulesPath + projectName + "/"; return new FolderIterator(((FolderRepository) designRepo).listFiles(srcProjectPath, version)); } else { // Project in design repository is stored as a zip file FileItem srcPrj = designRepo.readHistory(rulesPath + projectName, version); if (srcPrj == null) { throw new FileNotFoundException("File '" + rulesPath + projectName + "' for version " + version + " is not found"); } IOUtils.closeQuietly(openedStream); ZipInputStream stream = new ZipInputStream(srcPrj.getStream()); openedStream = stream; return new FileChangesFromZip(stream, deploymentPath + projectName).iterator(); } } catch (Exception e) { log.error(e.getMessage(), e); return null; } }
String projectName = pd.getProjectName(); FileItem srcPrj = designRepo.readHistory("DESIGN/rules/" + projectName, version); stream = srcPrj.getStream(); FileData dest = new FileData(); dest.setName(deploymentPath + "/" + projectName); dest.setAuthor(userName); dest.setComment(srcPrj.getData().getComment()); dest.setSize(srcPrj.getData().getSize()); deployRepo.save(dest, stream); } finally {
private FileItem createFileItem(ResultSet rs) throws SQLException { FileData fileData = createFileData(rs); InputStream data = rs.getBinaryStream("file_data"); if (data == null) { return null; } // ResultSet will be closed, so InputStream can be closed too, that's // why copy it to byte array before. ByteArrayOutputStream out = new ByteArrayOutputStream(); try { IOUtils.copy(data, out); } catch (IOException e) { throw new SQLException(e); } return new FileItem(fileData, new ByteArrayInputStream(out.toByteArray())); }
public InputStream getContent() throws ProjectException { try { if (isHistoric()) { return getRepository().readHistory(getFileData().getName(), getFileData().getVersion()).getStream(); } else { return getRepository().read(getFileData().getName()).getStream(); } } catch (IOException ex) { throw new ProjectException(ex.getMessage(), ex); } }