Search in sources :

Example 61 with HTTPCarbonMessage

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

the class IdentifierLiteralServiceTest method testIdentifierLiteralsInPayload.

@Test(description = "Test identifier literals payload", enabled = false)
public void testIdentifierLiteralsInPayload() {
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/identifierLiteral/resource2", "GET");
    HTTPCarbonMessage response = Services.invokeNew(application, cMsg);
    Assert.assertNotNull(response);
    String payload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(payload, "hello");
}
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)

Example 62 with HTTPCarbonMessage

use of org.wso2.transport.http.netty.message.HTTPCarbonMessage 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 63 with HTTPCarbonMessage

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

the class HttpUtil method getCarbonMsg.

public static HTTPCarbonMessage getCarbonMsg(BStruct struct, HTTPCarbonMessage defaultMsg) {
    HTTPCarbonMessage httpCarbonMessage = (HTTPCarbonMessage) struct.getNativeData(TRANSPORT_MESSAGE);
    if (httpCarbonMessage != null) {
        return httpCarbonMessage;
    }
    addCarbonMsg(struct, defaultMsg);
    return defaultMsg;
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage)

Example 64 with HTTPCarbonMessage

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

the class HttpUtil method createHttpCarbonMessage.

public static HTTPCarbonMessage createHttpCarbonMessage(boolean isRequest) {
    HTTPCarbonMessage httpCarbonMessage;
    if (isRequest) {
        httpCarbonMessage = new HTTPCarbonMessage(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, ""));
    } else {
        httpCarbonMessage = new HTTPCarbonMessage(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK));
    }
    httpCarbonMessage.completeMessage();
    return httpCarbonMessage;
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse)

Example 65 with HTTPCarbonMessage

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

the class BallerinaWebSubConnectionListener method autoRespondToIntentVerification.

/**
 * Method to automatically respond to intent verification requests for subscriptions/unsubscriptions if a resource
 * named {@link WebSubSubscriberConstants#RESOURCE_NAME_VERIFY_INTENT} is not specified.
 *
 * @param httpCarbonMessage the message/request received
 */
private void autoRespondToIntentVerification(HTTPCarbonMessage httpCarbonMessage) {
    String annotatedTopic = httpCarbonMessage.getProperty(WebSubSubscriberConstants.ANNOTATED_TOPIC).toString();
    PrintStream console = System.out;
    if (httpCarbonMessage.getProperty(HttpConstants.QUERY_STR) != null) {
        String queryString = (String) httpCarbonMessage.getProperty(HttpConstants.QUERY_STR);
        BMap<String, BString> params = new BMap<>();
        try {
            HTTPCarbonMessage response = HttpUtil.createHttpCarbonMessage(false);
            response.waitAndReleaseAllEntities();
            URIUtil.populateQueryParamMap(queryString, params);
            String mode = params.get(WebSubSubscriberConstants.PARAM_HUB_MODE).stringValue();
            if ((WebSubSubscriberConstants.SUBSCRIBE.equals(mode) || WebSubSubscriberConstants.UNSUBSCRIBE.equals(mode)) && annotatedTopic.equals(params.get(WebSubSubscriberConstants.PARAM_HUB_TOPIC).stringValue())) {
                String challenge = params.get(WebSubSubscriberConstants.PARAM_HUB_CHALLENGE).stringValue();
                response.addHttpContent(new DefaultLastHttpContent(Unpooled.wrappedBuffer(challenge.getBytes(StandardCharsets.UTF_8))));
                response.setProperty(HttpConstants.HTTP_STATUS_CODE, 202);
                console.println("ballerina: Intent Verification agreed - Mode [" + mode + "], Topic [" + annotatedTopic + "], Lease Seconds [" + params.get(WebSubSubscriberConstants.PARAM_HUB_LEASE_SECONDS) + "]");
            } else {
                console.println("ballerina: Intent Verification denied - Mode [" + mode + "] for Incorrect Topic [" + annotatedTopic + "]");
                response.setProperty(HttpConstants.HTTP_STATUS_CODE, 404);
            }
            HttpUtil.sendOutboundResponse(httpCarbonMessage, response);
        } catch (UnsupportedEncodingException e) {
            throw new BallerinaException("Error responding to intent verification request: " + e.getMessage());
        }
    }
}
Also used : PrintStream(java.io.PrintStream) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) BMap(org.ballerinalang.model.values.BMap) BString(org.ballerinalang.model.values.BString) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Aggregations

HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)308 Test (org.testng.annotations.Test)247 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)233 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)179 BJSON (org.ballerinalang.model.values.BJSON)108 BString (org.ballerinalang.model.values.BString)45 BStruct (org.ballerinalang.model.values.BStruct)43 Request (org.wso2.msf4j.Request)34 ArrayList (java.util.ArrayList)25 BValue (org.ballerinalang.model.values.BValue)19 Header (org.wso2.carbon.messaging.Header)14 Tracer (org.ballerinalang.util.tracer.Tracer)11 HttpCarbonMessage (org.wso2.transport.http.netty.message.HttpCarbonMessage)10 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)9 IOException (java.io.IOException)8 ResponseCacheControlStruct (org.ballerinalang.net.http.caching.ResponseCacheControlStruct)7 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)5 Semaphore (java.util.concurrent.Semaphore)5 APIMgtSecurityException (org.wso2.carbon.apimgt.rest.api.common.exception.APIMgtSecurityException)5 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)4