Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/main/java/com/pusher/client/util/ConnectionFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.pusher.client.util;

/**
* Abstract factory to be used for
* building HttpAuthorizer connections
*/
public abstract class ConnectionFactory {
private String channelName;
private String socketId;

public ConnectionFactory() {
}

public abstract String getBody();

public abstract String getCharset();

public abstract String getContentType();

public String getChannelName() {
return channelName;
}

public void setChannelName(String channelName) {
this.channelName = channelName;
}

public String getSocketId() {
return socketId;
}

public void setSocketId(String socketId) {
this.socketId = socketId;
}
}
69 changes: 33 additions & 36 deletions src/main/java/com/pusher/client/util/HttpAuthorizer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.pusher.client.util;

import com.pusher.client.AuthorizationFailureException;
import com.pusher.client.Authorizer;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
Expand All @@ -8,15 +10,10 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;

import com.pusher.client.AuthorizationFailureException;
import com.pusher.client.Authorizer;

/**
* Used to authenticate a {@link com.pusher.client.channel.PrivateChannel
* private} or {@link com.pusher.client.channel.PresenceChannel presence}
Expand All @@ -36,8 +33,7 @@ public class HttpAuthorizer implements Authorizer {

private final URL endPoint;
private Map<String, String> mHeaders = new HashMap<String, String>();
private Map<String, String> mQueryStringParameters = new HashMap<String, String>();
private final String ENCODING_CHARACTER_SET = "UTF-8";
private ConnectionFactory mConnectionFactory = null;

/**
* Creates a new authorizer.
Expand All @@ -48,12 +44,28 @@ public class HttpAuthorizer implements Authorizer {
public HttpAuthorizer(final String endPoint) {
try {
this.endPoint = new URL(endPoint);
this.mConnectionFactory = new UrlEncodedConnectionFactory();
}
catch (final MalformedURLException e) {
throw new IllegalArgumentException("Could not parse authentication end point into a valid URL", e);
}
}

/**
* Creates a new authorizer.
*
* @param endPoint The endpoint to be called when authenticating.
* @param connectionFactory a custom connection factory to be used for building the connection
*/
public HttpAuthorizer(final String endPoint, final ConnectionFactory connectionFactory) {
try {
this.endPoint = new URL(endPoint);
this.mConnectionFactory = connectionFactory;
} catch (final MalformedURLException e) {
throw new IllegalArgumentException("Could not parse authentication end point into a valid URL", e);
}
}

/**
* Set additional headers to be sent as part of the request.
*
Expand All @@ -71,31 +83,16 @@ public Boolean isSSL() {
return endPoint.getProtocol().equals("https");
}

/**
* This methods is for passing extra parameters authentication that needs to
* be added to query string.
*
* @param queryStringParameters
* the query parameters
*/
public void setQueryStringParameters(final HashMap<String, String> queryStringParameters) {
mQueryStringParameters = queryStringParameters;
}

@Override
public String authorize(final String channelName, final String socketId) throws AuthorizationFailureException {

try {
final StringBuffer urlParameters = new StringBuffer();
urlParameters.append("channel_name=").append(URLEncoder.encode(channelName, ENCODING_CHARACTER_SET));
urlParameters.append("&socket_id=").append(URLEncoder.encode(socketId, ENCODING_CHARACTER_SET));

// Adding extra parameters supplied to be added to query string.
for (final String parameterName : mQueryStringParameters.keySet()) {
urlParameters.append("&").append(parameterName).append("=");
urlParameters.append(URLEncoder.encode(mQueryStringParameters.get(parameterName),
ENCODING_CHARACTER_SET));
}
mConnectionFactory.setChannelName(channelName);
mConnectionFactory.setSocketId(socketId);
String body = mConnectionFactory.getBody();

final HashMap<String, String> defaultHeaders = new HashMap<String, String>();
defaultHeaders.put("Content-Type", mConnectionFactory.getContentType());
defaultHeaders.put("charset", mConnectionFactory.getCharset());

HttpURLConnection connection;
if (isSSL()) {
Expand All @@ -108,22 +105,22 @@ public String authorize(final String channelName, final String socketId) throws
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length",
"" + Integer.toString(urlParameters.toString().getBytes().length));

// Add in the user defined headers
for (final String headerName : mHeaders.keySet()) {
final String headerValue = mHeaders.get(headerName);
defaultHeaders.putAll(mHeaders);
// Add in the Content-Length, so it can't be overwritten by mHeaders
defaultHeaders.put("Content-Length","" + Integer.toString(body.getBytes().length));

for (final String headerName : defaultHeaders.keySet()) {
final String headerValue = defaultHeaders.get(headerName);
connection.setRequestProperty(headerName, headerValue);
}

connection.setUseCaches(false);

// Send request
final DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters.toString());
wr.writeBytes(body);
wr.flush();
wr.close();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.pusher.client.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
* Form URL-Encoded Connection Factory
*
* Allows HttpAuthorizer to write URL parameters to the connection
*/
public class UrlEncodedConnectionFactory extends ConnectionFactory {

private Map<String, String> mQueryStringParameters = new HashMap<String, String>();

/**
* Create a Form URL-encoded factory
*/
public UrlEncodedConnectionFactory() {
}

/**
* Create a Form URL-encoded factory
*
* @param queryStringParameters extra parameters that need to be added to query string.
*/
public UrlEncodedConnectionFactory(final Map<String, String> queryStringParameters) {
this.mQueryStringParameters = queryStringParameters;
}

@Override
public String getCharset() {
return "UTF-8";
}

@Override
public String getContentType() {
return "application/x-www-form-urlencoded";
}

public String getBody() {
final StringBuffer urlParameters = new StringBuffer();
try {
urlParameters.append("channel_name=").append(URLEncoder.encode(getChannelName(), getCharset()));
urlParameters.append("&socket_id=").append(URLEncoder.encode(getSocketId(), getCharset()));

// Adding extra parameters supplied to be added to query string.
for (final String parameterName : mQueryStringParameters.keySet()) {
urlParameters.append("&").append(parameterName).append("=");
urlParameters.append(URLEncoder.encode(mQueryStringParameters.get(parameterName), getCharset()));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return urlParameters.toString();
}
}