@Authenticated(value = {LoggedIn.class, HasRole.class}) @Authorized(value = "admin") @Transactional @RequireCSRFCheck public Result postCreateArchive() { Form<ArchiveUpsertForm> archiveUpsertForm = Form.form(ArchiveUpsertForm.class).bindFromRequest(); if (formHasErrors(archiveUpsertForm)) { return showCreateArchive(0, archiveUpsertForm); } ArchiveUpsertForm archiveUpsertData = archiveUpsertForm.get(); if (!archiveUpsertData.parentJid.isEmpty() && !archiveService.archiveExistsByJid(archiveUpsertData.parentJid)) { archiveUpsertForm.reject(Messages.get("error.archive.notExist")); return showCreateArchive(0, archiveUpsertForm); } long archiveId = archiveService.createArchive( archiveUpsertData.parentJid, archiveUpsertData.name, archiveUpsertData.description, IdentityUtils.getUserJid(), IdentityUtils.getIpAddress()); return redirect(routes.ArchiveController.viewArchives(archiveId)); }
@SuppressWarnings("unchecked") private O createArchiveOutputStream(boolean overwrite) throws IOException { O archiveOut; FileOutputStream out; if (!file.exists() || file.length() == 0 || overwrite) { out = new FileOutputStream(file); } else { throw new FileNotFoundException( "can't open for output, check existence or access rights: " + file.getAbsolutePath()); } String pathStr = path.toString(); Set<String> streamCodecs = CompressCodecService.getCodecs(); for (String codec : streamCodecs) { if (pathStr.endsWith("." + codec)) { archiveOut = (O) archiveService .getCodec(getName()) .createArchiveOutputStream(codecService.getCodec(codec).encode(out)); archiveOut.setWatcher(watcher); return archiveOut; } } archiveOut = (O) archiveService.getCodec(getName()).createArchiveOutputStream(out); archiveOut.setWatcher(watcher); return archiveOut; }
@SuppressWarnings("unchecked") private I createArchiveInputStream() throws IOException { I archiveIn; FileInputStream in; if (file.isFile() && file.canRead()) { in = new FileInputStream(file); } else { throw new FileNotFoundException( "can't open for input, check existence or access rights: " + path); } String pathStr = path.toString(); Set<String> streamCodecs = CompressCodecService.getCodecs(); for (String codec : streamCodecs) { if (pathStr.endsWith("." + codec)) { archiveIn = (I) archiveService .getCodec(getName()) .createArchiveInputStream(codecService.getCodec(codec).decode(in)); archiveIn.setWatcher(watcher); return archiveIn; } } archiveIn = (I) archiveService.getCodec(getName()).createArchiveInputStream(in); archiveIn.setWatcher(watcher); return archiveIn; }
private Result showCreateArchive( long parentArchiveId, Form<ArchiveUpsertForm> archiveUpsertForm) { LazyHtml content = new LazyHtml(createArchiveView.render(archiveUpsertForm, archiveService.getAllArchives())); content.appendLayout(c -> headingLayout.render(Messages.get("archive.create"), c)); JerahmeelControllerUtils.getInstance().appendSidebarLayout(content); ArchiveControllerUtils.appendBreadcrumbsLayout( content, new InternalLink( Messages.get("archive.create"), routes.ArchiveController.createArchive(parentArchiveId))); JerahmeelControllerUtils.getInstance().appendTemplateLayout(content, "Archive - Create"); return JerahmeelControllerUtils.getInstance().lazyOk(content); }
@Authenticated(value = {LoggedIn.class, HasRole.class}) @Authorized(value = "admin") @Transactional(readOnly = true) @AddCSRFToken public Result createArchive(long parentArchiveId) throws ArchiveNotFoundException { ArchiveUpsertForm archiveUpsertData = new ArchiveUpsertForm(); if (parentArchiveId != 0) { Archive archive = archiveService.findArchiveById(parentArchiveId); archiveUpsertData.parentJid = archive.getJid(); } Form<ArchiveUpsertForm> archiveUpsertForm = Form.form(ArchiveUpsertForm.class).fill(archiveUpsertData); return showCreateArchive(parentArchiveId, archiveUpsertForm); }
@Authenticated(value = {LoggedIn.class, HasRole.class}) @Authorized(value = "admin") @Transactional @RequireCSRFCheck public Result postEditArchive(long archiveId) throws ArchiveNotFoundException { Archive archive = archiveService.findArchiveById(archiveId); Form<ArchiveUpsertForm> archiveUpsertForm = Form.form(ArchiveUpsertForm.class).bindFromRequest(); if (formHasErrors(archiveUpsertForm)) { return showEditArchive(archiveUpsertForm, archive); } ArchiveUpsertForm archiveUpsertData = archiveUpsertForm.get(); archiveService.updateArchive( archive.getJid(), archiveUpsertData.parentJid, archiveUpsertData.name, archiveUpsertData.description, IdentityUtils.getUserJid(), IdentityUtils.getIpAddress()); return redirect(routes.ArchiveController.viewArchives(archive.getId())); }
@Authenticated(value = {LoggedIn.class, HasRole.class}) @Authorized(value = "admin") @Transactional(readOnly = true) @AddCSRFToken public Result editArchive(long archiveId) throws ArchiveNotFoundException { Archive archive = archiveService.findArchiveById(archiveId); ArchiveUpsertForm archiveUpsertData = new ArchiveUpsertForm(); if (archive.getParentArchive() != null) { archiveUpsertData.parentJid = archive.getParentArchive().getJid(); } archiveUpsertData.name = archive.getName(); archiveUpsertData.description = archive.getDescription(); Form<ArchiveUpsertForm> archiveUpsertForm = Form.form(ArchiveUpsertForm.class).fill(archiveUpsertData); return showEditArchive(archiveUpsertForm, archive); }
private Result showEditArchive(Form<ArchiveUpsertForm> archiveUpsertForm, Archive archive) { LazyHtml content = new LazyHtml( editArchiveView.render( archiveUpsertForm, archive.getId(), archiveService .getAllArchives() .stream() .filter(f -> !f.containsJidInHierarchy(archive.getJid())) .collect(Collectors.toList()))); ArchiveControllerUtils.appendUpdateLayout(content, archive); JerahmeelControllerUtils.getInstance().appendSidebarLayout(content); ArchiveControllerUtils.appendBreadcrumbsLayout( content, new InternalLink( Messages.get("archive.edit"), routes.ArchiveController.editArchive(archive.getId()))); JerahmeelControllerUtils.getInstance().appendTemplateLayout(content, "Archive - Edit"); return JerahmeelControllerUtils.getInstance().lazyOk(content); }
private Result showListArchivesProblemSets( long archiveId, long pageIndex, String orderBy, String orderDir, String filterString) throws ArchiveNotFoundException { Archive archive = archiveService.findArchiveById(archiveId); Archive parentArchive = archive.getParentArchive(); LazyHtml content; if (!JerahmeelUtils.isGuest()) { List<ArchiveWithScore> childArchivesWithScore = archiveService.getChildArchivesWithScore(archive.getJid(), IdentityUtils.getUserJid()); Page<ProblemSetWithScore> pageOfProblemSetsWithScore = problemSetService.getPageOfProblemSetsWithScore( archive, IdentityUtils.getUserJid(), pageIndex, PAGE_SIZE, orderBy, orderDir, filterString); content = new LazyHtml( listArchivesAndProblemSetsWithScoreView.render( archive, childArchivesWithScore, pageOfProblemSetsWithScore, orderBy, orderDir, filterString)); } else { List<Archive> childArchives = archiveService.getChildArchives(archive.getJid()); Page<ProblemSet> pageOfProblemSets = problemSetService.getPageOfProblemSets( archive, pageIndex, PAGE_SIZE, orderBy, orderDir, filterString); content = new LazyHtml( listArchivesAndProblemSetsView.render( archive, childArchives, pageOfProblemSets, orderBy, orderDir, filterString)); } if (!archive.getDescription().isEmpty()) { content.appendLayout(c -> descriptionHtmlLayout.render(archive.getDescription(), c)); } final String parentArchiveName; final Call backCall; if (parentArchive == null) { parentArchiveName = Messages.get("training.home"); backCall = org.iatoki.judgels.jerahmeel.training.routes.TrainingController.index(); } else { parentArchiveName = parentArchive.getName(); backCall = routes.ArchiveController.viewArchives(parentArchive.getId()); } if (JerahmeelUtils.hasRole("admin")) { ImmutableList.Builder<InternalLink> actionsBuilder = ImmutableList.builder(); actionsBuilder.add( new InternalLink( Messages.get("commons.button.edit"), routes.ArchiveController.editArchive(archiveId))); actionsBuilder.add( new InternalLink( Messages.get("archive.create"), routes.ArchiveController.createArchive(archiveId))); actionsBuilder.add( new InternalLink( Messages.get("archive.problemSet.create"), org.iatoki.judgels.jerahmeel.problemset.routes.ProblemSetController.createProblemSet( archive.getId()))); content.appendLayout( c -> headingWithActionsAndBackLayout.render( Messages.get("archive.archive") + " " + archive.getName(), actionsBuilder.build(), new InternalLink( Messages.get("archive.backTo") + " " + parentArchiveName, backCall), c)); } else { content.appendLayout( c -> headingWithBackLayout.render( archive.getName(), new InternalLink( Messages.get("archive.backTo") + " " + parentArchiveName, backCall), c)); } JerahmeelControllerUtils.getInstance().appendSidebarLayout(content); ImmutableList.Builder<InternalLink> breadcrumbsBuilder = ImmutableList.builder(); ArchiveControllerUtils.fillBreadcrumbsBuilder(breadcrumbsBuilder, archive); ArchiveControllerUtils.appendBreadcrumbsLayout(content, breadcrumbsBuilder.build()); JerahmeelControllerUtils.getInstance().appendTemplateLayout(content, "Archives"); return JerahmeelControllerUtils.getInstance().lazyOk(content); }
/** A basic archive session */ public abstract class ArchiveSession<I extends ArchiveInputStream, O extends ArchiveOutputStream> implements Session<ArchivePacket> { private static final ESLogger logger = ESLoggerFactory.getLogger(ArchiveSession.class.getSimpleName()); private static final CompressCodecService codecService = CompressCodecService.getInstance(); private static final ArchiveService archiveService = ArchiveService.getInstance(); private boolean isOpen; private EnumSet<Mode> mode; private File file; private Path path; private I in; private O out; private BytesProgressWatcher watcher; private long packetCounter; private AtomicLong archiveCounter = new AtomicLong(); protected ArchiveSession(BytesProgressWatcher watcher) { this.watcher = watcher; this.packetCounter = 0L; } public BytesProgressWatcher getWatcher() { if (watcher == null) { watcher = new BytesProgressWatcher(0L); } return watcher; } public abstract String getName(); @Override public synchronized void open(EnumSet<Mode> mode, Path path, File file) throws IOException { if (isOpen) { return; } this.mode = mode; this.file = file; this.path = path; if (mode.contains(Mode.READ)) { this.in = createArchiveInputStream(); this.isOpen = this.in != null; if (!isOpen) { throw new FileNotFoundException( "can't open for input, check existence or access rights: " + file.getAbsolutePath()); } } else if (mode.contains(Mode.WRITE)) { this.out = createArchiveOutputStream(false); this.isOpen = this.out != null; if (!isOpen) { throw new FileNotFoundException( "can't open for output, check existence or access rights: " + file.getAbsolutePath()); } } else if (mode.contains(Mode.OVERWRITE)) { this.out = createArchiveOutputStream(true); this.isOpen = this.out != null; if (!isOpen) { throw new FileNotFoundException( "can't open for output, check existence or access rights: " + file.getAbsolutePath()); } } } @SuppressWarnings("unchecked") private I createArchiveInputStream() throws IOException { I archiveIn; FileInputStream in; if (file.isFile() && file.canRead()) { in = new FileInputStream(file); } else { throw new FileNotFoundException( "can't open for input, check existence or access rights: " + path); } String pathStr = path.toString(); Set<String> streamCodecs = CompressCodecService.getCodecs(); for (String codec : streamCodecs) { if (pathStr.endsWith("." + codec)) { archiveIn = (I) archiveService .getCodec(getName()) .createArchiveInputStream(codecService.getCodec(codec).decode(in)); archiveIn.setWatcher(watcher); return archiveIn; } } archiveIn = (I) archiveService.getCodec(getName()).createArchiveInputStream(in); archiveIn.setWatcher(watcher); return archiveIn; } @SuppressWarnings("unchecked") private O createArchiveOutputStream(boolean overwrite) throws IOException { O archiveOut; FileOutputStream out; if (!file.exists() || file.length() == 0 || overwrite) { out = new FileOutputStream(file); } else { throw new FileNotFoundException( "can't open for output, check existence or access rights: " + file.getAbsolutePath()); } String pathStr = path.toString(); Set<String> streamCodecs = CompressCodecService.getCodecs(); for (String codec : streamCodecs) { if (pathStr.endsWith("." + codec)) { archiveOut = (O) archiveService .getCodec(getName()) .createArchiveOutputStream(codecService.getCodec(codec).encode(out)); archiveOut.setWatcher(watcher); return archiveOut; } } archiveOut = (O) archiveService.getCodec(getName()).createArchiveOutputStream(out); archiveOut.setWatcher(watcher); return archiveOut; } @Override public ArchivePacket newPacket() { return new ArchivePacket(); } @Override public synchronized ArchivePacket read() throws IOException { if (!isOpen()) { throw new IOException("not open"); } if (in == null) { throw new IOException("no input stream found"); } ArchiveEntry entry = in.getNextEntry(); if (entry == null) { return null; } ArchivePacket packet = newPacket(); String name = entry.getName(); packet.meta("name", name); ArchiveUtils.decodeArchiveEntryName(packet, name); int size = (int) entry.getEntrySize(); if (size >= 0) { byte[] b = new byte[size]; // naive but fast, heap may explode int num = in.read(b, 0, size); // fill byte array from stream packet.payload(new String(b)); } else { // slow copy, unknown size (zip deflate method) ByteArrayOutputStream b = new ByteArrayOutputStream(); Streams.copy(in, b); packet.payload(new String(b.toByteArray())); } packetCounter++; return packet; } @Override @SuppressWarnings("unchecked") public synchronized void write(ArchivePacket packet) throws IOException { if (!isOpen()) { throw new IOException("not open"); } if (out == null) { throw new IOException("no output stream found"); } if (packet == null || packet.payload() == null) { throw new IOException("no payload to write for entry"); } byte[] buf = packet.payload().toString().getBytes(); String name = ArchiveUtils.encodeArchiveEntryName(packet); ArchiveEntry entry = out.newArchiveEntry(); entry.setName(name); entry.setLastModified(new Date()); entry.setEntrySize(buf.length); out.putArchiveEntry(entry); out.write(buf); out.closeArchiveEntry(); packetCounter++; if (watcher.getBytesToTransfer() != 0 && watcher.getBytesTransferred() > watcher.getBytesToTransfer()) { logger.debug( "bytes watcher: transferred = {}, rate {}", watcher.getBytesTransferred(), watcher.getRecentByteRatePerSecond()); switchToNextArchive(); watcher.resetWatcher(); } } @Override public synchronized void close() throws IOException { if (!isOpen) { return; } if (out != null) { out.close(); } if (in != null) { in.close(); } this.isOpen = false; } @Override public long getPacketCounter() { return packetCounter; } @Override public boolean isOpen() { return isOpen; } /** * Switches to next archive stream if a certain byte limit was set * * @throws IOException */ private void switchToNextArchive() throws IOException { close(); String filename = file.getName(); String prefix = Long.toString(archiveCounter.get()) + "."; if (filename.startsWith(prefix)) { filename = filename.substring(prefix.length()); } filename = archiveCounter.incrementAndGet() + "." + filename; this.file = new File(file.getParent() + File.separator + filename); this.path = file.toPath(); open(mode, path, file); } }