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 TAG_HEADER_OCS_API = "OCS-APIREQUEST";
50 private static final String TAG_HEADER_OCS_API_VALUE = "true";
52 private static final String TAG_HEADER_CONTENT = "Content-Type";
53 private static final String TAG_HEADER_CONTENT_VALUE = "application/xml";
54 private static final String TAG_HEADER_COOKIE = "Cookie";
57 private static final String TAG_OCS_ROUTE ="/index.php/ocs/cloud/user?format=json";
60 private static final String TAG_OCS = "ocs";
61 private static final String TAG_DATA = "data";
62 private static final String TAG_ID = "id";
63 private static final String TAG_DISPLAY_NAME= "display-name";
64 private static final String TAG_EMAIL= "email";
67 private String mSessionCookie;
68 private String mUserName;
70 public String getUserName() {
75 public GetUserNameRemoteOperation(String url, String sessioncookie) {
77 mSessionCookie = sessioncookie;
81 protected RemoteOperationResult run(WebdavClient client) {
82 RemoteOperationResult result = null;
86 GetMethod get = new GetMethod(mUrl + TAG_OCS_ROUTE);
87 Log.d(TAG, "URL ------> " + mUrl + TAG_OCS_ROUTE);
89 get.addRequestHeader(TAG_HEADER_CONTENT, TAG_HEADER_CONTENT_VALUE);
90 get.addRequestHeader(TAG_HEADER_OCS_API, TAG_HEADER_OCS_API_VALUE);
91 get.setRequestHeader(TAG_HEADER_COOKIE, mSessionCookie);
95 status = client.executeMethod(get);
96 if(isSuccess(status)) {
97 Log.d(TAG, "Obtain RESPONSE");
98 String response = get.getResponseBodyAsString();
100 Log.d(TAG, "GET RESPONSE.................... " + response);
102 // Parse the response
103 JSONObject respJSON = new JSONObject(response);
104 JSONObject respOCS = respJSON.getJSONObject(TAG_OCS);
105 JSONObject respData = respOCS.getJSONObject(TAG_DATA);
106 String id = respData.getString(TAG_ID);
107 String displayName = respData.getString(TAG_DISPLAY_NAME);
108 String email = respData.getString(TAG_EMAIL);
111 result = new RemoteOperationResult(isSuccess(status), status, (get != null ? get.getResponseHeaders() : null));
112 result.setUserName(displayName);
114 Log.d(TAG, "Response: " + id + " - " + displayName + " - " + email);
117 } catch (HttpException e) {
118 result = new RemoteOperationResult(e);
120 } catch (IOException e) {
121 result = new RemoteOperationResult(e);
123 } catch (JSONException e) {
124 result = new RemoteOperationResult(e);
127 get.releaseConnection();
133 private boolean isSuccess(int status) {
134 return (status == HttpStatus.SC_OK);