001package com.pusher.client.util;
002
003import com.pusher.client.AuthenticationFailureException;
004import com.pusher.client.UserAuthenticator;
005
006import java.io.IOException;
007
008/**
009 * Used to authenticate the user when signing in on a Pusher Channels connection.
010 *
011 * <p>
012 * Makes an HTTP request to a defined HTTP endpoint. Expects a user authentication
013 * token to be returned.
014 * </p>
015 *
016 * <p>
017 * For more information see the <a
018 * href="http://pusher.com/docs/authenticating_users">Authenticating Users
019 * documentation</a>.
020 */
021public class HttpUserAuthenticator extends BaseHttpAuthClient implements UserAuthenticator {
022
023    /**
024     * Creates a new user authenticator.
025     *
026     * @param endPoint The endpoint to be called when authenticating.
027     */
028    public HttpUserAuthenticator(final String endPoint) {
029        super(endPoint);
030    }
031
032    /**
033     * Creates a new user authenticator.
034     *
035     * @param endPoint          The endpoint to be called when authenticating.
036     * @param connectionFactory a custom connection factory to be used for building the connection
037     */
038    public HttpUserAuthenticator(final String endPoint, final ConnectionFactory connectionFactory) {
039        super(endPoint, connectionFactory);
040    }
041
042    @Override
043    public String authenticate(final String socketId) throws AuthenticationFailureException {
044        mConnectionFactory.setSocketId(socketId);
045        return performAuthRequest();
046    }
047
048    @Override
049    protected RuntimeException authFailureException(String msg) {
050        return new AuthenticationFailureException(msg);
051    }
052
053    @Override
054    protected RuntimeException authFailureException(IOException e) {
055        return new AuthenticationFailureException(e);
056    }
057}