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.commons.httpclient.methods.HeadMethod;
25 import org.apache.http.HttpStatus;
26 import org.json.JSONException;
27 import org.json.JSONObject;
29 import android.util.Log;
31 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
32 import com.owncloud.android.oc_framework.operations.RemoteOperation;
33 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
40 * Get the UserName for a SAML connection, from a JSON with the format:
46 public class GetUserNameRemoteOperation extends RemoteOperation {
48 private static final String TAG = GetUserNameRemoteOperation.class.getSimpleName();
51 private static final String TAG_HEADER = "OCS-APIREQUEST";
52 private static final String TAG_HEADER_VALUE = "true";
55 private static final String TAG_OCS_ROUTE = "index.php/ocs/cloud/user?format=json";
58 private static final String TAG_ID = "id";
59 private static final String TAG_DISPLAY_NAME= "display_name";
60 private static final String TAG_EMAIL= "email";
62 private String mUserName;
65 public String getUserName() {
71 public GetUserNameRemoteOperation(String url) {
76 protected RemoteOperationResult run(WebdavClient client) {
77 RemoteOperationResult result = null;
81 GetMethod get = new GetMethod(mUrl);
83 get.addRequestHeader("application/xml", "Content-Type");
84 get.addRequestHeader(TAG_HEADER, TAG_HEADER_VALUE);
88 status = client.executeMethod(get);
89 if(isSuccess(status)) {
90 Log.d(TAG, "Obtain RESPONSE");
91 String response = get.getResponseBodyAsString();
96 JSONObject respJSON = new JSONObject(response);
97 String id = respJSON.getString(TAG_ID);
98 String display_name = respJSON.getString(TAG_DISPLAY_NAME);
99 String email = respJSON.getString(TAG_EMAIL);
101 Log.d(TAG, "Response: " + id + "-" + display_name + "-" + email);
104 } catch (HttpException e) {
105 result = new RemoteOperationResult(e);
107 } catch (IOException e) {
108 result = new RemoteOperationResult(e);
110 } catch (JSONException e) {
111 result = new RemoteOperationResult(e);
114 get.releaseConnection();
120 private boolean isSuccess(int status) {
121 return (status == HttpStatus.SC_OK);