Search in sources :

Example 1 with HttpService

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

the class WebSubDispatcher method findResource.

/**
 * This method finds the matching resource for the incoming request.
 *
 * @param servicesRegistry  the WebSubServicesRegistry including registered WebSub Services
 * @param httpCarbonMessage incoming message.
 * @return matching resource.
 */
static HttpResource findResource(WebSubServicesRegistry servicesRegistry, HTTPCarbonMessage httpCarbonMessage) {
    HttpResource resource = null;
    String protocol = (String) httpCarbonMessage.getProperty(HttpConstants.PROTOCOL);
    if (protocol == null) {
        throw new BallerinaConnectorException("protocol not defined in the incoming request");
    }
    try {
        HttpService service = HttpDispatcher.findService(servicesRegistry, httpCarbonMessage);
        if (service == null) {
            throw new BallerinaConnectorException("no service found to handle the service request");
        // Finer details of the errors are thrown from the dispatcher itself, ideally we shouldn't get here.
        }
        resource = WebSubResourceDispatcher.findResource(service, httpCarbonMessage);
    } catch (Throwable throwable) {
        handleError(httpCarbonMessage, throwable);
    }
    return resource;
}
Also used : HttpResource(org.ballerinalang.net.http.HttpResource) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) HttpService(org.ballerinalang.net.http.HttpService)

Example 2 with HttpService

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

the class WebSubServicesRegistry method registerWebSubSubscriberService.

/**
 * Register a WebSubSubscriber service in the map.
 *
 * @param service the {@link Service} to be registered
 */
public void registerWebSubSubscriberService(Service service) {
    HttpService httpService = WebSubHttpService.buildWebSubSubscriberHttpService(service);
    servicesInfoMap.put(httpService.getBasePath(), httpService);
    logger.info("Service deployed : " + service.getName() + " with context " + httpService.getBasePath());
    // basePath will get cached after registering service
    sortedServiceURIs.add(httpService.getBasePath());
    sortedServiceURIs.sort((basePath1, basePath2) -> basePath2.length() - basePath1.length());
    WebSubSubscriberServiceValidator.validateResources(httpService);
}
Also used : HttpService(org.ballerinalang.net.http.HttpService)

Example 3 with HttpService

use of org.ballerinalang.net.http.HttpService 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);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BMap(org.ballerinalang.model.values.BMap) HttpService(org.ballerinalang.net.http.HttpService) BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct)

Aggregations

HttpService (org.ballerinalang.net.http.HttpService)3 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)1 Struct (org.ballerinalang.connector.api.Struct)1 BMap (org.ballerinalang.model.values.BMap)1 BString (org.ballerinalang.model.values.BString)1 BStruct (org.ballerinalang.model.values.BStruct)1 HttpResource (org.ballerinalang.net.http.HttpResource)1