d8e7626ba0bd09a6d5219a78aaa5424fde98c302
[pub/Android/ownCloud.git] /
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.oc_framework.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.oc_framework.network.webdav.WebdavClient;
31 import com.owncloud.android.oc_framework.operations.RemoteOperation;
32 import com.owncloud.android.oc_framework.operations.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 TAG_HEADER_OCS_API = "OCS-APIREQUEST";
50 private static final String TAG_HEADER_OCS_API_VALUE = "true";
51
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";
55
56 // OCS Route
57 private static final String TAG_OCS_ROUTE ="/index.php/ocs/cloud/user?format=json";
58
59 // JSON Node names
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";
65
66 private String mUrl;
67 private String mSessionCookie;
68 private String mUserName;
69
70 public String getUserName() {
71 return mUserName;
72 }
73
74
75 public GetUserNameRemoteOperation(String url, String sessioncookie) {
76 mUrl = url;
77 mSessionCookie = sessioncookie;
78 }
79
80 @Override
81 protected RemoteOperationResult run(WebdavClient client) {
82 RemoteOperationResult result = null;
83 int status = -1;
84
85 // Get Method
86 GetMethod get = new GetMethod(mUrl + TAG_OCS_ROUTE);
87 Log.d(TAG, "URL ------> " + mUrl + TAG_OCS_ROUTE);
88 // Add the Header
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);
92
93 //Get the user
94 try {
95 status = client.executeMethod(get);
96 if(isSuccess(status)) {
97 Log.d(TAG, "Obtain RESPONSE");
98 String response = get.getResponseBodyAsString();
99
100 Log.d(TAG, "GET RESPONSE.................... " + response);
101
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);
109
110 // Result
111 result = new RemoteOperationResult(isSuccess(status), status, (get != null ? get.getResponseHeaders() : null));
112 result.setUserName(displayName);
113
114 Log.d(TAG, "Response: " + id + " - " + displayName + " - " + email);
115
116 }
117 } catch (HttpException e) {
118 result = new RemoteOperationResult(e);
119 e.printStackTrace();
120 } catch (IOException e) {
121 result = new RemoteOperationResult(e);
122 e.printStackTrace();
123 } catch (JSONException e) {
124 result = new RemoteOperationResult(e);
125 e.printStackTrace();
126 } finally {
127 get.releaseConnection();
128 }
129
130 return result;
131 }
132
133 private boolean isSuccess(int status) {
134 return (status == HttpStatus.SC_OK);
135 }
136
137 }