---
title: Reading and writing OpenTokens
description: You can use the Agent API to read an OpenToken from an HTTP request or and write an OpenToken to an HTTP response.
component: java
page_id: java:setup:pf_java_ik_reading_and_writing_opentokens
canonical_url: https://docs.pingidentity.com/integrations/java/setup/pf_java_ik_reading_and_writing_opentokens.html
revdate: June 21, 2024
section_ids:
  sample-code-java-native-multimap-method-standard-opentoken-agent: Sample code - Java native multimap method (standard OpenToken agent)
  sample-code-java-native-multimap-method-jakarta-ee-9-opentoken-agent: Sample code - Java native multimap method (Jakarta EE 9 OpenToken agent)
  sample-code-apache-commons-multimap-method-standard-opentoken-agent: Sample code - Apache commons multimap method (standard OpenToken agent)
  sample-code-apache-commons-multimap-method-jakarta-ee-9-opentoken-agent: Sample code - Apache commons multimap method (Jakarta EE 9 OpenToken agent)
---

# Reading and writing OpenTokens

You can use the Agent API to read an OpenToken from an HTTP request or and write an OpenToken to an HTTP response.

|   |                                                                                                                                                                                                                                                                                                                                            |
| - | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|   | As of Java Integration Kit 2.8, there are two versions of the OpenToken agent: the standard OpenToken agent, and the Jakarta EE 9 OpenToken agent.There are two new instances of sample code available for the Jakarta EE 9 OpenToken agent, which are documented below. These two instances of sample code contain new package locations. |

When the agent object is instantiated, it reads the OpenToken Adapter configuration from the `agent-config.txt` file. This configuration data includes the name of the cookie that the agent object writes, as well as the key to use when encrypting a new OpenToken. If the agent object doesn't find the `agent-config.txt` file, the agent constructor will throw an exception.

To instantiate the agent object, invoke a constructor and load the configuration using one of the following methods.

## Sample code - Java native multimap method (standard OpenToken agent)

```
import com.pingidentity.opentoken.TokenException;
import com.pingidentity.opentoken.generic.Agent;
import com.pingidentity.opentoken.generic.util.PingFederateMultiMap;
. . .
Agent agent = Agent.createUsingNativeMultiMap("<PATH_TO_FILE>/agent-config.txt");
```

|   |                                                                                                                                                                                        |
| - | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|   | This method requires the following library to be available:- `agent/lib/commons-logging.jar`You can find help in [Deploying the Java agent](pf_java_ik_deploying_the_java_agent.html). |

## Sample code - Java native multimap method (Jakarta EE 9 OpenToken agent)

```
import com.pingidentity.opentoken.jakarta.TokenException;
import com.pingidentity.opentoken.jakarta.generic.Agent;
import com.pingidentity.opentoken.jakarta.generic.util.PingFederateMultiMap;
. . .
Agent agent = Agent.createUsingNativeMultiMap("<PATH_TO_FILE>/agent-config.txt");
```

|   |                                                                                                                                                                                                            |
| - | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|   | This method requires the following library to be available:- `agent/jakarta/lib/commons-logging.jar`You can find more information in [Deploying the Java agent](pf_java_ik_deploying_the_java_agent.html). |

## Sample code - Apache commons multimap method (standard OpenToken agent)

```
import java.io.IOException;
import java.io.File;
import java.io.InputStream;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.map.MultiValueMap;

import com.pingidentity.opentoken.Agent;
import com.pingidentity.opentoken.TokenException;
import com.pingidentity.opentoken.util.UrlHelper;
. . . .
Agent agent = new Agent("<PATH_TO_FILE>/agent-config.txt");
```

|   |                                                                                                                                                                                                                                                                            |
| - | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|   | This method requires the following libraries to be available:- `agent/lib/commons-beanutils.jar`

- `agent/lib/commons-collections-3.2.2.jar`

- `agent/lib/commons-logging.jar`You can find help in [Deploying the Java agent](pf_java_ik_deploying_the_java_agent.html). |

## Sample code - Apache commons multimap method (Jakarta EE 9 OpenToken agent)

```
import java.io.IOException;
import java.io.File;
import java.io.InputStream;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.map.MultiValueMap;

import com.pingidentity.opentoken.jakarta.Agent;
import com.pingidentity.opentoken.jakarta.TokenException;
import com.pingidentity.opentoken.jakarta.util.UrlHelper;
. . . .
Agent agent = new Agent("<PATH_TO_FILE>/agent-config.txt");
```

|   |                                                                                                                                                                                                                                                                                                    |
| - | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|   | This method requires the following libraries to be available:- `agent/jakarta/lib/commons-beanutils.jar`

- `agent/jakarta/lib/commons-collections-3.2.2.jar`

- `agent/jakarta/lib/commons-logging.jar`You can find help in [Deploying the Java agent](pf_java_ik_deploying_the_java_agent.html). |
