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
1 changed files with 85 additions and 80 deletions

View File

@ -13,12 +13,15 @@ import com.onedrive.sdk.concurrency.ICallback;
import com.onedrive.sdk.core.ClientException;
import com.onedrive.sdk.core.DefaultClientConfig;
import com.onedrive.sdk.core.IClientConfig;
import com.onedrive.sdk.core.OneDriveErrorCodes;
import com.onedrive.sdk.extensions.IItemCollectionPage;
import com.onedrive.sdk.extensions.IItemCollectionRequestBuilder;
import com.onedrive.sdk.extensions.IOneDriveClient;
import com.onedrive.sdk.extensions.Item;
import com.onedrive.sdk.extensions.OneDriveClient;
import com.onedrive.sdk.http.OneDriveServiceException;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
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
public boolean requiresSetup(String path) {
@ -269,6 +245,7 @@ public class OneDriveStorage extends JavaFileStorageBase
@Override
public InputStream openFileForRead(String path) throws Exception {
try {
path = removeProtocol(path);
return oneDriveClient.getDrive()
.getRoot()
@ -277,9 +254,22 @@ public class OneDriveStorage extends JavaFileStorageBase
.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
public void uploadFile(String path, byte[] data, boolean writeTransactional) throws Exception {
try {
path = removeProtocol(path);
oneDriveClient.getDrive()
.getRoot()
@ -287,6 +277,9 @@ public class OneDriveStorage extends JavaFileStorageBase
.getContent()
.buildRequest()
.put(data);
} catch (OneDriveServiceException e) {
throw convertException(e);
}
}
@Override
@ -306,6 +299,7 @@ public class OneDriveStorage extends JavaFileStorageBase
@Override
public List<FileEntry> listFiles(String parentPath) throws Exception {
try {
ArrayList<FileEntry> result = new ArrayList<FileEntry>();
parentPath = removeProtocol(parentPath);
IItemCollectionPage itemsPage = oneDriveClient.getDrive()
@ -324,7 +318,7 @@ public class OneDriveStorage extends JavaFileStorageBase
for (Item i: items)
{
FileEntry e = getFileEntry(getProtocolId() +"://"+ parentPath + "/" + i.name, i);
FileEntry e = getFileEntry(parentPath + "/" + i.name, i);
Log.d("KP2AJ", e.path);
result.add(e);
}
@ -334,6 +328,9 @@ public class OneDriveStorage extends JavaFileStorageBase
itemsPage = nextPageReqBuilder.buildRequest().get();
}
} catch (OneDriveServiceException e) {
throw convertException(e);
}
}
@NonNull
@ -342,7 +339,7 @@ public class OneDriveStorage extends JavaFileStorageBase
e.sizeInBytes = i.size;
e.displayName = i.name;
e.canRead = e.canWrite = true;
e.path = path;
e.path = getProtocolId() +"://"+path;
e.lastModifiedTime = i.lastModifiedDateTime.getTimeInMillis();
e.isDirectory = i.folder != null;
return e;
@ -350,6 +347,7 @@ public class OneDriveStorage extends JavaFileStorageBase
@Override
public FileEntry getFileEntry(String filename) throws Exception {
try {
filename = removeProtocol(filename);
Item item = oneDriveClient.getDrive()
.getRoot()
@ -357,16 +355,23 @@ public class OneDriveStorage extends JavaFileStorageBase
.buildRequest()
.get();
return getFileEntry(filename, item);
} catch (OneDriveServiceException e) {
throw convertException(e);
}
}
@Override
public void delete(String path) throws Exception {
try {
path = removeProtocol(path);
oneDriveClient.getDrive()
.getRoot()
.getItemWithPath(path)
.buildRequest()
.delete();
} catch (OneDriveServiceException e) {
throw convertException(e);
}
}
@Override