d45a4066e0b57195456778f2d0dee437ca885cf8
[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.commons.httpclient.methods.HeadMethod;
25 import org.apache.http.HttpStatus;
26 import org.json.JSONException;
27 import org.json.JSONObject;
28
29 import android.util.Log;
30
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;
34
35
36 /**
37 *
38 * @author masensio
39 *
40 * Get the UserName for a SAML connection, from a JSON with the format:
41 * id
42 * display_name
43 * email
44 */
45
46 public class GetUserNameRemoteOperation extends RemoteOperation {
47
48 private static final String TAG = GetUserNameRemoteOperation.class.getSimpleName();
49
50 // HEADER
51 private static final String TAG_HEADER = "OCS-APIREQUEST";
52 private static final String TAG_HEADER_VALUE = "true";
53
54 // OCS Route
55 private static final String TAG_OCS_ROUTE = "index.php/ocs/cloud/user?format=json";
56
57 // JSON Node names
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";
61
62 private String mUserName;
63
64
65 public String getUserName() {
66 return mUserName;
67 }
68
69 private String mUrl;
70
71 public GetUserNameRemoteOperation(String url) {
72 mUrl = url;
73 }
74
75 @Override
76 protected RemoteOperationResult run(WebdavClient client) {
77 RemoteOperationResult result = null;
78 int status = -1;
79
80 // Get Method
81 GetMethod get = new GetMethod(mUrl);
82 // Add the Header
83 get.addRequestHeader("application/xml", "Content-Type");
84 get.addRequestHeader(TAG_HEADER, TAG_HEADER_VALUE);
85
86 //Get the user
87 try {
88 status = client.executeMethod(get);
89 if(isSuccess(status)) {
90 Log.d(TAG, "Obtain RESPONSE");
91 String response = get.getResponseBodyAsString();
92
93 Log.d(TAG, response);
94
95 // Parse the response
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);
100
101 Log.d(TAG, "Response: " + id + "-" + display_name + "-" + email);
102
103 }
104 } catch (HttpException e) {
105 result = new RemoteOperationResult(e);
106 e.printStackTrace();
107 } catch (IOException e) {
108 result = new RemoteOperationResult(e);
109 e.printStackTrace();
110 } catch (JSONException e) {
111 result = new RemoteOperationResult(e);
112 e.printStackTrace();
113 } finally {
114 get.releaseConnection();
115 }
116
117 return result;
118 }
119
120 private boolean isSuccess(int status) {
121 return (status == HttpStatus.SC_OK);
122 }
123
124 }