How to use properties in a Java callout

This page applies to Apigee and Apigee hybrid.

View Apigee Edge documentation.

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.

  1. Download or clone api-platform-samples to your system.
  2. 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.

  1. Be sure that you have Maven installed:
    mvn -version
    
  2. Execute the script java-properties/buildsetup.sh. This script installs the required JAR dependencies in your local Maven repo.
  3. cd to the java-properties/callout directory.
  4. Execute Maven:
    mvn clean package
    
  5. If you wish, verify that the JAR file edge-custom-policy-java-properties.jar was copied to java-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.

  1. cd to api-platform-samples/doc-samples/java-properties.
  2. Be sure you have javac in your path.

    javac -version
    
  3. 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
    
  4. Create a JAR file:

    jar -cvf edge-custom-policy-java-properties.jar ./com/apigeesample/JavaProperties.class
  5. 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.

  1. cd to api-platform-samples/doc-samples/java-properties
  2. The simplest way to deploy the proxy is to bundle it in a zip file and upload the proxy bundle to an environment in your Apigee organization. See Creating an API proxy. Be sure to use the Upload Proxy Bundle option. See also Tips and tricks for uploading API proxy in a proxy bundle in the Apigee community.
  3. When the proxy is deployed, try calling it:
    curl  https://$HOSTNAME/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 called X-PROPERTY-HELLO:

    messageContext.getMessage().setHeader("X-PROPERTY-HELLO", this.properties.get("prop"));