mirror of
https://github.com/nextcloud/android.git
synced 2024-11-27 17:46:37 +03:00
implementing propfind + mkcol methods and adding inputstream to post / put methods
don't crash when httpstream is null Signed-off-by: Phie <phie@phie.ovh> Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
This commit is contained in:
parent
ba81710bc3
commit
2fa0687493
2 changed files with 115 additions and 47 deletions
|
@ -20,6 +20,8 @@ package com.nextcloud.android.sso.aidl;
|
||||||
// Declare the interface.
|
// Declare the interface.
|
||||||
interface IInputStreamService {
|
interface IInputStreamService {
|
||||||
|
|
||||||
ParcelFileDescriptor performNextcloudRequest(in ParcelFileDescriptor input);
|
ParcelFileDescriptor performNextcloudRequestAndBodyStream(in ParcelFileDescriptor input,
|
||||||
|
in ParcelFileDescriptor requestBodyParcelFileDescriptor);
|
||||||
|
|
||||||
|
ParcelFileDescriptor performNextcloudRequest(in ParcelFileDescriptor input);
|
||||||
}
|
}
|
|
@ -26,6 +26,7 @@ import android.accounts.AuthenticatorException;
|
||||||
import android.accounts.OperationCanceledException;
|
import android.accounts.OperationCanceledException;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.ParcelFileDescriptor;
|
import android.os.ParcelFileDescriptor;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
@ -42,13 +43,21 @@ import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
|
||||||
import com.owncloud.android.lib.common.utils.Log_OC;
|
import com.owncloud.android.lib.common.utils.Log_OC;
|
||||||
import com.owncloud.android.utils.EncryptionUtils;
|
import com.owncloud.android.utils.EncryptionUtils;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.HttpConnection;
|
||||||
import org.apache.commons.httpclient.HttpMethodBase;
|
import org.apache.commons.httpclient.HttpMethodBase;
|
||||||
|
import org.apache.commons.httpclient.HttpState;
|
||||||
import org.apache.commons.httpclient.NameValuePair;
|
import org.apache.commons.httpclient.NameValuePair;
|
||||||
import org.apache.commons.httpclient.methods.DeleteMethod;
|
import org.apache.commons.httpclient.methods.DeleteMethod;
|
||||||
import org.apache.commons.httpclient.methods.GetMethod;
|
import org.apache.commons.httpclient.methods.GetMethod;
|
||||||
|
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
|
||||||
import org.apache.commons.httpclient.methods.PostMethod;
|
import org.apache.commons.httpclient.methods.PostMethod;
|
||||||
import org.apache.commons.httpclient.methods.PutMethod;
|
import org.apache.commons.httpclient.methods.PutMethod;
|
||||||
|
import org.apache.commons.httpclient.methods.RequestEntity;
|
||||||
import org.apache.commons.httpclient.methods.StringRequestEntity;
|
import org.apache.commons.httpclient.methods.StringRequestEntity;
|
||||||
|
import org.apache.jackrabbit.webdav.DavConstants;
|
||||||
|
import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
|
||||||
|
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
|
||||||
|
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
@ -100,9 +109,16 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
||||||
}
|
}
|
||||||
|
|
||||||
public ParcelFileDescriptor performNextcloudRequest(ParcelFileDescriptor input) {
|
public ParcelFileDescriptor performNextcloudRequest(ParcelFileDescriptor input) {
|
||||||
|
return performNextcloudRequestAndBodyStream(input, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ParcelFileDescriptor performNextcloudRequestAndBodyStream(ParcelFileDescriptor input,
|
||||||
|
ParcelFileDescriptor requestBodyParcelFileDescriptor) {
|
||||||
// read the input
|
// read the input
|
||||||
final InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(input);
|
final InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(input);
|
||||||
|
|
||||||
|
final InputStream requestBodyInputStream = requestBodyParcelFileDescriptor != null ?
|
||||||
|
new ParcelFileDescriptor.AutoCloseInputStream(requestBodyParcelFileDescriptor) : null;
|
||||||
Exception exception = null;
|
Exception exception = null;
|
||||||
InputStream httpStream = new InputStream() {
|
InputStream httpStream = new InputStream() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -110,10 +126,11 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Start request and catch exceptions
|
// Start request and catch exceptions
|
||||||
NextcloudRequest request = deserializeObjectAndCloseStream(is);
|
NextcloudRequest request = deserializeObjectAndCloseStream(is);
|
||||||
httpStream = processRequest(request);
|
httpStream = processRequest(request, requestBodyInputStream);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log_OC.e(TAG, "Error during Nextcloud request", e);
|
Log_OC.e(TAG, "Error during Nextcloud request", e);
|
||||||
exception = e;
|
exception = e;
|
||||||
|
@ -122,7 +139,12 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
||||||
try {
|
try {
|
||||||
// Write exception to the stream followed by the actual network stream
|
// Write exception to the stream followed by the actual network stream
|
||||||
InputStream exceptionStream = serializeObjectToInputStream(exception);
|
InputStream exceptionStream = serializeObjectToInputStream(exception);
|
||||||
InputStream resultStream = new java.io.SequenceInputStream(exceptionStream, httpStream);
|
InputStream resultStream;
|
||||||
|
if (httpStream != null) {
|
||||||
|
resultStream = new java.io.SequenceInputStream(exceptionStream, httpStream);
|
||||||
|
} else {
|
||||||
|
resultStream = exceptionStream;
|
||||||
|
}
|
||||||
return ParcelFileDescriptorUtil.pipeFrom(resultStream, thread -> Log.d(TAG, "Done sending result"));
|
return ParcelFileDescriptorUtil.pipeFrom(resultStream, thread -> Log.d(TAG, "Done sending result"));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log_OC.e(TAG, "Error while sending response back to client app", e);
|
Log_OC.e(TAG, "Error while sending response back to client app", e);
|
||||||
|
@ -139,7 +161,8 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
||||||
return new ByteArrayInputStream(baos.toByteArray());
|
return new ByteArrayInputStream(baos.toByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Serializable> T deserializeObjectAndCloseStream(InputStream is) throws IOException, ClassNotFoundException {
|
private <T extends Serializable> T deserializeObjectAndCloseStream(InputStream is) throws IOException,
|
||||||
|
ClassNotFoundException {
|
||||||
ObjectInputStream ois = new ObjectInputStream(is);
|
ObjectInputStream ois = new ObjectInputStream(is);
|
||||||
T result = (T) ois.readObject();
|
T result = (T) ois.readObject();
|
||||||
is.close();
|
is.close();
|
||||||
|
@ -147,30 +170,21 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class NCPropFindMethod extends PropFindMethod {
|
||||||
private InputStream processRequest(final NextcloudRequest request) throws UnsupportedOperationException, com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
|
NCPropFindMethod(String uri, int propfindType, int depth) throws IOException {
|
||||||
Account account = AccountUtils.getOwnCloudAccountByName(context, request.getAccountName()); // TODO handle case that account is not found!
|
super(uri, propfindType, new DavPropertyNameSet(), depth);
|
||||||
if(account == null) {
|
|
||||||
throw new IllegalStateException(EXCEPTION_ACCOUNT_NOT_FOUND);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate token
|
@Override
|
||||||
if (!isValid(request)) {
|
protected void processResponseBody(HttpState httpState, HttpConnection httpConnection) {
|
||||||
throw new IllegalStateException(EXCEPTION_INVALID_TOKEN);
|
// Do not process the response body here. Instead pass it on to client app.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate URL
|
private HttpMethodBase buildMethod(NextcloudRequest request, Uri baseUri, InputStream requestBodyInputStream)
|
||||||
if(request.getUrl().length() == 0 || request.getUrl().charAt(0) != PATH_SEPARATOR) {
|
throws IOException {
|
||||||
throw new IllegalStateException(EXCEPTION_INVALID_REQUEST_URL, new IllegalStateException("URL need to start with a /"));
|
String requestUrl = baseUri + request.getUrl();
|
||||||
}
|
|
||||||
|
|
||||||
OwnCloudClientManager ownCloudClientManager = OwnCloudClientManagerFactory.getDefaultSingleton();
|
|
||||||
OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
|
|
||||||
OwnCloudClient client = ownCloudClientManager.getClientFor(ocAccount, context);
|
|
||||||
|
|
||||||
String requestUrl = client.getBaseUri() + request.getUrl();
|
|
||||||
HttpMethodBase method;
|
HttpMethodBase method;
|
||||||
|
|
||||||
switch (request.getMethod()) {
|
switch (request.getMethod()) {
|
||||||
case "GET":
|
case "GET":
|
||||||
method = new GetMethod(requestUrl);
|
method = new GetMethod(requestUrl);
|
||||||
|
@ -178,7 +192,11 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
||||||
|
|
||||||
case "POST":
|
case "POST":
|
||||||
method = new PostMethod(requestUrl);
|
method = new PostMethod(requestUrl);
|
||||||
if (request.getRequestBody() != null) {
|
if(requestBodyInputStream != null){
|
||||||
|
RequestEntity requestEntity = new InputStreamRequestEntity(requestBodyInputStream);
|
||||||
|
((PostMethod) method).setRequestEntity(requestEntity);
|
||||||
|
}
|
||||||
|
else if (request.getRequestBody() != null) {
|
||||||
StringRequestEntity requestEntity = new StringRequestEntity(
|
StringRequestEntity requestEntity = new StringRequestEntity(
|
||||||
request.getRequestBody(),
|
request.getRequestBody(),
|
||||||
CONTENT_TYPE_APPLICATION_JSON,
|
CONTENT_TYPE_APPLICATION_JSON,
|
||||||
|
@ -189,7 +207,11 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
||||||
|
|
||||||
case "PUT":
|
case "PUT":
|
||||||
method = new PutMethod(requestUrl);
|
method = new PutMethod(requestUrl);
|
||||||
if (request.getRequestBody() != null) {
|
if(requestBodyInputStream != null){
|
||||||
|
RequestEntity requestEntity = new InputStreamRequestEntity(requestBodyInputStream);
|
||||||
|
((PutMethod) method).setRequestEntity(requestEntity);
|
||||||
|
}
|
||||||
|
else if (request.getRequestBody() != null) {
|
||||||
StringRequestEntity requestEntity = new StringRequestEntity(
|
StringRequestEntity requestEntity = new StringRequestEntity(
|
||||||
request.getRequestBody(),
|
request.getRequestBody(),
|
||||||
CONTENT_TYPE_APPLICATION_JSON,
|
CONTENT_TYPE_APPLICATION_JSON,
|
||||||
|
@ -202,10 +224,54 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
||||||
method = new DeleteMethod(requestUrl);
|
method = new DeleteMethod(requestUrl);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "PROPFIND":
|
||||||
|
method = new NCPropFindMethod(requestUrl, DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1);
|
||||||
|
if (request.getRequestBody() != null) {
|
||||||
|
//text/xml; charset=UTF-8 is taken from XmlRequestEntity... Should be application/xml
|
||||||
|
StringRequestEntity requestEntity = new StringRequestEntity(
|
||||||
|
request.getRequestBody(),
|
||||||
|
"text/xml; charset=UTF-8",
|
||||||
|
CHARSET_UTF8);
|
||||||
|
((PropFindMethod) method).setRequestEntity(requestEntity);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "MKCOL":
|
||||||
|
method = new MkColMethod(requestUrl);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new UnsupportedOperationException(EXCEPTION_UNSUPPORTED_METHOD);
|
throw new UnsupportedOperationException(EXCEPTION_UNSUPPORTED_METHOD);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
private InputStream processRequest(final NextcloudRequest request, final InputStream requestBodyInputStream)
|
||||||
|
throws UnsupportedOperationException,
|
||||||
|
com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException,
|
||||||
|
OperationCanceledException, AuthenticatorException, IOException {
|
||||||
|
Account account = AccountUtils.getOwnCloudAccountByName(context, request.getAccountName());
|
||||||
|
if(account == null) {
|
||||||
|
throw new IllegalStateException(EXCEPTION_ACCOUNT_NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate token
|
||||||
|
if (!isValid(request)) {
|
||||||
|
throw new IllegalStateException(EXCEPTION_INVALID_TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate URL
|
||||||
|
if (request.getUrl().length() == 0 || request.getUrl().charAt(0) != PATH_SEPARATOR) {
|
||||||
|
throw new IllegalStateException(EXCEPTION_INVALID_REQUEST_URL,
|
||||||
|
new IllegalStateException("URL need to start with a /"));
|
||||||
|
}
|
||||||
|
|
||||||
|
OwnCloudClientManager ownCloudClientManager = OwnCloudClientManagerFactory.getDefaultSingleton();
|
||||||
|
OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
|
||||||
|
OwnCloudClient client = ownCloudClientManager.getClientFor(ocAccount, context);
|
||||||
|
|
||||||
|
HttpMethodBase method = buildMethod(request, client.getBaseUri(), requestBodyInputStream);
|
||||||
|
|
||||||
method.setQueryString(convertMapToNVP(request.getParameter()));
|
method.setQueryString(convertMapToNVP(request.getParameter()));
|
||||||
method.addRequestHeader("OCS-APIREQUEST", "true");
|
method.addRequestHeader("OCS-APIREQUEST", "true");
|
||||||
|
|
Loading…
Reference in a new issue