Skip to content

Commit

Permalink
Merge pull request #123 from ubitricity/master
Browse files Browse the repository at this point in the history
Add support for HTTP Basic Authentication
  • Loading branch information
TVolden authored Oct 2, 2020
2 parents 3b41c63 + 72f6ca3 commit 65e792d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class JSONConfiguration {
public static final String REUSE_ADDR_PARAMETER = "REUSE_ADDR";
public static final String PROXY_PARAMETER = "PROXY";
public static final String PING_INTERVAL_PARAMETER = "PING_INTERVAL";
public static final String USERNAME_PARAMETER = "USERNAME";
public static final String PASSWORD_PARAMETER = "PASSWORD";

private final HashMap<String, Object> parameters = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ of this software and associated documentation files (the "Software"), to deal
import java.net.ConnectException;
import java.net.Proxy;
import java.net.URI;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.exceptions.WebsocketNotConnectedException;
Expand Down Expand Up @@ -63,8 +67,17 @@ public WebSocketTransmitter(Draft draft) {
public void connect(String uri, RadioEvents events) {
final URI resource = URI.create(uri);

Map<String,String> httpHeaders = new HashMap<>();
String username = configuration.getParameter(JSONConfiguration.USERNAME_PARAMETER);
String password = configuration.getParameter(JSONConfiguration.PASSWORD_PARAMETER);
if (username != null && password != null) {
String credentials = username + ":" + password;
byte[] base64Credentials = Base64.getEncoder().encode(credentials.getBytes());
httpHeaders.put("Authorization", "Basic " + new String(base64Credentials));
}

client =
new WebSocketClient(resource, draft) {
new WebSocketClient(resource, draft, httpHeaders) {
@Override
public void onOpen(ServerHandshake serverHandshake) {
logger.debug("On connection open (HTTP status: {})", serverHandshake.getHttpStatus());
Expand Down

0 comments on commit 65e792d

Please sign in to comment.