question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getOrDefault in WebSocketConnection.java always returns the default value

See original GitHub issue

In line 646 of the file WebSocketConnection.java,

String protocol = getOrDefault(serverHandshake.headers,
                                "Sec-WebSocket-Protocol", null);`

the function getOrDefault is called. This function then checks if (obj.containsKey(key) and returns the value if the key is present or the default value otherwise.

We had the key Sec-WebSocket-Protocol in our map (I printed the map within this function as well) and it was set to the value wamp.2.cbor. However, the if condition never gets executed and the default value (null) is always returned.

I am not sure what might be causing this issue. I fixed it temporarily by making the function return wamp.2.cbor instead of the default value for the time being (but that won’t work for everyone obviously).

This is also the root cause of the issue #420 (and probably even #146 ). Atleast, I can tell that from my experience of forking this library and figuring out this issue (which took multiple hours), this is the root issue!

I don’t know what sorcery this is or how this could be fixed but yes, this is a completely real issue and this was the root cause of our development being stuck for more than a week. And this, THIS was the main issue. Changing this one line of code fixed the entire thing!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
azamat-sharapovcommented, Jul 15, 2020

I was having similar problem today. In my case, I’m using Caddy as a server, which proxies requests to nexus router. Problem was that Caddy returns HTTP headers in different case, instead of Sec-WebSocket-Protocol, it returns Sec-Websocket-Protocol (notice lowecase S). I came to conclusion, that it’s autobahn-java problem, because it relies on header key case, when it should not.

As a workaround, I’ve added these lines in ServerHandshake.java:

        for (Map.Entry<String, String> entry : headers.entrySet()) {
            this.headers.put(entry.getKey().toLowerCase(), entry.getValue());
        }

and changed all hardcoded header strings in other files into lowercase.

1reaction
yashovardhan99commented, Mar 9, 2019

@om26er I’ll share some sample code and logs with you tomorrow (it’s past 2 am here:)

This issue seems to happen in both the latest and the 2nd latest version of the library. I had downloaded the zip of your latest release v19.3.1 to debug this issue. The mobile OS - Android Pie (Absurd 9.0 and also on Android N)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is .put() in .getOrDefault() computed every time? - Stack Overflow
It must be in order to create the value actually passed in to be returned as default value if the value is not...
Read more >
HashMap getOrDefault(key, defaultValue) method in Java with ...
If no value is mapped with the provided key then the default value is returned. Syntax: default V getOrDefault(Object key, V defaultValue).
Read more >
getOrDefault() in Java - Scaler Topics
Default Value -> It refers to the default value which is returned when there is no mapping or an associated mapped value, found...
Read more >
Autobahn-Java on Android Connection Timeout #420 - GitHub
When we try to set up a WAMP connection between Android Java (using ... getOrDefault in WebSocketConnection.java always returns the default ...
Read more >
Java HashMap getOrDefault() - Programiz
The Java HashMap getOrDefault() method returns the specified default value if the mapping for the specified key is not found in the hashmap....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found