Skip to content

Commit

Permalink
Emhamcement: Add support for content-type to influence the payload (B…
Browse files Browse the repository at this point in the history
…INARY vs TEXT)
  • Loading branch information
gvensan committed Mar 18, 2024
1 parent 61df913 commit 0fc1e4d
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 51 deletions.
3 changes: 3 additions & 0 deletions PARAMETERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ Options:
response message
--user-properties <PROPS...> [advanced] the user properties (e.g., "name1: value1" "name2:
value2")
--content-type <CONTENT_TYPE> [advanced] payload content type (default: "text/plain")
--output-mode <MODE> [advanced] message print mode: DEFAULT, CONCISE OR FULL
(default: "DEFAULT")
--pretty [BOOLEAN] [advanced] prettify the payload (default: false)
Expand Down Expand Up @@ -336,6 +337,7 @@ Options:
response message
--user-properties <PROPS...> [advanced] the user properties (e.g., "name1: value1" "name2:
value2")
--content-type <CONTENT_TYPE> [advanced] payload content type (default: "text/plain")
--output-mode <MODE> [advanced] message print mode: DEFAULT, CONCISE OR FULL
(default: "DEFAULT")
--pretty [BOOLEAN] [advanced] prettify the payload (default: false)
Expand Down Expand Up @@ -450,6 +452,7 @@ Options:
response message
--user-properties <PROPS...> [advanced] the user properties (e.g., "name1: value1" "name2:
value2")
--content-type <CONTENT_TYPE> [advanced] payload content type (default: "text/plain")
--output-mode <MODE> [advanced] message print mode: DEFAULT, CONCISE OR FULL
(default: "DEFAULT")
--pretty [BOOLEAN] [advanced] prettify the payload (default: false)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stm",
"version": "0.0.33",
"version": "0.0.34",
"description": "Solace Try-Me Command Line Tool",
"keywords": [
"solace",
Expand Down
29 changes: 19 additions & 10 deletions src/common/publish-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class SolaceClient extends VisualizeClient {
}

// Publish a message on a topic
publish(topicName: string, payload: string | Buffer | undefined, iteration: number = 0) {
publish(topicName: string, payload: string | Buffer | undefined, contentType: string, iteration: number = 0) {
if (this.exited) return;

if (!this.session) {
Expand All @@ -167,16 +167,25 @@ export class SolaceClient extends VisualizeClient {
let message = solace.SolclientFactory.createMessage();
message.setDestination(solace.SolclientFactory.createTopicDestination(topicName));
if (payload) {
if (typeof payload === 'object') {
const encoder = new TextEncoder();
const result = encoder.encode(JSON.stringify(payload));
message.setBinaryAttachment(result);
} else if (typeof payload === 'string') {
const encoder = new TextEncoder();
const result = encoder.encode(payload);
message.setBinaryAttachment(result);
if (contentType === 'application/xml' || contentType === 'text/plain') {
if (typeof payload === 'string')
message.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STRING, payload));
else
message.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STRING, JSON.stringify(payload)));
} else if (contentType === 'application/json') {
message.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STRING, JSON.stringify(payload)));
} else {
message.setBinaryAttachment(payload);
if (typeof payload === 'object') {
const encoder = new TextEncoder();
const result = encoder.encode(JSON.stringify(payload));
message.setBinaryAttachment(result);
} else if (typeof payload === 'string') {
const encoder = new TextEncoder();
const result = encoder.encode(payload);
message.setBinaryAttachment(result);
} else {
message.setBinaryAttachment(payload);
}
}
} else {
message.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STRING, ""));
Expand Down
37 changes: 24 additions & 13 deletions src/common/reply-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class SolaceClient extends VisualizeClient {
session:any = null;
clientName:string = "";
payload:any = null;
contentType:string = "";

constructor(options:any) {
super();
Expand Down Expand Up @@ -114,7 +115,7 @@ export class SolaceClient extends VisualizeClient {
type: 'replier', topicName: request.getDestination().getName(), clientName: this.clientName, uuid: uuid(), msgId: message.getApplicationMessageId()
})

this.reply(request, this.payload);
this.reply(request, this.payload, this.contentType);
} catch (error:any) {
Logger.logDetailedError('send reply failed - ', error.toString())
if (error.cause?.message) Logger.logDetailedError(``, `${error.cause?.message}`)
Expand All @@ -138,7 +139,7 @@ export class SolaceClient extends VisualizeClient {
}

// Subscribes to request topic on Solace PubSub+ Event Broker
subscribe = (topicNames: any, payload: string | Buffer | undefined) => {
subscribe = (topicNames: any, payload: string | Buffer | undefined, contentType: string) => {
//Check if the session has been established
if (!this.session) {
Logger.logWarn("cannot subscribe because not connected to Solace message router!");
Expand All @@ -147,6 +148,7 @@ export class SolaceClient extends VisualizeClient {

try {
this.payload = payload;
this.contentType = contentType;
topicNames.forEach((topicName:any) => {
Logger.logInfo(`subscribing to ${topicName}`);

Expand Down Expand Up @@ -187,28 +189,37 @@ export class SolaceClient extends VisualizeClient {
}
};

reply = (message:any, payload: string | Buffer | undefined) => {
reply = (message:any, payload: string | Buffer | undefined, contentType: string) => {
Logger.logSuccess(`request Received - ${message.getDestination()}, type - ${getType(message)}`)
Logger.dumpMessage(message, this.options.outputMode, this.options.pretty);
Logger.await(`replying to request on topic '${message.getDestination().getName()}'...`);
if (this.session !== null) {
var reply = solace.SolclientFactory.createMessage();
if (payload) {
if (typeof payload === 'object') {
const encoder = new TextEncoder();
const result = encoder.encode(JSON.stringify(payload));
reply.setBinaryAttachment(result);
} else if (typeof payload === 'string') {
const encoder = new TextEncoder();
const result = encoder.encode(payload);
reply.setBinaryAttachment(result);
if (contentType === 'application/xml' || contentType === 'text/plain') {
if (typeof payload === 'string')
reply.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STRING, payload));
else
reply.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STRING, JSON.stringify(payload)));
} else if (contentType === 'application/json') {
reply.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STRING, JSON.stringify(payload)));
} else {
reply.setBinaryAttachment(payload);
if (typeof payload === 'object') {
const encoder = new TextEncoder();
const result = encoder.encode(JSON.stringify(payload));
reply.setBinaryAttachment(result);
} else if (typeof payload === 'string') {
const encoder = new TextEncoder();
const result = encoder.encode(payload);
reply.setBinaryAttachment(result);
} else {
reply.setBinaryAttachment(payload);
}
}
} else {
reply.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STRING, ""));
}

reply.setApplicationMessageId(message.getApplicationMessageId());
if (this.options.replyToTopic)
reply.setDestination(solace.SolclientFactory.createTopicDestination(this.options.replyToTopic));
Expand Down
29 changes: 19 additions & 10 deletions src/common/request-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class SolaceClient extends VisualizeClient {
}

// sends one request
request = (topicName: string, payload: string | Buffer | undefined) => {
request = (topicName: string, payload: string | Buffer | undefined, contentType: string) => {
if (!this.session) {
Logger.logWarn("cannot subscribe because not connected to Solace message router!");
return;
Expand All @@ -123,16 +123,25 @@ export class SolaceClient extends VisualizeClient {
var request = solace.SolclientFactory.createMessage();
request.setDestination(solace.SolclientFactory.createTopicDestination(topicName));
if (payload) {
if (typeof payload === 'object') {
const encoder = new TextEncoder();
const result = encoder.encode(JSON.stringify(payload));
request.setBinaryAttachment(result);
} else if (typeof payload === 'string') {
const encoder = new TextEncoder();
const result = encoder.encode(payload);
request.setBinaryAttachment(result);
if (contentType === 'application/xml' || contentType === 'text/plain') {
if (typeof payload === 'string')
request.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STRING, payload));
else
request.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STRING, JSON.stringify(payload)));
} else if (contentType === 'application/json') {
request.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STRING, JSON.stringify(payload)));
} else {
request.setBinaryAttachment(payload);
if (typeof payload === 'object') {
const encoder = new TextEncoder();
const result = encoder.encode(JSON.stringify(payload));
request.setBinaryAttachment(result);
} else if (typeof payload === 'string') {
const encoder = new TextEncoder();
const result = encoder.encode(payload);
request.setBinaryAttachment(result);
} else {
request.setBinaryAttachment(payload);
}
}
} else {
request.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STRING, ""));
Expand Down
5 changes: 3 additions & 2 deletions src/lib/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const publish = async (
}, options.exitAfter * 1000);
}

var contentType:any = options.contentType as string;
var message:any = options.message as string;
message = (optionsSource.message !== 'cli' && (optionsSource.defaultMessage === 'default' || optionsSource.defaultMessage === 'cli')) ? getDefaultMessage() : message;

Expand Down Expand Up @@ -67,7 +68,7 @@ const publish = async (

if (count === 1) {
for (var i=0; i<options.topic.length; i++) {
publisher.publish(options.topic[i], message, 0);
publisher.publish(options.topic[i], message, contentType, 0);
}
if (options.waitBeforeExit) {
setTimeout(function exit() {
Expand All @@ -80,7 +81,7 @@ const publish = async (
} else {
for (var iter=count ? count : 1, n=1;iter > 0;iter--, n++) {
for (var i=0; i<options.topic.length; i++) {
publisher.publish(options.topic[i], message, n-1);
publisher.publish(options.topic[i], message, contentType, n-1);
}
if (interval) await delay(interval)
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const reply = async (
replier.exit();
});

var contentType:any = options.contentType as string;
var message:any = options.message as string;
message = (optionsSource.message !== 'cli' && (optionsSource.defaultMessage === 'default' || optionsSource.defaultMessage === 'cli')) ? getDefaultMessage() : message;

Expand Down Expand Up @@ -65,7 +66,7 @@ const reply = async (
}

try {
replier.subscribe(options.topic, message);
replier.subscribe(options.topic, message, contentType);
} catch (error:any) {
Logger.logError('exiting...')
process.exit(1)
Expand Down
3 changes: 2 additions & 1 deletion src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const request = async (
requestor.exit();
});

var contentType:any = options.contentType as string;
var message:any = options.message as string;
message = (optionsSource.message !== 'cli' && (optionsSource.defaultMessage === 'default' || optionsSource.defaultMessage === 'cli')) ? getDefaultMessage() : message;

Expand Down Expand Up @@ -68,7 +69,7 @@ const request = async (
// if (options.replyToTopic)
// requestor.subscribe(options.replyToTopic)
var topicName = (typeof options.topic === 'object') ? options.topic[0] : options.topic;
requestor.request(topicName, message);
requestor.request(topicName, message, contentType);
} catch (error:any) {
Logger.logError('exiting...')
process.exit(1)
Expand Down
1 change: 1 addition & 0 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ declare global {
userProperties?: Record<string, string | string[]>
waitBeforeExit?: number
exitAfter?: number
contentType?: string
outputMode?: string
pretty?: boolean
traceVisualization?: boolean
Expand Down
3 changes: 3 additions & 0 deletions src/utils/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const defaultMessageConfig:any = {
// userCos: NOT CONSIDERED
// userData: NOT CONSIDERED
userPropertyMap: undefined,
contentType: "text/plain",
outputMode: 'DEFAULT',
pretty: false
}
Expand Down Expand Up @@ -202,6 +203,7 @@ export const defaultMessageRequestConfig:any = {
enabled: false, // guaranteed publisher
windowSize: 50,

contentType: "text/plain",
outputMode: 'DEFAULT',
pretty: false,

Expand All @@ -228,6 +230,7 @@ export const defaultMessageReplyConfig:any = {
exitAfter: 0,
traceVisualization: false,

contentType: "text/plain",
outputMode: 'DEFAULT',
pretty: false,

Expand Down
2 changes: 2 additions & 0 deletions src/utils/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class MessageClientOptionsEmpty implements StmConfigOptions, MessageConne
deliveryMode?: string | undefined
replyToTopic?: string | undefined
userProperties?: Record<string, string | string[]> | undefined
contentType?: string | undefined
outputMode?: string | undefined
pretty?: boolean | undefined
waitBeforeExit?: number | undefined;
Expand Down Expand Up @@ -118,6 +119,7 @@ export class MessageClientOptionsEmpty implements StmConfigOptions, MessageConne
this.deliveryMode = 'DIRECT'
this.replyToTopic = ""
this.userProperties = { "key": "value" }
this.contentType = "text/plain"
this.outputMode = "DEFAULT"
this.pretty = false
this.waitBeforeExit = 0;
Expand Down
Loading

0 comments on commit 0fc1e4d

Please sign in to comment.