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,6 +245,7 @@ public class OneDriveStorage extends JavaFileStorageBase
@Override @Override
public InputStream openFileForRead(String path) throws Exception { public InputStream openFileForRead(String path) throws Exception {
try {
path = removeProtocol(path); path = removeProtocol(path);
return oneDriveClient.getDrive() return oneDriveClient.getDrive()
.getRoot() .getRoot()
@ -277,9 +254,22 @@ public class OneDriveStorage extends JavaFileStorageBase
.buildRequest() .buildRequest()
.get(); .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 {
try {
path = removeProtocol(path); path = removeProtocol(path);
oneDriveClient.getDrive() oneDriveClient.getDrive()
.getRoot() .getRoot()
@ -287,6 +277,9 @@ public class OneDriveStorage extends JavaFileStorageBase
.getContent() .getContent()
.buildRequest() .buildRequest()
.put(data); .put(data);
} catch (OneDriveServiceException e) {
throw convertException(e);
}
} }
@Override @Override
@ -306,6 +299,7 @@ public class OneDriveStorage extends JavaFileStorageBase
@Override @Override
public List<FileEntry> listFiles(String parentPath) throws Exception { public List<FileEntry> listFiles(String parentPath) throws Exception {
try {
ArrayList<FileEntry> result = new ArrayList<FileEntry>(); ArrayList<FileEntry> result = new ArrayList<FileEntry>();
parentPath = removeProtocol(parentPath); parentPath = removeProtocol(parentPath);
IItemCollectionPage itemsPage = oneDriveClient.getDrive() IItemCollectionPage itemsPage = oneDriveClient.getDrive()
@ -324,7 +318,7 @@ public class OneDriveStorage extends JavaFileStorageBase
for (Item i: items) for (Item i: items)
{ {
FileEntry e = getFileEntry(getProtocolId() +"://"+ parentPath + "/" + i.name, i); FileEntry e = getFileEntry(parentPath + "/" + i.name, i);
Log.d("KP2AJ", e.path); Log.d("KP2AJ", e.path);
result.add(e); result.add(e);
} }
@ -334,6 +328,9 @@ public class OneDriveStorage extends JavaFileStorageBase
itemsPage = nextPageReqBuilder.buildRequest().get(); itemsPage = nextPageReqBuilder.buildRequest().get();
} }
} catch (OneDriveServiceException e) {
throw convertException(e);
}
} }
@NonNull @NonNull
@ -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,6 +347,7 @@ public class OneDriveStorage extends JavaFileStorageBase
@Override @Override
public FileEntry getFileEntry(String filename) throws Exception { public FileEntry getFileEntry(String filename) throws Exception {
try {
filename = removeProtocol(filename); filename = removeProtocol(filename);
Item item = oneDriveClient.getDrive() Item item = oneDriveClient.getDrive()
.getRoot() .getRoot()
@ -357,16 +355,23 @@ public class OneDriveStorage extends JavaFileStorageBase
.buildRequest() .buildRequest()
.get(); .get();
return getFileEntry(filename, item); 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 {
try {
path = removeProtocol(path); path = removeProtocol(path);
oneDriveClient.getDrive() oneDriveClient.getDrive()
.getRoot() .getRoot()
.getItemWithPath(path) .getItemWithPath(path)
.buildRequest() .buildRequest()
.delete(); .delete();
} catch (OneDriveServiceException e) {
throw convertException(e);
}
} }
@Override @Override