Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement mechanism for providing unique ids for JSON-RPC clients (#5775
- Loading branch information
Dmytro Kulieshov
committed
Jul 26, 2017
1 parent
b12bf55
commit 953894f
Showing
18 changed files
with
428 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...re-api-core/src/main/java/org/eclipse/che/api/core/websocket/impl/WebsocketIdService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2017 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.api.core.websocket.impl; | ||
|
||
import org.eclipse.che.api.core.jsonrpc.commons.JsonRpcException; | ||
import org.eclipse.che.api.core.jsonrpc.commons.RequestHandlerConfigurator; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import javax.websocket.Session; | ||
import java.util.Random; | ||
|
||
/** | ||
* Identification service provide means to set and get unique identifiers for | ||
* websocket clients. There are several identifier elements to distinguish: | ||
* clientId, endpointId, combinedId. Client id is the identifier of a client | ||
* that is passed over websocket to the client and back. EndpointId is called | ||
* to identify a websocket endpoint client connects through. CombinedId is a | ||
* combination of client and endpoint identifiers separated by a sequence of | ||
* special charaters, it is used internally. | ||
*/ | ||
@Singleton | ||
public class WebsocketIdService { | ||
private static final String ERROR_MESSAGE = "No session is associated with provided endpoint id"; | ||
private static final String SEPARATOR = "<-:->"; | ||
private static final Random GENERATOR = new Random(); | ||
|
||
private final WebSocketSessionRegistry registry; | ||
|
||
@Inject | ||
public WebsocketIdService(WebSocketSessionRegistry registry) { | ||
this.registry = registry; | ||
} | ||
|
||
private static String clientId() { | ||
return String.valueOf(GENERATOR.nextInt(Integer.MAX_VALUE)); | ||
} | ||
|
||
@Inject | ||
private void configureHandler(RequestHandlerConfigurator requestHandlerConfigurator) { | ||
requestHandlerConfigurator.newConfiguration() | ||
.methodName("websocketIdService/getId") | ||
.noParams() | ||
.resultAsString() | ||
.withFunction(this::extractClientId); | ||
|
||
requestHandlerConfigurator.newConfiguration() | ||
.methodName("websocketIdService/setId") | ||
.paramsAsString() | ||
.noResult() | ||
.withBiConsumer((oldCombinedId, newClientId) -> { | ||
String endpointId = extractEndpointId(oldCombinedId); | ||
String newCombinedId = getCombinedId(endpointId, newClientId); | ||
Session session = registry.remove(oldCombinedId) | ||
.orElseThrow(() -> new JsonRpcException(-27000, ERROR_MESSAGE)); | ||
|
||
registry.add(newCombinedId, session); | ||
}); | ||
} | ||
|
||
public String getCombinedId(String endpointId) { | ||
return clientId() + SEPARATOR + endpointId; | ||
} | ||
|
||
public String getCombinedId(String endpointId, String clientId) { | ||
return clientId + SEPARATOR + endpointId; | ||
} | ||
|
||
public String extractClientId(String combinedId) { | ||
return combinedId.split(SEPARATOR)[0]; | ||
} | ||
|
||
public String extractEndpointId(String combinedId) { | ||
return combinedId.split(SEPARATOR)[1]; | ||
} | ||
} |
85 changes: 0 additions & 85 deletions
85
...ore/src/test/java/org/eclipse/che/api/core/websocket/impl/BasicWebSocketEndpointTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.