Search in sources :

Example 1 with Annotation

use of org.ballerinalang.connector.api.Annotation in project ballerina by ballerina-lang.

the class WebSocketServicesRegistry method findFullWebSocketUpgradePath.

/**
 * Find the Full path for WebSocket upgrade.
 *
 * @param service {@link WebSocketService} which the full path should be found.
 * @return the full path of the WebSocket upgrade.
 */
private String findFullWebSocketUpgradePath(WebSocketService service) {
    // Find Base path for WebSocket
    Annotation configAnnotation = WebSocketUtil.getServiceConfigAnnotation(service, HttpConstants.PROTOCOL_PACKAGE_HTTP);
    String basePath = null;
    if (configAnnotation != null) {
        Struct annStruct = configAnnotation.getValue();
        String annotationValue = annStruct.getStringField(HttpConstants.ANN_CONFIG_ATTR_BASE_PATH);
        if (annotationValue != null && !annotationValue.trim().isEmpty()) {
            basePath = refactorUri(annotationValue);
        }
    }
    return basePath;
}
Also used : Annotation(org.ballerinalang.connector.api.Annotation) Struct(org.ballerinalang.connector.api.Struct)

Example 2 with Annotation

use of org.ballerinalang.connector.api.Annotation in project ballerina by ballerina-lang.

the class HttpResource method buildHttpResource.

public static HttpResource buildHttpResource(Resource resource, HttpService httpService) {
    HttpResource httpResource = new HttpResource(resource, httpService);
    Annotation resourceConfigAnnotation = getResourceConfigAnnotation(resource);
    if (resourceConfigAnnotation == null) {
        if (log.isDebugEnabled()) {
            log.debug("resourceConfig not specified in the Resource instance, using default sub path");
        }
        httpResource.setPath(resource.getName());
        httpResource.prepareAndValidateSignatureParams();
        return httpResource;
    }
    Struct resourceConfig = resourceConfigAnnotation.getValue();
    httpResource.setPath(resourceConfig.getStringField(PATH_FIELD));
    httpResource.setMethods(getAsStringList(resourceConfig.getArrayField(METHODS_FIELD)));
    httpResource.setConsumes(getAsStringList(resourceConfig.getArrayField(CONSUMES_FIELD)));
    httpResource.setProduces(getAsStringList(resourceConfig.getArrayField(PRODUCES_FIELD)));
    httpResource.setEntityBodyAttributeValue(resourceConfig.getStringField(BODY_FIELD));
    httpResource.setCorsHeaders(CorsHeaders.buildCorsHeaders(resourceConfig.getStructField(CORS_FIELD)));
    httpResource.setTransactionInfectable(resourceConfig.getBooleanField(TRANSACTION_INFECTABLE_FIELD));
    processResourceCors(httpResource, httpService);
    httpResource.prepareAndValidateSignatureParams();
    return httpResource;
}
Also used : Annotation(org.ballerinalang.connector.api.Annotation) Struct(org.ballerinalang.connector.api.Struct)

Example 3 with Annotation

use of org.ballerinalang.connector.api.Annotation 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 Annotation

use of org.ballerinalang.connector.api.Annotation in project ballerina by ballerina-lang.

the class HttpUtil method setCompressionHeaders.

private static void setCompressionHeaders(Context context, HTTPCarbonMessage requestMsg, HTTPCarbonMessage outboundResponseMsg) {
    Service serviceInstance = BLangConnectorSPIUtil.getService(context.getProgramFile(), context.getServiceInfo().getType());
    Annotation configAnnot = getServiceConfigAnnotation(serviceInstance, PROTOCOL_PACKAGE_HTTP);
    if (configAnnot == null) {
        return;
    }
    String contentEncoding = outboundResponseMsg.getHeaders().get(HttpHeaderNames.CONTENT_ENCODING);
    if (contentEncoding != null) {
        return;
    }
    String compressionValue = configAnnot.getValue().getEnumField(ANN_CONFIG_ATTR_COMPRESSION);
    String acceptEncodingValue = requestMsg.getHeaders().get(HttpHeaderNames.ACCEPT_ENCODING);
    if (ALWAYS.equalsIgnoreCase(compressionValue)) {
        if (acceptEncodingValue == null || HTTP_TRANSFER_ENCODING_IDENTITY.equals(acceptEncodingValue)) {
            outboundResponseMsg.getHeaders().set(HttpHeaderNames.CONTENT_ENCODING, ENCODING_GZIP);
        }
    } else if (NEVER.equalsIgnoreCase(compressionValue)) {
        outboundResponseMsg.getHeaders().set(HttpHeaderNames.CONTENT_ENCODING, HTTP_TRANSFER_ENCODING_IDENTITY);
    }
}
Also used : Service(org.ballerinalang.connector.api.Service) BString(org.ballerinalang.model.values.BString) Annotation(org.ballerinalang.connector.api.Annotation)

Example 5 with Annotation

use of org.ballerinalang.connector.api.Annotation 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)

Aggregations

Annotation (org.ballerinalang.connector.api.Annotation)6 Struct (org.ballerinalang.connector.api.Struct)4 ArrayList (java.util.ArrayList)2 Resource (org.ballerinalang.connector.api.Resource)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)1 Service (org.ballerinalang.connector.api.Service)1 BString (org.ballerinalang.model.values.BString)1 HttpResource (org.ballerinalang.net.http.HttpResource)1 URITemplateException (org.ballerinalang.net.uri.URITemplateException)1 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)1