Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.pubnub.api;
  
  import javax.crypto.Mac;
  import java.util.Date;
 import java.util.UUID;
 
 import static com.pubnub.api.PubnubError.PNERROBJ_SECRET_KEY_MISSING;
 import static com.pubnub.api.PubnubError.getErrorObject;

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

Author(s):
Pubnub
 
 
 abstract class PubnubCoreShared 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 PubnubCoreShared(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 PubnubCoreShared(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 PubnubCoreShared(String publish_keyString subscribe_key) {
         super(publish_keysubscribe_key""""false);
     }

    

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

    

Parameters:
publish_key
subscribe_key
secret_key
 
     public PubnubCoreShared(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 PubnubCoreShared(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();
    }
    public 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();
    }
    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 ttlCallback callback) {
        final Callback cb = getWrappedCallback(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=" + PubnubUtil.urlEncode(auth_key) + "&"  ;
        sign_input += "channel=" + PubnubUtil.urlEncode(channel) + "&" + "pnsdk=" + PubnubUtil.urlEncode(getUserAgent()) + "&" + "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 >= -1) 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) {
                invokeCallback(channelresponse"payload"cb, 4);
            }
            public void handleError(HttpRequest hreqPubnubError error) {
                cb.errorCallback(channelerror);
                return;
            }
        });
        _request(hreq);
    }
    public void pamGrantChannelGroup(final String groupboolean read,
                                     boolean managementCallback callback) {
        pamGrantChannelGroup(groupreadmanagement, -1, callback);
    }
    public void pamGrantChannelGroup(final String groupboolean read,
                                     boolean managementint ttlCallback callback) {
        pamGrantChannelGroup(groupnullreadmanagementttlcallback);
    }
    public void pamGrantChannelGroup(final String groupString auth_keyboolean read,
                                     boolean managementCallback callback) {
        pamGrantChannelGroup(groupauth_keyreadmanagement, -1, callback);
    }
    public void pamGrantChannelGroup(final String groupString auth_keyboolean readboolean managementint ttl,
                                               Callback callback) {
        String signature;
        final Callback cb = getWrappedCallback(callback);
        Hashtable parameters = PubnubUtil.hashtableClone();
        String r = (read) ? "1" : "0";
        String m = (management) ? "1" : "0";
        int timestamp = (int) ((new Date().getTime()) / 1000);
        if (this..length() == 0) {
            callback.errorCallback(groupgetErrorObject(, 1));
            return;
        }
        String sign_input = this. + "\n" + this. + "\n" + "grant" + "\n";
        if (auth_key != null && auth_key.length() > 0)
            sign_input += "auth=" + PubnubUtil.urlEncode(auth_key) + "&"  ;
        sign_input += "channel-group=" + PubnubUtil.urlEncode(group) + "&"
                + "m=" + m + "&"
                + "pnsdk=" + PubnubUtil.urlEncode(getUserAgent()) + "&"
                + "r=" + r + "&"
                + "timestamp=" + timestamp
                + ((ttl >= -1)?"&" + "ttl=" + ttl:"");
        try {
            signature = pamSign(this.sign_input);
        } catch (PubnubException e1) {
            callback.errorCallback(groupe1.getPubnubError());
            return;
        }
        parameters.put("timestamp", String.valueOf(timestamp));
        parameters.put("signature"signature);
        parameters.put("r"r);
        parameters.put("m"m);
        parameters.put("channel-group"group);
        if (ttl >= -1) parameters.put("ttl", String.valueOf(ttl));
        if (auth_key != null && auth_key.length() > 0 ) parameters.put("auth"auth_key);
        String[] urlComponents = { getPubnubUrl(), "v1""auth""grant""sub-key",
                this.
        };
        HttpRequest hreq = new HttpRequest(urlComponentsparameters,
                new ResponseHandler() {
                    public void handleResponse(HttpRequest hreqString response) {
                        invokeCallback(groupresponse"payload"cb, 4);
                    }
                    public void handleError(HttpRequest hreqPubnubError error) {
                        cb.errorCallback(grouperror);
                    }
                });
        _request(hreq);
    }

    
ULS Audit

Parameters:
callback
    public void pamAudit(Callback callback) {
        final Callback cb = getWrappedCallback(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" + "pnsdk=" + PubnubUtil.urlEncode(getUserAgent()) + "&"
                            + "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) {
                invokeCallback(""response"payload"cb, 5 );
            }
            public void handleError(HttpRequest hreqPubnubError error) {
                cb.errorCallback(""error);
                return;
            }
        });
        _request(hreq);
    }

    
ULS audit by channel

Parameters:
channel
callback
    public void pamAudit(final String channel,
                         Callback callback) {
        final Callback cb = getWrappedCallback(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="
                            + PubnubUtil.urlEncode(channel) + "&" + "pnsdk=" + PubnubUtil.urlEncode(getUserAgent()) + "&" + "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) {
                invokeCallback(channelresponse"payload"cb, 6);
            }
            public void handleError(HttpRequest hreqPubnubError error) {
                cb.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,
                         Callback callback) {
        final Callback cb = getWrappedCallback(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=" + PubnubUtil.urlEncode(auth_key) + "&" + "channel="
                            + PubnubUtil.urlEncode(channel) + "&" + "pnsdk=" + PubnubUtil.urlEncode(getUserAgent()) + "&" + "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) {
                invokeCallback(channelresponse"payload"cb, 2);
            }
            public void handleError(HttpRequest hreqPubnubError error) {
                cb.errorCallback(channelerror);
                return;
            }
        });
        _request(hreq);
    }
    public void pamAuditChannelGroup(final String groupCallback callback) {
        pamAuditChannelGroup(groupnullcallback);
    }
    public void pamAuditChannelGroup(final String groupString auth_keyCallback callback) {
        String signature;
        final Callback cb = getWrappedCallback(callback);
        Hashtable parameters = PubnubUtil.hashtableClone();
        parameters.remove("auth");
        int timestamp = (int) ((new Date().getTime()) / 1000);
        if (this..length() == 0) {
            callback.errorCallback(groupgetErrorObject(, 3));
            return;
        }
        String sign_input = this. + "\n" + this. + "\n" + "audit" + "\n";
        if (auth_key != null && auth_key.length() > 0)
            sign_input += "auth=" + auth_key + "&"  ;
        sign_input += "channel-group=" + PubnubUtil.urlEncode(group) + "&"
                + "pnsdk=" + PubnubUtil.urlEncode(getUserAgent()) + "&"
                + "timestamp=" + timestamp;
        try {
            signature = pamSign(this.sign_input);
        } catch (PubnubException e1) {
            callback.errorCallback(groupe1.getPubnubError());
            return;
        }
        parameters.put("timestamp", String.valueOf(timestamp));
        parameters.put("signature"signature);
        parameters.put("channel-group"group);
        if (auth_key != null && auth_key.length() > 0 ) 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) {
                        invokeCallback(groupresponse"payload"cb, 6);
                    }
                    public void handleError(HttpRequest hreqPubnubError error) {
                        cb.errorCallback(grouperror);
                    }
                });
        _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);
    }
    public void pamRevokeChannelGroup(String groupCallback callback) {
        pamRevokeChannelGroup(groupnullcallback);
    }
    public void pamRevokeChannelGroup(String groupString auth_keyCallback callback) {
        pamGrantChannelGroup(groupauth_keyfalsefalse, -1, callback);
    }
New to GrepCode? Check out our FAQ X