All Downloads are FREE. Search and download functionalities are using the official Maven repository.

syncloud.google.docs.GDocsStorage Maven / Gradle / Ivy

The newest version!
package syncloud.google.docs;

import com.google.api.client.http.HttpRequestFactory;
import syncloud.core.log.Logger;
import syncloud.google.docs.auth.PasswordTransportAuthenticator;
import syncloud.google.docs.auth.TransportAuthenticator;
import syncloud.google.docs.model.DocsUrl;
import syncloud.google.docs.model.DocumentListEntry;
import syncloud.google.docs.model.Feed;
import syncloud.storage.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GDocsStorage implements IStorage, ISecured {

    private static Logger logger = Logger.getLogger(GDocsStorage.class);
    private HttpRequestFactory requestFactory;
    private StorageKey key;
    private TransportAuthenticator authenticator;
    public final static String ROOT = "/";
    private final NodeKey rootNodeKey;

    public GDocsStorage(StorageKey key) {
        this.key = key;
        authenticator = new PasswordTransportAuthenticator();
        rootNodeKey = NodeKey.create(this.key, ROOT);
    }

    @Override
    public StorageKey getKey() {
        return key;
    }

    @Override
    public List getRoots() throws StorageException {

        try {
            List roots = new ArrayList();
            DocumentListEntry entry = new DocumentListEntry(DocsUrl.ROOT_RESOURCE_ID);
            entry.setTitle("");
            Feed feed = Feed.first100Entries(requestFactory, DocsUrl.forDefaultPrivateFull());
            entry.links = feed.links;
            entry.setCollection(true);
            roots.add(new GDocsFolder(rootNodeKey, entry, requestFactory));
            return roots;
        } catch (IOException e) {
            String message = "Getting Google Docs roots failed";
            logger.error(message);
            throw new StorageException(message);
        }
    }

    @Override
    public boolean authenticate(String password) {
        boolean authenticated = authenticator.authorize(key.getUser().getLogin(), password);
        if (authenticated)
            requestFactory = RequestFactory.getRequestFactory(authenticator.getAuth());
        return authenticated;
    }

    public boolean authenticated() {
        return authenticator.authenticated();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy