001package com.pusher.client.util;
002
003import com.pusher.client.AuthorizationFailureException;
004import com.pusher.client.ChannelAuthorizer;
005
006import java.io.IOException;
007
008/**
009 * Used to authorize a {@link com.pusher.client.channel.PrivateChannel
010 * private} or {@link com.pusher.client.channel.PresenceChannel presence}
011 * channel subscription.
012 *
013 * <p>
014 * Makes an HTTP request to a defined HTTP endpoint. Expects a channel authorization
015 * token to be returned.
016 * </p>
017 *
018 * <p>
019 * For more information see the <a
020 * href="http://pusher.com/docs/authorizing_users">Authorizing Users
021 * documentation</a>.
022 */
023
024public class HttpChannelAuthorizer extends BaseHttpAuthClient implements ChannelAuthorizer {
025
026    /**
027     * Creates a new channel authorizer.
028     *
029     * @param endPoint The endpoint to be called when authorizing.
030     */
031    public HttpChannelAuthorizer(final String endPoint) {
032        super(endPoint);
033    }
034
035    /**
036     * Creates a new channel authorizer.
037     *
038     * @param endPoint          The endpoint to be called when authorizing.
039     * @param connectionFactory a custom connection factory to be used for building the connection
040     */
041    public HttpChannelAuthorizer(final String endPoint, final ConnectionFactory connectionFactory) {
042        super(endPoint, connectionFactory);
043    }
044
045    @Override
046    public String authorize(final String channelName, final String socketId) throws AuthorizationFailureException {
047        mConnectionFactory.setChannelName(channelName);
048        mConnectionFactory.setSocketId(socketId);
049        return performAuthRequest();
050    }
051
052    @Override
053    protected RuntimeException authFailureException(String msg) {
054        return new AuthorizationFailureException(msg);
055    }
056
057    @Override
058    protected RuntimeException authFailureException(IOException e) {
059        return new AuthorizationFailureException(e);
060    }
061}