9f5c0ff604b81a9cdf44dc1e970665c8959fd944
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / lib / operations / remote / GetUserNameRemoteOperation.java
1
2 /* ownCloud Android client application
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package com.owncloud.android.lib.operations.remote;
19
20 import java.io.IOException;
21
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;
27
28 import android.util.Log;
29
30 import com.owncloud.android.lib.network.OwnCloudClient;
31 import com.owncloud.android.lib.operations.common.RemoteOperation;
32 import com.owncloud.android.lib.operations.common.RemoteOperationResult;
33
34
35 /**
36 * @author masensio
37 *
38 * Get the UserName for a SAML connection, from a JSON with the format:
39 * id
40 * display-name
41 * email
42 */
43
44 public class GetUserNameRemoteOperation extends RemoteOperation {
45
46 private static final String TAG = GetUserNameRemoteOperation.class.getSimpleName();
47
48 // HEADER
49 private static final String HEADER_OCS_API = "OCS-APIREQUEST";
50 private static final String HEADER_OCS_API_VALUE = "true";
51
52 // OCS Route
53 private static final String OCS_ROUTE ="/index.php/ocs/cloud/user?format=json";
54
55 // JSON Node names
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";
61
62 private String mUserName;
63
64 public String getUserName() {
65 return mUserName;
66 }
67
68
69 public GetUserNameRemoteOperation() {
70 }
71
72 @Override
73 protected RemoteOperationResult run(OwnCloudClient client) {
74 RemoteOperationResult result = null;
75 int status = -1;
76
77 // Get Method
78 GetMethod get = new GetMethod(client.getBaseUri() + OCS_ROUTE);
79 Log.d(TAG, "URL ------> " + client.getBaseUri() + OCS_ROUTE);
80 // Add the Header
81 get.addRequestHeader(HEADER_OCS_API, HEADER_OCS_API_VALUE);
82
83 //Get the user
84 try {
85 status = client.executeMethod(get);
86 if(isSuccess(status)) {
87 Log.d(TAG, "Obtain RESPONSE");
88 String response = get.getResponseBodyAsString();
89
90 Log.d(TAG, "GET RESPONSE.................... " + response);
91
92 // Parse the 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);
99
100 // Result
101 result = new RemoteOperationResult(isSuccess(status), status, (get != null ? get.getResponseHeaders() : null));
102 mUserName = displayName;
103
104 Log.d(TAG, "Response: " + id + " - " + displayName + " - " + email);
105
106 }
107 } catch (HttpException e) {
108 result = new RemoteOperationResult(e);
109 e.printStackTrace();
110 } catch (IOException e) {
111 result = new RemoteOperationResult(e);
112 e.printStackTrace();
113 } catch (JSONException e) {
114 result = new RemoteOperationResult(e);
115 e.printStackTrace();
116 } finally {
117 get.releaseConnection();
118 }
119
120 return result;
121 }
122
123 private boolean isSuccess(int status) {
124 return (status == HttpStatus.SC_OK);
125 }
126
127 }