Problem with getting tokens

Hello, I have a problem connecting to a Java client on a private network with an openvid public server.

Exception [Request processing failed; nested exception is io.openvidu.java.client.OpenViduJavaClientException: Connect to 10.0.0.XX:443 [/10.0.0.XX] failed: Read timed out] with root cause

java.net.SocketTimeoutException: Read timed out

at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.8.0_212]


at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) ~[na:1.8.0_212]


at java.net.SocketInputStream.read(SocketInputStream.java:171) ~[na:1.8.0_212]


at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_212]


at sun.security.ssl.InputRecord.readFully(InputRecord.java:465) ~[na:1.8.0_212]


at sun.security.ssl.InputRecord.read(InputRecord.java:503) ~[na:1.8.0_212]


at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975) ~[na:1.8.0_212]


at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367) ~[na:1.8.0_212]


at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395) ~[na:1.8.0_212]


at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379) ~[na:1.8.0_212]


at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:436) ~[httpclient-4.5.11.jar!/:4.5.11]


at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:384) ~[httpclient-4.5.11.jar!/:4.5.11]


at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142) ~[httpclient-4.5.11.jar!/:4.5.11]


at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376) ~[httpclient-4.5.11.jar!/:4.5.11]


at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393) ~[httpclient-4.5.11.jar!/:4.5.11]


at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) ~[httpclient-4.5.11.jar!/:4.5.11]


at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186) ~[httpclient-4.5.11.jar!/:4.5.11]


at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) ~[httpclient-4.5.11.jar!/:4.5.11]


at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) ~[httpclient-4.5.11.jar!/:4.5.11]


at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) ~[httpclient-4.5.11.jar!/:4.5.11]


at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) ~[httpclient-4.5.11.jar!/:4.5.11]


at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) ~[httpclient-4.5.11.jar!/:4.5.11]


at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) ~[httpclient-4.5.11.jar!/:4.5.11]


at io.openvidu.java.client.Session.getSessionIdHttp(Session.java:473) ~[openvidu-java-client-2.14.0.jar!/:na]


at io.openvidu.java.client.Session.<init>(Session.java:57) ~[openvidu-java-client-2.14.0.jar!/:na]


at io.openvidu.java.client.OpenVidu.createSession(OpenVidu.java:131) ~[openvidu-java-client-2.14.0.jar!/:na]


at io.openvidu.js.java.service.video.SessionService.newSessionCreating(SessionService.java:123) ~[classes!/:2.11.0]


at io.openvidu.js.java.service.video.SessionService.getToken(SessionService.java:78) ~[classes!/:2.11.0]


at io.openvidu.js.java.controllers.JoiningRoomsController.getToken(JoiningRoomsController.java:36) ~[classes!/:2.11.0]


at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]


at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]


at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]


at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]


at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]


at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]


at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

When I am connected to a Java client on a private network with an openvid server on a private network, this problem does not occur and I receive the token correctly.

The Openvid server works fine when I tried the NodeJS demo also on a private network, so I have no idea why it happened.

What can i do?

It seems a problem with REST API used in Java code to connect to OpenVidu server.

Try to connect directly to the REST API with your own code, to test if you can do it. If it works to you, you can execute all operations directly by means of REST requests.

Or you can create a fork of official openvidu-java-client and change included http library by your own.

Now i’m using this method -

private String returnTokenForExistingSession(String username, String sessionName, OpenViduRole role, TokenOptions tokenOptions) throws OpenViduJavaClientException {
System.out.println("Existing session " + sessionName);
String token = null;
try {
token = this.mapSessions.get(sessionName).generateToken(tokenOptions);
addSessionRelationToUser(username, sessionName, role, token);
} catch (OpenViduHttpException e2) {
if (404 == e2.getStatus()) {
this.mapSessions.remove(sessionName);
this.mapSessionNamesTokens.remove(sessionName);
}
}
return token;
}

which uses generateToken method

public String generateToken(TokenOptions tokenOptions) throws OpenViduJavaClientException, OpenViduHttpException {

	if (!this.hasSessionId()) {
		this.getSessionId();
	}

	HttpPost request = new HttpPost(this.openVidu.hostname + OpenVidu.API_TOKENS);

	JSONObject json = new JSONObject();
	json.put("session", this.sessionId);
	json.put("role", tokenOptions.getRole().name());
	json.put("data", tokenOptions.getData());
	if (tokenOptions.getKurentoOptions() != null) {
		JSONObject kurentoOptions = new JSONObject();
		if (tokenOptions.getKurentoOptions().getVideoMaxRecvBandwidth() != null) {
			kurentoOptions.put("videoMaxRecvBandwidth",
					tokenOptions.getKurentoOptions().getVideoMaxRecvBandwidth());
		}
		if (tokenOptions.getKurentoOptions().getVideoMinRecvBandwidth() != null) {
			kurentoOptions.put("videoMinRecvBandwidth",
					tokenOptions.getKurentoOptions().getVideoMinRecvBandwidth());
		}
		if (tokenOptions.getKurentoOptions().getVideoMaxSendBandwidth() != null) {
			kurentoOptions.put("videoMaxSendBandwidth",
					tokenOptions.getKurentoOptions().getVideoMaxSendBandwidth());
		}
		if (tokenOptions.getKurentoOptions().getVideoMinSendBandwidth() != null) {
			kurentoOptions.put("videoMinSendBandwidth",
					tokenOptions.getKurentoOptions().getVideoMinSendBandwidth());
		}
		if (tokenOptions.getKurentoOptions().getAllowedFilters().length > 0) {
			JSONArray allowedFilters = new JSONArray();
			for (String filter : tokenOptions.getKurentoOptions().getAllowedFilters()) {
				allowedFilters.add(filter);
			}
			kurentoOptions.put("allowedFilters", allowedFilters);
		}
		json.put("kurentoOptions", kurentoOptions);
	}
	StringEntity params;
	try {
		params = new StringEntity(json.toString());
	} catch (UnsupportedEncodingException e1) {
		throw new OpenViduJavaClientException(e1.getMessage(), e1.getCause());
	}

	request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
	request.setEntity(params);

	HttpResponse response;
	try {
		response = this.openVidu.httpClient.execute(request);
	} catch (IOException e2) {
		throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
	}

	try {
		int statusCode = response.getStatusLine().getStatusCode();
		if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
			String token = (String) httpResponseToJson(response).get("id");
			log.info("Returning a TOKEN: {}", token);
			return token;
		} else {
			throw new OpenViduHttpException(statusCode);
		}
	} finally {
		EntityUtils.consumeQuietly(response.getEntity());
	}
}

I tried to do something about it but the Session object is created automatically and immediately assigned to the list of openviduu sessions, and something is breaking on it.

“”"
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) ~[httpclient-4.5.11.jar!/:4.5.11]

at io.openvidu.java.client.Session.getSessionIdHttp(Session.java:473) ~[openvidu-java-client-2.14.0.jar!/:na]

at io.openvidu.java.client.Session.(Session.java:57) ~[openvidu-java-client-2.14.0.jar!/:na]

at io.openvidu.java.client.OpenVidu.createSession(OpenVidu.java:131) ~[openvidu-java-client-2.14.0.jar!/:na]

at io.openvidu.js.java.service.video.SessionService.newSessionCreating(SessionService.java:123) ~[classes!/:2.11.0]
“”"

UPDATE:
I tried to write my own application where I use this code:
controller:
@GetMapping("/openvidurequest")
public void openviduRequest(){
sessionService.sampleRequest();
}

service method:
@SneakyThrows
public void sampleRequest(){
HttpPost request = new HttpPost(“https://10.0.0.XX”);
HttpClient httpClient = getHttpClient();
httpClient.execute(request);
}

a method I made from the library where I changed the timeout time:

HttpClient getHttpClient(){
HttpClient httpClient;

    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("OPENVIDUAPP", SECRET);
    provider.setCredentials(AuthScope.ANY, credentials);

    SSLContext sslContext;

    TrustStrategy trustStrategy = new TrustStrategy() {
        @Override
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }
    };

    try {
        sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build();
    } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
        throw new RuntimeException(e);
    }

    RequestConfig.Builder requestBuilder = RequestConfig.custom();
    requestBuilder = requestBuilder.setConnectTimeout(100000);
    requestBuilder = requestBuilder.setConnectionRequestTimeout(100000);

    httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestBuilder.build())
            .setConnectionTimeToLive(100, TimeUnit.SECONDS).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
            .setSSLContext(sslContext).setDefaultCredentialsProvider(provider).build();
    return  httpClient;
}

and still same problem :frowning:

{
“timestamp”: 1589831280601,
“status”: 500,
“error”: “Internal Server Error”,
“message”: “Connect to 10.0.0.XX:443 [/10.0.0.XX] failed: Read timed out”,
“path”: “/video/openvidurequest”
}

but now after 100 s

When I try to connect directly to the openvid server via postman, then no problem.

If Node works and Postman works, then Java http clients seems to have a problem with the network.

Is there any way to fix this?

I have no idea how to solve this :frowning:

The java client appears in the microservice architecture and is in the internal network, it also has its own reverseProxy which is shared with other services

Java client is working well in usual scenarios. If you provide us an easy way to reproduce the issue, we can take a look. If not, it is impossible to us to know what is happening.