Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public abstract class WatsonService {
private String endPoint;
private final String name;
protected Headers defaultHeaders = null;
private boolean skipAuthentication = false;
protected boolean skipAuthentication = false;

/** The Constant MESSAGE_CODE. */
protected static final String MESSAGE_CODE = "code";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,21 +346,25 @@ public void recognizeUsingWebSocket(final InputStream audio, final RecognizeOpti
Validator.notNull(options, "options cannot be null");
Validator.notNull(options.contentType(), "options.contentType cannot be null");
Validator.notNull(callback, "callback cannot be null");


getToken().enqueue(new ServiceCallback<String>() {
@Override
public void onFailure(Exception e) {
callback.onError(e);
}

@Override
public void onResponse(String token) {
String url = getEndPoint().replaceFirst("(https|http)", "wss");
WebSocketManager wsManager = new WebSocketManager(url + PATH_RECOGNIZE, configureHttpClient(), defaultHeaders, token);
wsManager.recognize(audio, options, callback);
}
});


final String url = getEndPoint().replaceFirst("(https|http)", "wss");

if (skipAuthentication) {
WebSocketManager wsManager = new WebSocketManager(url + PATH_RECOGNIZE, configureHttpClient(), defaultHeaders, null);
wsManager.recognize(audio, options, callback);
} else {
getToken().enqueue(new ServiceCallback<String>() {
@Override
public void onFailure(Exception e) {
callback.onError(e);
}

@Override
public void onResponse(String token) {
WebSocketManager wsManager = new WebSocketManager(url + PATH_RECOGNIZE, configureHttpClient(), defaultHeaders, token);
wsManager.recognize(audio, options, callback);
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,12 @@ public WebSocketManager(String url, OkHttpClient client, Headers defaultHeaders,
private WebSocketCall createConnection(RecognizeOptions options) {
String speechModel = options.model() == null ? "" : "?model=" + options.model();
Builder builder = new Request.Builder()
.url(url + speechModel)
.addHeader(HttpHeaders.X_WATSON_AUTHORIZATION_TOKEN, token);
.url(url + speechModel);

if (token != null) {
builder.addHeader(HttpHeaders.X_WATSON_AUTHORIZATION_TOKEN, token);
}

if (defaultHeaders != null) {
for (String key : defaultHeaders.names()) {
builder.header(key, defaultHeaders.get(key));
Expand Down