789b58518957f4a8940126a4e876827a73502dd8
[pub/Android/ownCloud.git] / src / com / owncloud / android / authenticator / oauth2 / connection / ConnectorOAuth2.java
1 package com.owncloud.android.authenticator.oauth2.connection;
2
3 import org.apache.http.HttpResponse;
4 import org.apache.http.client.entity.UrlEncodedFormEntity;
5 import org.apache.http.client.methods.HttpPost;
6 import org.apache.http.client.params.ClientPNames;
7 import org.apache.http.client.params.CookiePolicy;
8 import org.apache.http.impl.client.DefaultHttpClient;
9 import org.apache.http.params.BasicHttpParams;
10 import org.apache.http.params.HttpConnectionParams;
11 import org.apache.http.params.HttpParams;
12 import org.apache.http.protocol.BasicHttpContext;
13 import org.apache.http.protocol.HttpContext;
14 import org.apache.http.util.EntityUtils;
15
16 import android.util.Log;
17
18 /**
19 * Implements HTTP POST communications with an oAuth2 server.
20 *
21 * @author SolidGear S.L.
22 *
23 */
24 public class ConnectorOAuth2 {
25
26 private static final String TAG = "ConnectorOAuth2";
27 /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */
28 private static final int TRY_CONNECTION_TIMEOUT = 5000;
29
30 private DefaultHttpClient httpClient;
31 private HttpContext localContext;
32 private String ConnectorOAuth2Url;
33
34 public ConnectorOAuth2 (String destUrl) {
35 prepareConn();
36 setConnectorOAuth2Url(destUrl);
37 }
38
39 public ConnectorOAuth2 () {
40 prepareConn();
41 }
42
43 public String getConnectorOAuth2Url() {
44 return ConnectorOAuth2Url;
45 }
46
47 public void setConnectorOAuth2Url(String connectorOAuth2Url) {
48 ConnectorOAuth2Url = connectorOAuth2Url;
49 }
50
51 /**
52 * Starts the communication with the server.
53 *
54 * @param UrlEncodedFormEntity : parameters included in the POST call.
55 * @return String : data returned from the server in String format.
56 */
57
58 public String connPost(UrlEncodedFormEntity data) {
59 String dataOut = null;
60 HttpPost httpPost = null;
61 int responseCode = -1;
62 HttpResponse response = null;
63
64 httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
65
66 if (ConnectorOAuth2Url == null) {
67 Log.e(TAG, "connPost error: destination URI could not be null");
68 return null;
69 }
70
71 if (data == null){
72 Log.e(TAG, "connPost error: data to send to URI " + ConnectorOAuth2Url + "could not be null");
73 return null;
74 }
75
76 httpPost = new HttpPost(ConnectorOAuth2Url);
77
78 httpPost.setHeader("Accept","text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
79 httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
80 httpPost.setEntity((UrlEncodedFormEntity) data);
81
82 try {
83 response = httpClient.execute(httpPost,localContext);
84
85 if (response == null) {
86 Log.e(TAG, "connPost error: response from uri " + ConnectorOAuth2Url + " is null");
87 return null;
88 }
89
90 responseCode = response.getStatusLine().getStatusCode();
91
92 if ((responseCode != 200)) {
93 Log.e(TAG, "connPost error: response from uri "+ ConnectorOAuth2Url + " returns status " + responseCode);
94 return null;
95 }
96
97 dataOut = EntityUtils.toString(response.getEntity());
98
99 } catch (Exception e) {
100 Log.e(TAG, "connPost Exception: " + e);
101 }
102
103 return dataOut;
104 }
105
106 private void prepareConn () {
107 HttpParams localParams = new BasicHttpParams();
108 HttpConnectionParams.setConnectionTimeout(localParams, TRY_CONNECTION_TIMEOUT);
109 HttpConnectionParams.setSoTimeout(localParams, TRY_CONNECTION_TIMEOUT);
110 httpClient = new DefaultHttpClient(localParams);
111 localContext = new BasicHttpContext();
112 }
113 }