Skip to content
Emanuele Tajariol edited this page Sep 6, 2016 · 3 revisions

Some code examples

GNClient is a frontend interface used for all GeoNetwork versions.

You can instantiate different clients for different GeoNetwork instances and versions:

   GNClient client26  = new GN26Client(gnServiceURL, gnUsername, gnPassword);
   GNClient client28  = new GN28Client(gnServiceURL, gnUsername, gnPassword);
   GNClient client210 = new GN210Client(gnServiceURL, gnUsername, gnPassword);
   GNClient client3   = new GN3Client(gnServiceURL, gnUsername, gnPassword);

If you only need anonymous access, you may instantiate a client without auth info::

   GNClient client = new GN210Client(gnServiceURL);

Please notice that the various versions use different authentication mechanisms (earlier versions use a login service, while latest ones use a Spring-based HTTP basic auth). The client will automatically carry out the auth handshake whenever needed.

Insert a metadata

A simple example how to insert a metadata into GeoNetwork.

    private static final String gnServiceURL = "http://localhost:8080/geonetwork";
    private static final String gnUsername = "admin";
    private static final String gnPassword = "admin";

    // Create a GeoNetwork client pointing to the GeoNetwork service
    GNClient client = new GNClient210(gnServiceURL, gnUsername, gnPassword);

    // Create a configuration for the metadata.
    // These params are the ones needed by the GN service.
    GNInsertConfiguration cfg = new GNInsertConfiguration();
    cfg.setCategory("datasets");
    cfg.setGroup("1"); // group 1 is usually "all"
    cfg.setStyleSheet("_none_");
    cfg.setValidate(Boolean.FALSE);

    File file = new File("/your/path/here/metadata.xml");

    long id = client.insertMetadata(cfg, file);

    LOGGER.info("Metadata created with id " + id);

Reset privileges

How to reset privileges, using GN privileges numbering:

   GNClient client = new GNClient210(gnServiceURL, gnUsername, gnPassword);
 
   long metadataID = ...;

   GNPrivConfiguration pcfg = new GNPrivConfiguration();
   pcfg.addPrivileges(0, "012356");
   pcfg.addPrivileges(1, "012356");
   pcfg.addPrivileges(2, "012356");
   pcfg.addPrivileges(3, "012356");
   pcfg.addPrivileges(4, "012356");

   client.setPrivileges(id, pcfg);

You may alternatively use an enum defined in the library:

   // grant all privileges
   pcfg.addPrivileges(0, EnumSet.allOf(GNPriv.class));

   // grant privs by explicitely naming them
   pcfg.addPrivileges(1, EnumSet.of(GNPriv.DOWNLOAD, GNPriv.VIEW));

   // grant privs by using GN ids
   pcfg.addPrivileges(2, EnumSet.of(GNPriv.get(0), GNPriv.get(1)));

Since there are some mismatching between GeoNetwork docs and real code, please have a look to the javadoc in GNPriv.

Search and retrieve metadata

You will use GNSearchRequest for packing your search params.

You can add configs (addconfig()) or params (addParam()); configs and params are encoded the same way into GeoNetwork requests, but for sake of clarity (and future improvements, such as values' validity checks client side) we may want them to be differentiated.

   GNSearchRequest searchRequest = new GNSearchRequest();

   // add a predefined search field
   searchRequest.addParam(GNSearchRequest.Param.any, "your search string");
   // add a custom param
   searchRequest.addParam("customParam", "custom");

   // only local results
   searchRequest.addConfig(GNSearchRequest.Config.remote, "off");

   // Init your GN client (and perform login if required)
   GNClient client = new GNClient(gnServiceURL);

   // do the search!
   GNSearchResponse searchResponse = client.search(searchRequest);

   if(searchResponse.getCount() != 0 ) {
      // loop on all metadata
      for (GNSearchResponse.GNMetadata metadata : searchResponse) {     
         Long id = metadata.getId();
         // and this is the full metadata document, as a JDOM element.
         Element md = client.get(id);
         XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
         LOGGER.info("Metadata -> " + out.outputString(md));
      }
   }
Clone this wiki locally