use of org.wso2.transport.http.netty.contract.websocket.WsClientConnectorConfig in project ballerina by ballerina-lang.
the class Start method execute.
@Override
public void execute(Context context) {
HttpWsConnectorFactory connectorFactory = HttpUtil.createHttpWsConnectionFactory();
Struct clientEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
Struct clientEndpointConfig = clientEndpoint.getStructField(HttpConstants.CLIENT_ENDPOINT_CONFIG);
Object configs = clientEndpointConfig.getNativeData(WebSocketConstants.CLIENT_CONNECTOR_CONFIGS);
if (configs == null || !(configs instanceof WsClientConnectorConfig)) {
throw new BallerinaConnectorException("Initialize the service before starting it");
}
WebSocketClientConnector clientConnector = connectorFactory.createWsClientConnector((WsClientConnectorConfig) configs);
WebSocketClientConnectorListener clientConnectorListener = new WebSocketClientConnectorListener();
Object serviceConfig = clientEndpointConfig.getNativeData(WebSocketConstants.CLIENT_SERVICE_CONFIG);
if (serviceConfig == null || !(serviceConfig instanceof WebSocketService)) {
throw new BallerinaConnectorException("Initialize the service before starting it");
}
WebSocketService wsService = (WebSocketService) serviceConfig;
HandshakeFuture handshakeFuture = clientConnector.connect(clientConnectorListener);
handshakeFuture.setHandshakeListener(new WsHandshakeListener(context, wsService, clientConnectorListener));
context.setReturnValues();
}
use of org.wso2.transport.http.netty.contract.websocket.WsClientConnectorConfig in project ballerina by ballerina-lang.
the class InitEndpoint method execute.
@Override
public void execute(Context context) {
Struct clientEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
Struct clientEndpointConfig = clientEndpoint.getStructField(HttpConstants.CLIENT_ENDPOINT_CONFIG);
String remoteUrl = clientEndpointConfig.getStringField(WebSocketConstants.CLIENT_URL_CONFIG);
Value clientServiceType = clientEndpointConfig.getTypeField(WebSocketConstants.CLIENT_SERVICE_CONFIG);
Service service = BLangConnectorSPIUtil.getServiceFromType(context.getProgramFile(), clientServiceType);
if (service == null) {
throw new BallerinaConnectorException("Cannot find client service: " + clientServiceType);
}
if (WebSocketConstants.WEBSOCKET_CLIENT_ENDPOINT_NAME.equals(service.getEndpointName())) {
WebSocketService wsService = new WebSocketService(service);
// TODO: Fix this validation
// WebSocketServiceValidator.validateServiceEndpoint(wsService);
WsClientConnectorConfig clientConnectorConfig = new WsClientConnectorConfig(remoteUrl);
Value[] subProtocolValues = clientEndpointConfig.getArrayField(WebSocketConstants.CLIENT_SUBPROTOCOLS_CONFIG);
if (subProtocolValues != null) {
clientConnectorConfig.setSubProtocols(Arrays.stream(subProtocolValues).map(Value::getStringValue).toArray(String[]::new));
}
Map<String, Value> headerValues = clientEndpointConfig.getMapField(WebSocketConstants.CLIENT_CUSTOMHEADERS_CONFIG);
if (headerValues != null) {
clientConnectorConfig.addHeaders(getCustomHeaders(headerValues));
}
long idleTimeoutInSeconds = clientEndpointConfig.getIntField(WebSocketConstants.CLIENT_IDLETIMOUT_CONFIG);
if (idleTimeoutInSeconds > 0) {
clientConnectorConfig.setIdleTimeoutInMillis((int) (idleTimeoutInSeconds * 1000));
}
clientEndpointConfig.addNativeData(WebSocketConstants.CLIENT_SERVICE_CONFIG, wsService);
clientEndpointConfig.addNativeData(WebSocketConstants.CLIENT_CONNECTOR_CONFIGS, clientConnectorConfig);
} else {
throw new BallerinaConnectorException("Incorrect endpoint: " + service.getEndpointName());
}
context.setReturnValues();
}
Aggregations