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");
}
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);
}
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;
}
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;
}
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());
}
}
}
Aggregations