Class Beacon


  • public class Beacon
    extends java.lang.Object
    The Beacon class holds all the beacon data and the beacon protocol implementation.
    • Constructor Detail

      • Beacon

        public Beacon​(BeaconInitializer initializer,
                      BeaconConfiguration configuration)
        Creates a new beacon instance
        Parameters:
        initializer - provider of relevant parameters to initialize / create the beacon
        configuration - OpenKit related configuration.
    • Method Detail

      • createID

        public int createID()
        Create a unique identifier.

        The identifier returned is only unique per Beacon. Calling this method on two different Beacon instances, might give the same result.

        Returns:
        A unique identifier.
      • getCurrentTimestamp

        public long getCurrentTimestamp()
        Get the current timestamp in milliseconds by delegating to TimingProvider
        Returns:
        Current timestamp in milliseconds.
      • createSequenceNumber

        public int createSequenceNumber()
        Create a unique sequence number.

        The sequence number returned is only unique per Beacon. Calling this method on two different Beacon instances, might give the same result.

        Returns:
        A unique sequence number.
      • getSessionStartTime

        public long getSessionStartTime()
        Returns the time when the session was started (in milliseconds).
      • createTag

        public java.lang.String createTag​(int parentActionID,
                                          int tracerSeqNo)
        Create a web request tag.

        Web request tags can be attached as HTTP header for web request tracing. If PrivacyConfiguration.isWebRequestTracingAllowed() yields false an empty tag is returned.

        Parameters:
        parentActionID - The ID of the Action for which to create a web request tag or 0 if no parent action exists.
        tracerSeqNo - Sequence number of the WebRequestTracer.
        Returns:
        A web request tracer tag.
      • startSession

        public void startSession()
        Add start session event to Beacon.

        The serialized data is added to BeaconCache.

      • endSession

        public void endSession()
        Add SessionImpl to Beacon when session is ended.

        The serialized data is added to BeaconCache.

      • reportValue

        public void reportValue​(int parentActionID,
                                java.lang.String valueName,
                                int value)
        Add key-value-pair to Beacon.

        The serialized data is added to BeaconCache.

        Parameters:
        parentActionID - The ID of the Action on which this value was reported.
        valueName - Value's name.
        value - Actual value to report.
      • reportValue

        public void reportValue​(int parentActionID,
                                java.lang.String valueName,
                                long value)
        Add key-value-pair to Beacon.

        The serialized data is added to BeaconCache.

        Parameters:
        parentActionID - The ID of the Action on which this value was reported.
        valueName - Value's name.
        value - Actual value to report.
      • reportValue

        public void reportValue​(int parentActionID,
                                java.lang.String valueName,
                                double value)
        Add key-value-pair to Beacon.

        The serialized data is added to BeaconCache.

        Parameters:
        parentActionID - The ID of the Action on which this value was reported.
        valueName - Value's name.
        value - Actual value to report.
      • reportValue

        public void reportValue​(int parentActionID,
                                java.lang.String valueName,
                                java.lang.String value)
        Add key-value-pair to Beacon.

        The serialized data is added to BeaconCache.

        Parameters:
        parentActionID - The ID of the Action on which this value was reported.
        valueName - Value's name.
        value - Actual value to report.
      • reportEvent

        public void reportEvent​(int parentActionID,
                                java.lang.String eventName)
        Add event (aka. named event) to Beacon.

        The serialized data is added to BeaconCache.

        Parameters:
        parentActionID - The ID of the Action on which this event was reported.
        eventName - Event's name.
      • reportError

        public void reportError​(int parentActionID,
                                java.lang.String errorName,
                                int errorCode)
        Add error to Beacon.

        The serialized data is added to BeaconCache.

        Parameters:
        parentActionID - The ID of the Action on which this error was reported.
        errorName - Error's name.
        errorCode - Some error code.
      • reportError

        public void reportError​(int parentActionID,
                                java.lang.String errorName,
                                java.lang.String causeName,
                                java.lang.String causeDescription,
                                java.lang.String causeStackTrace)
      • reportError

        public void reportError​(int parentActionID,
                                java.lang.String errorName,
                                java.lang.Throwable throwable)
      • reportCrash

        public void reportCrash​(java.lang.String errorName,
                                java.lang.String reason,
                                java.lang.String stacktrace)
        Add crash to Beacon.

        The serialized data is added to BeaconCache.

        Parameters:
        errorName - Error's name.
        reason - Reason for that error.
        stacktrace - Crash stacktrace.
      • reportCrash

        public void reportCrash​(java.lang.Throwable throwable)
        Add crash to Beacon.

        The serialized data is added to BeaconCache.

        Parameters:
        throwable - Throwable to report.
      • addWebRequest

        public void addWebRequest​(int parentActionID,
                                  WebRequestTracerBaseImpl webRequestTracer)
        Add web request to Beacon.

        The serialized data is added to BeaconCache.

        Parameters:
        parentActionID - The id of the parent Action on which this web request was reported.
        webRequestTracer - Web request tracer to serialize.
      • identifyUser

        public void identifyUser​(java.lang.String userTag)
        Add user identification to Beacon.

        The serialized data is added to BeaconCache.

        Parameters:
        userTag - User tag containing data to serialize. The userTag is the only name-beacon data that can be null/empty.
      • send

        public StatusResponse send​(HTTPClientProvider provider,
                                   AdditionalQueryParameters additionalParameters)
        Send current state of Beacon.

        This method tries to send all so far collected and serialized data.

        Parameters:
        provider - Provider for getting an HTTPClient required to send the data.
        additionalParameters - additional parameters that will be send with the beacon request (can be null).
        Returns:
        Returns the last status response retrieved from the server side, or null if an error occurred.
      • sendBizEvent

        public void sendBizEvent​(java.lang.String type,
                                 java.util.Map<java.lang.String,​JSONValue> attributes)
      • sendEvent

        public void sendEvent​(java.lang.String name,
                              java.util.Map<java.lang.String,​JSONValue> attributes)
      • encodeBeaconChunk

        protected byte[] encodeBeaconChunk​(java.lang.String chunkToEncode)
                                    throws java.io.UnsupportedEncodingException
        Encodes the given chunk to an CHARSET byte array.

        This method should only be used by tests.

        Parameters:
        chunkToEncode - the beacon chunk to encode
        Returns:
        the encoded beacon chunk
        Throws:
        java.io.UnsupportedEncodingException
      • clearData

        public void clearData()
        Clears all previously collected data for this Beacon.

        This only affects the so far serialized data, which gets removed from the cache.

      • getDeviceID

        public long getDeviceID()
        Get a visitor ID for the current data collection level in case of level 2 (USER_BEHAVIOR) the value from the configuration is used in case of level 1 (PERFORMANCE) or 0 (OFF) a random number in the positive Long range is used
        Returns:
        The device identifier, which is truncated to 250 characters if level 2 (USER_BEHAVIOR) is used.
      • getSessionNumber

        public int getSessionNumber()
        Get a session ID for the current data collection level

        If session number reporting is allowed (see also PrivacyConfiguration.isSessionNumberReportingAllowed(), then the real session number is returned, otherwise 1 is returned.

        Returns:
        Pre calculated session number or 1 if session number reporting is not allowed.
      • getSessionSequenceNumber

        public int getSessionSequenceNumber()
        Returns sequence number of the session.

        The session sequence number is a consecutive number which is increased when a session is split due to exceeding the maximum number of allowed events. The split session will then have the same session number but an increased session sequence number.

      • isEmpty

        public boolean isEmpty()
        Tests if the Beacon is empty.

        A beacon is considered to be empty, if it does not contain any action or event data.

        Returns:
        true if the beacon is empty, false otherwise.
      • initializeServerConfiguration

        public void initializeServerConfiguration​(ServerConfiguration serverConfiguration)
        Initializes the beacon with the given ServerConfiguration.
        Parameters:
        serverConfiguration - the server configuration which will be used for initialization.
      • updateServerConfiguration

        public void updateServerConfiguration​(ServerConfiguration serverConfiguration)
        Updates this beacon with the given ServerConfiguration
        Parameters:
        serverConfiguration - the server configuration which will be used to update this beacon.
      • isServerConfigurationSet

        public boolean isServerConfigurationSet()
        Indicates whether a server configuration is set on this beacon's configuration or not.
      • setServerConfigurationUpdateCallback

        public void setServerConfigurationUpdateCallback​(ServerConfigurationUpdateCallback callback)
        Sets the callback when a server configuration is updated.
        Parameters:
        callback - the callback to be notified when the server configuration is updated.
      • isDataCapturingEnabled

        public boolean isDataCapturingEnabled()
        Indicates whether data capturing for this beacon is currently enabled or not.
      • isErrorCapturingEnabled

        public boolean isErrorCapturingEnabled()
        Indicates whether error capturing for this beacon is currently enabled or not.
      • isCrashCapturingEnabled

        public boolean isCrashCapturingEnabled()
        Indicates whether crash capturing for this beacon is currently enabled or not.
      • enableCapture

        public void enableCapture()
        Enables capturing for this beacon.

        This will implicitly cause isServerConfigurationSet() to return true.

      • disableCapture

        public void disableCapture()
        Disables capturing for this session

        This will implicitly cause isServerConfigurationSet() to return true.

      • isActionReportingAllowedByPrivacySettings

        public boolean isActionReportingAllowedByPrivacySettings()
        Get a boolean, indicating if action reporting is enabled by the privacy configuration
        Returns:
        true if action reporting is enabled by privacy configuration, false otherwise.