Class WebSocketExtension


  • public class WebSocketExtension
    extends java.lang.Object
    A class to hold the name and the parameters of a WebSocket extension.

    Read included Apache License 2.0: HERE

    All NeoVisionaries.WebSockets.* Code Obtained From: GitHub 'NV'  (Takahiko Kawasaki)  Public Archive.


    • Constructor Detail

      • WebSocketExtension

        🡅  🡇    
        public WebSocketExtension​(java.lang.String name)
        Constructor with an extension name.
        Parameters:
        name - The extension name.
        Throws:
        java.lang.IllegalArgumentException - The given name is not a valid token.
      • WebSocketExtension

        🡅  🡇    
        public WebSocketExtension​(WebSocketExtension source)
        Copy constructor.
        Parameters:
        source - A source extension. Must not be null.
        Throws:
        java.lang.IllegalArgumentException - The given argument is null.
        Since:
        1.6
    • Method Detail

      • getName

        🡅  🡇    
        public java.lang.String getName()
        Get the extension name.
        Returns:
        The extension name.
        Code:
        Exact Method Body:
         return mName;
        
      • getParameters

        🡅  🡇    
        public java.util.Map<java.lang.String,​java.lang.String> getParameters()
        Get the parameters.
        Returns:
        The parameters.
        Code:
        Exact Method Body:
         return mParameters;
        
      • containsParameter

        🡅  🡇    
        public boolean containsParameter​(java.lang.String key)
        Check if the parameter identified by the key is contained.
        Parameters:
        key - The name of the parameter.
        Returns:
        true if the parameter is contained.
        Code:
        Exact Method Body:
         return mParameters.containsKey(key);
        
      • getParameter

        🡅  🡇    
        public java.lang.String getParameter​(java.lang.String key)
        Get the value of the specified parameter.
        Parameters:
        key - The name of the parameter.
        Returns:
        The value of the parameter. null may be returned.
        Code:
        Exact Method Body:
         return mParameters.get(key);
        
      • setParameter

        🡅  🡇    
        public WebSocketExtension setParameter​(java.lang.String key,
                                               java.lang.String value)
        Set a value to the specified parameter.
        Parameters:
        key - The name of the parameter.
        value - The value of the parameter. If not null, it must be a valid token. Note that RFC 6455 says "When using the quoted-string syntax variant, the value after quoted-string unescaping MUST conform to the 'token' ABNF."
        Returns:
        this object.
        Throws:
        java.lang.IllegalArgumentException -
        • The key is not a valid token.
        • The value is not null and it is not a valid token.
        Code:
        Exact Method Body:
         // Check the validity of the key.
         if (Token.isValid(key) == false)
         {
             // The key is not a valid token.
             throw new IllegalArgumentException("'key' is not a valid token.");
         }
        
         // If the value is not null.
         if (value != null)
         {
             // Check the validity of the value.
             if (Token.isValid(value) == false)
             {
                 // The value is not a valid token.
                 throw new IllegalArgumentException("'value' is not a valid token.");
             }
         }
        
         mParameters.put(key, value);
        
         return this;
        
      • toString

        🡅  🡇    
        public java.lang.String toString()
        Stringify this object into the format "{name}[; {key}[={value}]]*".
        Overrides:
        toString in class java.lang.Object
        Code:
        Exact Method Body:
         StringBuilder builder = new StringBuilder(mName);
        
         for (Map.Entry<String, String> entry : mParameters.entrySet())
         {
             // "; {key}"
             builder.append("; ").append(entry.getKey());
        
             String value = entry.getValue();
        
             if (value != null && value.length() != 0)
             {
                 // "={value}"
                 builder.append("=").append(value);
             }
         }
        
         return builder.toString();
        
      • parse

        🡅    
        public static WebSocketExtension parse​(java.lang.String string)
        Parse a string as a WebSocketExtension. The input string should comply with the format described in 9.1. Negotiating Extensions in RFC 6455.
        Parameters:
        string - A string that represents a WebSocket extension.
        Returns:
        A new WebSocketExtension instance that represents the given string. If the input string does not comply with RFC 6455, null is returned.
        Code:
        Exact Method Body:
         if (string == null)
         {
             return null;
         }
        
         // Split the string by semi-colons.
         String[] elements = string.trim().split("\\s*;\\s*");
        
         if (elements.length == 0)
         {
             // Even an extension name is not included.
             return null;
         }
        
         // The first element is the extension name.
         String name = elements[0];
        
         if (Token.isValid(name) == false)
         {
             // The extension name is not a valid token.
             return null;
         }
        
         // Create an instance for the extension name.
         WebSocketExtension extension = createInstance(name);
        
         // For each "{key}[={value}]".
         for (int i = 1; i < elements.length; ++i)
         {
             // Split by '=' to get the key and the value.
             String[] pair = elements[i].split("\\s*=\\s*", 2);
        
             // If {key} is not contained.
             if (pair.length == 0 || pair[0].length() == 0)
             {
                 // Ignore.
                 continue;
             }
        
             // The name of the parameter.
             String key = pair[0];
        
             if (Token.isValid(key) == false)
             {
                 // The parameter name is not a valid token.
                 // Ignore this parameter.
                 continue;
             }
        
             // The value of the parameter.
             String value = extractValue(pair);
        
             if (value != null)
             {
                 if (Token.isValid(value) == false)
                 {
                     // The parameter value is not a valid token.
                     // Ignore this parameter.
                     continue;
                 }
             }
        
             // Add the pair of the key and the value.
             extension.setParameter(key, value);
         }
        
         return extension;