use of org.ballerinalang.connector.api.BallerinaConnectorException in project ballerina by ballerina-lang.
the class BallerinaHTTPConnectorListener method extractPropertiesAndStartResourceExecution.
protected void extractPropertiesAndStartResourceExecution(HTTPCarbonMessage httpCarbonMessage, HttpResource httpResource) {
boolean isTransactionInfectable = httpResource.isTransactionInfectable();
Map<String, Object> properties = collectRequestProperties(httpCarbonMessage, isTransactionInfectable);
properties.put(HttpConstants.REMOTE_ADDRESS, httpCarbonMessage.getProperty(HttpConstants.REMOTE_ADDRESS));
properties.put(HttpConstants.ORIGIN_HOST, httpCarbonMessage.getHeader(HttpConstants.ORIGIN_HOST));
BValue[] signatureParams = HttpDispatcher.getSignatureParameters(httpResource, httpCarbonMessage);
// invoke the request path filters
invokeRequestFilters(httpCarbonMessage, signatureParams[1], getRequestFilterContext(httpResource));
Tracer tracer = TraceManagerWrapper.newTracer(null, false);
httpCarbonMessage.getHeaders().entries().stream().filter(c -> c.getKey().startsWith(TraceConstants.TRACE_PREFIX)).forEach(e -> tracer.addProperty(e.getKey(), e.getValue()));
Map<String, String> tags = new HashMap<>();
tags.put("http.method", (String) httpCarbonMessage.getProperty("HTTP_METHOD"));
tags.put("http.url", (String) httpCarbonMessage.getProperty("REQUEST_URL"));
tracer.addTags(tags);
CallableUnitCallback callback = new HttpCallableUnitCallback(httpCarbonMessage);
// TODO handle BallerinaConnectorException
Executor.submit(httpResource.getBalResource(), callback, properties, tracer, signatureParams);
}
use of org.ballerinalang.connector.api.BallerinaConnectorException in project ballerina by ballerina-lang.
the class BallerinaWebSubConnectionListener method onMessage.
@Override
public void onMessage(HTTPCarbonMessage httpCarbonMessage) {
try {
HttpResource httpResource;
if (accessed(httpCarbonMessage)) {
if (httpCarbonMessage.getProperty(HTTP_RESOURCE) instanceof String && httpCarbonMessage.getProperty(HTTP_RESOURCE).equals(WebSubSubscriberConstants.ANNOTATED_TOPIC)) {
autoRespondToIntentVerification(httpCarbonMessage);
return;
}
httpResource = (HttpResource) httpCarbonMessage.getProperty(HTTP_RESOURCE);
extractPropertiesAndStartResourceExecution(httpCarbonMessage, httpResource);
return;
}
httpResource = WebSubDispatcher.findResource(webSubServicesRegistry, httpCarbonMessage);
if (httpCarbonMessage.getProperty(HTTP_RESOURCE) == null && HttpDispatcher.shouldDiffer(httpResource, hasFilters())) {
httpCarbonMessage.setProperty(HTTP_RESOURCE, httpResource);
return;
}
extractPropertiesAndStartResourceExecution(httpCarbonMessage, httpResource);
} catch (BallerinaException ex) {
HttpUtil.handleFailure(httpCarbonMessage, new BallerinaConnectorException(ex.getMessage(), ex.getCause()));
}
}
use of org.ballerinalang.connector.api.BallerinaConnectorException in project ballerina by ballerina-lang.
the class WebSubResourceDispatcher method findResource.
static HttpResource findResource(HttpService service, HTTPCarbonMessage inboundRequest) throws BallerinaConnectorException, ServerConnectorException {
String method = (String) inboundRequest.getProperty(HttpConstants.HTTP_METHOD);
HttpResource httpResource = null;
String resourceName;
switch(method) {
case HttpConstants.HTTP_METHOD_POST:
resourceName = WebSubSubscriberConstants.RESOURCE_NAME_ON_NOTIFICATION;
break;
case HttpConstants.HTTP_METHOD_GET:
resourceName = WebSubSubscriberConstants.RESOURCE_NAME_VERIFY_INTENT;
break;
default:
throw new BallerinaConnectorException("method not allowed for WebSub Subscriber Services : " + method);
}
for (HttpResource resource : service.getResources()) {
if (resource.getName().equals(resourceName)) {
httpResource = resource;
break;
}
}
if (httpResource == null) {
if (WebSubSubscriberConstants.RESOURCE_NAME_VERIFY_INTENT.equals(resourceName)) {
// if the request is a GET request indicating an intent verification request, and the user has not
// specified an onVerifyIntent resource, assume auto intent verification and respond
String annotatedTopic = (service.getBalService()).getAnnotationList(WebSubSubscriberConstants.WEBSUB_PACKAGE_PATH, WebSubSubscriberConstants.ANN_NAME_WEBSUB_SUBSCRIBER_SERVICE_CONFIG).get(0).getValue().getStringField(WebSubSubscriberConstants.ANN_WEBSUB_ATTR_TOPIC);
inboundRequest.setProperty(WebSubSubscriberConstants.ANNOTATED_TOPIC, annotatedTopic);
inboundRequest.setProperty(Constants.HTTP_RESOURCE, WebSubSubscriberConstants.ANNOTATED_TOPIC);
} else {
inboundRequest.setProperty(HttpConstants.HTTP_STATUS_CODE, 404);
throw new BallerinaConnectorException("no matching WebSub Subscriber service resource " + resourceName + " found for method : " + method);
}
}
return httpResource;
}
use of org.ballerinalang.connector.api.BallerinaConnectorException 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.BallerinaConnectorException 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