Send Docs Feedback

Note: Most user interface tasks can be performed in Edge Classic or the New Edge experience. For an overview, getting started topics, and release notes specific to the New Edge experience, see the docs.

How to use properties in a Java callout

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;
		}
	}
}
To retrieve the properties specified in the policy, all you need to do is implement the Execution class constructor as shown above. Then, in the Java code, you can get the property value you want by name.


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.

The POM file and configuration for the Maven compile should work, but are offered in the Git repository as an example only. You may need to make adjustments for your environment. If you make changes to the sample code, you may also need to adjust the Maven configuration. 
  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
    If you get a Maven error, be sure that you are in the java-properties/callout directory.
  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

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. 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 com/apigeesample/JavaProperties.class 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. 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 is https://api.enterprise.apigee.com; however, your domain may be different if you're using Edge Private Cloud.
  3. Save the setenv.sh file.
  4. Execute the deploy script:
    ./deploy.sh
  5. 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 called X-PROPERTY-HELLO:

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

Help or comments?