Convert the response to uppercase with a Java callout

You're viewing Apigee Edge documentation.
Go to the Apigee X documentation.

If you're trying out Java callout for the first time, we recommend you begin with How to create a Java callout.

This cookbook sample demonstrates how to create a simple JavaCallout policy that executes custom Java code within the context of a proxy flow.

What does the sample code do?

The API proxy in this sample calls a target service that returns a simple JSON response. The Java Callout policy is placed on the target response flow. The Java code converts the headers and content of the response to uppercase letters. It's a simple example; however, it illustrates how to get custom Java code working in the context of an API proxy on Edge.

Try the sample

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-cookbook project.

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 following the Maven example.

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

  1. cd to api-platform-samples/doc-samples/java-cookbook.
  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/
  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/ResponseUppercase.class apiproxy/resources/java

Deploy and call the proxy

A deploy script is provided in the ./java-cookbook directory. But before you run it, you need to do a quick setup.

  1. cd to api-platform-samples/doc-samples/java-cookbook
  2. If you haven't already done so, open the file ../../setup/ 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; however, your domain may be different if you're using Edge Private Cloud.
  3. Save the file.
  4. Execute the deploy script:
  5. If the deploy succeeds, execute the invoke script:

    The invoke script calls a cURL command that looks like this:

    curl  http://$org-$env.$api_domain/java-cookbook/json

The response looks something like this:

< HTTP/1.1 200 OK
< Date: Tue, 09 May 2017 20:31:08 GMT
< Content-Length: 68
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< X-Powered-By: APIGEE
< Server: Apigee Router
* Curl_http_done: called premature == 0
* Connection #0 to host left intact

Proxy flow

This is a shot of the trace tool showing the flow of the proxy:

Proxy structure

This is the structure of the proxy. Note that it includes a JAR file in the apiproxy/resources/java directory. The JAR is required. We provide it for the sample, but if you are doing custom work, you need to build it yourself and copy it into this location. For guidance on compiling and deploying a Java callout, see How to create a Java callout.



About the Java code

The Java Callout policy runs the following Java code, which converts response headers and the response body to uppercase. Note that the program uses Apigee-specific packages. These packages provide objects and methods that you can use to interact directly with the proxy flow. Methods exist that let you get and set flow variables, headers, message content, and more.

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.Set; 

public class ResponseUppercase implements Execution{
    public ExecutionResult execute(MessageContext messageContext, ExecutionContext executionContext) {
        try {
            Set headers = messageContext.getMessage().getHeaderNames();
            for (String header : headers) {
                String h = messageContext.getMessage().getHeader(header).toUpperCase();
                messageContext.getMessage().setHeader(header, h);
            String content = messageContext.getMessage().getContent();
            return ExecutionResult.SUCCESS;
        } catch (Exception e) {
                return ExecutionResult.ABORT;

Examine the sample proxy

We leave it to you to open the proxy and examine the files. Here is the Java Callout policy. Note how it references the class that the callout executes and the JAR file. All Java Callout policies follow this pattern. See also Java Callout policy.

<JavaCallout name="responseUppercase">