private ObjectNode fileAsJson(String path, org.tmatesoft.svn.core.io.SVNRepository repository) throws SVNException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); SVNProperties prop = new SVNProperties(); repository.getFile(path, -1l, prop, baos); long size = repository.info(path, -1l).getSize(); boolean isBinary; String mimeType; String data = null; if (size > MAX_FILE_SIZE_CAN_BE_VIEWED) { isBinary = true; mimeType = "application/octet-stream"; } else { byte[] bytes = baos.toByteArray(); isBinary = FileUtil.isBinary(bytes); if (!isBinary) { data = new String(bytes); } mimeType = new Tika().detect(bytes, path); } String author = prop.getStringValue(SVNProperty.LAST_AUTHOR); User user = User.findByLoginId(author); ObjectNode result = Json.newObject(); result.put("type", "file"); result.put("revisionNo", prop.getStringValue(SVNProperty.COMMITTED_REVISION)); result.put("author", author); result.put("avatar", getAvatar(user)); result.put("userName", user.name); result.put("userLoginId", user.loginId); result.put("createdDate", prop.getStringValue(SVNProperty.COMMITTED_DATE)); result.put("size", size); result.put("isBinary", isBinary); result.put("mimeType", mimeType); result.put("data", data); return result; }
private void doList( SVNRepository repos, long rev, final ISVNDirEntryHandler handler, boolean fetchLocks, SVNDepth depth, int entryFields, SVNURL externalParentUrl, String externalTarget) throws SVNException { boolean includeExternals = !getOperation().isIgnoreExternals(); Map<SVNURL, SVNPropertyValue> externals = includeExternals ? new HashMap<SVNURL, SVNPropertyValue>() : null; SVNURL url = repos.getLocation(); SVNURL reposRoot = repos.getRepositoryRoot(false); SVNDirEntry entry = null; SVNException error = null; try { entry = repos.info("", rev); } catch (SVNException svne) { if (svne.getErrorMessage().getErrorCode() == SVNErrorCode.RA_NOT_IMPLEMENTED) { error = svne; } else { throw svne; } } if (error != null) { SVNNodeKind kind = repos.checkPath("", rev); if (kind != SVNNodeKind.NONE) { if (!url.equals(reposRoot)) { String name = SVNPathUtil.tail(repos.getLocation().getPath()); repos.setLocation(repos.getLocation().removePathTail(), false); Collection<SVNDirEntry> dirEntries = repos.getDir("", rev, null, entryFields, (Collection) null); repos.setLocation(url, false); for (Iterator<SVNDirEntry> ents = dirEntries.iterator(); ents.hasNext(); ) { SVNDirEntry dirEntry = (SVNDirEntry) ents.next(); if (name.equals(dirEntry.getName())) { entry = dirEntry; break; } } if (entry != null) { entry.setRelativePath(kind == SVNNodeKind.FILE ? name : ""); } } else { SVNProperties props = new SVNProperties(); repos.getDir("", rev, props, entryFields, (Collection<SVNDirEntry>) null); SVNProperties revProps = repos.getRevisionProperties(rev, null); String author = revProps.getStringValue(SVNRevisionProperty.AUTHOR); String dateStr = revProps.getStringValue(SVNRevisionProperty.DATE); Date datestamp = null; if (dateStr != null) { datestamp = SVNDate.parseDateString(dateStr); } entry = new SVNDirEntry( url, reposRoot, "", kind, 0, !props.isEmpty(), rev, datestamp, author); entry.setRelativePath(""); } } } else if (entry != null) { if (entry.getKind() == SVNNodeKind.DIR) { entry.setName(""); } entry.setRelativePath(entry.getKind() == SVNNodeKind.DIR ? "" : entry.getName()); } if (entry == null) { SVNErrorMessage err = SVNErrorMessage.create( SVNErrorCode.FS_NOT_FOUND, "URL ''{0}'' non-existent in that revision", url); SVNErrorManager.error(err, SVNLogType.WC); } final Map locksMap = new SVNHashMap(); if (fetchLocks) { SVNLock[] locks = new SVNLock[0]; try { locks = repos.getLocks(""); } catch (SVNException e) { if (!(e.getErrorMessage() != null && e.getErrorMessage().getErrorCode() == SVNErrorCode.RA_NOT_IMPLEMENTED)) { throw e; } } if (locks != null && locks.length > 0) { SVNURL root = repos.getRepositoryRoot(true); for (int i = 0; i < locks.length; i++) { String repositoryPath = locks[i].getPath(); locksMap.put(root.appendPath(repositoryPath, false), locks[i]); } } } ISVNDirEntryHandler nestedHandler = new ISVNDirEntryHandler() { public void handleDirEntry(SVNDirEntry dirEntry) throws SVNException { dirEntry.setLock((SVNLock) locksMap.get(dirEntry.getURL())); handler.handleDirEntry(dirEntry); } }; entry.setExternalParentUrl(externalParentUrl); entry.setExternalTarget(externalTarget); nestedHandler.handleDirEntry(entry); if (entry.getKind() == SVNNodeKind.DIR && (depth == SVNDepth.FILES || depth == SVNDepth.IMMEDIATES || depth == SVNDepth.INFINITY)) { list( repos, "", rev, depth, entryFields, externals, externalParentUrl, externalTarget, nestedHandler); } if (includeExternals && externals != null && externals.size() > 0) { listExternals(repos, externals, depth, entryFields, fetchLocks, handler); } }
private static void list( SVNRepository repository, String path, long rev, SVNDepth depth, int entryFields, Map<SVNURL, SVNPropertyValue> externals, SVNURL externalParentUrl, String externalTarget, ISVNDirEntryHandler handler) throws SVNException { if (depth == SVNDepth.EMPTY) { return; } Collection entries = new TreeSet(); SVNProperties properties; try { properties = externals == null ? null : new SVNProperties(); entries = repository.getDir(path, rev, properties, entryFields, entries); } catch (SVNAuthenticationException e) { return; } catch (SVNException e) { if (e.getErrorMessage().getErrorCode() == SVNErrorCode.RA_NOT_AUTHORIZED) { return; } throw e; } SVNPropertyValue svnExternalsVaule = properties == null ? null : properties.getSVNPropertyValue(SVNProperty.EXTERNALS); if (svnExternalsVaule != null) { SVNURL location = repository.getLocation(); externals.put(location.appendPath(path, false), svnExternalsVaule); } for (Iterator iterator = entries.iterator(); iterator.hasNext(); ) { SVNDirEntry entry = (SVNDirEntry) iterator.next(); String childPath = SVNPathUtil.append(path, entry.getName()); entry.setRelativePath(childPath); if (entry.getKind() == SVNNodeKind.FILE || depth == SVNDepth.IMMEDIATES || depth == SVNDepth.INFINITY) { entry.setExternalParentUrl(externalParentUrl); entry.setExternalTarget(externalTarget); handler.handleDirEntry(entry); } if (entry.getKind() == SVNNodeKind.DIR && entry.getDate() != null && depth == SVNDepth.INFINITY) { list( repository, childPath, rev, depth, entryFields, externals, externalParentUrl, externalTarget, handler); } } }