Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.pubnub.api;
  
  import java.util.Date;
  import java.util.UUID;
 
 
 import  org.json.JSONArray;
 import  org.json.JSONException;
 import  org.json.JSONObject;
 
 import static com.pubnub.api.PubnubError.*;

Pubnub object facilitates querying channels for messages and listening on channels for presence/message events

Author(s):
Pubnub
 
 
 public class Pubnub extends PubnubCore {

    
Pubnub Constructor

Parameters:
publish_key Publish Key
subscribe_key Subscribe Key
secret_key Secret Key
cipher_key Cipher Key
ssl_on SSL on ?
 
     public Pubnub(String publish_keyString subscribe_keyString secret_key,
                   String cipher_keyboolean ssl_on) {
         super(publish_keysubscribe_keysecret_keycipher_keyssl_on);
     }

    
Pubnub Constructor

Parameters:
publish_key Publish key
subscribe_key Subscribe Key
secret_key Secret Key
ssl_on SSL on ?
 
     public Pubnub(String publish_keyString subscribe_keyString secret_key,
                   boolean ssl_on) {
         super(publish_keysubscribe_keysecret_key""ssl_on);
     }

    
Pubnub Constructor

Parameters:
publish_key Publish Key
subscribe_key Subscribe Key
 
     public Pubnub(String publish_keyString subscribe_key) {
         super(publish_keysubscribe_key""""false);
     }

    

Parameters:
publish_key Publish Key
subscribe_key Subscribe Key
ssl
 
     public Pubnub(String publish_keyString subscribe_keyboolean ssl) {
         super(publish_keysubscribe_key""""ssl);
     }

    

Parameters:
publish_key
subscribe_key
secret_key
 
     public Pubnub(String publish_keyString subscribe_keyString secret_key) {
         super(publish_keysubscribe_keysecret_key""false);
     }


    
Constructor for Pubnub Class

Parameters:
publish_key Publish Key
subscribe_key Subscribe Key
secret_key Secret Key
cipher_key Cipher Key
ssl_on SSL enabled ?
initialization_vector Initialization vector
    public Pubnub(String publish_keyString subscribe_key,
                  String secret_keyString cipher_keyboolean ssl_onString initialization_vector) {
        super(publish_keysubscribe_keysecret_keycipher_keyssl_oninitialization_vector);
    }

    
Sets value for UUID

Parameters:
uuid UUID value for Pubnub client
    public void setUUID(UUID uuid) {
        this. = uuid.toString();
    }
    protected String uuid() {
        return java.util.UUID.randomUUID().toString();
    }

    
This method sets timeout value for subscribe/presence. Default value is 310000 milliseconds i.e. 310 seconds

Parameters:
timeout Timeout value in milliseconds for subscribe/presence
    public void setSubscribeTimeout(int timeout) {
        super.setSubscribeTimeout(timeout);
    }

    
This method returns timeout value for subscribe/presence.

Returns:
Timeout value in milliseconds for subscribe/presence
    public int getSubscribeTimeout() {
        return super.getSubscribeTimeout();
    }

    
This method set timeout value for non subscribe operations like publish, history, hereNow. Default value is 15000 milliseconds i.e. 15 seconds.

Parameters:
timeout Timeout value in milliseconds for Non subscribe operations like publish, history, hereNow
    public void setNonSubscribeTimeout(int timeout) {
        super.setNonSubscribeTimeout(timeout);
    }
    
This method returns timeout value for non subscribe operations like publish, history, hereNow

Returns:
Timeout value in milliseconds for for Non subscribe operations like publish, history, hereNow
    public int getNonSubscribeTimeout() {
        return super.getNonSubscribeTimeout();
    }
    protected String getUserAgent() {
        return "Java/" + ;
    }

    
Send a message to a channel.

Deprecated:
as of version 3.5.2 and will be removed with 3.6.0 . Methods accepting Hashtable as arguments have been deprecated.
Parameters:
args Hashtable containing channel name, message.
callback object of sub class of Callback class
    @Deprecated
    @Override
    public void publish(Hashtable argsCallback callback) {
        super.publish(argscallback);
    }

    
Send a message to a channel.

Deprecated:
As of version 3.5.2 . Will be removed in 3.6.0 . Methods accepting Hashtable as arguments have been deprecated.
Parameters:
args Hashtable containing channel name, message, callback
    @Deprecated
    @Override
    public void publish(Hashtable args) {
        super.publish(args);
    }

    
Read DetailedHistory for a channel.

Deprecated:
as of version 3.5.2 and will be removed with 3.6.0 . Replaced by history(String channel, long start, long end, int count, boolean reverse, Callback callback)
Parameters:
channel Channel name for which detailed history is required
start Start time
end End time
count Upper limit on number of messages to be returned
reverse True if messages need to be in reverse order
callback Callback
    @Deprecated
    @Override
    public void detailedHistory(final String channellong startlong end,
                                int countboolean reversefinal Callback callback) {
        super.detailedHistory(channelstartendcountreversecallback);
    }

    
Read History for a channel.

Parameters:
channel Channel name for which detailed history is required
start Start time
end End time
count Upper limit on number of messages to be returned
reverse True if messages need to be in reverse order
callback Callback
    public void history(final String channellong startlong endint count,
                        boolean reversefinal Callback callback) {
        super.detailedHistory(channelstartendcountreversecallback);
    }

    
Read DetailedHistory for a channel.

Deprecated:
as of version 3.5.2 and will be removed with 3.6.0 . Replaced by history(String channel, long start, boolean reverse, Callback callback)
Parameters:
channel Channel name for which detailed history is required
start Start time
reverse True if messages need to be in reverse order
callback Callback
    @Deprecated
    @Override
    public void detailedHistory(String channellong startboolean reverse,
                                Callback callback) {
        super.detailedHistory(channelstartreversecallback);
    }

    
Read history for a channel.

Parameters:
channel Channel name for which detailed history is required
start Start time
reverse True if messages need to be in reverse order
callback Callback
    public void history(String channellong startboolean reverse,
                        Callback callback) {
        super.detailedHistory(channelstartreversecallback);
    }

    
Read DetailedHistory for a channel.

Deprecated:
as of version 3.5.2 and will be removed with 3.6.0 . Replaced by history(String channel, long start, long end, Callback callback)
Parameters:
channel Channel name for which detailed history is required
start Start time
end End time
callback Callback
    @Deprecated
    @Override
    public void detailedHistory(String channellong startlong end,
                                Callback callback) {
        super.detailedHistory(channelstartendcallback);
    }

    
Read History for a channel.

Parameters:
channel Channel name for which detailed history is required
start Start time
end End time
callback Callback
    public void history(String channellong startlong endCallback callback) {
        super.detailedHistory(channelstartendcallback);
    }

    
Read DetailedHistory for a channel.

Deprecated:
as of version 3.5.2 and will be removed with 3.6.0 . Replaced by history(String channel, long start, long end, boolean reverse, Callback callback)
Parameters:
channel Channel name for which detailed history is required
start Start time
end End time
reverse True if messages need to be in reverse order
callback Callback
    @Deprecated
    @Override
    public void detailedHistory(String channellong startlong end,
                                boolean reverseCallback callback) {
        super.detailedHistory(channelstartendreversecallback);
    }

    
Read History for a channel.

Parameters:
channel Channel name for which detailed history is required
start Start time
end End time
reverse True if messages need to be in reverse order
callback Callback
    public void history(String channellong startlong endboolean reverse,
                        Callback callback) {
        super.detailedHistory(channelstartendreversecallback);
    }

    
Read DetailedHistory for a channel.

Deprecated:
as of version 3.5.2 and will be removed with 3.6.0 . Replaced by history(String channel, int count, boolean reverse, Callback callback)
Parameters:
channel Channel name for which detailed history is required
count Upper limit on number of messages to be returned
reverse True if messages need to be in reverse order
callback Callback
    @Deprecated
    @Override
    public void detailedHistory(String channelint countboolean reverse,
                                Callback callback) {
        super.detailedHistory(channelcountreversecallback);
    }

    
Read History for a channel.

Parameters:
channel Channel name for which detailed history is required
count Upper limit on number of messages to be returned
reverse True if messages need to be in reverse order
callback Callback
    public void history(String channelint countboolean reverse,
                        Callback callback) {
        super.detailedHistory(channelcountreversecallback);
    }

    
Read DetailedHistory for a channel.

Deprecated:
as of version 3.5.2 and will be removed with 3.6.0 . Replaced by history(String channel, boolean reverse, Callback callback)
Parameters:
channel Channel name for which detailed history is required
reverse True if messages need to be in reverse order
callback Callback
    @Deprecated
    public void detailedHistory(String channelboolean reverse,
                                Callback callback) {
        super.detailedHistory(channelreversecallback);
    }

    
Read History for a channel.

Parameters:
channel Channel name for which detailed history is required
reverse True if messages need to be in reverse order
callback Callback
    public void history(String channelboolean reverseCallback callback) {
        super.detailedHistory(channelreversecallback);
    }

    
Read DetailedHistory for a channel.

Deprecated:
as of version 3.5.2 and will be removed with 3.6.0 . Replaced by history(String channel, int count, Callback callback)
Parameters:
channel Channel name for which detailed history is required
count Maximum number of messages
callback Callback object
    @Deprecated
    public void detailedHistory(String channelint countCallback callback) {
        super.detailedHistory(channelcountcallback);
    }

    
Read History for a channel.

Parameters:
channel Channel name for which detailed history is required
count Maximum number of messages
callback Callback object
    @Override
    public void history(String channelint countCallback callback) {
        super.detailedHistory(channelcountcallback);
    }

    
Unsubscribe/Disconnect from channel.

Deprecated:
as of version 3.5.2 and will be removed with 3.6.0 . Methods accepting Hashtable as arguments have been deprecated.
Parameters:
args Hashtable containing channel name.
    @Deprecated
    public void unsubscribe(Hashtable args) {
        String[] channelList = (String[]) args.get("channels");
        if (channelList == null) {
            channelList = new String[] { (Stringargs.get("channel") };
        }
        unsubscribe(channelList);
    }

    
Listen for a message on a channel.

Deprecated:
as of version 3.5.2 and will be removed with 3.6.0 .
Parameters:
args Hashtable containing channel name
callback Callback
Throws:
PubnubException Throws PubnubException if Callback is null
    @Deprecated
    public void subscribe(Hashtable argsCallback callback)
    throws PubnubException {
        args.put("callback"callback);
        super.subscribe(args);
    }

    
Listen for a message on a channel.

Deprecated:
as of version 3.5.2 and will be removed with 3.6.0 . Methods accepting Hashtable as arguments have been deprecated.
Parameters:
args Hashtable containing channel name, callback
Throws:
PubnubException Throws PubnubException if Callback is null
    @Deprecated
    public void subscribe(Hashtable argsthrows PubnubException {
        super.subscribe(args);
    }
    private String pamSign(String keyString datathrows PubnubException {
        Mac sha256_HMAC;
        try {
            sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(),
                    "HmacSHA256");
            sha256_HMAC.init(secret_key);
            byte[] hmacData = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
            return new String(Base64Encoder.encode(hmacData)).replace('+''-')
                   .replace('/''_');
        } catch (InvalidKeyException e1) {
            throw new PubnubException(getErrorObject(., 1, "Invalid Key : " + e1.toString()));
        } catch (NoSuchAlgorithmException e1) {
            throw new PubnubException(getErrorObject(., 2, "Invalid Algorithm : " + e1.toString()));
        } catch (IllegalStateException e1) {
            throw new PubnubException(getErrorObject(., 3, "Invalid State : " + e1.toString()));
        } catch (UnsupportedEncodingException e1) {
            throw new PubnubException(getErrorObject(., 4, "Unsupported encoding : " + e1.toString()));
        }
    }

    
Grant r/w access based on channel and auth key

Parameters:
channel
auth_key
read
write
callback
    public void pamGrant(final String channelString auth_keyboolean read,
                         boolean writefinal Callback callback) {
        pamGrant(channelauth_keyreadwrite, -1, callback);
    }

    
Grant r/w access based on channel

Parameters:
channel
read
write
callback
    public void pamGrant(final String channelboolean read,
                         boolean writefinal Callback callback) {
        pamGrant(channelnullreadwrite, -1, callback);
    }

    
Grant r/w access based on channel

Parameters:
channel
read
write
ttl
callback
    public void pamGrant(final String channelboolean read,
                         boolean writeint ttlfinal Callback callback) {
        pamGrant(channelnullreadwritettlcallback);
    }

    
Grant r/w access based on channel and auth key

Parameters:
channel
auth_key
read
write
ttl
callback
    public void pamGrant(final String channelString auth_keyboolean read,
                         boolean writeint ttlfinal Callback callback) {
        Hashtable parameters = PubnubUtil.hashtableClone();
        String r = (read) ? "1" : "0";
        String w = (write) ? "1" : "0";
        String signature = "0";
        int timestamp = (int) ((new Date().getTime()) / 1000);
        if (this..length() == 0) {
            callback.errorCallback(channel,
                                   getErrorObject(, 1));
            return;
        }
        String sign_input = this. + "\n" + this. + "\n" + "grant" + "\n" ;
        if (auth_key != null && auth_key.length() > 0)
            sign_input += "auth=" + auth_key + "&"  ;
        sign_input += "channel=" + channel + "&" + "r=" + r + "&" + "timestamp=" + timestamp
                            + ((ttl > -1)?"&" + "ttl=" + ttl:"")
                            + "&" + "w=" + w;
        try {
            signature = pamSign(this.sign_input);
        } catch (PubnubException e1) {
            callback.errorCallback(channel,
                                   e1.getPubnubError());
            return;
        }
        parameters.put("w"w);
        parameters.put("timestamp", String.valueOf(timestamp));
        parameters.put("signature"signature);
        parameters.put("r"r);
        parameters.put("channel"channel);
        if (auth_key != null && auth_key.length() > 0 ) parameters.put("auth"auth_key);
        if (ttl > 0) parameters.put("ttl", String.valueOf(ttl));
        String[] urlComponents = { getPubnubUrl(), "v1""auth""grant""sub-key",
                                   this.
                                 };
        HttpRequest hreq = new HttpRequest(urlComponentsparameters,
        new ResponseHandler() {
            public void handleResponse(HttpRequest hreqString response) {
                JSONObject jso;
                try {
                    jso = new JSONObject(response);
                } catch (JSONException e) {
                    handleError(hreqgetErrorObject(
                                    , 4,
                                    response));
                    return;
                }
                callback.successCallback(channeljso);
            }
            public void handleError(HttpRequest hreqPubnubError error) {
                callback.errorCallback(channelerror);
                return;
            }
        });
        _request(hreq);
    }

    
ULS Audit

Parameters:
callback
    public void pamAudit(final Callback callback) {
        Hashtable parameters = PubnubUtil.hashtableClone();
        parameters.remove("auth");
        String signature = "0";
        int timestamp = (int) ((new Date().getTime()) / 1000);
        if (this..length() == 0) {
            callback.errorCallback("",
                                   getErrorObject(, 2));
            return;
        }
        String sign_input = this. + "\n" + this. + "\n"
                            + "audit" + "\n"
                            + "timestamp=" + timestamp;
        try {
            signature = pamSign(this.sign_input);
        } catch (PubnubException e1) {
            callback.errorCallback("",
                                   e1.getPubnubError());
            return;
        }
        parameters.put("timestamp", String.valueOf(timestamp));
        parameters.put("signature"signature);
        String[] urlComponents = { getPubnubUrl(), "v1""auth""audit""sub-key",
                                   this.
                                 };
        HttpRequest hreq = new HttpRequest(urlComponentsparameters,
        new ResponseHandler() {
            public void handleResponse(HttpRequest hreqString response) {
                JSONObject jso;
                try {
                    jso = new JSONObject(response);
                } catch (JSONException e) {
                    handleError(hreqgetErrorObject(
                                    , 5,
                                    response));
                    return;
                }
                callback.successCallback(""jso);
            }
            public void handleError(HttpRequest hreqPubnubError error) {
                callback.errorCallback(""error);
                return;
            }
        });
        _request(hreq);
    }

    
ULS audit by channel

Parameters:
channel
callback
    public void pamAudit(final String channel,
                         final Callback callback) {
        Hashtable parameters = PubnubUtil.hashtableClone();
        parameters.remove("auth");
        String signature = "0";
        int timestamp = (int) ((new Date().getTime()) / 1000);
        if (this..length() == 0) {
            callback.errorCallback(channel,
                                   getErrorObject( , 3));
            return;
        }
        String sign_input = this. + "\n" + this. + "\n"
                            + "audit" + "\n" + "channel="
                            + channel + "&" + "timestamp=" + timestamp;
        try {
            signature = pamSign(this.sign_input);
        } catch (PubnubException e1) {
            callback.errorCallback(channel,
                                   e1.getPubnubError());
            return;
        }
        parameters.put("timestamp", String.valueOf(timestamp));
        parameters.put("signature"signature);
        parameters.put("channel"channel);
        String[] urlComponents = { getPubnubUrl(), "v1""auth""audit""sub-key",
                                   this.
                                 };
        HttpRequest hreq = new HttpRequest(urlComponentsparameters,
        new ResponseHandler() {
            public void handleResponse(HttpRequest hreqString response) {
                JSONObject jso;
                try {
                    jso = new JSONObject(response);
                } catch (JSONException e) {
                    handleError(hreqgetErrorObject(
                                    , 6,
                                    response));
                    return;
                }
                callback.successCallback(channeljso);
            }
            public void handleError(HttpRequest hreqPubnubError error) {
                callback.errorCallback(channelerror);
                return;
            }
        });
        _request(hreq);
    }

    
ULS audit by channel and auth key

Parameters:
channel
auth_key
callback
    public void pamAudit(final String channelString auth_key,
                         final Callback callback) {
        Hashtable parameters = PubnubUtil.hashtableClone();
        String signature = "0";
        int timestamp = (int) ((new Date().getTime()) / 1000);
        if (this..length() == 0) {
            callback.errorCallback(channel,
                                   getErrorObject(, 4));
            return;
        }
        String sign_input = this. + "\n" + this. + "\n"
                            + "audit" + "\n" + "auth=" + auth_key + "&" + "channel="
                            + channel + "&" + "timestamp=" + timestamp;
        try {
            signature = pamSign(this.sign_input);
        } catch (PubnubException e1) {
            callback.errorCallback(channel,
                                   e1.getPubnubError());
            return;
        }
        parameters.put("timestamp", String.valueOf(timestamp));
        parameters.put("signature"signature);
        parameters.put("channel"channel);
        parameters.put("auth"auth_key);
        String[] urlComponents = { getPubnubUrl(), "v1""auth""audit""sub-key",
                                   this.
                                 };
        HttpRequest hreq = new HttpRequest(urlComponentsparameters,
        new ResponseHandler() {
            public void handleResponse(HttpRequest hreqString response) {
                JSONObject jso;
                try {
                    jso = new JSONObject(response);
                } catch (JSONException e) {
                    handleError(hreq, PubnubError.getErrorObject(
                                    ., 2,
                                    response));
                    return;
                }
                callback.successCallback(channeljso);
            }
            public void handleError(HttpRequest hreqPubnubError error) {
                callback.errorCallback(channelerror);
                return;
            }
        });
        _request(hreq);
    }

    
ULS revoke by channel and auth key

Parameters:
channel
auth_key
callback
    public void pamRevoke(String channelString auth_keyCallback callback) {
        pamGrant(channelauth_keyfalsefalsecallback);
    }


    
ULS revoke by channel

Parameters:
channel
callback
    public void pamRevoke(String channelCallback callback) {
        pamGrant(channelnullfalsefalsecallback);
    }
New to GrepCode? Check out our FAQ X