Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support httpGet for http event adapter #854

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,14 @@ public HttpClient getHttpClient() {
public void run() {

UUID uuid = UUID.randomUUID();
EntityEnclosingMethod method = null;
HttpMethodBase method = null;

try {

if (clientMethod.equalsIgnoreCase(HTTPEventAdapterConstants.CONSTANT_HTTP_PUT)) {
method = new PutMethod(this.getUrl());
} else if (clientMethod.equalsIgnoreCase(HTTPEventAdapterConstants.CONSTANT_HTTP_GET)) {
method = new GetMethod(this.getUrl());
} else {
method = new PostMethod(this.getUrl());
}
Expand All @@ -318,8 +320,9 @@ public void run() {
}
}

method.setRequestEntity(new StringRequestEntity(this.getPayload(), contentType, "UTF-8"));

if (method instanceof EntityEnclosingMethod) {
((EntityEnclosingMethod) method).setRequestEntity(new StringRequestEntity(this.getPayload(), contentType, "UTF-8"));
}
if (this.getUsername() != null && this.getUsername().trim().length() > 0) {
method.setRequestHeader("Authorization", "Basic " + Base64
.encode((this.getUsername() + HTTPEventAdapterConstants.ENTRY_SEPARATOR + this
Expand Down Expand Up @@ -359,4 +362,4 @@ public void run() {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class HTTPEventAdapterConstants {
public static final String ADAPTER_HTTP_CLIENT_METHOD = "http.client.method";
public static final String CONSTANT_HTTP_POST = "HttpPost";
public static final String CONSTANT_HTTP_PUT = "HttpPut";
public static final String CONSTANT_HTTP_GET = "HttpGet";
public static final String ENABLE_FORM_URL_ENCODED = "enableFormUrlEncoded";

//configurations for the httpConnectionManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,10 @@ private EventPublisherConstants() {
public static final String METRIC_DELIMITER = ".";
public static final String METRIC_AGGREGATE_ANNOTATION = "[+]";
public static final String TEMP_CARBON_APPS_DIRECTORY = "carbonapps";
}

public static final String DYNAMIC_PROPERTY_HTTP_URL = "http.url";
public static final String DYNAMIC_PROPERTY_NOTIFICATION_EVENT = "notification-event";
public static final String SMS_OTP = "smsotp";
public static final String SEND_TO = "send-to";
public static final String OTP_TOKEN = "otpToken";
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ public void process(Event event) {

}

EventPublisherUtil.populateUrlPlaceholders(dynamicProperties, event.getArbitraryDataMap());
org.wso2.siddhi.core.event.Event siddhiEvent = EventPublisherUtil.convertToSiddhiEvent(event, inputStreamSize);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.wso2.carbon.event.publisher.core.internal.util;

import org.apache.axis2.engine.AxisConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xerces.impl.Constants;
Expand All @@ -38,6 +39,12 @@
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import static org.wso2.carbon.event.publisher.core.config.EventPublisherConstants.DYNAMIC_PROPERTY_HTTP_URL;
import static org.wso2.carbon.event.publisher.core.config.EventPublisherConstants.DYNAMIC_PROPERTY_NOTIFICATION_EVENT;
import static org.wso2.carbon.event.publisher.core.config.EventPublisherConstants.OTP_TOKEN;
import static org.wso2.carbon.event.publisher.core.config.EventPublisherConstants.SEND_TO;
import static org.wso2.carbon.event.publisher.core.config.EventPublisherConstants.SMS_OTP;

public class EventPublisherUtil {

private static final String JVM_BIT_ARCH_SYSTEM_PROPERTY = "sun.arch.data.model";
Expand Down Expand Up @@ -155,6 +162,40 @@ public static int getSize(org.wso2.carbon.databridge.commons.Event event) {
return size;
}

/**
* Replaces placeholders in the given URL with the corresponding values provided in the map.
*
* @param dynamicProperties The map containing the properties of the publisher.
* @param arbitraryDataMap The map containing the arbitrary data coming from the event.
*/
public static void populateUrlPlaceholders(Map<String, String> dynamicProperties, Map<String,
String> arbitraryDataMap) {

if (dynamicProperties == null || dynamicProperties.isEmpty() ||
StringUtils.isBlank(dynamicProperties.get(DYNAMIC_PROPERTY_HTTP_URL))) {
// Return if the URL is not set.
return;
}
if (arbitraryDataMap == null || arbitraryDataMap.isEmpty()) {
// Return if the arbitrary data map is not set.
return;
}

// Handle SMS OTP Flow.
if (arbitraryDataMap.get(DYNAMIC_PROPERTY_NOTIFICATION_EVENT) != null
&& SMS_OTP.equalsIgnoreCase(arbitraryDataMap.get(DYNAMIC_PROPERTY_NOTIFICATION_EVENT))) {
String url = dynamicProperties.get(DYNAMIC_PROPERTY_HTTP_URL);
String encodedSmsMessage = "Verification Code: ".replaceAll("\\s", "+");
if (StringUtils.isNotBlank(url) && StringUtils.isNotBlank(arbitraryDataMap.get(OTP_TOKEN)) &&
StringUtils.isNotBlank(arbitraryDataMap.get(SEND_TO))) {
url = url.replaceAll("\\$ctx.num", arbitraryDataMap.get(SEND_TO))
.replaceAll("\\$ctx.msg", encodedSmsMessage + arbitraryDataMap.get(OTP_TOKEN))
.replaceAll("\\$ctx.otp", arbitraryDataMap.get(OTP_TOKEN));
dynamicProperties.put(DYNAMIC_PROPERTY_HTTP_URL, url);
}
}
}

private static int getSize(Map<String, String> arbitraryDataMap) {
int size = 0;
if (arbitraryDataMap != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
<dependency>
<groupId>org.wso2.carbon.analytics-common</groupId>
<artifactId>org.wso2.carbon.analytics.gadget.rest.provider.template.feature</artifactId>
<version>5.2.62-SNAPSHOT</version>
<type>zip</type>
</dependency>
<dependency>
Expand Down Expand Up @@ -95,4 +94,4 @@
</plugin>
</plugins>
</build>
</project>
</project>