syncloud.google.docs.GDocsFile Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of syncloud-google-docs Show documentation
Show all versions of syncloud-google-docs Show documentation
Google Docs Syncloud Starage Adapter
The newest version!
package syncloud.google.docs;
import com.google.api.client.http.HttpRequestFactory;
import syncloud.core.log.Logger;
import syncloud.google.docs.model.DocsUrl;
import syncloud.google.docs.model.Entry;
import syncloud.google.docs.model.Feed;
import syncloud.storage.*;
import java.io.IOException;
import java.io.InputStream;
public class GDocsFile implements IFile {
private static Logger logger = Logger.getLogger(GDocsFile.class);
private Entry entry;
private HttpRequestFactory requestFactory;
private IFolder parent;
private NodeKey key;
private boolean deleted = false;
/*public GDocsFile(StorageKey key, Entry entry, HttpRequestFactory requestFactory, IFolder parent) {
this.entry = entry;
this.requestFactory = requestFactory;
this.parent = parent;
this.key = new NodeKey(key, getFullName());
}*/
public GDocsFile(Entry entry, HttpRequestFactory requestFactory, IFolder parent) {
this.entry = entry;
this.requestFactory = requestFactory;
this.parent = parent;
this.key = parent.getKey().child(entry.getTitle());
}
@Override
public NodeKey getKey() {
return key;
}
@Override
public boolean exists() throws StorageException {
if (deleted) {
logger.debug("file is marked as deleted");
return false;
}
Entry entryInfo = null;
try {
entryInfo = Feed.executeGet(requestFactory, DocsUrl.entrtyInfo(entry.getResourceId()), Entry.class);
} catch (IOException e) {
logger.info("file does not exist: " + entry);
return false;
}
return (!entryInfo.isTrashed() && !entryInfo.isDeleted() && !entryInfo.isRemoved());
}
@Override
public void delete() throws StorageException {
if (!exists())
throw new StorageException(String.format(DOES_NOT_EXIST, key.getNativePath()));
try {
ExponentialBackoff retrier = new ExponentialBackoff() {
@Override
public Object process() throws NotReady, IOException {
try {
Feed.executeDelete(requestFactory, DocsUrl.delete(entry.getResourceId(), true), entry.etag, true);
deleted = true;
logger.debug("set deleted flag");
} catch (IOException e) {
if (!exists())
return null;
throw new NotReady();
}
return null;
}
};
retrier.run();
/*Feed.executeDelete(requestFactory, DocsUrl.delete(entry.getResourceId(), true), entry.etag, true);
deleted = true;
logger.debug("set deleted flag");*/
} catch (Throwable e) {
String message = String.format(UNABLE_TO_DELETE_FILE, key.getNativePath());
logger.error(message, e);
throw new StorageException(ErrorCodeConverter.convert(e), message, e);
}
}
@Override
public InputStream getData() throws StorageException {
try {
return Feed.executeGetContent(requestFactory, new DocsUrl(entry.getContent().getSrc()));
} catch (IOException e) {
String message = UNABLE_TO_GET_DATA_FOR_FILE + entry.getTitle();
logger.error(message, e);
throw new StorageException(message);
}
}
@Override
public String getName() {
return entry.getTitle();
}
@Override
public String getVersion() throws StorageException {
if (deleted) return null;
try {
Entry entryInfo = Feed.executeGet(requestFactory, DocsUrl.entrtyInfo(entry.getResourceId()), Entry.class);
return entryInfo.mdsChecksum;
} catch (IOException e) {
String message = "file does not exist: " + entry;
logger.error(message, e);
return null;
}
}
@Override
public void save(InputStreamProvider inputStreamProviderdata, long size) throws StorageException {
GDocsFile file = (GDocsFile) parent.createFile(getName(), inputStreamProviderdata, size);
this.entry = file.entry;
}
@Override
public long size() {
return entry.getSize();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy