use of org.ballerinalang.connector.api.Struct in project ballerina by ballerina-lang.
the class RegisterWebSubSubscriberServiceEndpoint method execute.
@Override
public void execute(Context context) {
Service service = BLangConnectorSPIUtil.getServiceRegistered(context);
Struct subscriberServiceEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
Struct serviceEndpoint = ConnectorSPIModelHelper.createStruct((BStruct) ((BStruct) (subscriberServiceEndpoint.getVMValue())).getRefField(1));
WebSubServicesRegistry webSubServicesRegistry = (WebSubServicesRegistry) serviceEndpoint.getNativeData(WebSubSubscriberConstants.WEBSUB_SERVICE_REGISTRY);
webSubServicesRegistry.registerWebSubSubscriberService(service);
context.setReturnValues();
}
use of org.ballerinalang.connector.api.Struct in project ballerina by ballerina-lang.
the class RetrieveAnnotationsAndCallback method execute.
@Override
public void execute(Context context) {
Struct subscriberServiceEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
Struct serviceEndpoint = subscriberServiceEndpoint.getStructField("serviceEndpoint");
HttpService httpService = ((HttpService) ((WebSubServicesRegistry) serviceEndpoint.getNativeData(WebSubSubscriberConstants.WEBSUB_SERVICE_REGISTRY)).getServicesInfoByInterface().values().toArray()[0]);
BMap<String, BString> subscriptionDetails = new BMap<>();
Struct annotationStruct = httpService.getBalService().getAnnotationList(WebSubSubscriberConstants.WEBSUB_PACKAGE_PATH, WebSubSubscriberConstants.ANN_NAME_WEBSUB_SUBSCRIBER_SERVICE_CONFIG).get(0).getValue();
subscriptionDetails.put(WebSubSubscriberConstants.ANN_WEBSUB_ATTR_SUBSCRIBE_ON_STARTUP, new BString(Boolean.toString(annotationStruct.getBooleanField(WebSubSubscriberConstants.ANN_WEBSUB_ATTR_SUBSCRIBE_ON_STARTUP))));
subscriptionDetails.put(WebSubSubscriberConstants.ANN_WEBSUB_ATTR_HUB, new BString(annotationStruct.getStringField(WebSubSubscriberConstants.ANN_WEBSUB_ATTR_HUB)));
subscriptionDetails.put(WebSubSubscriberConstants.ANN_WEBSUB_ATTR_TOPIC, new BString(annotationStruct.getStringField(WebSubSubscriberConstants.ANN_WEBSUB_ATTR_TOPIC)));
subscriptionDetails.put(WebSubSubscriberConstants.ANN_WEBSUB_ATTR_LEASE_SECONDS, new BString(Long.toString(annotationStruct.getIntField(WebSubSubscriberConstants.ANN_WEBSUB_ATTR_LEASE_SECONDS))));
subscriptionDetails.put(WebSubSubscriberConstants.ANN_WEBSUB_ATTR_SECRET, new BString(annotationStruct.getStringField(WebSubSubscriberConstants.ANN_WEBSUB_ATTR_SECRET)));
// TODO: intro methods to return host+port and change instead of using connector ID, and fix http:// hack
String callback = httpService.getBasePath() + httpService.getResources().get(0).getPath();
BStruct serviceEndpointConfig = ((BStruct) ((BStruct) serviceEndpoint.getVMValue()).getRefField(1));
if (!serviceEndpointConfig.getStringField(0).equals("") && serviceEndpointConfig.getIntField(0) != 0) {
callback = serviceEndpointConfig.getStringField(0) + ":" + serviceEndpointConfig.getIntField(0) + callback;
} else {
callback = getServerConnector(serviceEndpoint).getConnectorID() + callback;
}
if (!callback.contains("://")) {
if (serviceEndpointConfig.getRefField(3) != null) {
// if secure socket is specified
callback = ("https://").concat(callback);
} else {
callback = ("http://").concat(callback);
}
}
subscriptionDetails.put("callback", new BString(callback));
context.setReturnValues(subscriptionDetails);
}
use of org.ballerinalang.connector.api.Struct in project ballerina by ballerina-lang.
the class StartWebSubSubscriberServiceEndpoint method execute.
@Override
public void execute(Context context) {
Struct subscriberServiceEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
Struct serviceEndpoint = ConnectorSPIModelHelper.createStruct((BStruct) ((BStruct) (subscriberServiceEndpoint.getVMValue())).getRefField(1));
ServerConnector serverConnector = getServerConnector(serviceEndpoint);
if (isHTTPTraceLoggerEnabled()) {
try {
((BLogManager) BLogManager.getLogManager()).setHttpTraceLogHandler();
} catch (IOException e) {
throw new BallerinaConnectorException("Invalid HTTP trace log parameters found.", e);
} catch (TraceLogConfigurationException e) {
throw new BallerinaConnectorException("Unsupported HTTP trace log configuration. " + e.getMessage(), e);
}
}
ServerConnectorFuture serverConnectorFuture = serverConnector.start();
WebSubServicesRegistry webSubServicesRegistry = (WebSubServicesRegistry) serviceEndpoint.getNativeData(WebSubSubscriberConstants.WEBSUB_SERVICE_REGISTRY);
HashSet<FilterHolder> filterHolder = getFilters(serviceEndpoint);
serverConnectorFuture.setHttpConnectorListener(new BallerinaWebSubConnectionListener(webSubServicesRegistry, filterHolder));
serverConnectorFuture.setPortBindingEventListener(new HttpConnectorPortBindingListener());
context.setReturnValues();
}
use of org.ballerinalang.connector.api.Struct 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();
}
use of org.ballerinalang.connector.api.Struct in project ballerina by ballerina-lang.
the class InitEndpoint method getListerConfig.
private ListenerConfiguration getListerConfig(Struct endpointConfig) {
String host = endpointConfig.getStringField(HttpConstants.ENDPOINT_CONFIG_HOST);
long port = endpointConfig.getIntField(HttpConstants.ENDPOINT_CONFIG_PORT);
String keepAlive = endpointConfig.getEnumField(HttpConstants.ENDPOINT_CONFIG_KEEP_ALIVE);
String transferEncoding = endpointConfig.getEnumField(HttpConstants.ENDPOINT_CONFIG_TRANSFER_ENCODING);
String chunking = endpointConfig.getEnumField(HttpConstants.ENDPOINT_CONFIG_CHUNKING);
Struct sslConfig = endpointConfig.getStructField(HttpConstants.ENDPOINT_CONFIG_SECURE_SOCKET);
String httpVersion = endpointConfig.getStringField(HttpConstants.ENDPOINT_CONFIG_VERSION);
Struct requestLimits = endpointConfig.getStructField(HttpConstants.ENDPOINT_REQUEST_LIMITS);
ListenerConfiguration listenerConfiguration = new ListenerConfiguration();
if (host == null || host.isEmpty()) {
listenerConfiguration.setHost(HttpConstants.HTTP_DEFAULT_HOST);
} else {
listenerConfiguration.setHost(host);
}
listenerConfiguration.setPort(Math.toIntExact(port));
listenerConfiguration.setKeepAliveConfig(HttpUtil.getKeepAliveConfig(keepAlive));
// chunking. Once we start supporting gzip, deflate, etc, we need to parse down the config.
if ((!transferEncoding.isEmpty()) && !HttpConstants.ANN_CONFIG_ATTR_CHUNKING.equalsIgnoreCase(transferEncoding)) {
throw new BallerinaConnectorException("Unsupported configuration found for Transfer-Encoding : " + transferEncoding);
}
listenerConfiguration.setChunkConfig(HttpUtil.getChunkConfig(chunking));
// Set Request validation limits.
if (requestLimits != null) {
setRequestSizeValidationConfig(requestLimits, listenerConfiguration);
}
// Set HTTP version
if (httpVersion != null) {
listenerConfiguration.setVersion(httpVersion);
}
if (sslConfig != null) {
return setSslConfig(sslConfig, listenerConfiguration);
}
listenerConfiguration.setServerHeader(getServerName());
return listenerConfiguration;
}
Aggregations