View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2014-2015 ForgeRock AS.
15   */
16  
17  package org.forgerock.json.resource.examples;
18  
19  import static org.forgerock.json.resource.examples.DemoUtils.*;
20  
21  import org.forgerock.json.resource.Connection;
22  import org.forgerock.json.resource.ConnectionFactory;
23  import org.forgerock.json.resource.Requests;
24  import org.forgerock.json.resource.ResourceException;
25  import org.forgerock.json.resource.ResourceResponse;
26  
27  /**
28   * An example client application which performs an asynchronous read, modify,
29   * write update cycle for a resource in an in memory resource container. This
30   * example does not require any command line arguments.
31   */
32  public final class ReadModifyWriteDemo {
33  
34      private ReadModifyWriteDemo() {
35          // No implementation.
36      }
37  
38      /**
39       * Main method.
40       *
41       * @param args
42       *            The command line arguments: this example does not have any.
43       * @throws ResourceException
44       *             If an unexpected error occurred.
45       */
46      public static void main(final String[] args) throws ResourceException {
47          try (final ConnectionFactory server = getConnectionFactory();
48               final Connection connection = server.getConnection()) {
49              log("Reading resource");
50              final ResourceResponse before = connection.read(ctx(), Requests.newReadRequest("users/1"));
51              log("Resource read and has revision " + before.getRevision());
52              log("Updating resource");
53              final ResourceResponse after =
54                      connection.update(ctx(), Requests.newUpdateRequest("users/1",
55                              userAliceWithIdAndRev(1, 1)));
56              log("Updated resource now has revision " + after.getRevision());
57          }
58      }
59  
60  }