From dc9a37db28dbe91b04416a94d858241768280c87 Mon Sep 17 00:00:00 2001 From: davigonz Date: Tue, 5 Jun 2018 18:22:54 +0200 Subject: [PATCH] Update GetMethod to be used in GetRemoteUserInfoOperation --- .../common/methods/nonwebdav/GetMethod.java | 8 +- .../users/GetRemoteUserInfoOperation.java | 79 ++++++++----------- 2 files changed, 38 insertions(+), 49 deletions(-) diff --git a/src/com/owncloud/android/lib/common/methods/nonwebdav/GetMethod.java b/src/com/owncloud/android/lib/common/methods/nonwebdav/GetMethod.java index 11fc49c8..6a297764 100644 --- a/src/com/owncloud/android/lib/common/methods/nonwebdav/GetMethod.java +++ b/src/com/owncloud/android/lib/common/methods/nonwebdav/GetMethod.java @@ -8,18 +8,18 @@ import okhttp3.Response; public class GetMethod extends HttpMethod { - public GetMethod(OkHttpClient okHttpClient, Request requestBase) { - super(okHttpClient, requestBase); + public GetMethod(OkHttpClient okHttpClient, Request baseRequest) { + super(okHttpClient, baseRequest); } @Override public int execute() throws IOException { - final Request request = mBaseRequest + mRequest = mBaseRequest .newBuilder() .get() .build(); - mResponse = mOkHttpClient.newCall(request).execute(); + mResponse = mOkHttpClient.newCall(mRequest).execute(); return mResponse.code(); } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.java b/src/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.java index 3639d5fa..987c29dc 100644 --- a/src/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.java +++ b/src/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.java @@ -1,5 +1,5 @@ /* ownCloud Android Library is available under MIT license - * Copyright (C) 2016 ownCloud GmbH. + * Copyright (C) 2018 ownCloud GmbH. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -37,12 +37,15 @@ import com.owncloud.android.lib.common.methods.nonwebdav.GetMethod; import okhttp3.Request; +import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; + /** * Gets information (id, display name, and e-mail address) about the user logged in. * * @author masensio * @author David A. Velasco + * @author David González Verdugo */ public class GetRemoteUserInfoOperation extends RemoteOperation { @@ -64,62 +67,49 @@ public class GetRemoteUserInfoOperation extends RemoteOperation { @Override protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result = null; + RemoteOperationResult result; //Get the user try { - final Request request = new Request.Builder() .url(client.getBaseUri() + OCS_ROUTE) .addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) .build(); GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); + int status = client.executeHttpMethod(getMethod); + String response = getMethod.getResponse().body().string(); -// client.executeHttpMethod(getMethod); -// -// get = new GetMethod(client.getBaseUri() + OCS_ROUTE); -// get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); -// status = client.executeMethod(get); -// if (isSuccess(status)) { -// String response = get.getResponseBodyAsString(); -// Log_OC.d(TAG, "Successful response"); -// -// // Parse the response -// JSONObject respJSON = new JSONObject(response); -// JSONObject respOCS = respJSON.getJSONObject(NODE_OCS); -// JSONObject respData = respOCS.getJSONObject(NODE_DATA); -// -// UserInfo userInfo = new UserInfo(); -// userInfo.mId = respData.getString(NODE_ID); -// userInfo.mDisplayName = respData.getString(NODE_DISPLAY_NAME); -// userInfo.mEmail = respData.getString(NODE_EMAIL); -// -// // Result -// result = new RemoteOperationResult(true, get); -// // Username in result.data -// ArrayList data = new ArrayList(); -// data.add(userInfo); -// result.setData(data); -// -// } else { -// result = new RemoteOperationResult(false, get); -// String response = get.getResponseBodyAsString(); -// Log_OC.e(TAG, "Failed response while getting user information "); -// if (response != null) { -// Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response); -// } else { -// Log_OC.e(TAG, "*** status code: " + status); -// } -// } + if (isSuccess(status)) { + Log_OC.d(TAG, "Successful response"); + + JSONObject respJSON = new JSONObject(response); + JSONObject respOCS = respJSON.getJSONObject(NODE_OCS); + JSONObject respData = respOCS.getJSONObject(NODE_DATA); + + UserInfo userInfo = new UserInfo(); + userInfo.mId = respData.getString(NODE_ID); + userInfo.mDisplayName = respData.getString(NODE_DISPLAY_NAME); + userInfo.mEmail = respData.getString(NODE_EMAIL); + + result = new RemoteOperationResult(OK); + + ArrayList data = new ArrayList<>(); + data.add(userInfo); + result.setData(data); + + } else { + result = new RemoteOperationResult(false, getMethod.getRequest(), getMethod.getResponse()); + Log_OC.e(TAG, "Failed response while getting user information "); + if (response != null) { + Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response); + } else { + Log_OC.e(TAG, "*** status code: " + status); + } + } } catch (Exception e) { result = new RemoteOperationResult(e); Log_OC.e(TAG, "Exception while getting OC user information", e); - - } finally { -// if (get != null) { -// get.releaseConnection(); -// } } return result; @@ -129,7 +119,6 @@ public class GetRemoteUserInfoOperation extends RemoteOperation { return (status == HttpStatus.SC_OK); } - public static class UserInfo { public String mId = ""; public String mDisplayName = "";