in grpc-web/callout/src/main/java/com/google/apigee/callouts/ProtobufDecoder.java [182:247]
public ExecutionResult execute(MessageContext messageContext, ExecutionContext executionContext) {
ByteArrayOutputStream stdoutOS = new ByteArrayOutputStream();
ByteArrayOutputStream stderrOS = new ByteArrayOutputStream();
PrintStream stderr = null;
PrintStream stdout = null;
try {
stdout = new PrintStream(stdoutOS, true, StandardCharsets.UTF_8.name());
stderr = new PrintStream(stderrOS, true, StandardCharsets.UTF_8.name());
VarResolver vars = new VarResolver(messageContext, this.properties);
new Debug(messageContext, CALLOUT_VAR_PREFIX);
String protoDescriptorBase64 = vars.getVar(vars.getProp(PROP_DESCRIPTOR_BASE64_REF));
String protoServiceMethodPath = vars.getVar(vars.getProp(PROP_SERVICE_METHOD_REF));
String protoDecodedMessageRef = vars.getProp(PROP_DECODED_MESSAGE_REF);
String protoProcessMessage = vars.getProp(PROP_MSG_REF);
Boolean protoMessageIsBase64 = "true".equals(vars.getProp(PB_MESSAGE_IS_BASE_64));
Descriptors.MethodDescriptor method = null;
Message msg;
try {
method = this.getMethod(protoDescriptorBase64, protoServiceMethodPath);
} catch (Exception ex) {
stdout.println("could not find protobuf service/method. " + ex.getMessage());
}
Descriptors.Descriptor desc;
if (PROCESS_RESPONSE.equals(protoProcessMessage)) {
msg = messageContext.getResponseMessage();
desc = method == null ? null : method.getOutputType();
} else {
msg = messageContext.getRequestMessage();
desc = method == null ? null : method.getInputType();
}
String decoded = null;
try {
if (desc == null) {
decoded = this.decodeAsText(msg.getContentAsStream(), protoMessageIsBase64, stdout, stderr);
messageContext.setVariable(String.format("%s.%s", CALLOUT_VAR_PREFIX, "message-format"), "text");
} else {
decoded = this.decodeAsJSON(msg.getContentAsStream(), protoMessageIsBase64, desc, stderr, stderr);
messageContext.setVariable(String.format("%s.%s", CALLOUT_VAR_PREFIX, "message-format"), "json");
}
} catch (Exception ex) {
stderr.println("could not decode protobuf. " + ex.getMessage());
}
if (decoded != null) {
messageContext.setVariable(String.format("%s.%s", CALLOUT_VAR_PREFIX, "message-data"), decoded);
if (protoDecodedMessageRef != null && !protoDecodedMessageRef.isEmpty()) {
messageContext.setVariable(protoDecodedMessageRef, decoded);
}
}
return ExecutionResult.SUCCESS;
} catch (Exception | Error ex) {
ex.printStackTrace(stderr);
return ExecutionResult.ABORT;
} finally {
this.saveOutputs(messageContext, stdoutOS, stderrOS);
}
}