I want to be able to pass a target server host and port as properties to a java callout policy. Is there a way to accomplish this?
I'm thinking some usage like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <JavaCallout async="false" continueOnError="false" enabled="true" name="Java-Callout-1"> <DisplayName>Java Callout-1</DisplayName> <Properties> <Property name="host" ref="target_server_host"/> </Properties> <ClassName>com.myorg.callouts.MyCallout</ClassName> <ResourceURL>java://my-callouts.jar</ResourceURL> </JavaCallout>
Yes.. Any property you pass under <Properties> will be accessible inside your Java class.
public class Callout implements Execution{ public Callout(Map<String, String> properties){ //Map containing Host and Port properties configured in the policy. } ... }
It's documented here under Samples > Retrieve Property value from Java.
Thanks for the response. I might not have formulated well my question. But what I really want to do is to access an environment target server by its name and maybe pass it to my Java callout as a property (if I cannot access it directly from the Java callout itself)
Within the Java callout policy you can get your variables directly off the context for instance for e.g.
//target.host, target.ip, target.port etc. messageContext.getVariable("target.url");
Full list of variables can be found here. I also recommend you read the best practices before investing a lot of time on Java Callout policy. They are discussed here.
yes, you can do it, by implementing in your callout the constructor that accepts a Map<String,String> , as Sudhee stated.
But to reference a variable you cannot use the "ref" attribute. There's no way, in the Java code, to retrieve the attributes present on a Property object. All you get is the string itself. The convention I have adopted is to wrap the value in curly-braces if it is intended to be a variable. And then at runtime, strip the braces and de-reference the variable if appropriate.
You can see a working callout that uses this approach in Edit-Xml-Node. For example, this policy config:
<JavaCallout name='Java-ReplaceXmlNode-1'> <Properties> <Property name='new-node-type'>text</Property> <Property name='new-node-text'>{request.queryparam.texttoinsert}</Property> <Property name='xpath'>{request.queryparam.xpath}</Property> <Property name='action'>replace</Property> <Property name='output-variable'>my_variable</Property> </Properties> <ClassName>com.dinochiesa.edgecallouts.EditXmlNode</ClassName> <ResourceURL>java://edge-custom-edit-xml-node.jar</ResourceURL> </JavaCallout>
...tells the callout to get the value for "new-node-text" by de-referencing a queryparam called "texttoinsert".
In the Java code, resolving a reference looks like this:
private static final String variableReferencePatternString = "(.*?)\\{([^\\{\\} ]+?)\\}(.*?)"; private static final Pattern variableReferencePattern = Pattern.compile(variableReferencePatternString); // If the value of a property contains a pair of curlies, // eg, {apiproxy.name}, then "resolve" the value by de-referencing // the context variable whose name appears between the curlies. private String resolvePropertyValue(String spec, MessageContext msgCtxt) { Matcher matcher = variableReferencePattern.matcher(spec); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, ""); sb.append(matcher.group(1)); sb.append((String) msgCtxt.getVariable(matcher.group(2))); sb.append(matcher.group(3)); } matcher.appendTail(sb); return sb.toString(); }
It returns an empty string if the variable is not known.
Thanks for the response. I might not have formulated well my question. But what I really want to do is to access an environment target server by its name and maybe pass it to my Java callout as a property (if I cannot access it directly from the Java callout itself)
I might not have formulated well my question. But what I really want to do is to access an environment target server by its name and maybe pass it to my Java callout as a property (if I cannot access out directly from the Java callout itself)