mirror of
https://github.com/nextcloud/android.git
synced 2024-11-27 09:39:25 +03:00
Merge pull request #3428 from PhieF/Propfind
adding propfind method to sso
This commit is contained in:
commit
4b9985303b
2 changed files with 115 additions and 47 deletions
|
@ -20,6 +20,8 @@ package com.nextcloud.android.sso.aidl;
|
|||
// Declare the interface.
|
||||
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.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.Uri;
|
||||
import android.os.Binder;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
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.utils.EncryptionUtils;
|
||||
|
||||
import org.apache.commons.httpclient.HttpConnection;
|
||||
import org.apache.commons.httpclient.HttpMethodBase;
|
||||
import org.apache.commons.httpclient.HttpState;
|
||||
import org.apache.commons.httpclient.NameValuePair;
|
||||
import org.apache.commons.httpclient.methods.DeleteMethod;
|
||||
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.PutMethod;
|
||||
import org.apache.commons.httpclient.methods.RequestEntity;
|
||||
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.ByteArrayInputStream;
|
||||
|
@ -100,9 +109,16 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|||
}
|
||||
|
||||
public ParcelFileDescriptor performNextcloudRequest(ParcelFileDescriptor input) {
|
||||
return performNextcloudRequestAndBodyStream(input, null);
|
||||
}
|
||||
|
||||
public ParcelFileDescriptor performNextcloudRequestAndBodyStream(ParcelFileDescriptor input,
|
||||
ParcelFileDescriptor requestBodyParcelFileDescriptor) {
|
||||
// read the input
|
||||
final InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(input);
|
||||
|
||||
final InputStream requestBodyInputStream = requestBodyParcelFileDescriptor != null ?
|
||||
new ParcelFileDescriptor.AutoCloseInputStream(requestBodyParcelFileDescriptor) : null;
|
||||
Exception exception = null;
|
||||
InputStream httpStream = new InputStream() {
|
||||
@Override
|
||||
|
@ -110,10 +126,11 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
// Start request and catch exceptions
|
||||
NextcloudRequest request = deserializeObjectAndCloseStream(is);
|
||||
httpStream = processRequest(request);
|
||||
httpStream = processRequest(request, requestBodyInputStream);
|
||||
} catch (Exception e) {
|
||||
Log_OC.e(TAG, "Error during Nextcloud request", e);
|
||||
exception = e;
|
||||
|
@ -122,7 +139,12 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|||
try {
|
||||
// Write exception to the stream followed by the actual network stream
|
||||
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"));
|
||||
} catch (IOException 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());
|
||||
}
|
||||
|
||||
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);
|
||||
T result = (T) ois.readObject();
|
||||
is.close();
|
||||
|
@ -147,9 +170,88 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|||
return result;
|
||||
}
|
||||
|
||||
public class NCPropFindMethod extends PropFindMethod {
|
||||
NCPropFindMethod(String uri, int propfindType, int depth) throws IOException {
|
||||
super(uri, propfindType, new DavPropertyNameSet(), depth);
|
||||
}
|
||||
|
||||
private InputStream processRequest(final NextcloudRequest request) throws UnsupportedOperationException, com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
|
||||
Account account = AccountUtils.getOwnCloudAccountByName(context, request.getAccountName()); // TODO handle case that account is not found!
|
||||
@Override
|
||||
protected void processResponseBody(HttpState httpState, HttpConnection httpConnection) {
|
||||
// Do not process the response body here. Instead pass it on to client app.
|
||||
}
|
||||
}
|
||||
|
||||
private HttpMethodBase buildMethod(NextcloudRequest request, Uri baseUri, InputStream requestBodyInputStream)
|
||||
throws IOException {
|
||||
String requestUrl = baseUri + request.getUrl();
|
||||
HttpMethodBase method;
|
||||
switch (request.getMethod()) {
|
||||
case "GET":
|
||||
method = new GetMethod(requestUrl);
|
||||
break;
|
||||
|
||||
case "POST":
|
||||
method = new PostMethod(requestUrl);
|
||||
if(requestBodyInputStream != null){
|
||||
RequestEntity requestEntity = new InputStreamRequestEntity(requestBodyInputStream);
|
||||
((PostMethod) method).setRequestEntity(requestEntity);
|
||||
}
|
||||
else if (request.getRequestBody() != null) {
|
||||
StringRequestEntity requestEntity = new StringRequestEntity(
|
||||
request.getRequestBody(),
|
||||
CONTENT_TYPE_APPLICATION_JSON,
|
||||
CHARSET_UTF8);
|
||||
((PostMethod) method).setRequestEntity(requestEntity);
|
||||
}
|
||||
break;
|
||||
|
||||
case "PUT":
|
||||
method = new PutMethod(requestUrl);
|
||||
if(requestBodyInputStream != null){
|
||||
RequestEntity requestEntity = new InputStreamRequestEntity(requestBodyInputStream);
|
||||
((PutMethod) method).setRequestEntity(requestEntity);
|
||||
}
|
||||
else if (request.getRequestBody() != null) {
|
||||
StringRequestEntity requestEntity = new StringRequestEntity(
|
||||
request.getRequestBody(),
|
||||
CONTENT_TYPE_APPLICATION_JSON,
|
||||
CHARSET_UTF8);
|
||||
((PutMethod) method).setRequestEntity(requestEntity);
|
||||
}
|
||||
break;
|
||||
|
||||
case "DELETE":
|
||||
method = new DeleteMethod(requestUrl);
|
||||
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:
|
||||
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);
|
||||
}
|
||||
|
@ -160,52 +262,16 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|||
}
|
||||
|
||||
// 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 /"));
|
||||
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);
|
||||
|
||||
String requestUrl = client.getBaseUri() + request.getUrl();
|
||||
HttpMethodBase method;
|
||||
|
||||
switch (request.getMethod()) {
|
||||
case "GET":
|
||||
method = new GetMethod(requestUrl);
|
||||
break;
|
||||
|
||||
case "POST":
|
||||
method = new PostMethod(requestUrl);
|
||||
if (request.getRequestBody() != null) {
|
||||
StringRequestEntity requestEntity = new StringRequestEntity(
|
||||
request.getRequestBody(),
|
||||
CONTENT_TYPE_APPLICATION_JSON,
|
||||
CHARSET_UTF8);
|
||||
((PostMethod) method).setRequestEntity(requestEntity);
|
||||
}
|
||||
break;
|
||||
|
||||
case "PUT":
|
||||
method = new PutMethod(requestUrl);
|
||||
if (request.getRequestBody() != null) {
|
||||
StringRequestEntity requestEntity = new StringRequestEntity(
|
||||
request.getRequestBody(),
|
||||
CONTENT_TYPE_APPLICATION_JSON,
|
||||
CHARSET_UTF8);
|
||||
((PutMethod) method).setRequestEntity(requestEntity);
|
||||
}
|
||||
break;
|
||||
|
||||
case "DELETE":
|
||||
method = new DeleteMethod(requestUrl);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new UnsupportedOperationException(EXCEPTION_UNSUPPORTED_METHOD);
|
||||
|
||||
}
|
||||
HttpMethodBase method = buildMethod(request, client.getBaseUri(), requestBodyInputStream);
|
||||
|
||||
method.setQueryString(convertMapToNVP(request.getParameter()));
|
||||
method.addRequestHeader("OCS-APIREQUEST", "true");
|
||||
|
|
Loading…
Reference in a new issue