You're viewing Apigee Edge documentation.
Go to the
Apigee X documentation. info
What is a Java callout?
If you're new to Java callouts, we recommend that you start with How to create a Java callout.
Using properties in a Java callout
Properties let you specify name/value pairs in a Java callout policy that you can access from your Java code at runtime. You must specify a literal string value for each property; you cannot reference flow variables in this element.
Let's walk through a simple Java callout example that uses properties. In this example, we
create a proxy with that includes a Java callout policy. The policy uses the
<Properties>
element to specify a name/value pair. In the Java code, we
retrieve the value and use it to set a response header.
Download the project
To make things simple, you can download this project from the Apigee api-platform-samples repository on GitHub.
- Download or clone api-platform-samples to your system.
- In a terminal or code editor of your choice, go to the
api-platform-samples/doc-samples/java-properties
project.
The Java callout policy
The policy uses the <Properties>
element. This element lets you specify
name/value pairs. At runtime, your Java code can access the values of the properties specified in
the policy, as we'll see shortly.
<JavaCallout name="java-callout"> <ClassName>com.apigeesample.JavaProperties</ClassName> <ResourceURL>java://edge-custom-policy-java-properties.jar</ResourceURL> <Properties> <Property name="prop">WORLD!</Property> </Properties> </JavaCallout>
The sample Java code
The Java code for this sample shows you how to retrieve a property that was specified in the
Java callout policy. In the sample project, you can find the source code in
java-properties/callout/src/main/java/JavaProperties.java
. We'll walk through the
steps for compiling and deploying this code later in this topic.
package com.apigeesample; import com.apigee.flow.execution.ExecutionContext; import com.apigee.flow.execution.ExecutionResult; import com.apigee.flow.execution.spi.Execution; import com.apigee.flow.message.MessageContext; import java.util.Map; public class JavaProperties implements Execution { private Map <String,String> properties; // read-only public JavaProperties(Map <String,String> properties) { this.properties = properties; } public ExecutionResult execute(MessageContext messageContext, ExecutionContext executionContext) { try { messageContext.getMessage().setHeader("X-PROPERTY-HELLO", this.properties.get("prop")); return ExecutionResult.SUCCESS; } catch (Exception e) { return ExecutionResult.ABORT; } } }
Compile your code with Maven
The project is set up so that you can compile with Maven. If you want to use
javac
, we'll include an example as well.
- Be sure that you have Maven installed:
mvn -version
- Execute the script
java-properties/buildsetup.sh
. This script installs the required JAR dependencies in your local Maven repo. - cd to the
java-properties/callout
directory. - Execute Maven:
mvn clean package
- If you wish, verify that the JAR file
edge-custom-policy-java-properties.jar
was copied tojava-properties/apiproxy/resources/java
. This is the required location for JAR files that you wish to deploy with a proxy.
Compile with javac (optional)
If you want to use javac
to compile the code, you can do something similar to the
following (from the java-properties
directory). The required JAR files are provided
for you in the java-properties/lib
directory.
- cd to
api-platform-samples/doc-samples/java-properties
. - Be sure you have javac in your path.
javac -version
- Execute the following javac command:
javac -d . -classpath ./lib/expressions-1.0.0.jar:./lib/message-flow-1.0.0.jar:. callout/src/main/java/JavaProperties.java
- Create a JAR file:
jar -cvf edge-custom-policy-java-properties.jar ./com/apigeesample/JavaProperties.class
- Copy the JAR file to the apiproxy/resources/java directory. This is the required location
for JAR files that you wish to deploy with a proxy.
cp edge-custom-policy-java-properties.jar apiproxy/resources/java
Deploy and call the proxy
A deploy script is provided in the ./java-properties
directory. But before you
run it, you need to do a quick setup.
- cd to
api-platform-samples/doc-samples/java-properties
- If you haven't already done so, open the file
../../setup/setenv.sh
and edit it as indicated with your Apigee account information: your username (the email address associated with your account), your organization name, and the domain you use to make API management calls. For example, for Edge cloud, the domain ishttps://api.enterprise.apigee.com
; however, your domain may be different if you're using Edge Private Cloud. - Save the
setenv.sh
file. - Execute the deploy script:
./deploy.sh
- If the deploy succeeds, execute the invoke script:
./invoke.sh
The invoke script calls a cURL command that looks like this:
curl http://$org-$env.$api_domain/java-properties
The proxy returns the header:
X-PROPERTY-HELLO: WORLD!
. Remember that in the policy, we added a property name/value pair"prop/WORLD!"
. The Java callout retrieves the value"WORLD!"
and sets it in a header calledX-PROPERTY-HELLO
:messageContext.getMessage().setHeader("X-PROPERTY-HELLO", this.properties.get("prop"));