diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java index f6603bf..d827dd2 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java @@ -27,6 +27,7 @@ package it.geosolutions.geoserver.rest; import it.geosolutions.geoserver.rest.decoder.RESTCoverageList; import it.geosolutions.geoserver.rest.decoder.RESTCoverageStore; 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.GSNamespaceEncoder; import it.geosolutions.geoserver.rest.encoder.GSPostGISDatastoreEncoder; @@ -83,6 +84,122 @@ public class GeoServerRESTPublisher { this.gspass = password; } + + // ========================================================================== + // === BACKUP and RESTORE + // ========================================================================== + + /** + * Issues a GeoServer BACKUP. + *

+ * This is the equivalent call with cUrl: + * + *

+     * {@code curl -u admin:geoserver -XPOST \
+     *      -H 'Content-type: text/xml' \
+     *      --data "<task><path>${BACKUP_DATADIR}</path></task>" \
+     *      http://$GSIP:$GSPORT/$SERVLET/rest/bkprst/backup}
+     * 
+ * + * @param backupDir + * the target Backup Dir String. + * + * @return id 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. + *

+ * This is the equivalent call with cUrl: + * + *

+     * {@code curl -u admin:geoserver -XPOST \
+     *      -H 'Content-type: text/xml' \
+     *      --data "<task><path>${BACKUP_DATADIR}</path><includedata>${includedata}</includedata><includegwc>${includegwc}</includegwc><includelog>${includelog}</includelog></task>" \
+     *      http://$GSIP:$GSPORT/$SERVLET/rest/bkprst/backup}
+     * 
+ * + * @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 id 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. + *

+ * This is the equivalent call with cUrl: + * + *

+     * {@code curl -u admin:geoserver -XPOST \
+     *      -H 'Content-type: text/xml' \
+     *      --data "<task><path>${BACKUP_DATADIR}</path></task>" \
+     *      http://$GSIP:$GSPORT/$SERVLET/rest/bkprst/restore}
+     * 
+ * + * @param backupDir + * the target Backup Dir String. + * + * @return id 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 // ========================================================================== diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSBackupEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSBackupEncoder.java new file mode 100644 index 0000000..d089fc5 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSBackupEncoder.java @@ -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; + } + } +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSBackupEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSBackupEncoderTest.java new file mode 100644 index 0000000..3471450 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSBackupEncoderTest.java @@ -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 . + */ +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()); +} +}