Search in sources :

Example 6 with Http2PushPromise

use of org.wso2.transport.http.netty.message.Http2PushPromise in project ballerina by ballerina-lang.

the class GetHeaders method execute.

@Override
public void execute(Context context) {
    BStruct pushPromiseStruct = (BStruct) context.getRefArgument(0);
    Http2PushPromise http2PushPromise = HttpUtil.getPushPromise(pushPromiseStruct, HttpUtil.createHttpPushPromise(pushPromiseStruct));
    String headerName = context.getStringArgument(0);
    context.setReturnValues(new BStringArray(http2PushPromise.getHeaders(headerName)));
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Http2PushPromise(org.wso2.transport.http.netty.message.Http2PushPromise) BStringArray(org.ballerinalang.model.values.BStringArray)

Example 7 with Http2PushPromise

use of org.wso2.transport.http.netty.message.Http2PushPromise in project ballerina by ballerina-lang.

the class HttpUtil method createHttpPushPromise.

/**
 * Creates native {@code Http2PushPromise} from PushPromise struct.
 *
 * @param struct the PushPromise struct
 * @return the populated the native {@code Http2PushPromise}
 */
public static Http2PushPromise createHttpPushPromise(BStruct struct) {
    String method = struct.getStringField(HttpConstants.PUSH_PROMISE_METHOD_INDEX);
    if (method == null || method.isEmpty()) {
        method = HttpConstants.HTTP_METHOD_GET;
    }
    String path = struct.getStringField(HttpConstants.PUSH_PROMISE_PATH_INDEX);
    if (path == null || path.isEmpty()) {
        path = HttpConstants.DEFAULT_BASE_PATH;
    }
    return new Http2PushPromise(method, path);
}
Also used : Http2PushPromise(org.wso2.transport.http.netty.message.Http2PushPromise) BString(org.ballerinalang.model.values.BString)

Example 8 with Http2PushPromise

use of org.wso2.transport.http.netty.message.Http2PushPromise in project ballerina by ballerina-lang.

the class HttpUtil method getPushPromise.

/**
 * Gets the {@code Http2PushPromise} represented by the PushPromise struct.
 *
 * @param pushPromiseStruct  the push promise struct
 * @param defaultPushPromise the Http2PushPromise to use if the struct does not have native data of a push promise
 * @return the {@code Http2PushPromise} represented by the PushPromise struct
 */
public static Http2PushPromise getPushPromise(BStruct pushPromiseStruct, Http2PushPromise defaultPushPromise) {
    Http2PushPromise pushPromise = (Http2PushPromise) pushPromiseStruct.getNativeData(HttpConstants.TRANSPORT_PUSH_PROMISE);
    if (pushPromise != null) {
        return pushPromise;
    }
    pushPromiseStruct.addNativeData(HttpConstants.TRANSPORT_PUSH_PROMISE, defaultPushPromise);
    return defaultPushPromise;
}
Also used : Http2PushPromise(org.wso2.transport.http.netty.message.Http2PushPromise)

Example 9 with Http2PushPromise

use of org.wso2.transport.http.netty.message.Http2PushPromise in project ballerina by ballerina-lang.

the class PushPromiseNativeFunctionTest method testAddHeader.

@Test(description = "Test addHeader function of PushPromise")
public void testAddHeader() {
    BStruct promise = BCompileUtil.createAndGetStruct(result.getProgFile(), protocolPackageHttp, promiseStruct);
    String headerName = "header1";
    String headerValue = "value1";
    BString key = new BString(headerName);
    BString value = new BString(headerValue);
    BValue[] inputArg = { promise, key, value };
    BValue[] returnVal = BRunUtil.invoke(result, "testAddHeader", inputArg);
    Assert.assertFalse(returnVal == null || returnVal.length == 0 || returnVal[0] == null, "Invalid Return Values.");
    Assert.assertTrue(returnVal[0] instanceof BStruct);
    Http2PushPromise http2PushPromise = (Http2PushPromise) ((BStruct) returnVal[0]).getNativeData(HttpConstants.TRANSPORT_PUSH_PROMISE);
    Assert.assertEquals(http2PushPromise.getHeader(headerName), headerValue);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Http2PushPromise(org.wso2.transport.http.netty.message.Http2PushPromise) BString(org.ballerinalang.model.values.BString) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 10 with Http2PushPromise

use of org.wso2.transport.http.netty.message.Http2PushPromise in project ballerina by ballerina-lang.

the class PushPromiseNativeFunctionTest method testGetHeaders.

@Test(description = "Test getHeaders function of PushPromise")
public void testGetHeaders() {
    BStruct promise = BCompileUtil.createAndGetStruct(result.getProgFile(), protocolPackageHttp, promiseStruct);
    Http2PushPromise http2PushPromise = new Http2PushPromise(HttpConstants.HTTP_METHOD_GET, HttpConstants.DEFAULT_BASE_PATH);
    String headerName = "header";
    String headerValue1 = "value1";
    String headerValue2 = "value2";
    http2PushPromise.addHeader(headerName, headerValue1);
    http2PushPromise.addHeader(headerName, headerValue2);
    HttpUtil.populatePushPromiseStruct(promise, http2PushPromise);
    BString key = new BString(headerName);
    BValue[] inputArg = { promise, key };
    BValue[] returnVal = BRunUtil.invoke(result, "testGetHeaders", inputArg);
    Assert.assertFalse(returnVal == null || returnVal.length == 0 || returnVal[0] == null, "Invalid Return Values.");
    Assert.assertEquals(((BStringArray) returnVal[0]).get(0), headerValue1);
    Assert.assertEquals(((BStringArray) returnVal[0]).get(1), headerValue2);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Http2PushPromise(org.wso2.transport.http.netty.message.Http2PushPromise) BString(org.ballerinalang.model.values.BString) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Aggregations

Http2PushPromise (org.wso2.transport.http.netty.message.Http2PushPromise)18 BStruct (org.ballerinalang.model.values.BStruct)17 BString (org.ballerinalang.model.values.BString)8 BValue (org.ballerinalang.model.values.BValue)8 Test (org.testng.annotations.Test)6 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)3 HttpClientConnector (org.wso2.transport.http.netty.contract.HttpClientConnector)2 HttpResponseFuture (org.wso2.transport.http.netty.contract.HttpResponseFuture)2 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)2 BBoolean (org.ballerinalang.model.values.BBoolean)1 BStringArray (org.ballerinalang.model.values.BStringArray)1 DataContext (org.ballerinalang.net.http.DataContext)1 MessageDataSource (org.ballerinalang.runtime.message.MessageDataSource)1