Search in sources :

Example 1 with FilterHolder

use of org.ballerinalang.net.http.serviceendpoint.FilterHolder in project ballerina by ballerina-lang.

the class BallerinaHTTPConnectorListener method invokeRequestFilters.

/**
 * Invokes the set of filters for the request path. If one filter fails, the execution would stop and the
 * relevant error message given from the filter will be returned to the user.
 *
 * @param httpCarbonMessage {@link HTTPCarbonMessage} instance
 * @param requestObject     Representation of ballerina.net.Request struct
 * @param filterCtxt        filtering criteria
 */
private void invokeRequestFilters(HTTPCarbonMessage httpCarbonMessage, BValue requestObject, BValue filterCtxt) {
    if (!hasFilters()) {
        // no filters, return
        return;
    }
    for (FilterHolder filterHolder : filterHolders) {
        // get the request filter function and invoke
        BValue[] returnValue = BLangFunctions.invokeCallable(filterHolder.getRequestFilterFunction().getFunctionInfo(), new BValue[] { requestObject, filterCtxt });
        BStruct filterResultStruct = (BStruct) returnValue[0];
        if (filterResultStruct.getBooleanField(0) == 0) {
            // filter returned false, stop further execution
            // get the status code and the message
            int filterStatusCode = Math.toIntExact(filterResultStruct.getIntField(0));
            // if the status code returned by filter result is 4xx, use it, else use the code 400
            int responseStatusCode = filterStatusCode >= 400 && filterStatusCode < 500 ? filterStatusCode : 400;
            httpCarbonMessage.setProperty(HttpConstants.HTTP_STATUS_CODE, responseStatusCode);
            throw new BallerinaException("Request failed: " + filterResultStruct.getStringField(0));
        }
    // filter has returned true, can continue
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) FilterHolder(org.ballerinalang.net.http.serviceendpoint.FilterHolder) BValue(org.ballerinalang.model.values.BValue) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 2 with FilterHolder

use of org.ballerinalang.net.http.serviceendpoint.FilterHolder 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();
}
Also used : BLogManager(org.ballerinalang.logging.BLogManager) BStruct(org.ballerinalang.model.values.BStruct) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) FilterHolder(org.ballerinalang.net.http.serviceendpoint.FilterHolder) HttpConnectorPortBindingListener(org.ballerinalang.net.http.HttpConnectorPortBindingListener) ServerConnectorFuture(org.wso2.transport.http.netty.contract.ServerConnectorFuture) BallerinaWebSubConnectionListener(org.ballerinalang.net.websub.BallerinaWebSubConnectionListener) IOException(java.io.IOException) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct) ServerConnector(org.wso2.transport.http.netty.contract.ServerConnector) TraceLogConfigurationException(org.ballerinalang.logging.exceptions.TraceLogConfigurationException) WebSubServicesRegistry(org.ballerinalang.net.websub.WebSubServicesRegistry)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)2 FilterHolder (org.ballerinalang.net.http.serviceendpoint.FilterHolder)2 IOException (java.io.IOException)1 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)1 Struct (org.ballerinalang.connector.api.Struct)1 BLogManager (org.ballerinalang.logging.BLogManager)1 TraceLogConfigurationException (org.ballerinalang.logging.exceptions.TraceLogConfigurationException)1 BValue (org.ballerinalang.model.values.BValue)1 HttpConnectorPortBindingListener (org.ballerinalang.net.http.HttpConnectorPortBindingListener)1 BallerinaWebSubConnectionListener (org.ballerinalang.net.websub.BallerinaWebSubConnectionListener)1 WebSubServicesRegistry (org.ballerinalang.net.websub.WebSubServicesRegistry)1 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)1 ServerConnector (org.wso2.transport.http.netty.contract.ServerConnector)1 ServerConnectorFuture (org.wso2.transport.http.netty.contract.ServerConnectorFuture)1