Skip to content

Commit

Permalink
Support all component options; Support ActiveMQ (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnyz32 authored Oct 16, 2024
1 parent 0114c7c commit 5033c1d
Show file tree
Hide file tree
Showing 19 changed files with 177 additions and 58 deletions.
6 changes: 6 additions & 0 deletions camel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
<artifactId>camel-google-pubsub</artifactId>
<version>3.14.6</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-activemq</artifactId>
<version>3.14.6</version>
<!-- use the same version as your Camel core version -->
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-twilio</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.ini4j.Ini;
import org.ini4j.InvalidFileFormatException;
Expand All @@ -11,6 +13,7 @@
public abstract class Config {

public static final String DIRECTORY_OVERRIDE_PROPERTY = "manzan.configdir";
public static final String COMPONENT_OPTIONS_PREFIX = "componentOptions.";

protected static boolean isIBMi() {
final String osName = System.getProperty("os.name", "Misty");
Expand Down Expand Up @@ -70,6 +73,27 @@ protected String getRequiredString(final String _name, final String _key) {
return ret;
}

protected Map<String, String> getComponentOptions(final String _name) {
Map<String, String> section = m_ini.get(_name);

// Map to store key-value pairs
Map<String, String> componentOptionsMap = new HashMap<>();
if (section != null) {
// Iterate through the section's keys and values
for (Map.Entry<String, String> entry : section.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();

// Check if the key starts with "componentOptions."
if (key.startsWith(COMPONENT_OPTIONS_PREFIX)) {
// Store it in the map
componentOptionsMap.put(key.substring(COMPONENT_OPTIONS_PREFIX.length()), value);
}
}
}
return componentOptionsMap;
}

protected int getOptionalInt(final String _name, final String _key) {
final String ret = m_ini.get(_name, _key);
if (StringUtils.isEmpty(ret)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.github.theprez.manzan.routes.dest.GrafanaLokiDestination;
import com.github.theprez.manzan.routes.dest.HttpDestination;
import com.github.theprez.manzan.routes.dest.KafkaDestination;
import com.github.theprez.manzan.routes.dest.ActiveMqDestination;
import com.github.theprez.manzan.routes.dest.SentryDestination;
import com.github.theprez.manzan.routes.dest.SlackDestination;
import com.github.theprez.manzan.routes.dest.StreamDestination;
Expand Down Expand Up @@ -54,9 +55,10 @@ public synchronized Map<String, ManzanRoute> getRoutes(CamelContext context) {
final String name = section;
final Section sectionObj = getIni().get(name);
final String format = getOptionalString(name, "format");
final Map<String, String> componentOptions = getComponentOptions(name);
switch (type) {
case "stdout":
ret.put(name, new StreamDestination(name, getOptionalString(name, "format")));
ret.put(name, new StreamDestination(context, name, format, componentOptions));
break;
case "slack": {
final String webhook = getRequiredString(name, "webhook");
Expand All @@ -66,21 +68,26 @@ public synchronized Map<String, ManzanRoute> getRoutes(CamelContext context) {
break;
case "kafka":
final String topic = getRequiredString(name, "topic");
ret.put(name, new KafkaDestination(name, topic, format, getUriAndHeaderParameters(name, sectionObj, "topic")));
ret.put(name, new KafkaDestination(context, name, topic, format, componentOptions, getUriAndHeaderParameters(name, sectionObj, "topic")));
break;
case "activemq":
final String destName = getRequiredString(name, "destinationName");
String destType = getOptionalString(name, "destinationType");
destType = (destType != null && destType.equals("topic")) ? "topic" : "queue";
ret.put(name, new ActiveMqDestination(context, name, destType, destName, format, componentOptions, getUriAndHeaderParameters(name, sectionObj, "destinationName", "destinationType")));
break;
case "google-pubsub":
final String projectId = getRequiredString(name, "projectId");
final String topicName = getRequiredString(name, "topicName");
final String serviceAccountKey = getRequiredString(name, "serviceAccountKey");
ret.put(name, new GooglePubSubDestination(context, name, projectId, topicName, serviceAccountKey, format, getUriAndHeaderParameters(name, sectionObj, "projectId", "topicName", "serviceAccountKey")));
ret.put(name, new GooglePubSubDestination(context, name, projectId, topicName, format, componentOptions, getUriAndHeaderParameters(name, sectionObj, "projectId", "topicName")));
break;
case "file":
final String file = getRequiredString(name, "file");
ret.put(name, new FileDestination(name, file, format, getUriAndHeaderParameters(name, sectionObj, "file")));
ret.put(name, new FileDestination(context, name, file, format, componentOptions, getUriAndHeaderParameters(name, sectionObj, "file")));
break;
case "dir":
final String dir = getRequiredString(name, "dir");
ret.put(name, new DirDestination(name, dir, format, getUriAndHeaderParameters(name, sectionObj, "dir")));
ret.put(name, new DirDestination(context, name, dir, format, componentOptions, getUriAndHeaderParameters(name, sectionObj, "dir")));
break;
case "sentry":
final String dsn = getRequiredString(name, "dsn");
Expand All @@ -103,18 +110,18 @@ public synchronized Map<String, ManzanRoute> getRoutes(CamelContext context) {
case "smtps":
final String server = getRequiredString(name, "server");
final int port = getOptionalInt(name, "port");
final EmailDestination d = new EmailDestination(name, type, server, port, format, getUriAndHeaderParameters(name, sectionObj, "server", "port"), null);
final EmailDestination d = new EmailDestination(context, name, type, server, format, port, componentOptions, getUriAndHeaderParameters(name, sectionObj, "server", "port"), null);
ret.put(name, d);
break;
case "twilio":
final String sid = getRequiredString(name, "sid");
final String token = getRequiredString(name, "token");
ret.put(name, new TwilioDestination(context, name, sid, token, format, getUriAndHeaderParameters(name, sectionObj, "sid", "token")));
ret.put(name, new TwilioDestination(context, name, format,
componentOptions,
getUriAndHeaderParameters(name, sectionObj, "sid", "token")));
break;
case "http":
case "https":
final String url = getRequiredString(name, "url");
ret.put(name, HttpDestination.get(name, type, url, format, getUriAndHeaderParameters(name, sectionObj, "url")));
ret.put(name, HttpDestination.get(context, name, type, url, format, componentOptions, getUriAndHeaderParameters(name, sectionObj, "url")));
break;
default:
throw new RuntimeException("Unknown destination type: " + type);
Expand All @@ -128,7 +135,7 @@ private Map<String, String> getUriAndHeaderParameters(final String _name, Sectio
List<String> exclusions = new LinkedList<>(Arrays.asList(_exclusions));
exclusions.addAll(Arrays.asList("type", "filter", "format"));
for (final String sectionKey : sectionObj.keySet()) {
if (exclusions.contains(sectionKey)) {
if (exclusions.contains(sectionKey) || sectionKey.startsWith(Config.COMPONENT_OPTIONS_PREFIX)) {
continue;
}
pathParameters.put(sectionKey, getRequiredString(_name, sectionKey));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import java.util.Map;
import java.util.Map.Entry;

import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.Exchange;

import com.github.theprez.jcmdutils.StringUtils;
import com.github.theprez.manzan.ManzanMessageFormatter;
import org.apache.camel.spi.PropertyConfigurer;


public abstract class ManzanGenericCamelRoute extends ManzanRoute {

Expand All @@ -17,15 +21,22 @@ public abstract class ManzanGenericCamelRoute extends ManzanRoute {
private final String m_path;
protected final Map<String, String> m_uriParams;

public ManzanGenericCamelRoute(final String _name, final String _camelComponent, final String _path,
final String _format, final Map<String, String> _uriParams, final Map<String, Object> _headerParams) {
public ManzanGenericCamelRoute(final CamelContext _context, final String _name, final String _camelComponent, final String _path,
final String _format, final Map<String, String> _uriParams, final Map<String, Object> _headerParams,
final Map<String, String> componentOptions) {
super(_name);
m_uriParams = null == _uriParams ? new HashMap<String, String>(1) : _uriParams;
m_headerParams = null == _headerParams ? new HashMap<String, Object>(1) : _headerParams;
m_camelComponent = _camelComponent;
m_path = _path;
m_formatter = StringUtils.isEmpty(_format) ? null : new ManzanMessageFormatter(_format);

Component component = _context.getComponent(_camelComponent, true, false);
PropertyConfigurer configurer = component.getComponentPropertyConfigurer();
componentOptions.forEach((key, value) -> {
configurer.configure(_context, component, key, value, true);
});

}

protected abstract void customPostProcess(Exchange exchange);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.theprez.manzan.routes.dest;

import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import com.github.theprez.manzan.routes.ManzanGenericCamelRoute;

public class ActiveMqDestination extends ManzanGenericCamelRoute {
public ActiveMqDestination(final CamelContext _context, final String _name, final String _destType, final String _destName, final String _format, final Map<String, String> _componentOptions, final Map<String, String> _uriParams) {
super(_context, _name, "activemq", _destType + ":" + _destName, _format, _uriParams, null, _componentOptions);
}

@Override
protected void customPostProcess(Exchange exchange) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;

import com.github.theprez.manzan.routes.ManzanGenericCamelRoute;

public class DirDestination extends ManzanGenericCamelRoute {
public DirDestination(final String _name, final String _file, final String _format, final Map<String, String> _uriParams) {
super(_name, "file", _file, _format, _uriParams, null);
public DirDestination(final CamelContext _context, final String _name, final String _file, final String _format, final Map<String, String> _componentOptions, final Map<String, String> _uriParams) {
super(_context, _name, "file", _file, _format, _uriParams, null, _componentOptions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import com.github.theprez.manzan.routes.ManzanGenericCamelRoute;

public class EmailDestination extends ManzanGenericCamelRoute {

public EmailDestination(final String _name, final String _type, final String _smtpServer, final int _port, final String _format, final Map<String, String> _uriParams, final Map<String, Object> _headerParams) {
super(_name, _type, (_port == -1) ? _smtpServer : _smtpServer + ":" + _port, _format, _uriParams, _headerParams);
public EmailDestination(final CamelContext _context, final String _name, final String _type,
final String _smtpServer, final String _format, final int _port,
final Map<String, String> _componentOptions, final Map<String, String> _uriParams,
final Map<String, Object> _headerParams
) {
super(_context, _name, _type, (_port == -1) ? _smtpServer : _smtpServer + ":" + _port, _format, _uriParams, _headerParams, _componentOptions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;

import com.github.theprez.manzan.routes.ManzanGenericCamelRoute;

public class FileDestination extends ManzanGenericCamelRoute {
public FileDestination(final String _name, final String _file, final String _format, final Map<String, String> _uriParams) {
super(_name, "stream", "file", _format, addToMap(_uriParams, "fileName", _file), null);
public FileDestination(final CamelContext _context, final String _name, final String _file, final String _format, final Map<String, String> _componentOptions, final Map<String, String> _uriParams) {
super(_context, _name, "stream", "file", _format, addToMap(_uriParams, "fileName", _file), null, _componentOptions);
}

private static Map<String, String> addToMap(Map<String, String> _uriParams, String _key, String _val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
import com.github.theprez.manzan.routes.ManzanGenericCamelRoute;

public class GooglePubSubDestination extends ManzanGenericCamelRoute {
public GooglePubSubDestination(CamelContext context, final String _name, final String _projectId, final String _topicName, final String _serviceAccountKey, final String _format, final Map<String, String> _uriParams) {
super(_name, "google-pubsub", _projectId + ":" + _topicName, _format, _uriParams, null);
GooglePubsubComponent pubsub = context.getComponent("google-pubsub", GooglePubsubComponent.class);
pubsub.setServiceAccountKey(_serviceAccountKey);
public GooglePubSubDestination(final CamelContext _context, final String _name, final String _projectId, final String _topicName, final String _format, final Map<String, String> _componentOptions, final Map<String, String> _uriParams) {
super(_context, _name, "google-pubsub", _projectId + ":" + _topicName, _format, _uriParams, null, _componentOptions);
GooglePubsubComponent pubsub = _context.getComponent("google-pubsub", GooglePubsubComponent.class);
pubsub.init();
pubsub.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import java.util.Map;
import java.util.Map.Entry;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;

import com.github.theprez.manzan.routes.ManzanGenericCamelRoute;

public class HttpDestination extends ManzanGenericCamelRoute {

public static HttpDestination get(final String _name, String _type, final String _url, final String _format, Map<String, String> _parameters) {
public static HttpDestination get(final CamelContext _context, final String _name, String _type, final String _url, final String _format, final Map<String, String> _componentOptions, Map<String, String> _parameters) {
Map<String, Object> headerParameters = new LinkedHashMap<String,Object>();
Map<String, String> uriParameters = new LinkedHashMap<String,String>();
String hostVal = _url.replaceFirst("^http(s)?://", "").replaceAll("\\/.*","");
Expand All @@ -29,10 +30,10 @@ public static HttpDestination get(final String _name, String _type, final String
uriParameters.put(parmEntry.getKey(), parmEntry.getValue());
}
}
return new HttpDestination(_name, _type, _url, _format, uriParameters, headerParameters);
return new HttpDestination(_context, _name, _type, _url, _format, _componentOptions, uriParameters, headerParameters);
}
private HttpDestination(final String _name, String _type, final String _url, final String _format, Map<String, String> _uriParams, Map<String, Object> _headerParams) {
super(_name, _type, _url.replaceFirst("^http(s)?://", ""), _format, _uriParams, _headerParams);
private HttpDestination(final CamelContext _context, final String _name, String _type, final String _url, final String _format, final Map<String, String> _componentOptions, Map<String, String> _uriParams, Map<String, Object> _headerParams) {
super(_context, _name, _type, _url.replaceFirst("^http(s)?://", ""), _format, _uriParams, _headerParams,_componentOptions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;

import com.github.theprez.manzan.routes.ManzanGenericCamelRoute;

public class KafkaDestination extends ManzanGenericCamelRoute {
public KafkaDestination(final String _name, final String _topic, final String _format, final Map<String, String> _uriParams) {
super(_name, "kafka", _topic, _format, _uriParams, null);
public KafkaDestination(final CamelContext _context, final String _name, final String _topic, final String _format, final Map<String, String> _componentOptions, final Map<String, String> _uriParams) {
super(_context, _name, "kafka", _topic, _format, _uriParams, null, _componentOptions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.github.theprez.manzan.routes.dest;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;

import com.github.theprez.manzan.routes.ManzanGenericCamelRoute;

import java.util.Map;

public class StreamDestination extends ManzanGenericCamelRoute {
public StreamDestination(final String _name, final String _format) {
super(_name, "stream", "out", _format, null, null);
public StreamDestination(final CamelContext _context, final String _name, final String _format, final Map<String, String> _componentOptions) {
super(_context, _name, "stream", "out", _format, null, null, _componentOptions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.component.twilio.TwilioComponent;

import com.github.theprez.manzan.routes.ManzanGenericCamelRoute;

public class TwilioDestination extends ManzanGenericCamelRoute {
public TwilioDestination(CamelContext context, final String _name, String sid, String token, final String _format, final Map<String, String> _uriParams) {
super(_name, "twilio", "message/create", _format, _uriParams, null);

TwilioComponent twilio = context.getComponent("twilio", TwilioComponent.class);
twilio.setUsername(sid);
twilio.setPassword(token);
public TwilioDestination(final CamelContext _context, final String _name, final String _format, Map<String, String> componentOptions, final Map<String, String> _uriParams) {
super(_context, _name, "twilio", "message/create", _format, _uriParams, null, componentOptions);
}

@Override
Expand Down
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
* [Sentry](config/examples/sentry.md)
* [Grafana Loki](config/examples/grafanaLoki.md)
* [Google Pub/Sub](config/examples/googlePubSub.md)
* [ActiveMQ](config/examples/activeMQ.md)
* [Contributing](contributing.md)
Loading

0 comments on commit 5033c1d

Please sign in to comment.