2 /* ownCloud Android client application
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com.owncloud.android.oc_framework.operations.remote;
20 import java.io.IOException;
22 import org.apache.commons.httpclient.HttpException;
23 import org.apache.commons.httpclient.methods.GetMethod;
24 import org.apache.http.HttpStatus;
25 import org.json.JSONException;
26 import org.json.JSONObject;
28 import android.util.Log;
30 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
31 import com.owncloud.android.oc_framework.operations.RemoteOperation;
32 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
38 * Get the UserName for a SAML connection, from a JSON with the format:
44 public class GetUserNameRemoteOperation extends RemoteOperation {
46 private static final String TAG = GetUserNameRemoteOperation.class.getSimpleName();
49 private static final String HEADER_OCS_API = "OCS-APIREQUEST";
50 private static final String HEADER_OCS_API_VALUE = "true";
53 private static final String OCS_ROUTE ="/index.php/ocs/cloud/user?format=json";
56 private static final String NODE_OCS = "ocs";
57 private static final String NODE_DATA = "data";
58 private static final String NODE_ID = "id";
59 private static final String NODE_DISPLAY_NAME= "display-name";
60 private static final String NODE_EMAIL= "email";
62 private String mUserName;
64 public String getUserName() {
69 public GetUserNameRemoteOperation() {
73 protected RemoteOperationResult run(WebdavClient client) {
74 RemoteOperationResult result = null;
78 GetMethod get = new GetMethod(client.getBaseUri() + OCS_ROUTE);
79 Log.d(TAG, "URL ------> " + client.getBaseUri() + OCS_ROUTE);
81 get.addRequestHeader(HEADER_OCS_API, HEADER_OCS_API_VALUE);
85 status = client.executeMethod(get);
86 if(isSuccess(status)) {
87 Log.d(TAG, "Obtain RESPONSE");
88 String response = get.getResponseBodyAsString();
90 Log.d(TAG, "GET RESPONSE.................... " + response);
93 JSONObject respJSON = new JSONObject(response);
94 JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
95 JSONObject respData = respOCS.getJSONObject(NODE_DATA);
96 String id = respData.getString(NODE_ID);
97 String displayName = respData.getString(NODE_DISPLAY_NAME);
98 String email = respData.getString(NODE_EMAIL);
101 result = new RemoteOperationResult(isSuccess(status), status, (get != null ? get.getResponseHeaders() : null));
102 mUserName = displayName;
104 Log.d(TAG, "Response: " + id + " - " + displayName + " - " + email);
107 } catch (HttpException e) {
108 result = new RemoteOperationResult(e);
110 } catch (IOException e) {
111 result = new RemoteOperationResult(e);
113 } catch (JSONException e) {
114 result = new RemoteOperationResult(e);
117 get.releaseConnection();
123 private boolean isSuccess(int status) {
124 return (status == HttpStatus.SC_OK);