Search in sources :

Example 16 with HttpMessageDataStreamer

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

the class HttpUtil method populateEntityBody.

/**
 * Populate entity with the relevant body content.
 *
 * @param context           Represent ballerina context
 * @param httpMessageStruct Represent ballerina request/response
 * @param entity            Represent an entity
 * @param isRequest         boolean representing whether the message is a request or a response
 */
protected static void populateEntityBody(Context context, BStruct httpMessageStruct, BStruct entity, boolean isRequest) {
    HTTPCarbonMessage httpCarbonMessage = HttpUtil.getCarbonMsg(httpMessageStruct, HttpUtil.createHttpCarbonMessage(isRequest));
    HttpMessageDataStreamer httpMessageDataStreamer = new HttpMessageDataStreamer(httpCarbonMessage);
    String contentType = httpCarbonMessage.getHeader(HttpHeaderNames.CONTENT_TYPE.toString());
    if (MimeUtil.isNotNullAndEmpty(contentType) && contentType.startsWith(MULTIPART_AS_PRIMARY_TYPE) && context != null) {
        MultipartDecoder.parseBody(context, entity, contentType, httpMessageDataStreamer.getInputStream());
    } else {
        int contentLength = NO_CONTENT_LENGTH_FOUND;
        String lengthStr = httpCarbonMessage.getHeader(HttpHeaderNames.CONTENT_LENGTH.toString());
        try {
            contentLength = lengthStr != null ? Integer.parseInt(lengthStr) : contentLength;
            if (contentLength == NO_CONTENT_LENGTH_FOUND) {
                contentLength = httpCarbonMessage.getFullMessageLength();
            }
            MimeUtil.setContentLength(entity, contentLength);
        } catch (NumberFormatException e) {
            throw new BallerinaException("Invalid content length");
        }
        EntityBodyHandler.setDiscreteMediaTypeBodyContent(entity, httpMessageDataStreamer.getInputStream());
    }
    httpMessageStruct.addNativeData(MESSAGE_ENTITY, entity);
    httpMessageStruct.addNativeData(IS_BODY_BYTE_CHANNEL_ALREADY_SET, true);
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 17 with HttpMessageDataStreamer

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

the class UriTemplateDispatcherTest method testSpecialCharacterURI.

@Test(description = "Test dispatching with OPTIONS request with wildcard")
public void testSpecialCharacterURI() {
    String path = "/ech%5Bo/ech%5Bo/b%5Bar";
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "GET", null);
    HTTPCarbonMessage response = Services.invokeNew(application, TEST_EP, cMsg);
    Assert.assertNotNull(response, "Response message not found");
    BJSON bJson = new BJSON(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(bJson.value().get("echo113").asText(), "b[ar", "Resource dispatched to wrong template");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 18 with HttpMessageDataStreamer

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

the class UriTemplateDispatcherTest method testOPTIONSMethods.

@Test(description = "Test dispatching with OPTIONS method")
public void testOPTIONSMethods() {
    String path = "/options/hi";
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "OPTIONS", "hi");
    HTTPCarbonMessage response = Services.invokeNew(application, TEST_EP, cMsg);
    Assert.assertNotNull(response, "Response message not found");
    BJSON bJson = new BJSON(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(bJson.value().get("echo").asText(), "wso2", "Resource dispatched to wrong template");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 19 with HttpMessageDataStreamer

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

the class UriTemplateDispatcherTest method testValidUrlTemplate5Dispatching.

@Test(description = "Test accessing the variables parsed with URL. /products5/{productId}/reg")
public void testValidUrlTemplate5Dispatching() {
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/ecommerceservice/products5/PID125/reg?regID=RID125&para1=value1", "GET");
    HTTPCarbonMessage response = Services.invokeNew(application, TEST_EP, cMsg);
    Assert.assertNotNull(response, "Response message not found");
    // Expected Json message : {"Template":"T5","ProductID":"PID125","RegID":"RID125"}
    BJSON bJson = new BJSON(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(bJson.value().get("Template").asText(), "T5", "Resource dispatched to wrong template");
    Assert.assertEquals(bJson.value().get("ProductID").asText(), "PID125", "ProductID variable not set properly.");
    Assert.assertEquals(bJson.value().get("RegID").asText(), "RID125", "RegID variable not set properly.");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 20 with HttpMessageDataStreamer

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

the class UriTemplateDispatcherTest method testInValidUrlTemplateDispatching.

@Test(description = "Test resource dispatchers with invalid URL. /products/{productId}/{regId}", dataProvider = "inValidUrl")
public void testInValidUrlTemplateDispatching(String path) {
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "GET");
    final String xOrderIdHeadeName = "X-ORDER-ID";
    final String xOrderIdHeadeValue = "ORD12345";
    cMsg.setHeader(xOrderIdHeadeName, xOrderIdHeadeValue);
    HTTPCarbonMessage response = Services.invokeNew(application, TEST_EP, cMsg);
    Assert.assertEquals(response.getProperty(HttpConstants.HTTP_STATUS_CODE), 404, "Response code mismatch");
    // checking the exception message
    String errorMessage = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertNotNull(errorMessage, "Message body null");
    Assert.assertTrue(errorMessage.contains("no matching resource found for path"), "Expected error not found.");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Aggregations

HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)176 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)172 Test (org.testng.annotations.Test)172 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)172 BJSON (org.ballerinalang.model.values.BJSON)104 BString (org.ballerinalang.model.values.BString)21 ArrayList (java.util.ArrayList)5 Header (org.wso2.carbon.messaging.Header)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 ExecutorService (java.util.concurrent.ExecutorService)4 Semaphore (java.util.concurrent.Semaphore)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 MimeTypeParseException (javax.activation.MimeTypeParseException)2 BStruct (org.ballerinalang.model.values.BStruct)2 StringDataSource (org.ballerinalang.runtime.message.StringDataSource)2 MIMEPart (org.jvnet.mimepull.MIMEPart)2 EncoderException (io.netty.handler.codec.EncoderException)1 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1