Skip to content

Commit

Permalink
add file list functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Iege committed Feb 18, 2021
1 parent de63056 commit b642e42
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 10 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,16 @@ for (RepositorySummary repoSummary : artifactory.storage().getStorageInfo().getR
##### Getting Items
```
ItemHandle fileItem = artifactory.repository("RepoName").file("path/to/file.txt");
ItemHandle folderItem = artifactory.repository("RepoName").folder("path/to/folder");
FolderHandle folderItem = artifactory.repository("RepoName").folder("path/to/folder");
```

##### Getting All Items Under A Folder
```
FolderHandle folder = artifactory.repository("RepoName").folder("path/to/folder");
boolean deep = true;
boolean listFolders = true;
boolean timeStamps = true;
FileList list = folder.list(deep, listFolders, timeStamps);
```

##### Copying Items
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.jfrog.artifactory.client;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.jfrog.artifactory.client.model.FileList;

@JsonIgnoreProperties(ignoreUnknown = true)
public interface FolderHandle extends ItemHandle {
FileList list(boolean deep, boolean listFolders, boolean timestamps);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public interface RepositoryHandle {

ItemHandle folder(String folderName);
FolderHandle folder(String folderName);

ItemHandle file(String filePath);

Expand Down
11 changes: 11 additions & 0 deletions api/src/main/java/org/jfrog/artifactory/client/model/FileList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jfrog.artifactory.client.model;

import java.util.Date;
import java.util.List;

public interface FileList {

String getUri();
Date getCreated();
List<ListItem> getFiles();
}
11 changes: 11 additions & 0 deletions api/src/main/java/org/jfrog/artifactory/client/model/ListItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jfrog.artifactory.client.model;

import java.util.Date;

public interface ListItem {
String getUri();
Long getSize();
Date getLastModified();
Boolean isFolder();
String getSha1();
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
currentVersion=2.9.x-SNAPSHOT
currentVersion=2.9.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.jfrog.artifactory.client.impl;

import org.jfrog.artifactory.client.FolderHandle;
import org.jfrog.artifactory.client.model.FileList;
import org.jfrog.artifactory.client.model.impl.FileListImpl;

import java.io.IOException;

public class FolderHandleImpl extends ItemHandleImpl implements FolderHandle {

private final String baseApiPath;

private final ArtifactoryImpl artifactory;
private final String repo;
private final String path;

FolderHandleImpl(ArtifactoryImpl artifactory, String baseApiPath, String repo, String path, Class itemType) {
super(artifactory, baseApiPath, repo, path, itemType);
this.artifactory = artifactory;
this.repo = repo;
this.path = path;
this.baseApiPath = baseApiPath;
}

public FileList list(boolean deep, boolean listFolders, boolean timestamps) {
String deepInt = toInt(deep);
String listFoldersInt = toInt(listFolders);
String timestampsInt = toInt(timestamps);
String url = String.format("%s/storage/%s/%s?list&deep=%s&listFolders=%s&mdTimestamps=%s",
baseApiPath, repo, path, deepInt, listFoldersInt, timestampsInt);

FileListImpl fileListImpl = new FileListImpl();
try {
return artifactory.get(url, fileListImpl.getClass(), null);
} catch (IOException e) {
return null;
}
}

private String toInt(boolean b) {
return b?"1":"0";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ class RepositoryHandleImpl implements RepositoryHandle {
this.repoKey = repoKey
}

//TODO: [by yl] Use a FileHandler and a FolderHandler instead or returning Items
@Override
ItemHandle folder(String folderName) {
new ItemHandleImpl(artifactory, baseApiPath, repoKey, folderName, FolderImpl)
FolderHandle folder(String folderName) {
new FolderHandleImpl(artifactory, baseApiPath, repoKey, folderName, FolderImpl)
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.jfrog.artifactory.client.model.impl;

import org.jfrog.artifactory.client.model.FileList;
import org.jfrog.artifactory.client.model.ListItem;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class FileListImpl implements FileList {

String uri;
Date created;
ListItemImpl[] files;

FileListImpl(String uri, Date created, ListItemImpl[] files) {
this.uri = uri;
this.created = created;
this.files = files;
}

public FileListImpl() {
}

@Override
public String getUri() {
return uri;
}

@Override
public Date getCreated() {
return created;
}

@Override
public List<ListItem> getFiles() {
return Arrays.asList(files);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.jfrog.artifactory.client.model.impl;

import org.jfrog.artifactory.client.model.File;
import org.jfrog.artifactory.client.model.ListItem;

import java.util.Date;

public class ListItemImpl implements ListItem {
String uri;
Long size;
Date lastModified;
Boolean folder;
String sha1;

ListItemImpl(String uri, Long size, Date lastModified, Boolean folder) {
this.uri = uri;
this.size = size;
this.lastModified = lastModified;
this.folder = folder;
}

ListItemImpl() {
}

@Override
public String getUri() {
return uri;
}

@Override
public Long getSize() {
return size;
}

@Override
public Date getLastModified() {
return lastModified;
}

@Override
public Boolean isFolder() {
return folder;
}

@Override
public String getSha1() {
return sha1;
}

}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2.9.1
version=2.9.x-SNAPSHOT
21 changes: 18 additions & 3 deletions services/src/test/java/org/jfrog/artifactory/client/ItemTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.jfrog.artifactory.client.impl.ArtifactoryRequestImpl;
import org.jfrog.artifactory.client.impl.CopyMoveException;
import org.jfrog.artifactory.client.model.File;
import org.jfrog.artifactory.client.model.FileList;
import org.jfrog.artifactory.client.model.Folder;
import org.jfrog.artifactory.client.model.Item;
import org.jfrog.artifactory.client.model.LocalRepository;
Expand All @@ -13,9 +14,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import static org.testng.Assert.*;

Expand All @@ -29,6 +28,22 @@ public class ItemTests extends ArtifactoryTestsBase {
protected static final String NEW_LOCAL_FROM = "new-local-from";
protected static final String NEW_LOCAL_TO = "new-local-to";

@Test
public void testFolderList() throws IOException {
artifactory.repository(getJCenterRepoName()).download("junit/junit/4.10/junit-4.10-sources.jar").doDownload();

FileList list = artifactory.repository(getJcenterCacheName()).folder("junit").list(true,true,true);
assertNotNull(list);
assertEquals(list.getFiles().size(),3);
assertEquals(list.getFiles().get(2).getSha1(),"6c98d6766e72d5575f96c9479d1c1d3b865c6e25");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(1317323539000L);
assertEquals(list.getFiles().get(2).getLastModified().getTime(),calendar.getTime().getTime());

FileList list2 = artifactory.repository(getJcenterCacheName()).folder("junit").list(true,false,false);
assertEquals(list2.getFiles().size(),1);
}

@Test
public void testFolderInfo() throws IOException {
// Get the folder to the cache
Expand Down

0 comments on commit b642e42

Please sign in to comment.