Search in sources :

Example 1 with URITemplateException

use of org.ballerinalang.net.uri.URITemplateException in project ballerina by ballerina-lang.

the class WebSocketServicesRegistry method addUpgradableServiceByName.

public void addUpgradableServiceByName(WebSocketService service, String basePath) {
    basePath = urlDecode(basePath);
    service.setBasePath(basePath);
    try {
        getUriTemplate().parse(basePath, service, new WebSocketDataElementFactory());
    } catch (URITemplateException | UnsupportedEncodingException e) {
        throw new BallerinaConnectorException(e.getMessage());
    }
    logger.info("Service deployed : " + service.getName() + " with context " + basePath);
}
Also used : URITemplateException(org.ballerinalang.net.uri.URITemplateException) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with URITemplateException

use of org.ballerinalang.net.uri.URITemplateException in project ballerina by ballerina-lang.

the class HttpResourceDispatcher method findResource.

public static HttpResource findResource(HttpService service, HTTPCarbonMessage inboundRequest) throws BallerinaConnectorException {
    String method = (String) inboundRequest.getProperty(HttpConstants.HTTP_METHOD);
    String subPath = (String) inboundRequest.getProperty(HttpConstants.SUB_PATH);
    subPath = sanitizeSubPath(subPath);
    Map<String, String> resourceArgumentValues = new HashMap<>();
    try {
        HttpResource resource = service.getUriTemplate().matches(subPath, resourceArgumentValues, inboundRequest);
        if (resource != null) {
            inboundRequest.setProperty(HttpConstants.RESOURCE_ARGS, resourceArgumentValues);
            inboundRequest.setProperty(HttpConstants.RESOURCES_CORS, resource.getCorsHeaders());
            return resource;
        } else {
            if (method.equals(HttpConstants.HTTP_METHOD_OPTIONS)) {
                handleOptionsRequest(inboundRequest, service);
            } else {
                inboundRequest.setProperty(HttpConstants.HTTP_STATUS_CODE, 404);
                throw new BallerinaConnectorException("no matching resource found for path : " + inboundRequest.getProperty(HttpConstants.TO) + " , method : " + method);
            }
            return null;
        }
    } catch (URITemplateException e) {
        throw new BallerinaConnectorException(e.getMessage());
    }
}
Also used : URITemplateException(org.ballerinalang.net.uri.URITemplateException) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) HashMap(java.util.HashMap)

Example 3 with URITemplateException

use of org.ballerinalang.net.uri.URITemplateException in project ballerina by ballerina-lang.

the class HttpService method buildHttpService.

public static HttpService buildHttpService(Service service) {
    HttpService httpService = new HttpService(service);
    Annotation serviceConfigAnnotation = getHttpServiceConfigAnnotation(service);
    if (serviceConfigAnnotation == null) {
        log.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()));
    } else {
        Struct serviceConfig = serviceConfigAnnotation.getValue();
        httpService.setBasePath(serviceConfig.getStringField(BASE_PATH_FIELD));
        httpService.setCompression(serviceConfig.getEnumField(COMPRESSION_FIELD));
        httpService.setCorsHeaders(CorsHeaders.buildCorsHeaders(serviceConfig.getStructField(CORS_FIELD)));
        httpService.setWebSocketUpgradeConfig(serviceConfig.getStructField(WEBSOCKET_UPGRADE_FIELD));
    }
    List<HttpResource> resources = new ArrayList<>();
    for (Resource resource : httpService.getBallerinaService().getResources()) {
        HttpResource httpResource = HttpResource.buildHttpResource(resource, httpService);
        try {
            httpService.getUriTemplate().parse(httpResource.getPath(), httpResource, new HttpResourceElementFactory());
        } catch (URITemplateException | UnsupportedEncodingException e) {
            throw new BallerinaConnectorException(e.getMessage());
        }
        resources.add(httpResource);
    }
    httpService.setResources(resources);
    httpService.setAllAllowedMethods(DispatcherUtil.getAllResourceMethods(httpService));
    return httpService;
}
Also used : URITemplateException(org.ballerinalang.net.uri.URITemplateException) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) ArrayList(java.util.ArrayList) Resource(org.ballerinalang.connector.api.Resource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Annotation(org.ballerinalang.connector.api.Annotation) Struct(org.ballerinalang.connector.api.Struct)

Example 4 with URITemplateException

use of org.ballerinalang.net.uri.URITemplateException in project ballerina by ballerina-lang.

the class WebSocketServicesRegistry method registerService.

public void registerService(WebSocketService service) {
    String basePath = findFullWebSocketUpgradePath(service);
    if (basePath == null) {
        basePath = "/";
    }
    basePath = urlDecode(basePath);
    service.setBasePath(basePath);
    // TODO: Add websocket services to the service registry when service creation get available.
    try {
        getUriTemplate().parse(basePath, service, new WebSocketDataElementFactory());
    } catch (URITemplateException | UnsupportedEncodingException e) {
        throw new BallerinaConnectorException(e.getMessage());
    }
    logger.info("Service deployed : " + service.getName() + " with context " + basePath);
}
Also used : URITemplateException(org.ballerinalang.net.uri.URITemplateException) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)4 URITemplateException (org.ballerinalang.net.uri.URITemplateException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Annotation (org.ballerinalang.connector.api.Annotation)1 Resource (org.ballerinalang.connector.api.Resource)1 Struct (org.ballerinalang.connector.api.Struct)1