use of com.google.appengine.tools.cloudstorage.ListItem in project appengine-gcs-client by GoogleCloudPlatform.
the class LocalRawGcsService method list.
@Override
public ListItemBatch list(String bucket, String prefix, String delimiter, String marker, int maxResults, long timeoutMillis) throws IOException {
ensureInitialized();
Query query = makeQuery(bucket);
int prefixLength;
if (!Strings.isNullOrEmpty(prefix)) {
Key keyPrefix = makeKey(bucket, prefix);
query.setFilter(new FilterPredicate(KEY_RESERVED_PROPERTY, GREATER_THAN_OR_EQUAL, keyPrefix));
prefixLength = prefix.length();
} else {
prefixLength = 0;
}
FetchOptions fetchOptions = FetchOptions.Builder.withDefaults();
if (marker != null) {
fetchOptions.startCursor(Cursor.fromWebSafeString(marker));
}
List<ListItem> items = new ArrayList<>(maxResults);
Set<String> prefixes = new HashSet<>();
QueryResultIterator<Entity> dsResults = datastore.prepare(query).asQueryResultIterator(fetchOptions);
while (items.size() < maxResults && dsResults.hasNext()) {
Entity entity = dsResults.next();
String name = entity.getKey().getName();
if (prefixLength > 0 && !name.startsWith(prefix)) {
break;
}
if (!Strings.isNullOrEmpty(delimiter)) {
int delimiterIdx = name.indexOf(delimiter, prefixLength);
if (delimiterIdx > 0) {
name = name.substring(0, delimiterIdx + 1);
if (prefixes.add(name)) {
items.add(new ListItem.Builder().setName(name).setDirectory(true).build());
}
continue;
}
}
GcsFilename filename = new GcsFilename(bucket, name);
GcsFileMetadata metadata = createGcsFileMetadata(entity, filename);
ListItem listItem = new ListItem.Builder().setName(name).setLength(metadata.getLength()).setLastModified(metadata.getLastModified()).build();
items.add(listItem);
}
Cursor cursor = dsResults.getCursor();
String nextMarker = null;
if (items.size() == maxResults && cursor != null) {
nextMarker = cursor.toWebSafeString();
}
return new ListItemBatch(items, nextMarker);
}
use of com.google.appengine.tools.cloudstorage.ListItem in project appengine-gcs-client by GoogleCloudPlatform.
the class OauthRawGcsService method list.
@Override
public ListItemBatch list(String bucket, String prefix, String delimiter, String marker, int maxResults, long timeoutMillis) throws IOException {
GcsFilename filename = new GcsFilename(bucket, "");
Map<String, String> queryStrings = new LinkedHashMap<>();
if (!Strings.isNullOrEmpty(prefix)) {
queryStrings.put(PREFIX, prefix);
}
if (!Strings.isNullOrEmpty(delimiter)) {
queryStrings.put(DELIMITER, delimiter);
}
if (!Strings.isNullOrEmpty(marker)) {
queryStrings.put(MARKER, marker);
}
if (maxResults >= 0) {
queryStrings.put(MAX_KEYS, String.valueOf(maxResults));
}
HTTPRequest req = makeRequest(filename, queryStrings, GET, timeoutMillis);
HTTPResponse resp;
try {
resp = urlfetch.fetch(req);
} catch (IOException e) {
throw createIOException(new HTTPRequestInfo(req), e);
}
if (resp.getResponseCode() != 200) {
throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
}
String nextMarker = null;
List<ListItem> items = new ArrayList<>();
try {
XmlHandler xmlHandler = new XmlHandler(resp.getContent(), PATHS);
while (xmlHandler.hasNext()) {
XmlHandler.XmlEvent event = xmlHandler.next();
if (event.getEventType() == EventType.CLOSE_ELEMENT) {
switch(event.getName()) {
case "NextMarker":
nextMarker = event.getValue();
break;
case "Prefix":
String name = event.getValue();
items.add(new ListItem.Builder().setName(name).setDirectory(true).build());
break;
default:
break;
}
} else if (event.getName().equals("Contents")) {
items.add(parseContents(xmlHandler));
}
}
} catch (XMLStreamException e) {
throw HttpErrorHandler.createException("Failed to parse response", e.getMessage());
}
return new ListItemBatch(items, nextMarker);
}
Aggregations