use of org.ballerinalang.model.tree.ResourceNode in project ballerina by ballerina-lang.
the class ClientContextHolder method buildContext.
/**
* Build a parsable context from a Ballerina {@link ServiceNode}.
*
* @param service {@code ServiceNode} for a valid ballerina source file
* @param endpoints list of endpoints to be used as service endpoints for generated client
* @return A parsable data model for provided ballerina {@code service}
*/
public static ClientContextHolder buildContext(ServiceNode service, List<EndpointNode> endpoints) throws CodeGeneratorException {
ClientContextHolder context = new ClientContextHolder();
context.name = service.getName().getValue();
context.resources = new ArrayList<>();
context.endpoints = new ArrayList<>();
// Extract bound endpoint details
for (EndpointNode ep : endpoints) {
EndpointContextHolder epContext = EndpointContextHolder.buildContext(service, ep);
if (epContext != null) {
context.endpoints.add(EndpointContextHolder.buildContext(service, ep));
}
}
// Extract ballerina resource nodes as parsable resources
for (ResourceNode resource : service.getResources()) {
ResourceContextHolder operation = ResourceContextHolder.buildContext(resource);
context.resources.add(operation);
}
return context;
}
use of org.ballerinalang.model.tree.ResourceNode in project ballerina by ballerina-lang.
the class SwaggerResourceMapper method parseHttpResourceConfig.
/**
* Parses the 'http:resourceConfig' annotation attachment and build swagger operation.
* @param resource The ballerina resource definition.
* @param operationAdaptor The operation adaptor..
*/
private void parseHttpResourceConfig(ResourceNode resource, OperationAdaptor operationAdaptor) {
Optional<? extends AnnotationAttachmentNode> responsesAnnotation = resource.getAnnotationAttachments().stream().filter(a -> null != this.httpAlias && this.httpAlias.equals(a.getPackageAlias().getValue()) && "resourceConfig".equals(a.getAnnotationName().getValue())).findFirst();
if (responsesAnnotation.isPresent()) {
Map<String, AnnotationAttachmentAttributeValueNode> configAttributes = this.listToMap(responsesAnnotation.get());
if (configAttributes.containsKey("methods") && configAttributes.get("methods").getValueArray().size() > 0) {
List<? extends AnnotationAttachmentAttributeValueNode> methodsValues = configAttributes.get("methods").getValueArray();
// Since there is only one http method.
operationAdaptor.setHttpOperation(this.getStringLiteralValue(methodsValues.get(0)));
}
if (configAttributes.containsKey("path")) {
operationAdaptor.setPath(this.getStringLiteralValue(configAttributes.get("path")));
}
if (configAttributes.containsKey("produces") && configAttributes.get("produces").getValueArray().size() > 0) {
List<String> produces = new LinkedList<>();
List<? extends AnnotationAttachmentAttributeValueNode> producesValues = configAttributes.get("produces").getValueArray();
for (AnnotationAttachmentAttributeValueNode producesValue : producesValues) {
produces.add(this.getStringLiteralValue(producesValue));
}
operationAdaptor.getOperation().setProduces(produces);
}
if (configAttributes.containsKey("consumes") && configAttributes.get("consumes").getValueArray().size() > 0) {
List<String> consumes = new LinkedList<>();
List<? extends AnnotationAttachmentAttributeValueNode> consumesValues = configAttributes.get("consumes").getValueArray();
for (AnnotationAttachmentAttributeValueNode consumesValue : consumesValues) {
consumes.add(this.getStringLiteralValue(consumesValue));
}
operationAdaptor.getOperation().setConsumes(consumes);
}
}
}
use of org.ballerinalang.model.tree.ResourceNode in project ballerina by ballerina-lang.
the class ServiceProtoUtils method getStreamingServiceDefinition.
private static Service getStreamingServiceDefinition(ServiceNode serviceNode, ServiceConfiguration serviceConfig, File.Builder fileBuilder) throws GrpcServerException {
// Protobuf service definition builder.
Service.Builder serviceBuilder = Service.newBuilder(serviceNode.getName().getValue());
Message requestMessage = null;
Message responseMessage = null;
for (ResourceNode resourceNode : serviceNode.getResources()) {
if (ON_MESSAGE_RESOURCE.equals(resourceNode.getName().getValue())) {
requestMessage = getRequestMessage(resourceNode);
Message respMsg = getResponseMessage(resourceNode);
if (respMsg != null && !(MessageKind.EMPTY.equals(respMsg.getMessageKind()))) {
responseMessage = getResponseMessage(resourceNode);
break;
}
}
if (ON_COMPLETE_RESOURCE.equals(resourceNode.getName().getValue())) {
Message respMsg = getResponseMessage(resourceNode);
if (respMsg != null && !(MessageKind.EMPTY.equals(respMsg.getMessageKind()))) {
responseMessage = respMsg;
}
}
}
if (requestMessage == null) {
throw new GrpcServerException("Cannot find request message definition for streaming service: " + serviceNode.getName().getValue());
}
if (responseMessage == null) {
responseMessage = generateMessageDefinition(new BNullType());
/*throw new GrpcServerException("Cannot find response message definition for streaming service: " +
serviceNode.getName().getValue());*/
}
if (requestMessage.getMessageKind() == MessageKind.USER_DEFINED) {
if (!fileBuilder.getRegisteredMessages().contains(requestMessage.getDescriptorProto())) {
fileBuilder.setMessage(requestMessage);
}
}
if (requestMessage.getDependency() != null) {
if (!fileBuilder.getRegisteredDependencies().contains(requestMessage.getDependency())) {
fileBuilder.setDependeny(requestMessage.getDependency());
}
}
if (responseMessage.getDependency() != null) {
if (!fileBuilder.getRegisteredDependencies().contains(responseMessage.getDependency())) {
fileBuilder.setDependeny(responseMessage.getDependency());
}
}
Method resourceMethod = Method.newBuilder(serviceConfig.getRpcEndpoint()).setClientStreaming(serviceConfig.isClientStreaming()).setServerStreaming(serviceConfig.isServerStreaming()).setInputType(requestMessage.getMessageType()).setOutputType(responseMessage.getMessageType()).build();
serviceBuilder.addMethod(resourceMethod);
return serviceBuilder.build();
}
use of org.ballerinalang.model.tree.ResourceNode in project ballerina by ballerina-lang.
the class ServiceProtoUtils method getUnaryServiceDefinition.
private static Service getUnaryServiceDefinition(ServiceNode serviceNode, File.Builder fileBuilder) throws GrpcServerException {
// Protobuf service definition builder.
Service.Builder serviceBuilder = Service.newBuilder(serviceNode.getName().getValue());
for (ResourceNode resourceNode : serviceNode.getResources()) {
Message requestMessage = getRequestMessage(resourceNode);
if (requestMessage.getMessageKind() == MessageKind.USER_DEFINED) {
if (!fileBuilder.getRegisteredMessages().contains(requestMessage.getDescriptorProto())) {
fileBuilder.setMessage(requestMessage);
}
}
if (requestMessage.getDependency() != null) {
if (!fileBuilder.getRegisteredDependencies().contains(requestMessage.getDependency())) {
fileBuilder.setDependeny(requestMessage.getDependency());
}
}
Message responseMessage = getResponseMessage(resourceNode);
if (responseMessage == null) {
throw new GrpcServerException("Connection send expression not found in resource body");
}
if (responseMessage.getMessageKind() == MessageKind.USER_DEFINED) {
if (!fileBuilder.getRegisteredMessages().contains(responseMessage.getDescriptorProto())) {
fileBuilder.setMessage(responseMessage);
}
}
if (responseMessage.getDependency() != null) {
if (!fileBuilder.getRegisteredDependencies().contains(responseMessage.getDependency())) {
fileBuilder.setDependeny(responseMessage.getDependency());
}
}
boolean serverStreaming = isServerStreaming(resourceNode);
Method resourceMethod = Method.newBuilder(resourceNode.getName().getValue()).setClientStreaming(false).setServerStreaming(serverStreaming).setInputType(requestMessage.getMessageType()).setOutputType(responseMessage.getMessageType()).build();
serviceBuilder.addMethod(resourceMethod);
}
return serviceBuilder.build();
}
use of org.ballerinalang.model.tree.ResourceNode in project ballerina by ballerina-lang.
the class BLangPackageBuilder method startResourceDef.
public void startResourceDef() {
ResourceNode resourceNode = TreeBuilder.createResourceNode();
invokableNodeStack.push(resourceNode);
startEndpointDeclarationScope(((BLangResource) resourceNode).endpoints);
}
Aggregations