Search in sources :

Example 26 with BallerinaConnectorException

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);
}
Also used : HttpConnectorListener(org.wso2.transport.http.netty.contract.HttpConnectorListener) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) HashMap(java.util.HashMap) BLangFunctions(org.ballerinalang.util.program.BLangFunctions) BLangConnectorSPIUtil(org.ballerinalang.connector.api.BLangConnectorSPIUtil) Executor(org.ballerinalang.connector.api.Executor) TraceManagerWrapper(org.ballerinalang.util.tracer.TraceManagerWrapper) HashSet(java.util.HashSet) TraceConstants(org.ballerinalang.util.tracer.TraceConstants) PROTOCOL_PACKAGE_HTTP(org.ballerinalang.net.http.HttpConstants.PROTOCOL_PACKAGE_HTTP) BTypeDescValue(org.ballerinalang.model.values.BTypeDescValue) Tracer(org.ballerinalang.util.tracer.Tracer) Map(java.util.Map) FilterHolder(org.ballerinalang.net.http.serviceendpoint.FilterHolder) Constants(org.ballerinalang.runtime.Constants) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) HashMap(java.util.HashMap) BValue(org.ballerinalang.model.values.BValue) Tracer(org.ballerinalang.util.tracer.Tracer) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 27 with BallerinaConnectorException

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()));
    }
}
Also used : HttpResource(org.ballerinalang.net.http.HttpResource) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 28 with BallerinaConnectorException

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;
}
Also used : HttpResource(org.ballerinalang.net.http.HttpResource) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException)

Example 29 with BallerinaConnectorException

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();
}
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)

Example 30 with BallerinaConnectorException

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();
}
Also used : BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) WsClientConnectorConfig(org.wso2.transport.http.netty.contract.websocket.WsClientConnectorConfig) Value(org.ballerinalang.connector.api.Value) Service(org.ballerinalang.connector.api.Service) WebSocketService(org.ballerinalang.net.http.WebSocketService) WebSocketService(org.ballerinalang.net.http.WebSocketService) Struct(org.ballerinalang.connector.api.Struct)

Aggregations

BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)32 Struct (org.ballerinalang.connector.api.Struct)9 BString (org.ballerinalang.model.values.BString)9 BStruct (org.ballerinalang.model.values.BStruct)9 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)7 HashMap (java.util.HashMap)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 URITemplateException (org.ballerinalang.net.uri.URITemplateException)4 BValue (org.ballerinalang.model.values.BValue)3 HttpResource (org.ballerinalang.net.http.HttpResource)3 URI (java.net.URI)2 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)2 AnnAttrValue (org.ballerinalang.connector.api.AnnAttrValue)2 BLangConnectorSPIUtil (org.ballerinalang.connector.api.BLangConnectorSPIUtil)2 Executor (org.ballerinalang.connector.api.Executor)2 ParamDetail (org.ballerinalang.connector.api.ParamDetail)2 Resource (org.ballerinalang.connector.api.Resource)2