Search in sources :

Example 31 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class ProgramFile method addAttributeInfo.

@Override
public void addAttributeInfo(AttributeInfo.Kind attributeKind, AttributeInfo attributeInfo) {
    attributeInfoMap.put(attributeKind, attributeInfo);
    if (attributeKind == AttributeInfo.Kind.VARIABLE_TYPE_COUNT_ATTRIBUTE) {
        // TODO Move this out of the program file to a program context.. Runtime representation of a program.
        // TODO ProgramFile is the static program data.
        VarTypeCountAttributeInfo varTypeCountAttribInfo = (VarTypeCountAttributeInfo) attributeInfo;
        int[] globalVarCount = varTypeCountAttribInfo.getVarTypeCount();
        // TODO Introduce an abstraction for memory blocks
        // Initialize global memory block
        BStructType dummyType = new BStructType(null, "", "", 0);
        dummyType.setFieldTypeCount(globalVarCount);
        this.globalMemoryBlock = new BStruct(dummyType);
    }
}
Also used : VarTypeCountAttributeInfo(org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo) BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct)

Example 32 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class BLangFunctions method invokeNonNativeCallable.

public static WorkerExecutionContext invokeNonNativeCallable(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs, boolean waitForResponse, int flags) {
    WorkerSet workerSet = callableUnitInfo.getWorkerSet();
    int generalWorkersCount = workerSet.generalWorkers.length;
    CallableWorkerResponseContext respCtx = createWorkerResponseContext(callableUnitInfo.getRetParamTypes(), generalWorkersCount);
    WaitForResponseCallback respCallback = null;
    if (waitForResponse) {
        respCallback = new WaitForResponseCallback();
        respCtx.registerResponseCallback(respCallback);
    }
    if (TraceManagerWrapper.getInstance().isTraceEnabled() && FunctionFlags.isObserved(flags)) {
        respCtx.registerResponseCallback(new TraceableCallback(parentCtx));
    }
    respCtx.joinTargetContextInfo(parentCtx, retRegs);
    WorkerDataIndex wdi = callableUnitInfo.retWorkerIndex;
    /* execute the init worker and extract the local variables created by it */
    WorkerData initWorkerLocalData = null;
    CodeAttributeInfo initWorkerCAI = null;
    if (workerSet.initWorker != null) {
        initWorkerLocalData = executeInitWorker(parentCtx, argRegs, callableUnitInfo, workerSet.initWorker, wdi);
        if (initWorkerLocalData == null) {
            handleError(parentCtx);
            return null;
        }
        initWorkerCAI = workerSet.initWorker.getCodeAttributeInfo();
    }
    for (int i = 1; i < generalWorkersCount; i++) {
        executeWorker(respCtx, parentCtx, argRegs, callableUnitInfo, workerSet.generalWorkers[i], wdi, initWorkerLocalData, initWorkerCAI, false);
    }
    WorkerExecutionContext runInCallerCtx = executeWorker(respCtx, parentCtx, argRegs, callableUnitInfo, workerSet.generalWorkers[0], wdi, initWorkerLocalData, initWorkerCAI, true);
    if (waitForResponse) {
        BLangScheduler.executeNow(runInCallerCtx);
        respCallback.waitForResponse();
        // An error in the context at this point means an unhandled runtime error has propagated
        // all the way up to the entry point. Hence throw a {@link BLangRuntimeException} and
        // terminate the execution.
        BStruct error = parentCtx.getError();
        if (error != null) {
            handleError(parentCtx);
        }
        return null;
    } else {
        return runInCallerCtx;
    }
}
Also used : WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) WorkerSet(org.ballerinalang.util.codegen.CallableUnitInfo.WorkerSet) BStruct(org.ballerinalang.model.values.BStruct) CodeAttributeInfo(org.ballerinalang.util.codegen.attributes.CodeAttributeInfo) WorkerData(org.ballerinalang.bre.bvm.WorkerData) TraceableCallback(org.ballerinalang.util.tracer.TraceableCallback) SyncCallableWorkerResponseContext(org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext) CallableWorkerResponseContext(org.ballerinalang.bre.bvm.CallableWorkerResponseContext)

Example 33 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class AbstractHTTPAction method send.

/**
 * Send outbound request through the client connector. If the Content-Type is multipart, check whether the boundary
 * exist. If not get a new boundary string and add it as a parameter to Content-Type, just before sending header
 * info through wire. If a boundary string exist at this point, serialize multipart entity body, else serialize
 * entity body which can either be a message data source or a byte channel.
 *
 * @param dataContext               holds the ballerina context and callback
 * @param outboundRequestMsg        Outbound request that needs to be sent across the wire
 * @param async                     whether a handle should be return
 * @return connector future for this particular request
 * @throws Exception When an error occurs while sending the outbound request via client connector
 */
private void send(DataContext dataContext, HTTPCarbonMessage outboundRequestMsg, boolean async) throws Exception {
    BStruct bConnector = (BStruct) dataContext.context.getRefArgument(0);
    Struct httpClient = BLangConnectorSPIUtil.toStruct(bConnector);
    HttpClientConnector clientConnector = (HttpClientConnector) httpClient.getNativeData(HttpConstants.HTTP_CLIENT);
    String contentType = HttpUtil.getContentTypeFromTransportMessage(outboundRequestMsg);
    String boundaryString = null;
    if (HeaderUtil.isMultipart(contentType)) {
        boundaryString = HttpUtil.addBoundaryIfNotExist(outboundRequestMsg, contentType);
    }
    HttpMessageDataStreamer outboundMsgDataStreamer = new HttpMessageDataStreamer(outboundRequestMsg);
    OutputStream messageOutputStream = outboundMsgDataStreamer.getOutputStream();
    RetryConfig retryConfig = getRetryConfiguration(httpClient);
    HTTPClientConnectorListener httpClientConnectorLister = new HTTPClientConnectorListener(dataContext, retryConfig, outboundRequestMsg, outboundMsgDataStreamer);
    HttpResponseFuture future = clientConnector.send(outboundRequestMsg);
    if (async) {
        future.setResponseHandleListener(httpClientConnectorLister);
    } else {
        future.setHttpConnectorListener(httpClientConnectorLister);
    }
    try {
        if (boundaryString != null) {
            serializeMultiparts(dataContext.context, messageOutputStream, boundaryString);
        } else {
            serializeDataSource(dataContext.context, messageOutputStream);
        }
    } catch (IOException | EncoderException serializerException) {
        // We don't have to do anything here as the client connector will notify
        // the error though the listener
        logger.warn("couldn't serialize the message", serializerException);
    }
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) BStruct(org.ballerinalang.model.values.BStruct) HttpClientConnector(org.wso2.transport.http.netty.contract.HttpClientConnector) RetryConfig(org.ballerinalang.net.http.RetryConfig) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) OutputStream(java.io.OutputStream) HttpResponseFuture(org.wso2.transport.http.netty.contract.HttpResponseFuture) IOException(java.io.IOException) ResponseCacheControlStruct(org.ballerinalang.net.http.caching.ResponseCacheControlStruct) Struct(org.ballerinalang.connector.api.Struct) BStruct(org.ballerinalang.model.values.BStruct)

Example 34 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class AbstractHTTPAction method serializeDataSource.

private void serializeDataSource(Context context, OutputStream messageOutputStream) throws IOException {
    BStruct requestStruct = ((BStruct) context.getRefArgument(1));
    BStruct entityStruct = MimeUtil.extractEntity(requestStruct);
    if (entityStruct != null) {
        MessageDataSource messageDataSource = EntityBodyHandler.getMessageDataSource(entityStruct);
        if (messageDataSource != null) {
            messageDataSource.serializeData(messageOutputStream);
            HttpUtil.closeMessageOutputStream(messageOutputStream);
        } else {
            // When the entity body is a byte channel
            try {
                EntityBodyHandler.writeByteChannelToOutputStream(entityStruct, messageOutputStream);
            } finally {
                HttpUtil.closeMessageOutputStream(messageOutputStream);
            }
        }
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) MessageDataSource(org.ballerinalang.runtime.message.MessageDataSource)

Example 35 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class AbstractHTTPAction method createResponseStruct.

/**
 * Creates InResponse using the native {@code HTTPCarbonMessage}.
 *
 * @param context           ballerina context
 * @param httpCarbonMessage the HTTPCarbonMessage
 * @return the Response struct
 */
BStruct createResponseStruct(Context context, HTTPCarbonMessage httpCarbonMessage) {
    BStruct responseStruct = createStruct(context, HttpConstants.RESPONSE, HttpConstants.PROTOCOL_PACKAGE_HTTP);
    BStruct entity = createStruct(context, HttpConstants.ENTITY, PROTOCOL_PACKAGE_MIME);
    BStruct mediaType = createStruct(context, MEDIA_TYPE, PROTOCOL_PACKAGE_MIME);
    ResponseCacheControlStruct responseCacheControl = new ResponseCacheControlStruct(context.getProgramFile().getPackageInfo(PROTOCOL_PACKAGE_HTTP).getStructInfo(RESPONSE_CACHE_CONTROL));
    HttpUtil.populateInboundResponse(responseStruct, entity, mediaType, responseCacheControl, httpCarbonMessage);
    return responseStruct;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) ResponseCacheControlStruct(org.ballerinalang.net.http.caching.ResponseCacheControlStruct)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)460 BValue (org.ballerinalang.model.values.BValue)187 Test (org.testng.annotations.Test)161 BString (org.ballerinalang.model.values.BString)131 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)53 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)39 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)37 BInteger (org.ballerinalang.model.values.BInteger)33 BMap (org.ballerinalang.model.values.BMap)29 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)26 BStructType (org.ballerinalang.model.types.BStructType)25 IOException (java.io.IOException)23 BBoolean (org.ballerinalang.model.values.BBoolean)23 BJSON (org.ballerinalang.model.values.BJSON)22 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)21 StructInfo (org.ballerinalang.util.codegen.StructInfo)21 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)20 File (java.io.File)17 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)17 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)16