[Geoserver-devel] Reload geoserver configuration via REST

Hi all,

I have read the email the from Francesco Izzi (21.04.2009). He was searching for a possibility to reload the Geoserver configuration via Rest. -> http://www.mail-archive.com/geoserver-devel@lists.sourceforge.net/msg04914.html
I had the same problem, so I tried to find a solution.
I'm not sure if the way I have implemented the function is really correct, but it seems to work.
Hoping somebody can take a look at the code to upgrade and correct it.

Justin Deoliveira provides an indication to take a look at the restconfig extension and specially at the CatalogResource Base.java.

Greetings, Lars Schrader

1. The solution should accept the following url to reload the catalog

curl -u username:password -v -XPOST http://localhost:8080/geoserver/rest/catalogreload

2.1 adding the following code into the applicationContext.xml of the restconfig extension
        <entry>
          <key><value>/catalogreload</value></key>
          <value>catalogReloader</value>
        </entry>
        .............
        <bean id="catalogReloader" class="org.geoserver.catalog.rest.CatalogReloader" parent="abstractCatalogFinder"/>

2.2 create a new class org.geoserver.catalog.rest.CatalogReloader

package org.geoserver.catalog.rest;

import java.util.logging.Logger;

public class CatalogReloader extends AbstractCatalogFinder {
       static Logger LOGGER = Logging.getLogger( "org.geoserver.catalog.rest");
     public CatalogReloader(Catalog catalog) {
        super(catalog);
           }
       @Override
      public Resource findTarget(Request request, Response response){
      try{
        CatalogResourceBase.reloadCatalog();
      }catch(Exception e){
        LOGGER.warning(CatalogReloader.class.getName()+" : catalog couldn't be reloaded.");
      }
      response.setStatus(Status.SUCCESS_ACCEPTED);
      return null;
    }
}

2.3 adding the following code into the org.geoserver.catalog.rest.CatalogResourceBase of the restconfig extension

/**
     * Static method to reload the catalog
     */
    protected static void reloadCatalog() throws Exception {
      GeoServerLoader loader = GeoServerExtensions.bean( GeoServerLoader.class );
      try {
          synchronized (org.geoserver.config.GeoServer.CONFIGURATION_LOCK) {
              loader.reload();
              LOGGER.info(CatalogResourceBase.class.getName()+" : catalog could be reloaded.");
          }
      }
      catch (Exception e) {
          throw new RuntimeException( e );
      }
    }

3. After that I have created a new restconfig-1.7.x.jar with the complete restconfig extension.

The answer of the curl statement is:
< HTTP/1.1 202 Accepted
< Server: Apache-Coyote/1.1
< Date: Fri, 24 Jul 2009 08:05:01 GMT
< Server: Noelios-Restlet-Engine/1.0..8
< Transfer-Encoding: chunked
<
* Connection #0 to host localhost left intact
* Closing connection #0

4. You can also access the rest patch via java. The solution based on the email from
Santiago Montico (21.07.2009). -> http://www.mail-archive.com/geoserver-devel@lists.sourceforge.net/msg06425.html

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import org.apache.log4j.Logger;

import sun.misc.BASE64Encoder;

public class GeoServerCatalogReloadRest {
  private static Logger geoServerCatalogReloadLogger = Logger
      .getLogger(GeoServerCatalogReloadRest.class.getName());

  public GeoServerCatalogReloadRest () {

  }
public static Boolean doReload (String urlGeoserver, String usernameGeoserver,
      String passwordGeoserver) {
    String result = null;
    try {
      // example:
      // username:admin
      // password:geoserver
      // url:http://localhost:8080/geoserver/rest/catalogreload
        URL url = new URL(urlGeoserver + "rest/catalogreload");
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("POST");
      // write auth header
      BASE64Encoder encoder = new BASE64Encoder();
      String encodedCredential = encoder.encode((usernameGeoserver + ":" + passwordGeoserver)
          .getBytes());
      connection.setRequestProperty("Authorization", "Basic " + encodedCredential);
      connection.setRequestProperty("Content-Type", "text/xml");
      connection.connect();
      result = connection.getResponseMessage();

    } catch (MalformedURLException mue) {
      geoServerCatalogReloadLogger.warn(GeoServerCatalogReloadRest.class.getName()
          + ": url not correct -> " + result + " : " + mue.getMessage());
       return new Boolean(false);
    } catch (ProtocolException pe) {
      geoServerCatalogReloadLogger.warn(GeoServerCatalogReloadRest.class.getName()
          + ": protocol not correct -> " + result + " : " + pe.getMessage());
       return new Boolean(false);
    } catch (IOException ioe) {
      geoServerCatalogReloadLogger.warn(GeoServerCatalogReloadRest.class.getName()
          + ": connection failed -> " + result + " : " + ioe.getMessage());
      return new Boolean(false);
    }
    return new Boolean(true);
  }

}

Hi Lars,

The solution looks quite nice!! Thanks for the contribution.

My first question is do you have the code in a patch format that I can readily apply?

The second is that the patch will need some reworking for trunk. The reason being the way the catalog is loaded has changed. But it should not be much work.

Thank again.

-Justin

Lars Schrader wrote:

Hi all,

I have read the email the from Francesco Izzi (21.04.2009). He was searching for a possibility to reload the Geoserver configuration via Rest. -> http://www.mail-archive.com/geoserver-devel@lists.sourceforge.net/msg04914.html
I had the same problem, so I tried to find a solution.
I'm not sure if the way I have implemented the function is really correct, but it seems to work.
Hoping somebody can take a look at the code to upgrade and correct it.

Justin Deoliveira provides an indication to take a look at the restconfig extension and specially at the CatalogResource Base.java.

Greetings, Lars Schrader

1. The solution should accept the following url to reload the catalog

curl -u username:password -v -XPOST http://localhost:8080/geoserver/rest/catalogreload

2.1 adding the following code into the applicationContext.xml of the restconfig extension
        <entry>
          <key><value>/catalogreload</value></key>
          <value>catalogReloader</value>
        </entry>
        .............
        <bean id="catalogReloader" class="org.geoserver.catalog.rest.CatalogReloader" parent="abstractCatalogFinder"/>

2.2 create a new class org.geoserver.catalog.rest.CatalogReloader

package org.geoserver.catalog.rest;

import java.util.logging.Logger;

public class CatalogReloader extends AbstractCatalogFinder {
       static Logger LOGGER = Logging.getLogger( "org.geoserver.catalog.rest");
     public CatalogReloader(Catalog catalog) {
        super(catalog);
           }
       @Override
      public Resource findTarget(Request request, Response response){
      try{
        CatalogResourceBase.reloadCatalog();
      }catch(Exception e){
        LOGGER.warning(CatalogReloader.class.getName()+" : catalog couldn't be reloaded.");
      }
      response.setStatus(Status.SUCCESS_ACCEPTED);
      return null;
    }
}

2.3 adding the following code into the org.geoserver.catalog.rest.CatalogResourceBase of the restconfig extension

/**
     * Static method to reload the catalog
     */
    protected static void reloadCatalog() throws Exception {
      GeoServerLoader loader = GeoServerExtensions.bean( GeoServerLoader.class );
      try {
          synchronized (org.geoserver.config.GeoServer.CONFIGURATION_LOCK) {
              loader.reload();
              LOGGER.info(CatalogResourceBase.class.getName()+" : catalog could be reloaded.");
          }
      }
      catch (Exception e) {
          throw new RuntimeException( e );
      }
    }

3. After that I have created a new restconfig-1.7.x.jar with the complete restconfig extension.

The answer of the curl statement is:
< HTTP/1.1 202 Accepted
< Server: Apache-Coyote/1.1
< Date: Fri, 24 Jul 2009 08:05:01 GMT
< Server: Noelios-Restlet-Engine/1.0..8
< Transfer-Encoding: chunked
<
* Connection #0 to host localhost left intact
* Closing connection #0

4. You can also access the rest patch via java. The solution based on the email from
Santiago Montico (21.07.2009). -> http://www.mail-archive.com/geoserver-devel@lists.sourceforge.net/msg06425.html

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import org.apache.log4j.Logger;

import sun.misc.BASE64Encoder;

public class GeoServerCatalogReloadRest {
  private static Logger geoServerCatalogReloadLogger = Logger
      .getLogger(GeoServerCatalogReloadRest.class.getName());

  public GeoServerCatalogReloadRest () {

  }
public static Boolean doReload (String urlGeoserver, String usernameGeoserver,
      String passwordGeoserver) {
    String result = null;
    try {
      // example:
      // username:admin
      // password:geoserver
      // url:http://localhost:8080/geoserver/rest/catalogreload
        URL url = new URL(urlGeoserver + "rest/catalogreload");
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("POST");
      // write auth header
      BASE64Encoder encoder = new BASE64Encoder();
      String encodedCredential = encoder.encode((usernameGeoserver + ":" + passwordGeoserver)
          .getBytes());
      connection.setRequestProperty("Authorization", "Basic " + encodedCredential);
      connection.setRequestProperty("Content-Type", "text/xml");
      connection.connect();
      result = connection.getResponseMessage();

    } catch (MalformedURLException mue) {
      geoServerCatalogReloadLogger.warn(GeoServerCatalogReloadRest.class.getName()
          + ": url not correct -> " + result + " : " + mue.getMessage());
       return new Boolean(false);
    } catch (ProtocolException pe) {
      geoServerCatalogReloadLogger.warn(GeoServerCatalogReloadRest.class.getName()
          + ": protocol not correct -> " + result + " : " + pe.getMessage());
       return new Boolean(false);
    } catch (IOException ioe) {
      geoServerCatalogReloadLogger.warn(GeoServerCatalogReloadRest.class.getName()
          + ": connection failed -> " + result + " : " + ioe.getMessage());
      return new Boolean(false);
    }
    return new Boolean(true);
  }

}

------------------------------------------------------------------------------
_______________________________________________
Geoserver-devel mailing list
Geoserver-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geoserver-devel

--
Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.

Hi Justin,

it's the first time for me to work at geoserver.
How is the buildup of such a patch?

You can have the solution as source code (*.java, *.xml).
Is this helpful for you?

I have build a new restconfig-1.7.x.jar which includes the catalog patch.
The jar-File contains the class- as, the java-files as also the necessary
xml-file.

Greetings,
Lars

Hi Lars,

The solution looks quite nice!! Thanks for the contribution.

My first question is do you have the code in a patch format that I can
readily apply?

The second is that the patch will need some reworking for trunk. The
reason being the way the catalog is loaded has changed. But it should

not be much work.

Thank again.

-Justin

--
View this message in context: http://www.nabble.com/Reload-geoserver-configuration-via-REST-tp24668144p24676214.html
Sent from the GeoServer - Dev mailing list archive at Nabble.com.

Hi Lars,

The easiest way to generate a patch is by working with the source code directory from svn. If you have a checkout available you can use the 'svn diff' command and it will generate a patch file you. With that patch i can directly apply the changes with a single command. So it is a much nicer way to share changes.

However in this case most of the code is additive and not much existing code is being modified so the .java and .xml files should work ok.

One thing however is could you open a jira task with your code attached to it. That would be helpful as well. Also some links that may be of interest in the developers guide:

http://docs.geoserver.org/1.7.x/en/developer/quickstart/index.html
http://docs.geoserver.org/1.7.x/en/developer/policies/index.html

-Justin

Lars Schrader wrote:

Hi Justin,

it's the first time for me to work at geoserver.
How is the buildup of such a patch?

You can have the solution as source code (*.java, *.xml).
Is this helpful for you?

I have build a new restconfig-1.7.x.jar which includes the catalog patch.
The jar-File contains the class- as, the java-files as also the necessary
xml-file.

Greetings,
Lars

Hi Lars,

The solution looks quite nice!! Thanks for the contribution.

My first question is do you have the code in a patch format that I can readily apply?

The second is that the patch will need some reworking for trunk. The reason being the way the catalog is loaded has changed. But it should

not be much work.

Thank again.

-Justin

--
Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.

Hi Justin,

I will send you the source code also open a jira task at next monday or
tuesday.

In the future I try to use the svn repository to create a patch file.
Thanks für your help.

Greetings, Lars
  
<Hi Lars,
<
<The easiest way to generate a patch is by working with the source code
<directory from svn. If you have a checkout available you can use the
<'svn diff' command and it will generate a patch file you. With that
<patch i can directly apply the changes with a single command. So it is a
<much nicer way to share changes.
<
<However in this case most of the code is additive and not much existing
<code is being modified so the .java and .xml files should work ok.
<
<One thing however is could you open a jira task with your code attached
<to it. That would be helpful as well. Also some links that may be of
<interest in the developers guide:
<
<http://docs.geoserver.org/1.7.x/en/developer/quickstart/index.html
<http://docs.geoserver.org/1.7.x/en/developer/policies/index.html
<
<-Justin

--
View this message in context: http://www.nabble.com/Reload-geoserver-configuration-via-REST-tp24668144p24726662.html
Sent from the GeoServer - Dev mailing list archive at Nabble.com.

Hi Justin,

that's the jira task -> http://jira.codehaus.org/browse/GEOS-3317
There's also the upload of the source code.

Greetings, Lars

--
View this message in context: http://www.nabble.com/Reload-geoserver-configuration-via-REST-tp24668144p24786789.html
Sent from the GeoServer - Dev mailing list archive at Nabble.com.

Great, thanks Lars.

I have a number of REST config related things to do this week, and will throw reviewing and applying your patch into the mix. So I hope to get go it by end of week.

Thanks again for the contribution!! Keep them coming :slight_smile:

-Justin

Lars Schrader wrote:

Hi Justin,

that's the jira task -> http://jira.codehaus.org/browse/GEOS-3317
There's also the upload of the source code.

Greetings, Lars

--
Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.