1 package com
.owncloud
.android
.authenticator
.oauth2
.connection
;
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
;
16 import android
.util
.Log
;
19 * Implements HTTP POST communications with an oAuth2 server.
21 * @author SolidGear S.L.
24 public class ConnectorOAuth2
{
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;
30 private DefaultHttpClient httpClient
;
31 private HttpContext localContext
;
32 private String ConnectorOAuth2Url
;
34 public ConnectorOAuth2 (String destUrl
) {
36 setConnectorOAuth2Url(destUrl
);
39 public ConnectorOAuth2 () {
43 public String
getConnectorOAuth2Url() {
44 return ConnectorOAuth2Url
;
47 public void setConnectorOAuth2Url(String connectorOAuth2Url
) {
48 ConnectorOAuth2Url
= connectorOAuth2Url
;
52 * Starts the communication with the server.
54 * @param UrlEncodedFormEntity : parameters included in the POST call.
55 * @return String : data returned from the server in String format.
58 public String
connPost(UrlEncodedFormEntity data
) {
59 String dataOut
= null
;
60 HttpPost httpPost
= null
;
61 int responseCode
= -1;
62 HttpResponse response
= null
;
64 httpClient
.getParams().setParameter(ClientPNames
.COOKIE_POLICY
, CookiePolicy
.RFC_2109
);
66 if (ConnectorOAuth2Url
== null
) {
67 Log
.e(TAG
, "connPost error: destination URI could not be null");
72 Log
.e(TAG
, "connPost error: data to send to URI " + ConnectorOAuth2Url
+ "could not be null");
76 httpPost
= new HttpPost(ConnectorOAuth2Url
);
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
);
83 response
= httpClient
.execute(httpPost
,localContext
);
85 if (response
== null
) {
86 Log
.e(TAG
, "connPost error: response from uri " + ConnectorOAuth2Url
+ " is null");
90 responseCode
= response
.getStatusLine().getStatusCode();
92 if ((responseCode
!= 200)) {
93 Log
.e(TAG
, "connPost error: response from uri "+ ConnectorOAuth2Url
+ " returns status " + responseCode
);
97 dataOut
= EntityUtils
.toString(response
.getEntity());
99 } catch (Exception e
) {
100 Log
.e(TAG
, "connPost Exception: " + e
);
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();