provide backup/restore interface. close #15

This commit is contained in:
ccancellieri 2012-05-08 00:08:20 +02:00
parent 8b4def8793
commit e0df2fe99f
3 changed files with 445 additions and 0 deletions

View File

@ -27,6 +27,7 @@ package it.geosolutions.geoserver.rest;
import it.geosolutions.geoserver.rest.decoder.RESTCoverageList; import it.geosolutions.geoserver.rest.decoder.RESTCoverageList;
import it.geosolutions.geoserver.rest.decoder.RESTCoverageStore; import it.geosolutions.geoserver.rest.decoder.RESTCoverageStore;
import it.geosolutions.geoserver.rest.decoder.utils.NameLinkElem; import it.geosolutions.geoserver.rest.decoder.utils.NameLinkElem;
import it.geosolutions.geoserver.rest.encoder.GSBackupEncoder;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import it.geosolutions.geoserver.rest.encoder.GSNamespaceEncoder; import it.geosolutions.geoserver.rest.encoder.GSNamespaceEncoder;
import it.geosolutions.geoserver.rest.encoder.GSPostGISDatastoreEncoder; import it.geosolutions.geoserver.rest.encoder.GSPostGISDatastoreEncoder;
@ -83,6 +84,122 @@ public class GeoServerRESTPublisher {
this.gspass = password; this.gspass = password;
} }
// ==========================================================================
// === BACKUP and RESTORE
// ==========================================================================
/**
* Issues a GeoServer BACKUP.
* <P>
* This is the equivalent call with cUrl:
*
* <PRE>
* {@code curl -u admin:geoserver -XPOST \
* -H 'Content-type: text/xml' \
* --data "&lt;task&gt;&lt;path&gt;${BACKUP_DATADIR}&lt;/path&gt;&lt;/task&gt;" \
* http://$GSIP:$GSPORT/$SERVLET/rest/bkprst/backup}
* </PRE>
*
* @param backupDir
* the target Backup Dir String.
*
* @return <TT>id</TT> of the backup.
* @throws IllegalArgumentException
* if the backup_dir is null or empty
*/
public String backup(final String backupDir) throws IllegalArgumentException
{
return backup(backupDir, false, false, false);
}
/**
* Issues a GeoServer BACKUP.
* <P>
* This is the equivalent call with cUrl:
*
* <PRE>
* {@code curl -u admin:geoserver -XPOST \
* -H 'Content-type: text/xml' \
* --data "&lt;task&gt;&lt;path&gt;${BACKUP_DATADIR}&lt;/path&gt;&lt;includedata&gt;${includedata}&lt;/includedata&gt;&lt;includegwc&gt;${includegwc}&lt;/includegwc&gt;&lt;includelog&gt;${includelog}&lt;/includelog&gt;&lt;/task&gt;" \
* http://$GSIP:$GSPORT/$SERVLET/rest/bkprst/backup}
* </PRE>
*
* @param backupDir
* the target Backup Dir String.
* @param includedata
* whether or not include the data dir Boolean.
* @param includegwc
* whether or not include the geowebcache dir Boolean.
* @param includelog
* whether or not include the log dir Boolean.
*
* @return <TT>id</TT> of the backup.
* @throws IllegalArgumentException
* if the backup_dir is null or empty
*/
public String backup(final String backupDir,
final boolean includedata,
final boolean includegwc,
final boolean includelog) throws IllegalArgumentException
{
if ((backupDir == null) || backupDir.isEmpty())
{
throw new IllegalArgumentException(
"The backup_dir must not be null or empty");
}
StringBuilder bkpUrl = new StringBuilder(restURL);
bkpUrl.append("/rest/bkprst/backup");
final GSBackupEncoder bkpenc = new GSBackupEncoder(backupDir);
bkpenc.setIncludeData(includedata);
bkpenc.setIncludeGwc(includegwc);
bkpenc.setIncludeLog(includelog);
final String result = HTTPUtils.post(bkpUrl.toString(), bkpenc.toString(),
"text/xml", gsuser, gspass);
return result;
}
/**
* Issues a GeoServer RESTORE.
* <P>
* This is the equivalent call with cUrl:
*
* <PRE>
* {@code curl -u admin:geoserver -XPOST \
* -H 'Content-type: text/xml' \
* --data "&lt;task&gt;&lt;path&gt;${BACKUP_DATADIR}&lt;/path&gt;&lt;/task&gt;" \
* http://$GSIP:$GSPORT/$SERVLET/rest/bkprst/restore}
* </PRE>
*
* @param backupDir
* the target Backup Dir String.
*
* @return <TT>id</TT> of the backup.
* @throws IllegalArgumentException
* if the backup_dir is null or empty
*/
public String restore(final String backupDir) throws IllegalArgumentException
{
if ((backupDir == null) || backupDir.isEmpty())
{
throw new IllegalArgumentException(
"The backup_dir must not be null or empty");
}
StringBuilder bkpUrl = new StringBuilder(restURL);
bkpUrl.append("/rest/bkprst/restore");
final GSBackupEncoder bkpenc = new GSBackupEncoder(backupDir);
final String result = HTTPUtils.post(bkpUrl.toString(), bkpenc.toString(),
"text/xml", gsuser, gspass);
return result;
}
// ========================================================================== // ==========================================================================
// === WORKSPACES // === WORKSPACES
// ========================================================================== // ==========================================================================

View File

@ -0,0 +1,264 @@
/*
* GeoServer-Manager - Simple Manager Library for GeoServer
*
* Copyright (C) 2007,2011 GeoSolutions S.A.S.
* http://www.geo-solutions.it
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package it.geosolutions.geoserver.rest.encoder;
import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils;
import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder;
import org.jdom.Element;
/**
*
* @author Alessio Fabiani (alessio.fabiani at geo-solutions.it)
*/
public class GSBackupEncoder extends PropertyXMLEncoder
{
public static final String TASK = "task";
public static final String PATH = "path";
public static final String INCLUDE_DATA = "includedata";
public static final String INCLUDE_GWC = "includegwc";
public static final String INCLUDE_LOG = "includelog";
public GSBackupEncoder()
{
super(TASK);
}
/**
* @param path the task path
*/
public GSBackupEncoder(String name)
{
super(TASK);
addPath(name);
}
/**
* Add the path to this task
* @param path
* @throws IllegalStateException if path is already set
* @deprecated will be set to protected in the next release
*/
public void addPath(final String path)
{
final Element el = ElementUtils.contains(getRoot(), PATH);
if (el == null)
{
add(PATH, path);
}
else
{
throw new IllegalStateException("Task path is already set: " + el.getText());
}
}
/**
* add or change (if already set) the task path
* @param path
*/
public void setPath(final String path)
{
final Element el = ElementUtils.contains(getRoot(), PATH);
if (el == null)
{
add(PATH, path);
}
else
{
el.setText(path);
}
}
public String getPath()
{
final Element el = ElementUtils.contains(getRoot(), PATH);
if (el != null)
{
return el.getTextTrim();
}
else
{
return null;
}
}
/**
* INCLUDE DATA ELEMENT
*/
/**
* Add the includedata to this task
* @param includedata
* @deprecated will be set to protected in the next release
*/
public void addIncludeData(final Boolean includedata)
{
final Element el = ElementUtils.contains(getRoot(), INCLUDE_DATA);
if (el == null)
{
add(INCLUDE_DATA, includedata.toString());
}
else
{
el.setText(includedata.toString());
}
}
/**
* add or change (if already set) the task includedata
* @param includedata
*/
public void setIncludeData(final Boolean includedata)
{
final Element el = ElementUtils.contains(getRoot(), INCLUDE_DATA);
if (el == null)
{
add(INCLUDE_DATA, includedata.toString());
}
else
{
el.setText(includedata.toString());
}
}
public String getIncludeData()
{
final Element el = ElementUtils.contains(getRoot(), INCLUDE_DATA);
if (el != null)
{
return el.getTextTrim();
}
else
{
return null;
}
}
/**
* INCLUDE GWC ELEMENT
*/
/**
* Add the includegwc to this task
* @param includegwc
* @deprecated will be set to protected in the next release
*/
public void addIncludeGwc(final Boolean includegwc)
{
final Element el = ElementUtils.contains(getRoot(), INCLUDE_GWC);
if (el == null)
{
add(INCLUDE_GWC, includegwc.toString());
}
else
{
el.setText(includegwc.toString());
}
}
/**
* add or change (if already set) the task includegwc
* @param includegwc
*/
public void setIncludeGwc(final Boolean includegwc)
{
final Element el = ElementUtils.contains(getRoot(), INCLUDE_GWC);
if (el == null)
{
add(INCLUDE_GWC, includegwc.toString());
}
else
{
el.setText(includegwc.toString());
}
}
public String getIncludeGwc()
{
final Element el = ElementUtils.contains(getRoot(), INCLUDE_GWC);
if (el != null)
{
return el.getTextTrim();
}
else
{
return null;
}
}
/**
* INCLUDE LOG ELEMENT
*/
/**
* Add the includelog to this task
* @param includelog
* @deprecated will be set to protected in the next release
*/
public void addIncludeLog(final Boolean includelog)
{
final Element el = ElementUtils.contains(getRoot(), INCLUDE_LOG);
if (el == null)
{
add(INCLUDE_LOG, includelog.toString());
}
else
{
el.setText(includelog.toString());
}
}
/**
* add or change (if already set) the task includelog
* @param includelog
*/
public void setIncludeLog(final Boolean includelog)
{
final Element el = ElementUtils.contains(getRoot(), INCLUDE_LOG);
if (el == null)
{
add(INCLUDE_LOG, includelog.toString());
}
else
{
el.setText(includelog.toString());
}
}
public String getIncludeLog()
{
final Element el = ElementUtils.contains(getRoot(), INCLUDE_LOG);
if (el != null)
{
return el.getTextTrim();
}
else
{
return null;
}
}
}

View File

@ -0,0 +1,64 @@
/*
* Copyright (C) 2007 - 2011 GeoSolutions S.A.S.
* http://www.geo-solutions.it
*
* GPLv3 + Classpath exception
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package it.geosolutions.geoserver.rest.encoder;
import junit.framework.TestCase;
import org.apache.log4j.Logger;
import org.junit.Test;
/**
*
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
*/
public class GSBackupEncoderTest extends TestCase
{
/**
* Default logger
*/
protected static final Logger LOGGER = Logger.getLogger(GSBackupEncoderTest.class);
public GSBackupEncoderTest()
{
}
@Test
public void testAll()
{
final GSBackupEncoder bkpenc = new GSBackupEncoder("BK1");
LOGGER.info(bkpenc.toString());
bkpenc.setPath("test_path");
LOGGER.info(bkpenc.toString());
bkpenc.setPath("new_path");
LOGGER.info(bkpenc.toString());
bkpenc.setIncludeData(true);
LOGGER.info(bkpenc.toString());
bkpenc.setIncludeGwc(false);
LOGGER.info(bkpenc.toString());
bkpenc.setIncludeLog(false);
LOGGER.info(bkpenc.toString());
}
}