OneDrive: introduce error conversion for FileNotFoundException

This commit is contained in:
Philipp Crocoll 2016-11-22 11:37:56 +01:00
parent d483f840de
commit 0633f8d808

View File

@ -13,12 +13,15 @@ import com.onedrive.sdk.concurrency.ICallback;
import com.onedrive.sdk.core.ClientException; import com.onedrive.sdk.core.ClientException;
import com.onedrive.sdk.core.DefaultClientConfig; import com.onedrive.sdk.core.DefaultClientConfig;
import com.onedrive.sdk.core.IClientConfig; import com.onedrive.sdk.core.IClientConfig;
import com.onedrive.sdk.core.OneDriveErrorCodes;
import com.onedrive.sdk.extensions.IItemCollectionPage; import com.onedrive.sdk.extensions.IItemCollectionPage;
import com.onedrive.sdk.extensions.IItemCollectionRequestBuilder; import com.onedrive.sdk.extensions.IItemCollectionRequestBuilder;
import com.onedrive.sdk.extensions.IOneDriveClient; import com.onedrive.sdk.extensions.IOneDriveClient;
import com.onedrive.sdk.extensions.Item; import com.onedrive.sdk.extensions.Item;
import com.onedrive.sdk.extensions.OneDriveClient; import com.onedrive.sdk.extensions.OneDriveClient;
import com.onedrive.sdk.http.OneDriveServiceException;
import java.io.FileNotFoundException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -51,33 +54,6 @@ public class OneDriveStorage extends JavaFileStorageBase
} }
public void bla(final Activity activity) {
android.util.Log.d("KP2A", "0");
android.util.Log.d("KP2A", "1");
android.util.Log.d("KP2A", "2");
oneDriveClient
.getDrive()
.getRoot()
.buildRequest()
.get(new ICallback<Item>() {
@Override
public void success(final Item result) {
final String msg = "Found Root " + result.id;
Toast.makeText(activity, msg, Toast.LENGTH_SHORT)
.show();
}
@Override
public void failure(ClientException ex) {
Toast.makeText(activity, ex.toString(), Toast.LENGTH_SHORT)
.show();
}
});
android.util.Log.d("KP2A", "3");
}
@Override @Override
public boolean requiresSetup(String path) { public boolean requiresSetup(String path) {
@ -269,24 +245,41 @@ public class OneDriveStorage extends JavaFileStorageBase
@Override @Override
public InputStream openFileForRead(String path) throws Exception { public InputStream openFileForRead(String path) throws Exception {
path = removeProtocol(path); try {
return oneDriveClient.getDrive() path = removeProtocol(path);
.getRoot() return oneDriveClient.getDrive()
.getItemWithPath(path) .getRoot()
.getContent() .getItemWithPath(path)
.buildRequest() .getContent()
.get(); .buildRequest()
.get();
}
catch (OneDriveServiceException e)
{
throw convertException(e);
}
}
private Exception convertException(OneDriveServiceException e) {
if (e.isError(OneDriveErrorCodes.ItemNotFound))
return new FileNotFoundException(e.getMessage());
return e;
} }
@Override @Override
public void uploadFile(String path, byte[] data, boolean writeTransactional) throws Exception { public void uploadFile(String path, byte[] data, boolean writeTransactional) throws Exception {
path = removeProtocol(path); try {
oneDriveClient.getDrive() path = removeProtocol(path);
.getRoot() oneDriveClient.getDrive()
.getItemWithPath(path) .getRoot()
.getContent() .getItemWithPath(path)
.buildRequest() .getContent()
.put(data); .buildRequest()
.put(data);
} catch (OneDriveServiceException e) {
throw convertException(e);
}
} }
@Override @Override
@ -306,33 +299,37 @@ public class OneDriveStorage extends JavaFileStorageBase
@Override @Override
public List<FileEntry> listFiles(String parentPath) throws Exception { public List<FileEntry> listFiles(String parentPath) throws Exception {
ArrayList<FileEntry> result = new ArrayList<FileEntry>(); try {
parentPath = removeProtocol(parentPath); ArrayList<FileEntry> result = new ArrayList<FileEntry>();
IItemCollectionPage itemsPage = oneDriveClient.getDrive() parentPath = removeProtocol(parentPath);
.getRoot() IItemCollectionPage itemsPage = oneDriveClient.getDrive()
.getItemWithPath(parentPath) .getRoot()
.getChildren() .getItemWithPath(parentPath)
.buildRequest() .getChildren()
.get(); .buildRequest()
if (parentPath.endsWith("/")) .get();
parentPath = parentPath.substring(0,parentPath.length()-1); if (parentPath.endsWith("/"))
while (true) parentPath = parentPath.substring(0,parentPath.length()-1);
{ while (true)
List<Item> items = itemsPage.getCurrentPage();
if (items.isEmpty())
return result;
for (Item i: items)
{ {
FileEntry e = getFileEntry(getProtocolId() +"://"+ parentPath + "/" + i.name, i); List<Item> items = itemsPage.getCurrentPage();
Log.d("KP2AJ", e.path); if (items.isEmpty())
result.add(e); return result;
}
IItemCollectionRequestBuilder nextPageReqBuilder = itemsPage.getNextPage();
if (nextPageReqBuilder == null)
return result;
itemsPage = nextPageReqBuilder.buildRequest().get();
for (Item i: items)
{
FileEntry e = getFileEntry(parentPath + "/" + i.name, i);
Log.d("KP2AJ", e.path);
result.add(e);
}
IItemCollectionRequestBuilder nextPageReqBuilder = itemsPage.getNextPage();
if (nextPageReqBuilder == null)
return result;
itemsPage = nextPageReqBuilder.buildRequest().get();
}
} catch (OneDriveServiceException e) {
throw convertException(e);
} }
} }
@ -342,7 +339,7 @@ public class OneDriveStorage extends JavaFileStorageBase
e.sizeInBytes = i.size; e.sizeInBytes = i.size;
e.displayName = i.name; e.displayName = i.name;
e.canRead = e.canWrite = true; e.canRead = e.canWrite = true;
e.path = path; e.path = getProtocolId() +"://"+path;
e.lastModifiedTime = i.lastModifiedDateTime.getTimeInMillis(); e.lastModifiedTime = i.lastModifiedDateTime.getTimeInMillis();
e.isDirectory = i.folder != null; e.isDirectory = i.folder != null;
return e; return e;
@ -350,23 +347,31 @@ public class OneDriveStorage extends JavaFileStorageBase
@Override @Override
public FileEntry getFileEntry(String filename) throws Exception { public FileEntry getFileEntry(String filename) throws Exception {
filename = removeProtocol(filename); try {
Item item = oneDriveClient.getDrive() filename = removeProtocol(filename);
.getRoot() Item item = oneDriveClient.getDrive()
.getItemWithPath(filename) .getRoot()
.buildRequest() .getItemWithPath(filename)
.get(); .buildRequest()
return getFileEntry(filename, item); .get();
return getFileEntry(filename, item);
} catch (OneDriveServiceException e) {
throw convertException(e);
}
} }
@Override @Override
public void delete(String path) throws Exception { public void delete(String path) throws Exception {
path = removeProtocol(path); try {
oneDriveClient.getDrive() path = removeProtocol(path);
.getRoot() oneDriveClient.getDrive()
.getItemWithPath(path) .getRoot()
.buildRequest() .getItemWithPath(path)
.delete(); .buildRequest()
.delete();
} catch (OneDriveServiceException e) {
throw convertException(e);
}
} }
@Override @Override