use of org.ballerinalang.connector.api.Resource in project ballerina by ballerina-lang.
the class WebSocketDispatcher method dispatchIdleTimeout.
public static void dispatchIdleTimeout(WebSocketOpenConnectionInfo connectionInfo, WebSocketControlMessage controlMessage) {
WebSocketService wsService = connectionInfo.getService();
Resource onIdleTimeoutResource = wsService.getResourceByName(WebSocketConstants.RESOURCE_NAME_ON_IDLE_TIMEOUT);
if (onIdleTimeoutResource == null) {
return;
}
List<ParamDetail> paramDetails = onIdleTimeoutResource.getParamDetails();
BValue[] bValues = new BValue[paramDetails.size()];
bValues[0] = connectionInfo.getWsConnection();
// TODO handle BallerinaConnectorException
Executor.submit(onIdleTimeoutResource, new WebSocketEmptyCallableUnitCallback(), null, null, bValues);
}
use of org.ballerinalang.connector.api.Resource in project ballerina by ballerina-lang.
the class WebSocketDispatcher method dispatchTextMessage.
public static void dispatchTextMessage(WebSocketOpenConnectionInfo connectionInfo, WebSocketTextMessage textMessage) {
WebSocketService wsService = connectionInfo.getService();
Resource onTextMessageResource = wsService.getResourceByName(WebSocketConstants.RESOURCE_NAME_ON_TEXT_MESSAGE);
if (onTextMessageResource == null) {
return;
}
List<ParamDetail> paramDetails = onTextMessageResource.getParamDetails();
BValue[] bValues = new BValue[paramDetails.size()];
bValues[0] = connectionInfo.getWsConnection();
BStruct wsTextFrame = wsService.createTextFrameStruct();
wsTextFrame.setStringField(0, textMessage.getText());
if (textMessage.isFinalFragment()) {
wsTextFrame.setBooleanField(0, 1);
} else {
wsTextFrame.setBooleanField(0, 0);
}
bValues[1] = wsTextFrame;
// TODO handle BallerinaConnectorException
Executor.submit(onTextMessageResource, new WebSocketEmptyCallableUnitCallback(), null, null, bValues);
}
use of org.ballerinalang.connector.api.Resource in project ballerina by ballerina-lang.
the class WebSocketDispatcher method dispatchPingMessage.
private static void dispatchPingMessage(WebSocketOpenConnectionInfo connectionInfo, WebSocketControlMessage controlMessage) {
WebSocketService wsService = connectionInfo.getService();
Resource onPingMessageResource = wsService.getResourceByName(WebSocketConstants.RESOURCE_NAME_ON_PING);
if (onPingMessageResource == null) {
pingAutomatically(controlMessage);
return;
}
List<ParamDetail> paramDetails = onPingMessageResource.getParamDetails();
BValue[] bValues = new BValue[paramDetails.size()];
bValues[0] = connectionInfo.getWsConnection();
BStruct wsPingFrame = wsService.createPingFrameStruct();
byte[] data = controlMessage.getByteArray();
wsPingFrame.setBlobField(0, data);
bValues[1] = wsPingFrame;
// TODO handle BallerinaConnectorException
Executor.submit(onPingMessageResource, new WebSocketEmptyCallableUnitCallback(), null, null, bValues);
}
use of org.ballerinalang.connector.api.Resource in project ballerina by ballerina-lang.
the class WebSocketDispatcher method dispatchCloseMessage.
public static void dispatchCloseMessage(WebSocketOpenConnectionInfo connectionInfo, WebSocketCloseMessage closeMessage) {
WebSocketService wsService = connectionInfo.getService();
Resource onCloseResource = wsService.getResourceByName(WebSocketConstants.RESOURCE_NAME_ON_CLOSE);
if (onCloseResource == null) {
return;
}
List<ParamDetail> paramDetails = onCloseResource.getParamDetails();
BValue[] bValues = new BValue[paramDetails.size()];
bValues[0] = connectionInfo.getWsConnection();
BStruct wsCloseFrame = wsService.createCloseFrameStruct();
wsCloseFrame.setIntField(0, closeMessage.getCloseCode());
wsCloseFrame.setStringField(0, closeMessage.getCloseReason());
bValues[1] = wsCloseFrame;
// TODO handle BallerinaConnectorException
Executor.submit(onCloseResource, new WebSocketEmptyCallableUnitCallback(), null, null, bValues);
}
use of org.ballerinalang.connector.api.Resource in project ballerina by ballerina-lang.
the class WebSocketServerConnectorListener method onMessage.
@Override
public void onMessage(WebSocketInitMessage webSocketInitMessage) {
HTTPCarbonMessage msg = new HTTPCarbonMessage(((DefaultWebSocketInitMessage) webSocketInitMessage).getHttpRequest());
Map<String, String> pathParams = new HashMap<>();
WebSocketService wsService = WebSocketDispatcher.findService(servicesRegistry, pathParams, webSocketInitMessage, msg);
BStruct serviceEndpoint = BLangConnectorSPIUtil.createBStruct(wsService.getResources()[0].getResourceInfo().getServiceInfo().getPackageInfo().getProgramFile(), PROTOCOL_PACKAGE_HTTP, WEBSOCKET_ENDPOINT);
BStruct serverConnector = WebSocketUtil.createAndGetBStruct(wsService.getResources()[0]);
serverConnector.addNativeData(WebSocketConstants.WEBSOCKET_MESSAGE, webSocketInitMessage);
serverConnector.addNativeData(WebSocketConstants.WEBSOCKET_SERVICE, wsService);
serviceEndpoint.setRefField(SERVICE_ENDPOINT_CONNECTION_INDEX, serverConnector);
serviceEndpoint.setRefField(3, new BMap());
serverConnector.addNativeData(WEBSOCKET_ENDPOINT, serviceEndpoint);
Map<String, String> upgradeHeaders = webSocketInitMessage.getHeaders();
BMap<String, BString> bUpgradeHeaders = new BMap<>();
upgradeHeaders.forEach((key, value) -> bUpgradeHeaders.put(key, new BString(value)));
serviceEndpoint.setRefField(4, bUpgradeHeaders);
Resource onUpgradeResource = wsService.getResourceByName(WebSocketConstants.RESOURCE_NAME_ON_UPGRADE);
if (onUpgradeResource != null) {
Semaphore semaphore = new Semaphore(0);
AtomicBoolean isResourceExeSuccessful = new AtomicBoolean(false);
BStruct inRequest = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), PROTOCOL_PACKAGE_HTTP, HttpConstants.REQUEST);
BStruct inRequestEntity = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), org.ballerinalang.mime.util.Constants.PROTOCOL_PACKAGE_MIME, Constants.ENTITY);
BStruct mediaType = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), org.ballerinalang.mime.util.Constants.PROTOCOL_PACKAGE_MIME, Constants.MEDIA_TYPE);
BStruct cacheControlStruct = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), PROTOCOL_PACKAGE_HTTP, REQUEST_CACHE_CONTROL);
RequestCacheControlStruct requestCacheControl = new RequestCacheControlStruct(cacheControlStruct);
HttpUtil.populateInboundRequest(inRequest, inRequestEntity, mediaType, msg, requestCacheControl);
List<ParamDetail> paramDetails = onUpgradeResource.getParamDetails();
BValue[] bValues = new BValue[paramDetails.size()];
bValues[0] = serviceEndpoint;
bValues[1] = inRequest;
WebSocketDispatcher.setPathParams(bValues, paramDetails, pathParams, 2);
Tracer tracer = TraceManagerWrapper.newTracer(null, false);
upgradeHeaders.entrySet().stream().filter(c -> c.getKey().startsWith(TraceConstants.TRACE_PREFIX)).forEach(e -> tracer.addProperty(e.getKey(), e.getValue()));
Executor.submit(onUpgradeResource, new CallableUnitCallback() {
@Override
public void notifySuccess() {
isResourceExeSuccessful.set(true);
semaphore.release();
}
@Override
public void notifyFailure(BStruct error) {
ErrorHandlerUtils.printError("error: " + BLangVMErrors.getPrintableStackTrace(error));
semaphore.release();
}
}, null, tracer, bValues);
try {
semaphore.acquire();
if (isResourceExeSuccessful.get() && !webSocketInitMessage.isCancelled() && !webSocketInitMessage.isHandshakeStarted()) {
WebSocketUtil.handleHandshake(wsService, null, serverConnector);
}
} catch (InterruptedException e) {
throw new BallerinaConnectorException("Connection interrupted during handshake");
}
} else {
WebSocketUtil.handleHandshake(wsService, null, serverConnector);
}
}
Aggregations