From: masensio Date: Mon, 13 Jan 2014 10:43:49 +0000 (+0100) Subject: OC-2541: Add GetUserNameRemoteOperation to get userName from API (JSON). X-Git-Tag: oc-android-1.5.5~73^2~10 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/ddce8f769a1d6f863d4846ab193212e0218d14f5?ds=inline;hp=--cc OC-2541: Add GetUserNameRemoteOperation to get userName from API (JSON). --- ddce8f769a1d6f863d4846ab193212e0218d14f5 diff --git a/oc_framework/src/com/owncloud/android/oc_framework/operations/RemoteOperationResult.java b/oc_framework/src/com/owncloud/android/oc_framework/operations/RemoteOperationResult.java index 58accf9f..5c857fb1 100644 --- a/oc_framework/src/com/owncloud/android/oc_framework/operations/RemoteOperationResult.java +++ b/oc_framework/src/com/owncloud/android/oc_framework/operations/RemoteOperationResult.java @@ -33,6 +33,7 @@ import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.jackrabbit.webdav.DavException; +import org.json.JSONException; import com.owncloud.android.oc_framework.accounts.AccountUtils.AccountNotFoundException; import com.owncloud.android.oc_framework.network.CertificateCombinedException; @@ -89,7 +90,8 @@ public class RemoteOperationResult implements Serializable { ACCOUNT_EXCEPTION, ACCOUNT_NOT_NEW, ACCOUNT_NOT_THE_SAME, - INVALID_CHARACTER_IN_NAME + INVALID_CHARACTER_IN_NAME, + JSON_EXCEPTION } private boolean mSuccess = false; @@ -192,6 +194,9 @@ public class RemoteOperationResult implements Serializable { mCode = ResultCode.SSL_ERROR; } + } else if (e instanceof JSONException) { + mCode = ResultCode.JSON_EXCEPTION; + } else { mCode = ResultCode.UNKNOWN_ERROR; } @@ -294,6 +299,8 @@ public class RemoteOperationResult implements Serializable { } else if (mException instanceof AccountsException) { return "Exception while using account"; + } else if (mException instanceof JSONException) { + return "JSON exception"; } else { return "Unexpected exception"; } diff --git a/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/GetUserNameRemoteOperation.java b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/GetUserNameRemoteOperation.java new file mode 100644 index 00000000..d45a4066 --- /dev/null +++ b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/GetUserNameRemoteOperation.java @@ -0,0 +1,124 @@ + +/* ownCloud Android client application + * Copyright (C) 2012-2013 ownCloud Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.owncloud.android.oc_framework.operations.remote; + +import java.io.IOException; + +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.HeadMethod; +import org.apache.http.HttpStatus; +import org.json.JSONException; +import org.json.JSONObject; + +import android.util.Log; + +import com.owncloud.android.oc_framework.network.webdav.WebdavClient; +import com.owncloud.android.oc_framework.operations.RemoteOperation; +import com.owncloud.android.oc_framework.operations.RemoteOperationResult; + + +/** + * + * @author masensio + * + * Get the UserName for a SAML connection, from a JSON with the format: + * id + * display_name + * email + */ + +public class GetUserNameRemoteOperation extends RemoteOperation { + + private static final String TAG = GetUserNameRemoteOperation.class.getSimpleName(); + + // HEADER + private static final String TAG_HEADER = "OCS-APIREQUEST"; + private static final String TAG_HEADER_VALUE = "true"; + + // OCS Route + private static final String TAG_OCS_ROUTE = "index.php/ocs/cloud/user?format=json"; + + // JSON Node names + private static final String TAG_ID = "id"; + private static final String TAG_DISPLAY_NAME= "display_name"; + private static final String TAG_EMAIL= "email"; + + private String mUserName; + + + public String getUserName() { + return mUserName; + } + + private String mUrl; + + public GetUserNameRemoteOperation(String url) { + mUrl = url; + } + + @Override + protected RemoteOperationResult run(WebdavClient client) { + RemoteOperationResult result = null; + int status = -1; + + // Get Method + GetMethod get = new GetMethod(mUrl); + // Add the Header + get.addRequestHeader("application/xml", "Content-Type"); + get.addRequestHeader(TAG_HEADER, TAG_HEADER_VALUE); + + //Get the user + try { + status = client.executeMethod(get); + if(isSuccess(status)) { + Log.d(TAG, "Obtain RESPONSE"); + String response = get.getResponseBodyAsString(); + + Log.d(TAG, response); + + // Parse the response + JSONObject respJSON = new JSONObject(response); + String id = respJSON.getString(TAG_ID); + String display_name = respJSON.getString(TAG_DISPLAY_NAME); + String email = respJSON.getString(TAG_EMAIL); + + Log.d(TAG, "Response: " + id + "-" + display_name + "-" + email); + + } + } catch (HttpException e) { + result = new RemoteOperationResult(e); + e.printStackTrace(); + } catch (IOException e) { + result = new RemoteOperationResult(e); + e.printStackTrace(); + } catch (JSONException e) { + result = new RemoteOperationResult(e); + e.printStackTrace(); + } finally { + get.releaseConnection(); + } + + return result; + } + + private boolean isSuccess(int status) { + return (status == HttpStatus.SC_OK); + } + +}