Example usage for org.springframework.util Assert hasLength

List of usage examples for org.springframework.util Assert hasLength

Introduction

In this page you can find the example usage for org.springframework.util Assert hasLength.

Prototype

@Deprecated
public static void hasLength(@Nullable String text) 

Source Link

Document

Assert that the given String is not empty; that is, it must not be null and not the empty String.

Usage

From source file:de.blizzy.documentr.page.PageStore.java

@Override
public List<String> listChildPagePaths(String projectName, String branchName, final String path)
        throws IOException {
    Assert.hasLength(projectName);
    Assert.hasLength(branchName);//from   www. j  av  a 2s .  c  o  m
    Assert.hasLength(path);

    ILockedRepository repo = null;
    try {
        repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName);
        File workingDir = RepositoryUtil.getWorkingDir(repo.r());
        File pagesDir = Util.toFile(new File(workingDir, DocumentrConstants.PAGES_DIR_NAME), path);
        List<String> paths = Lists.newArrayList(listPagePaths(pagesDir, false));
        Function<String, String> function = new Function<String, String>() {
            @Override
            public String apply(String childName) {
                return path + "/" + childName; //$NON-NLS-1$
            }
        };
        paths = Lists.transform(paths, function);
        return paths;
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }
}

From source file:de.blizzy.documentr.page.PageStore.java

@Override
public void deletePage(String projectName, String branchName, final String path, User user) throws IOException {
    Assert.hasLength(projectName);
    Assert.hasLength(branchName);//  w  ww. j  a  v  a2 s . com
    Assert.hasLength(path);
    Assert.notNull(user);

    ILockedRepository repo = null;
    List<String> oldPagePaths;
    boolean deleted = false;
    try {
        repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName);
        File workingDir = RepositoryUtil.getWorkingDir(repo.r());

        File pagesDir = new File(workingDir, DocumentrConstants.PAGES_DIR_NAME);

        File oldSubPagesDir = Util.toFile(pagesDir, path);
        oldPagePaths = listPagePaths(oldSubPagesDir, true);
        oldPagePaths = Lists.newArrayList(Lists.transform(oldPagePaths, new Function<String, String>() {
            @Override
            public String apply(String p) {
                return path + "/" + p; //$NON-NLS-1$
            }
        }));
        oldPagePaths.add(path);

        Git git = Git.wrap(repo.r());

        File file = Util.toFile(pagesDir, path + DocumentrConstants.PAGE_SUFFIX);
        if (file.isFile()) {
            git.rm().addFilepattern(
                    DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX) //$NON-NLS-1$
                    .call();
            deleted = true;
        }
        file = Util.toFile(pagesDir, path + DocumentrConstants.META_SUFFIX);
        if (file.isFile()) {
            git.rm().addFilepattern(
                    DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.META_SUFFIX) //$NON-NLS-1$
                    .call();
            deleted = true;
        }
        file = Util.toFile(pagesDir, path);
        if (file.isDirectory()) {
            git.rm().addFilepattern(DocumentrConstants.PAGES_DIR_NAME + "/" + path) //$NON-NLS-1$
                    .call();
            deleted = true;
        }

        File attachmentsDir = new File(workingDir, DocumentrConstants.ATTACHMENTS_DIR_NAME);
        file = Util.toFile(attachmentsDir, path);
        if (file.isDirectory()) {
            git.rm().addFilepattern(DocumentrConstants.ATTACHMENTS_DIR_NAME + "/" + path) //$NON-NLS-1$
                    .call();
            deleted = true;
        }

        if (deleted) {
            PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail());
            git.commit().setAuthor(ident).setCommitter(ident).setMessage("delete " + path) //$NON-NLS-1$
                    .call();
            git.push().call();

            PageUtil.updateProjectEditTime(projectName);
        }
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }

    if (deleted) {
        eventBus.post(new PagesDeletedEvent(projectName, branchName, Sets.newHashSet(oldPagePaths)));
    }
}

From source file:de.blizzy.documentr.page.PageStore.java

@Override
public PageMetadata getPageMetadata(String projectName, String branchName, String path) throws IOException {
    Assert.hasLength(projectName);
    Assert.hasLength(branchName);/*  www. j a v  a 2  s  .c om*/
    Assert.hasLength(path);

    return getPageMetadataInternal(projectName, branchName, path, DocumentrConstants.PAGES_DIR_NAME);
}

From source file:de.blizzy.documentr.page.PageStore.java

@Override
public PageMetadata getAttachmentMetadata(String projectName, String branchName, String path, String name)
        throws IOException {

    Assert.hasLength(projectName);
    Assert.hasLength(branchName);// ww  w.j a v a  2s  . c om
    Assert.hasLength(path);
    Assert.hasLength(name);

    return getPageMetadataInternal(projectName, branchName, path + "/" + name, //$NON-NLS-1$
            DocumentrConstants.ATTACHMENTS_DIR_NAME);
}

From source file:de.blizzy.documentr.page.PageStore.java

@Override
public void relocatePage(final String projectName, final String branchName, final String path,
        String newParentPagePath, User user) throws IOException {

    Assert.hasLength(projectName);
    Assert.hasLength(branchName);//  w w  w. j a  v  a 2s.  c  o  m
    Assert.hasLength(path);
    Assert.hasLength(newParentPagePath);
    Assert.notNull(user);
    // check if pages exist by trying to load them
    getPage(projectName, branchName, path, false);
    getPage(projectName, branchName, newParentPagePath, false);

    ILockedRepository repo = null;
    List<String> oldPagePaths;
    List<String> deletedPagePaths;
    List<String> newPagePaths;
    List<PagePathChangedEvent> pagePathChangedEvents;
    try {
        repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName);
        String pageName = path.contains("/") ? StringUtils.substringAfterLast(path, "/") : path; //$NON-NLS-1$ //$NON-NLS-2$
        final String newPagePath = newParentPagePath + "/" + pageName; //$NON-NLS-1$

        File workingDir = RepositoryUtil.getWorkingDir(repo.r());

        File oldSubPagesDir = Util.toFile(new File(workingDir, DocumentrConstants.PAGES_DIR_NAME), path);
        oldPagePaths = listPagePaths(oldSubPagesDir, true);
        oldPagePaths = Lists.newArrayList(Lists.transform(oldPagePaths, new Function<String, String>() {
            @Override
            public String apply(String p) {
                return path + "/" + p; //$NON-NLS-1$
            }
        }));
        oldPagePaths.add(path);

        File deletedPagesSubDir = Util.toFile(new File(workingDir, DocumentrConstants.PAGES_DIR_NAME),
                newPagePath);
        deletedPagePaths = listPagePaths(deletedPagesSubDir, true);
        deletedPagePaths = Lists.newArrayList(Lists.transform(deletedPagePaths, new Function<String, String>() {
            @Override
            public String apply(String p) {
                return newPagePath + "/" + p; //$NON-NLS-1$
            }
        }));
        deletedPagePaths.add(newPagePath);

        Git git = Git.wrap(repo.r());
        AddCommand addCommand = git.add();

        for (String dirName : Sets.newHashSet(DocumentrConstants.PAGES_DIR_NAME,
                DocumentrConstants.ATTACHMENTS_DIR_NAME)) {
            File dir = new File(workingDir, dirName);

            File newSubPagesDir = Util.toFile(dir, newPagePath);
            if (newSubPagesDir.exists()) {
                git.rm().addFilepattern(dirName + "/" + newPagePath).call(); //$NON-NLS-1$
            }
            File newPageFile = Util.toFile(dir, newPagePath + DocumentrConstants.PAGE_SUFFIX);
            if (newPageFile.exists()) {
                git.rm().addFilepattern(dirName + "/" + newPagePath + DocumentrConstants.PAGE_SUFFIX).call(); //$NON-NLS-1$
            }
            File newMetaFile = Util.toFile(dir, newPagePath + DocumentrConstants.META_SUFFIX);
            if (newMetaFile.exists()) {
                git.rm().addFilepattern(dirName + "/" + newPagePath + DocumentrConstants.META_SUFFIX).call(); //$NON-NLS-1$
            }

            File newParentPageDir = Util.toFile(dir, newParentPagePath);
            File subPagesDir = Util.toFile(dir, path);
            if (subPagesDir.exists()) {
                FileUtils.copyDirectoryToDirectory(subPagesDir, newParentPageDir);
                git.rm().addFilepattern(dirName + "/" + path).call(); //$NON-NLS-1$
                addCommand.addFilepattern(dirName + "/" + newParentPagePath + "/" + pageName); //$NON-NLS-1$ //$NON-NLS-2$
            }
            File pageFile = Util.toFile(dir, path + DocumentrConstants.PAGE_SUFFIX);
            if (pageFile.exists()) {
                FileUtils.copyFileToDirectory(pageFile, newParentPageDir);
                git.rm().addFilepattern(dirName + "/" + path + DocumentrConstants.PAGE_SUFFIX).call(); //$NON-NLS-1$
                addCommand.addFilepattern(
                        dirName + "/" + newParentPagePath + "/" + pageName + DocumentrConstants.PAGE_SUFFIX); //$NON-NLS-1$ //$NON-NLS-2$
            }
            File metaFile = Util.toFile(dir, path + DocumentrConstants.META_SUFFIX);
            if (metaFile.exists()) {
                FileUtils.copyFileToDirectory(metaFile, newParentPageDir);
                git.rm().addFilepattern(dirName + "/" + path + DocumentrConstants.META_SUFFIX).call(); //$NON-NLS-1$
                addCommand.addFilepattern(
                        dirName + "/" + newParentPagePath + "/" + pageName + DocumentrConstants.META_SUFFIX); //$NON-NLS-1$ //$NON-NLS-2$
            }
        }

        addCommand.call();
        PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail());
        git.commit().setAuthor(ident).setCommitter(ident)
                .setMessage("move " + path + " to " + newParentPagePath + "/" + pageName) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                .call();
        git.push().call();

        newPagePaths = Lists.transform(oldPagePaths, new Function<String, String>() {
            @Override
            public String apply(String p) {
                return newPagePath + StringUtils.removeStart(p, path);
            }
        });

        pagePathChangedEvents = Lists.transform(oldPagePaths, new Function<String, PagePathChangedEvent>() {
            @Override
            public PagePathChangedEvent apply(String oldPath) {
                String newPath = newPagePath + StringUtils.removeStart(oldPath, path);
                return new PagePathChangedEvent(projectName, branchName, oldPath, newPath);
            }
        });

        PageUtil.updateProjectEditTime(projectName);
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }

    Set<String> allDeletedPagePaths = Sets.newHashSet(oldPagePaths);
    allDeletedPagePaths.addAll(deletedPagePaths);
    eventBus.post(new PagesDeletedEvent(projectName, branchName, allDeletedPagePaths));

    for (String newPath : newPagePaths) {
        eventBus.post(new PageChangedEvent(projectName, branchName, newPath));
    }

    for (PagePathChangedEvent event : pagePathChangedEvents) {
        eventBus.post(event);
    }
}

From source file:de.blizzy.documentr.page.PageStore.java

@Override
public Map<String, String> getMarkdown(String projectName, String branchName, String path, Set<String> versions)
        throws IOException {

    Assert.hasLength(projectName);
    Assert.hasLength(branchName);//from w  w w  . j a  va  2s. c om
    Assert.hasLength(path);
    // check if page exists by trying to load it
    getPage(projectName, branchName, path, false);
    Assert.notEmpty(versions);

    Map<String, String> result = Maps.newHashMap();
    ILockedRepository repo = null;
    try {
        repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName);
        String filePath = DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX; //$NON-NLS-1$

        Set<String> realVersions = Sets.newHashSet();
        for (String version : versions) {
            if (version.equals(VERSION_LATEST)) {
                RevCommit latestCommit = CommitUtils.getLastCommit(repo.r(), filePath);
                String commitId = latestCommit.getName();
                result.put(VERSION_LATEST, commitId);
                realVersions.add(commitId);
            } else if (version.equals(VERSION_PREVIOUS)) {
                RevCommit latestCommit = CommitUtils.getLastCommit(repo.r(), filePath);
                if (latestCommit.getParentCount() > 0) {
                    RevCommit parentCommit = latestCommit.getParent(0);
                    RevCommit previousCommit = CommitUtils.getLastCommit(repo.r(), parentCommit.getName(),
                            filePath);
                    if (previousCommit != null) {
                        String commitId = previousCommit.getName();
                        result.put(VERSION_PREVIOUS, commitId);
                        realVersions.add(commitId);
                    }
                }
            } else {
                realVersions.add(version);
            }
        }

        for (String version : realVersions) {
            String markdown = BlobUtils.getContent(repo.r(), version, filePath);
            if (markdown != null) {
                result.put(version, markdown);
            }
        }
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }
    return result;
}

From source file:de.blizzy.documentr.page.PageStore.java

@Override
public List<PageVersion> listPageVersions(String projectName, String branchName, String path)
        throws IOException {
    Assert.hasLength(projectName);
    Assert.hasLength(branchName);//  w w w .j av  a2 s.c  o  m
    Assert.hasLength(path);
    // check if page exists by trying to load it
    getPage(projectName, branchName, path, false);

    ILockedRepository repo = null;
    List<PageVersion> result = Lists.newArrayList();
    try {
        repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName);

        CommitFinder finder = new CommitFinder(repo.r());
        TreeFilter pathFilter = PathFilterUtils
                .or(DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX); //$NON-NLS-1$
        finder.setFilter(pathFilter);
        CommitListFilter commits = new CommitListFilter();
        finder.setMatcher(new AndCommitFilter(new CommitLimitFilter(50), commits));
        finder.find();

        for (RevCommit commit : commits.getCommits()) {
            result.add(PageUtil.toPageVersion(commit));
        }
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }
    return result;
}

From source file:de.blizzy.documentr.page.PageStore.java

@Override
public void deleteAttachment(String projectName, String branchName, String pagePath, String name, User user)
        throws IOException {

    Assert.hasLength(projectName);
    Assert.hasLength(branchName);//from   ww w . ja v  a  2s .c o  m
    Assert.hasLength(pagePath);
    Assert.hasLength(name);
    Assert.notNull(user);

    ILockedRepository repo = null;
    try {
        repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName);
        File workingDir = RepositoryUtil.getWorkingDir(repo.r());

        Git git = Git.wrap(repo.r());

        File attachmentsDir = new File(workingDir, DocumentrConstants.ATTACHMENTS_DIR_NAME);
        boolean deleted = false;
        File file = Util.toFile(attachmentsDir, pagePath + "/" + name + DocumentrConstants.PAGE_SUFFIX); //$NON-NLS-1$
        if (file.isFile()) {
            FileUtils.forceDelete(file);
            git.rm().addFilepattern(DocumentrConstants.ATTACHMENTS_DIR_NAME + "/" + //$NON-NLS-1$
                    pagePath + "/" + name + DocumentrConstants.PAGE_SUFFIX) //$NON-NLS-1$
                    .call();
            deleted = true;
        }
        file = Util.toFile(attachmentsDir, pagePath + "/" + name + DocumentrConstants.META_SUFFIX); //$NON-NLS-1$
        if (file.isFile()) {
            FileUtils.forceDelete(file);
            git.rm().addFilepattern(DocumentrConstants.ATTACHMENTS_DIR_NAME + "/" + //$NON-NLS-1$
                    pagePath + "/" + name + DocumentrConstants.META_SUFFIX) //$NON-NLS-1$
                    .call();
            deleted = true;
        }

        if (deleted) {
            PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail());
            git.commit().setAuthor(ident).setCommitter(ident).setMessage("delete " + pagePath + "/" + name) //$NON-NLS-1$ //$NON-NLS-2$
                    .call();
            git.push().call();

            PageUtil.updateProjectEditTime(projectName);
        }
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }
}

From source file:de.blizzy.documentr.page.PageStore.java

@Override
public void restorePageVersion(String projectName, String branchName, String path, String version, User user)
        throws IOException {

    Assert.hasLength(projectName);
    Assert.hasLength(branchName);//  w  w w.  j  a  v a  2s . c o m
    Assert.hasLength(path);
    Assert.hasLength(version);

    ILockedRepository repo = null;
    try {
        repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName);
        String text = BlobUtils.getContent(repo.r(), version,
                DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX); //$NON-NLS-1$
        File workingDir = RepositoryUtil.getWorkingDir(repo.r());
        File pagesDir = new File(workingDir, DocumentrConstants.PAGES_DIR_NAME);
        File file = Util.toFile(pagesDir, path + DocumentrConstants.PAGE_SUFFIX);
        FileUtils.writeStringToFile(file, text, Charsets.UTF_8.name());

        Git git = Git.wrap(repo.r());
        git.add()
                .addFilepattern(DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX) //$NON-NLS-1$
                .call();
        PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail());
        git.commit().setAuthor(ident).setCommitter(ident)
                .setMessage(DocumentrConstants.PAGES_DIR_NAME + "/" + path) //$NON-NLS-1$
                .call();
        git.push().call();
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }

    eventBus.post(new PageChangedEvent(projectName, branchName, path));
}

From source file:de.blizzy.documentr.repository.ProjectRepositoryManager.java

ILockedRepository createBranchRepository(String branchName, String startingBranch)
        throws IOException, GitAPIException {
    Assert.hasLength(branchName);
    if (startingBranch != null) {
        Assert.hasLength(startingBranch);
    }// w  w w . j a v  a 2 s  .co  m

    File repoDir = new File(reposDir, branchName);
    if (repoDir.isDirectory()) {
        throw new IllegalStateException("repository already exists: " + repoDir.getAbsolutePath()); //$NON-NLS-1$
    }

    List<String> branches = listBranches();
    if (branches.contains(branchName)) {
        throw new IllegalArgumentException("branch already exists: " + branchName); //$NON-NLS-1$
    }

    if ((startingBranch == null) && !branches.isEmpty()) {
        throw new IllegalArgumentException("must specify a starting branch"); //$NON-NLS-1$
    }

    ILock lock = lockManager.lockAll();
    try {
        Repository centralRepo = null;
        File centralRepoGitDir;
        try {
            centralRepo = getCentralRepositoryInternal(true);
            centralRepoGitDir = centralRepo.getDirectory();
        } finally {
            RepositoryUtil.closeQuietly(centralRepo);
            centralRepo = null;
        }

        Repository repo = null;
        try {
            repo = Git.cloneRepository().setURI(centralRepoGitDir.toURI().toString()).setDirectory(repoDir)
                    .call().getRepository();

            try {
                centralRepo = getCentralRepositoryInternal(true);
                if (!RepositoryUtils.getBranches(centralRepo).contains(branchName)) {
                    CreateBranchCommand createBranchCommand = Git.wrap(centralRepo).branchCreate();
                    if (startingBranch != null) {
                        createBranchCommand.setStartPoint(startingBranch);
                    }
                    createBranchCommand.setName(branchName).call();
                }
            } finally {
                RepositoryUtil.closeQuietly(centralRepo);
            }

            Git git = Git.wrap(repo);
            RefSpec refSpec = new RefSpec("refs/heads/" + branchName + ":refs/remotes/origin/" + branchName); //$NON-NLS-1$ //$NON-NLS-2$
            git.fetch().setRemote("origin").setRefSpecs(refSpec).call(); //$NON-NLS-1$
            git.branchCreate().setName(branchName).setStartPoint("origin/" + branchName).call(); //$NON-NLS-1$
            git.checkout().setName(branchName).call();
        } finally {
            RepositoryUtil.closeQuietly(repo);
        }
    } finally {
        lockManager.unlock(lock);
    }

    return getBranchRepository(branchName);
}