|
11 | 11 | import android.os.RemoteException;
|
12 | 12 | import android.util.Log;
|
13 | 13 |
|
14 |
| -import androidx.annotation.NonNull; |
15 |
| - |
| 14 | +import com.google.gson.Gson; |
| 15 | +import com.google.gson.internal.LinkedTreeMap; |
16 | 16 | import com.nextcloud.android.sso.Constants;
|
17 | 17 | import com.nextcloud.android.sso.aidl.IInputStreamService;
|
18 | 18 | import com.nextcloud.android.sso.aidl.IThreadListener;
|
|
27 | 27 | import java.io.InputStream;
|
28 | 28 | import java.io.ObjectInputStream;
|
29 | 29 | import java.io.ObjectOutputStream;
|
| 30 | +import java.io.Serializable; |
| 31 | +import java.util.ArrayList; |
30 | 32 | import java.util.concurrent.atomic.AtomicBoolean;
|
31 | 33 |
|
| 34 | +import androidx.annotation.NonNull; |
| 35 | + |
32 | 36 | import static com.nextcloud.android.sso.exceptions.SSOException.parseNextcloudCustomException;
|
33 | 37 |
|
34 | 38 | public class AidlNetworkRequest extends NetworkRequest {
|
@@ -138,6 +142,31 @@ private void waitForApi() throws NextcloudApiNotRespondingException {
|
138 | 142 | * @return InputStream answer from server as InputStream
|
139 | 143 | * @throws Exception or SSOException
|
140 | 144 | */
|
| 145 | + public Response performNetworkRequestV2(NextcloudRequest request, InputStream requestBodyInputStream) throws Exception { |
| 146 | + |
| 147 | + |
| 148 | + ParcelFileDescriptor output = performAidlNetworkRequestV2(request, requestBodyInputStream); |
| 149 | + InputStream os = new ParcelFileDescriptor.AutoCloseInputStream(output); |
| 150 | + ExceptionResponse response = deserializeObjectV2(os); |
| 151 | + |
| 152 | + // Handle Remote Exceptions |
| 153 | + if (response.getException() != null) { |
| 154 | + if (response.getException().getMessage() != null) { |
| 155 | + throw parseNextcloudCustomException(response.getException()); |
| 156 | + } |
| 157 | + throw response.getException(); |
| 158 | + } |
| 159 | + return new Response(os, response.headers); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * The InputStreams needs to be closed after reading from it |
| 164 | + * |
| 165 | + * @param request {@link NextcloudRequest} request to be executed on server via Files app |
| 166 | + * @param requestBodyInputStream inputstream to be sent to the server |
| 167 | + * @return InputStream answer from server as InputStream |
| 168 | + * @throws Exception or SSOException |
| 169 | + */ |
141 | 170 | public InputStream performNetworkRequest(NextcloudRequest request, InputStream requestBodyInputStream) throws Exception {
|
142 | 171 | InputStream os = null;
|
143 | 172 | Exception exception;
|
@@ -168,7 +197,7 @@ public InputStream performNetworkRequest(NextcloudRequest request, InputStream r
|
168 | 197 | * @throws IOException
|
169 | 198 | */
|
170 | 199 | private ParcelFileDescriptor performAidlNetworkRequest(NextcloudRequest request, InputStream requestBodyInputStream)
|
171 |
| - throws IOException, RemoteException, NextcloudApiNotRespondingException { |
| 200 | + throws IOException, RemoteException, NextcloudApiNotRespondingException { |
172 | 201 |
|
173 | 202 | // Check if we are on the main thread
|
174 | 203 | if(Looper.myLooper() == Looper.getMainLooper()) {
|
@@ -218,10 +247,128 @@ public void onThreadFinished(Thread thread) {
|
218 | 247 | return output;
|
219 | 248 | }
|
220 | 249 |
|
| 250 | + /** |
| 251 | + * DO NOT CALL THIS METHOD DIRECTLY - use @link(performNetworkRequestV2) instead |
| 252 | + * |
| 253 | + * @param request |
| 254 | + * @return |
| 255 | + * @throws IOException |
| 256 | + */ |
| 257 | + private ParcelFileDescriptor performAidlNetworkRequestV2(NextcloudRequest request, |
| 258 | + InputStream requestBodyInputStream) |
| 259 | + throws IOException, RemoteException, NextcloudApiNotRespondingException { |
| 260 | + |
| 261 | + // Check if we are on the main thread |
| 262 | + if (Looper.myLooper() == Looper.getMainLooper()) { |
| 263 | + throw new NetworkOnMainThreadException(); |
| 264 | + } |
| 265 | + |
| 266 | + // Wait for api to be initialized |
| 267 | + waitForApi(); |
| 268 | + |
| 269 | + // Log.d(TAG, request.url); |
| 270 | + request.setAccountName(getAccountName()); |
| 271 | + request.setToken(getAccountToken()); |
| 272 | + |
| 273 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 274 | + ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 275 | + oos.writeObject(request); |
| 276 | + oos.close(); |
| 277 | + baos.close(); |
| 278 | + InputStream is = new ByteArrayInputStream(baos.toByteArray()); |
| 279 | + |
| 280 | + ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is, |
| 281 | + new IThreadListener() { |
| 282 | + @Override |
| 283 | + public void onThreadFinished(Thread thread) { |
| 284 | + Log.d(TAG, "copy data from service finished"); |
| 285 | + } |
| 286 | + }); |
| 287 | + |
| 288 | + ParcelFileDescriptor requestBodyParcelFileDescriptor = null; |
| 289 | + if (requestBodyInputStream != null) { |
| 290 | + requestBodyParcelFileDescriptor = ParcelFileDescriptorUtil.pipeFrom(requestBodyInputStream, |
| 291 | + new IThreadListener() { |
| 292 | + @Override |
| 293 | + public void onThreadFinished(Thread thread) { |
| 294 | + Log.d(TAG, "copy data from service finished"); |
| 295 | + } |
| 296 | + }); |
| 297 | + } |
| 298 | + |
| 299 | + ParcelFileDescriptor output; |
| 300 | + if (requestBodyParcelFileDescriptor != null) { |
| 301 | + output = mService.performNextcloudRequestAndBodyStreamV2(input, requestBodyParcelFileDescriptor); |
| 302 | + } else { |
| 303 | + output = mService.performNextcloudRequestV2(input); |
| 304 | + } |
| 305 | + |
| 306 | + return output; |
| 307 | + } |
221 | 308 |
|
222 | 309 | private static <T> T deserializeObject(InputStream is) throws IOException, ClassNotFoundException {
|
223 | 310 | ObjectInputStream ois = new ObjectInputStream(is);
|
224 | 311 | T result = (T) ois.readObject();
|
225 | 312 | return result;
|
226 | 313 | }
|
| 314 | + |
| 315 | + private ExceptionResponse deserializeObjectV2(InputStream is) throws IOException, ClassNotFoundException { |
| 316 | + ObjectInputStream ois = new ObjectInputStream(is); |
| 317 | + ArrayList<PlainHeader> headerList = new ArrayList<>(); |
| 318 | + Exception exception = (Exception) ois.readObject(); |
| 319 | + |
| 320 | + if (exception == null) { |
| 321 | + String headers = (String) ois.readObject(); |
| 322 | + ArrayList list = new Gson().fromJson(headers, ArrayList.class); |
| 323 | + |
| 324 | + for (Object o : list) { |
| 325 | + LinkedTreeMap treeMap = (LinkedTreeMap) o; |
| 326 | + headerList.add(new PlainHeader((String) treeMap.get("name"), (String) treeMap.get("value"))); |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + return new ExceptionResponse(exception, headerList); |
| 331 | + } |
| 332 | + |
| 333 | + public class PlainHeader implements Serializable { |
| 334 | + private String name; |
| 335 | + private String value; |
| 336 | + |
| 337 | + PlainHeader(String name, String value) { |
| 338 | + this.name = name; |
| 339 | + this.value = value; |
| 340 | + } |
| 341 | + |
| 342 | + public String getName() { |
| 343 | + return name; |
| 344 | + } |
| 345 | + |
| 346 | + public String getValue() { |
| 347 | + return value; |
| 348 | + } |
| 349 | + |
| 350 | + private void writeObject(ObjectOutputStream oos) throws IOException{ |
| 351 | + oos.writeObject(name); |
| 352 | + oos.writeObject(value); |
| 353 | + } |
| 354 | + |
| 355 | + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { |
| 356 | + name = (String) in.readObject(); |
| 357 | + value = (String) in.readObject(); |
| 358 | + } |
| 359 | + } |
| 360 | + |
| 361 | + private class ExceptionResponse { |
| 362 | + private Exception exception; |
| 363 | + private ArrayList<PlainHeader> headers; |
| 364 | + |
| 365 | + public ExceptionResponse(Exception exception, ArrayList<PlainHeader> headers) { |
| 366 | + this.exception = exception; |
| 367 | + this.headers = headers; |
| 368 | + } |
| 369 | + |
| 370 | + public Exception getException() { |
| 371 | + return exception; |
| 372 | + } |
| 373 | + } |
227 | 374 | }
|
0 commit comments