Search in sources :

Example 1 with ListItem

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);
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) ArrayList(java.util.ArrayList) Cursor(com.google.appengine.api.datastore.Cursor) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) ListItem(com.google.appengine.tools.cloudstorage.ListItem) GcsFileMetadata(com.google.appengine.tools.cloudstorage.GcsFileMetadata) BlobKey(com.google.appengine.api.blobstore.BlobKey) Key(com.google.appengine.api.datastore.Key) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename) HashSet(java.util.HashSet)

Example 2 with ListItem

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);
}
Also used : HTTPRequest(com.google.appengine.api.urlfetch.HTTPRequest) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) XMLStreamException(javax.xml.stream.XMLStreamException) HTTPRequestInfo(com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo) ListItem(com.google.appengine.tools.cloudstorage.ListItem) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename)

Aggregations

GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)2 ListItem (com.google.appengine.tools.cloudstorage.ListItem)2 ArrayList (java.util.ArrayList)2 BlobKey (com.google.appengine.api.blobstore.BlobKey)1 Cursor (com.google.appengine.api.datastore.Cursor)1 Entity (com.google.appengine.api.datastore.Entity)1 FetchOptions (com.google.appengine.api.datastore.FetchOptions)1 Key (com.google.appengine.api.datastore.Key)1 Query (com.google.appengine.api.datastore.Query)1 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)1 HTTPRequest (com.google.appengine.api.urlfetch.HTTPRequest)1 HTTPResponse (com.google.appengine.api.urlfetch.HTTPResponse)1 GcsFileMetadata (com.google.appengine.tools.cloudstorage.GcsFileMetadata)1 HTTPRequestInfo (com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 XMLStreamException (javax.xml.stream.XMLStreamException)1