Search in sources :

Example 1 with HttpResource

use of org.ballerinalang.net.http.HttpResource 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 HttpResource

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

the class WebSubHttpService method buildWebSubSubscriberHttpService.

/**
 * Builds the HTTP service representation of the service.
 *
 * @param service   the service for which the HTTP representation is built
 * @return  the built HttpService representation
 */
static HttpService buildWebSubSubscriberHttpService(Service service) {
    WebSubHttpService httpService = new WebSubHttpService(service);
    Annotation serviceConfigAnnotation = getWebSubSubscriberServiceConfigAnnotation(service);
    if (serviceConfigAnnotation == null) {
        logger.debug("ServiceConfig not specified in the Service instance, using default base path");
        // service name cannot start with / hence concat
        httpService.setBasePath(HttpConstants.DEFAULT_BASE_PATH.concat(httpService.getName()));
        return httpService;
    }
    Struct serviceConfig = serviceConfigAnnotation.getValue();
    httpService.setBasePath(serviceConfig.getStringField(BASE_PATH_FIELD));
    List<HttpResource> resources = new ArrayList<>();
    for (Resource resource : httpService.getBallerinaService().getResources()) {
        HttpResource httpResource = WebSubHttpResource.buildWebSubHttpResource(resource, httpService);
        resources.add(httpResource);
    }
    httpService.setResources(resources);
    httpService.setAllAllowedMethods(DispatcherUtil.getAllResourceMethods(httpService));
    return httpService;
}
Also used : HttpResource(org.ballerinalang.net.http.HttpResource) ArrayList(java.util.ArrayList) HttpResource(org.ballerinalang.net.http.HttpResource) Resource(org.ballerinalang.connector.api.Resource) Annotation(org.ballerinalang.connector.api.Annotation) Struct(org.ballerinalang.connector.api.Struct)

Example 3 with HttpResource

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

the class Services method invokeNew.

public static HTTPCarbonMessage invokeNew(CompileResult compileResult, String pkgName, String endpointName, HTTPTestRequest request) {
    ProgramFile programFile = compileResult.getProgFile();
    BStruct connectorEndpoint = BLangConnectorSPIUtil.getPackageEndpoint(programFile, pkgName, endpointName);
    HTTPServicesRegistry httpServicesRegistry = (HTTPServicesRegistry) connectorEndpoint.getNativeData("HTTP_SERVICE_REGISTRY");
    TestCallableUnitCallback callback = new TestCallableUnitCallback(request);
    request.setCallback(callback);
    HttpResource resource = HttpDispatcher.findResource(httpServicesRegistry, request);
    if (resource == null) {
        return callback.getResponseMsg();
    }
    // TODO below should be fixed properly
    // basically need to find a way to pass information from server connector side to client connector side
    Map<String, Object> properties = null;
    if (request.getProperty(HttpConstants.SRC_HANDLER) != null) {
        Object srcHandler = request.getProperty(HttpConstants.SRC_HANDLER);
        properties = Collections.singletonMap(HttpConstants.SRC_HANDLER, srcHandler);
    }
    BValue[] signatureParams = HttpDispatcher.getSignatureParameters(resource, request);
    callback.setRequestStruct(signatureParams[0]);
    Executor.submit(resource.getBalResource(), callback, properties, null, signatureParams);
    callback.sync();
    return callback.getResponseMsg();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HTTPServicesRegistry(org.ballerinalang.net.http.HTTPServicesRegistry) HttpResource(org.ballerinalang.net.http.HttpResource) BValue(org.ballerinalang.model.values.BValue) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Example 4 with HttpResource

use of org.ballerinalang.net.http.HttpResource 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 5 with HttpResource

use of org.ballerinalang.net.http.HttpResource 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)

Aggregations

HttpResource (org.ballerinalang.net.http.HttpResource)5 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)3 ArrayList (java.util.ArrayList)1 Annotation (org.ballerinalang.connector.api.Annotation)1 Resource (org.ballerinalang.connector.api.Resource)1 Struct (org.ballerinalang.connector.api.Struct)1 BString (org.ballerinalang.model.values.BString)1 BStruct (org.ballerinalang.model.values.BStruct)1 BValue (org.ballerinalang.model.values.BValue)1 HTTPServicesRegistry (org.ballerinalang.net.http.HTTPServicesRegistry)1 HttpService (org.ballerinalang.net.http.HttpService)1 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)1 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)1