11package com .pusher .client .util ;
22
3+ import com .pusher .client .AuthorizationFailureException ;
4+ import com .pusher .client .Authorizer ;
35import java .io .BufferedReader ;
46import java .io .DataOutputStream ;
57import java .io .IOException ;
810import java .net .HttpURLConnection ;
911import java .net .MalformedURLException ;
1012import java .net .URL ;
11- import java .net .URLEncoder ;
1213import java .util .HashMap ;
1314import java .util .Map ;
14-
1515import javax .net .ssl .HttpsURLConnection ;
1616
17- import com .pusher .client .AuthorizationFailureException ;
18- import com .pusher .client .Authorizer ;
19-
2017/**
2118 * Used to authenticate a {@link com.pusher.client.channel.PrivateChannel
2219 * private} or {@link com.pusher.client.channel.PresenceChannel presence}
@@ -36,8 +33,7 @@ public class HttpAuthorizer implements Authorizer {
3633
3734 private final URL endPoint ;
3835 private Map <String , String > mHeaders = new HashMap <String , String >();
39- private Map <String , String > mQueryStringParameters = new HashMap <String , String >();
40- private final String ENCODING_CHARACTER_SET = "UTF-8" ;
36+ private ConnectionFactory mConnectionFactory = null ;
4137
4238 /**
4339 * Creates a new authorizer.
@@ -48,12 +44,28 @@ public class HttpAuthorizer implements Authorizer {
4844 public HttpAuthorizer (final String endPoint ) {
4945 try {
5046 this .endPoint = new URL (endPoint );
47+ this .mConnectionFactory = new UrlEncodedConnectionFactory ();
5148 }
5249 catch (final MalformedURLException e ) {
5350 throw new IllegalArgumentException ("Could not parse authentication end point into a valid URL" , e );
5451 }
5552 }
5653
54+ /**
55+ * Creates a new authorizer.
56+ *
57+ * @param endPoint The endpoint to be called when authenticating.
58+ * @param connectionFactory a custom connection factory to be used for building the connection
59+ */
60+ public HttpAuthorizer (final String endPoint , final ConnectionFactory connectionFactory ) {
61+ try {
62+ this .endPoint = new URL (endPoint );
63+ this .mConnectionFactory = connectionFactory ;
64+ } catch (final MalformedURLException e ) {
65+ throw new IllegalArgumentException ("Could not parse authentication end point into a valid URL" , e );
66+ }
67+ }
68+
5769 /**
5870 * Set additional headers to be sent as part of the request.
5971 *
@@ -71,31 +83,16 @@ public Boolean isSSL() {
7183 return endPoint .getProtocol ().equals ("https" );
7284 }
7385
74- /**
75- * This methods is for passing extra parameters authentication that needs to
76- * be added to query string.
77- *
78- * @param queryStringParameters
79- * the query parameters
80- */
81- public void setQueryStringParameters (final HashMap <String , String > queryStringParameters ) {
82- mQueryStringParameters = queryStringParameters ;
83- }
84-
8586 @ Override
8687 public String authorize (final String channelName , final String socketId ) throws AuthorizationFailureException {
87-
8888 try {
89- final StringBuffer urlParameters = new StringBuffer ();
90- urlParameters .append ("channel_name=" ).append (URLEncoder .encode (channelName , ENCODING_CHARACTER_SET ));
91- urlParameters .append ("&socket_id=" ).append (URLEncoder .encode (socketId , ENCODING_CHARACTER_SET ));
92-
93- // Adding extra parameters supplied to be added to query string.
94- for (final String parameterName : mQueryStringParameters .keySet ()) {
95- urlParameters .append ("&" ).append (parameterName ).append ("=" );
96- urlParameters .append (URLEncoder .encode (mQueryStringParameters .get (parameterName ),
97- ENCODING_CHARACTER_SET ));
98- }
89+ mConnectionFactory .setChannelName (channelName );
90+ mConnectionFactory .setSocketId (socketId );
91+ String body = mConnectionFactory .getBody ();
92+
93+ final HashMap <String , String > defaultHeaders = new HashMap <String , String >();
94+ defaultHeaders .put ("Content-Type" , mConnectionFactory .getContentType ());
95+ defaultHeaders .put ("charset" , mConnectionFactory .getCharset ());
9996
10097 HttpURLConnection connection ;
10198 if (isSSL ()) {
@@ -108,22 +105,22 @@ public String authorize(final String channelName, final String socketId) throws
108105 connection .setDoInput (true );
109106 connection .setInstanceFollowRedirects (false );
110107 connection .setRequestMethod ("POST" );
111- connection .setRequestProperty ("Content-Type" , "application/x-www-form-urlencoded" );
112- connection .setRequestProperty ("charset" , "utf-8" );
113- connection .setRequestProperty ("Content-Length" ,
114- "" + Integer .toString (urlParameters .toString ().getBytes ().length ));
115108
116109 // Add in the user defined headers
117- for (final String headerName : mHeaders .keySet ()) {
118- final String headerValue = mHeaders .get (headerName );
110+ defaultHeaders .putAll (mHeaders );
111+ // Add in the Content-Length, so it can't be overwritten by mHeaders
112+ defaultHeaders .put ("Content-Length" ,"" + Integer .toString (body .getBytes ().length ));
113+
114+ for (final String headerName : defaultHeaders .keySet ()) {
115+ final String headerValue = defaultHeaders .get (headerName );
119116 connection .setRequestProperty (headerName , headerValue );
120117 }
121118
122119 connection .setUseCaches (false );
123120
124121 // Send request
125122 final DataOutputStream wr = new DataOutputStream (connection .getOutputStream ());
126- wr .writeBytes (urlParameters . toString () );
123+ wr .writeBytes (body );
127124 wr .flush ();
128125 wr .close ();
129126
0 commit comments