Skip to content

Commit ba47305

Browse files
authored
Merge pull request #170 from TylerMcCraw/master
Enable additional body types for POST authorization
2 parents 69508e7 + 3586917 commit ba47305

File tree

3 files changed

+126
-36
lines changed

3 files changed

+126
-36
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.pusher.client.util;
2+
3+
/**
4+
* Abstract factory to be used for
5+
* building HttpAuthorizer connections
6+
*/
7+
public abstract class ConnectionFactory {
8+
private String channelName;
9+
private String socketId;
10+
11+
public ConnectionFactory() {
12+
}
13+
14+
public abstract String getBody();
15+
16+
public abstract String getCharset();
17+
18+
public abstract String getContentType();
19+
20+
public String getChannelName() {
21+
return channelName;
22+
}
23+
24+
public void setChannelName(String channelName) {
25+
this.channelName = channelName;
26+
}
27+
28+
public String getSocketId() {
29+
return socketId;
30+
}
31+
32+
public void setSocketId(String socketId) {
33+
this.socketId = socketId;
34+
}
35+
}

src/main/java/com/pusher/client/util/HttpAuthorizer.java

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.pusher.client.util;
22

3+
import com.pusher.client.AuthorizationFailureException;
4+
import com.pusher.client.Authorizer;
35
import java.io.BufferedReader;
46
import java.io.DataOutputStream;
57
import java.io.IOException;
@@ -8,15 +10,10 @@
810
import java.net.HttpURLConnection;
911
import java.net.MalformedURLException;
1012
import java.net.URL;
11-
import java.net.URLEncoder;
1213
import java.util.HashMap;
1314
import java.util.Map;
14-
1515
import 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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.pusher.client.util;
2+
3+
import java.io.UnsupportedEncodingException;
4+
import java.net.URLEncoder;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* Form URL-Encoded Connection Factory
10+
*
11+
* Allows HttpAuthorizer to write URL parameters to the connection
12+
*/
13+
public class UrlEncodedConnectionFactory extends ConnectionFactory {
14+
15+
private Map<String, String> mQueryStringParameters = new HashMap<String, String>();
16+
17+
/**
18+
* Create a Form URL-encoded factory
19+
*/
20+
public UrlEncodedConnectionFactory() {
21+
}
22+
23+
/**
24+
* Create a Form URL-encoded factory
25+
*
26+
* @param queryStringParameters extra parameters that need to be added to query string.
27+
*/
28+
public UrlEncodedConnectionFactory(final Map<String, String> queryStringParameters) {
29+
this.mQueryStringParameters = queryStringParameters;
30+
}
31+
32+
@Override
33+
public String getCharset() {
34+
return "UTF-8";
35+
}
36+
37+
@Override
38+
public String getContentType() {
39+
return "application/x-www-form-urlencoded";
40+
}
41+
42+
public String getBody() {
43+
final StringBuffer urlParameters = new StringBuffer();
44+
try {
45+
urlParameters.append("channel_name=").append(URLEncoder.encode(getChannelName(), getCharset()));
46+
urlParameters.append("&socket_id=").append(URLEncoder.encode(getSocketId(), getCharset()));
47+
48+
// Adding extra parameters supplied to be added to query string.
49+
for (final String parameterName : mQueryStringParameters.keySet()) {
50+
urlParameters.append("&").append(parameterName).append("=");
51+
urlParameters.append(URLEncoder.encode(mQueryStringParameters.get(parameterName), getCharset()));
52+
}
53+
} catch (UnsupportedEncodingException e) {
54+
e.printStackTrace();
55+
}
56+
return urlParameters.toString();
57+
}
58+
}

0 commit comments

Comments
 (0)