anchor.diff:  TIP: Where possible, the Activity Collector mechanism described in the previous section should be used to aggregate and record the activity information, as this is more efficient that each system individually reporting events to the server.
   
   
  -===== Direct Injection
  -
  -The simpliest approach is to leverage CDI or OSGi (blueprint) to directly inject the Activity Server implementation.
  +===== Java API
  +
  +First step is to obtain a reference to the Activity Server,
   
   ----
   import org.overlord.rtgov.activity.server.ActivityServer;
   
   ....
   
  -@Inject
   private ActivityServer _activityServer=null;
  +
  +....
  +
  +// Get ActivityServer asynchronously
  +ServiceRegistryUtil.addServiceListener(ActivityServer.class, new ServiceListener<ActivityServer>() {
  +
  +    public void registered(ActivityServer service) {
  +        _activityServer = service;
  +    }
  +
  +    public void unregistered(ActivityServer service) {
  +        _activityServer = null;
  +    }            
  +});
  +
  +// Or synchronously
  +_activityServer = ServiceRegistryUtil.getSingleService(ActivityServer.class);
   ----
   
   Once the reference to the Activity Server has been obtained, then call the +store+ method to record a list of Activity Units.
  
positive.code: import org.codehaus.jackson.type.TypeReference;
  import org.overlord.rtgov.activity.model.ActivityType;
  
  
  .....
          java.net.URL queryUrl = new java.net.URL(....);   // <host>/overlord-rtgov/activity/events?type=<type>&value=<value>
  
          // Note: add optional query parameters &from=<fromTimestamp>&to=<toTimestamp> to define a time frame
  
          java.net.HttpURLConnection connection = (java.net.HttpURLConnection) queryUrl.openConnection();
  
          String userPassword = username + ":" + password;
          String encoding = org.apache.commons.codec.binary.Base64.encodeBase64String(userPassword.getBytes());
          
          connection.setRequestProperty("Authorization", "Basic " + encoding);
  
          connection.setRequestMethod("GET");
          connection.setDoOutput(true);
          connection.setDoInput(true);
          connection.setUseCaches(false);
          connection.setAllowUserInteraction(false);
          connection.setRequestProperty("Content-Type", "application/json");
  
          java.io.InputStream is=connection.getInputStream();
  
          java.util.List<ActivityType> activities = mapper.readValue(is, new TypeReference<java.util.List<ActivityType>>() {});
  
          is.close();
  ----
  
  See the REST API documentation in the docs folder of the distribution.
  
negative.code: import org.codehaus.jackson.map.ObjectMapper;
  import org.overlord.rtgov.activity.model.ActivityUnit;
  
  .....
  
  java.util.List<ActivityUnit> activities=........
  java.net.URL storeUrl = new java.net.URL(....);   // <host>/overlord-rtgov/activity/store
  
  java.net.HttpURLConnection connection = (java.net.HttpURLConnection) storeUrl.openConnection();
  
  String userPassword = username + ":" + password;
  String encoding = org.apache.commons.codec.binary.Base64.encodeBase64String(userPassword.getBytes());
  
  connection.setRequestProperty("Authorization", "Basic " + encoding);
  
  connection.setRequestMethod("POST");
  connection.setDoOutput(true);
  connection.setDoInput(true);
  connection.setUseCaches(false);
  connection.setAllowUserInteraction(false);
  connection.setRequestProperty("Content-Type", "application/json");
  
  java.io.OutputStream os=connection.getOutputStream();
  
  ObjectMapper mapper=new ObjectMapper();    // Use jackson to serialize the activity units
  mapper.writeValue(os, activities);
  
  os.flush();
  os.close();
  
  java.io.InputStream is=connection.getInputStream();
  
  byte[] result=new byte[is.available()];
  
  is.read(result);
  is.close();
  ----
  
  See the REST API information in the docs folder of the distribution.
  