|
@@ -104,16 +104,6 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|
|
this.accountManager = accountManager;
|
|
|
}
|
|
|
|
|
|
- private NameValuePair[] convertMapToNVP(Map<String, String> map) {
|
|
|
- NameValuePair[] nvp = new NameValuePair[map.size()];
|
|
|
- int i = 0;
|
|
|
- for (String key : map.keySet()) {
|
|
|
- nvp[i] = new NameValuePair(key, map.get(key));
|
|
|
- i++;
|
|
|
- }
|
|
|
- return nvp;
|
|
|
- }
|
|
|
-
|
|
|
public ParcelFileDescriptor performNextcloudRequestV2(ParcelFileDescriptor input) {
|
|
|
return performNextcloudRequestAndBodyStreamV2(input, null);
|
|
|
}
|
|
@@ -143,7 +133,9 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|
|
InputStream exceptionStream = serializeObjectToInputStreamV2(exception, response.getPlainHeadersString());
|
|
|
InputStream resultStream = new java.io.SequenceInputStream(exceptionStream, response.getBody());
|
|
|
|
|
|
- return ParcelFileDescriptorUtil.pipeFrom(resultStream, thread -> Log.d(TAG, "Done sending result"));
|
|
|
+ return ParcelFileDescriptorUtil.pipeFrom(resultStream,
|
|
|
+ thread -> Log.d(TAG, "Done sending result"),
|
|
|
+ response.getMethod());
|
|
|
} catch (IOException e) {
|
|
|
Log_OC.e(TAG, "Error while sending response back to client app", e);
|
|
|
}
|
|
@@ -163,6 +155,7 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|
|
final InputStream requestBodyInputStream = requestBodyParcelFileDescriptor != null ?
|
|
|
new ParcelFileDescriptor.AutoCloseInputStream(requestBodyParcelFileDescriptor) : null;
|
|
|
Exception exception = null;
|
|
|
+ HttpMethodBase httpMethod = null;
|
|
|
InputStream httpStream = new InputStream() {
|
|
|
@Override
|
|
|
public int read() {
|
|
@@ -173,7 +166,8 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|
|
try {
|
|
|
// Start request and catch exceptions
|
|
|
NextcloudRequest request = deserializeObjectAndCloseStream(is);
|
|
|
- httpStream = processRequest(request, requestBodyInputStream);
|
|
|
+ httpMethod = processRequest(request, requestBodyInputStream);
|
|
|
+ httpStream = httpMethod.getResponseBodyAsStream();
|
|
|
} catch (Exception e) {
|
|
|
Log_OC.e(TAG, "Error during Nextcloud request", e);
|
|
|
exception = e;
|
|
@@ -188,7 +182,9 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|
|
} 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"),
|
|
|
+ httpMethod);
|
|
|
} catch (IOException e) {
|
|
|
Log_OC.e(TAG, "Error while sending response back to client app", e);
|
|
|
}
|
|
@@ -309,7 +305,7 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|
|
return method;
|
|
|
}
|
|
|
|
|
|
- private InputStream processRequest(final NextcloudRequest request, final InputStream requestBodyInputStream)
|
|
|
+ private HttpMethodBase processRequest(final NextcloudRequest request, final InputStream requestBodyInputStream)
|
|
|
throws UnsupportedOperationException,
|
|
|
com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException,
|
|
|
OperationCanceledException, AuthenticatorException, IOException {
|
|
@@ -354,23 +350,21 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|
|
|
|
|
// Check if status code is 2xx --> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
|
|
|
if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
|
|
|
- return method.getResponseBodyAsStream();
|
|
|
+ return method;
|
|
|
} else {
|
|
|
- StringBuilder total = new StringBuilder();
|
|
|
InputStream inputStream = method.getResponseBodyAsStream();
|
|
|
+ String total = "No response body";
|
|
|
+
|
|
|
// If response body is available
|
|
|
if (inputStream != null) {
|
|
|
- BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
- String line = reader.readLine();
|
|
|
- while (line != null) {
|
|
|
- total.append(line).append('\n');
|
|
|
- line = reader.readLine();
|
|
|
- }
|
|
|
- Log_OC.e(TAG, total.toString());
|
|
|
+ total = inputStreamToString(inputStream);
|
|
|
+ Log_OC.e(TAG, total);
|
|
|
}
|
|
|
+
|
|
|
+ method.releaseConnection();
|
|
|
throw new IllegalStateException(EXCEPTION_HTTP_REQUEST_FAILED,
|
|
|
new IllegalStateException(String.valueOf(status),
|
|
|
- new IllegalStateException(total.toString())));
|
|
|
+ new IllegalStateException(total)));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -419,23 +413,21 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|
|
|
|
|
// Check if status code is 2xx --> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
|
|
|
if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
|
|
|
- return new Response(method.getResponseBodyAsStream(), method.getResponseHeaders());
|
|
|
+ return new Response(method);
|
|
|
} else {
|
|
|
- StringBuilder total = new StringBuilder();
|
|
|
InputStream inputStream = method.getResponseBodyAsStream();
|
|
|
+ String total = "No response body";
|
|
|
+
|
|
|
// If response body is available
|
|
|
if (inputStream != null) {
|
|
|
- BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
- String line = reader.readLine();
|
|
|
- while (line != null) {
|
|
|
- total.append(line).append('\n');
|
|
|
- line = reader.readLine();
|
|
|
- }
|
|
|
- Log_OC.e(TAG, total.toString());
|
|
|
+ total = inputStreamToString(inputStream);
|
|
|
+ Log_OC.e(TAG, total);
|
|
|
}
|
|
|
+
|
|
|
+ method.releaseConnection();
|
|
|
throw new IllegalStateException(EXCEPTION_HTTP_REQUEST_FAILED,
|
|
|
new IllegalStateException(String.valueOf(status),
|
|
|
- new IllegalStateException(total.toString())));
|
|
|
+ new IllegalStateException(total)));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -474,4 +466,29 @@ public class InputStreamBinder extends IInputStreamService.Stub {
|
|
|
}
|
|
|
return result == 0;
|
|
|
}
|
|
|
+
|
|
|
+ private static String inputStreamToString(InputStream inputStream) {
|
|
|
+ try {
|
|
|
+ StringBuilder total = new StringBuilder();
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
+ String line = reader.readLine();
|
|
|
+ while (line != null) {
|
|
|
+ total.append(line).append('\n');
|
|
|
+ line = reader.readLine();
|
|
|
+ }
|
|
|
+ return total.toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static NameValuePair[] convertMapToNVP(Map<String, String> map) {
|
|
|
+ NameValuePair[] nvp = new NameValuePair[map.size()];
|
|
|
+ int i = 0;
|
|
|
+ for (String key : map.keySet()) {
|
|
|
+ nvp[i] = new NameValuePair(key, map.get(key));
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ return nvp;
|
|
|
+ }
|
|
|
}
|