PageRenderTime 27ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredGridCoverageReaderManager.java

http://github.com/geosolutions-it/geoserver-manager
Java | 499 lines | 259 code | 33 blank | 207 comment | 44 complexity | 34ecf858b4d87f30977e82a5fdfb3809 MD5 | raw file
  1. /*
  2. * GeoServer-Manager - Simple Manager Library for GeoServer
  3. *
  4. * Copyright (C) 2007,2012 GeoSolutions S.A.S.
  5. * http://www.geo-solutions.it
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. package it.geosolutions.geoserver.rest.manager;
  26. import it.geosolutions.geoserver.rest.GeoServerRESTPublisher.UploadMethod;
  27. import it.geosolutions.geoserver.rest.HTTPUtils;
  28. import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList;
  29. import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageIndexSchema;
  30. import java.io.File;
  31. import java.io.IOException;
  32. import java.io.UnsupportedEncodingException;
  33. import java.net.MalformedURLException;
  34. import java.net.URL;
  35. import java.net.URLEncoder;
  36. import java.util.zip.ZipFile;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. /**
  40. * Manage GeoTools StructuredGridCoverageReader. It allows to create a store from a file or harvest the coverages contained in a file, to delete
  41. * granules from an existing coverage and eventually to get information about the granules inside a StructuredGridCoverageReader.
  42. *
  43. * @author Simone Giannecchini, GeoSolutions
  44. */
  45. public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerRESTAbstractManager {
  46. /**
  47. * Option that tells GeoServer whether or not to configure all the coverages for a certain ImageMosaic.
  48. *
  49. * @author Simone Giannecchini, GeoSolutions
  50. *
  51. */
  52. public enum ConfigureCoveragesOption{
  53. NONE,
  54. ALL;
  55. public static ConfigureCoveragesOption getDefault() {
  56. return ALL;
  57. }
  58. }
  59. /**
  60. * Default logger
  61. */
  62. private final static Logger LOGGER = LoggerFactory
  63. .getLogger(GeoServerRESTStructuredGridCoverageReaderManager.class);
  64. /**
  65. * Default constructor.
  66. *
  67. * @param restURL GeoServer REST API endpoint
  68. * @param username GeoServer REST API authorized username
  69. * @param password GeoServer REST API password for the former username
  70. * @throws IllegalArgumentException
  71. */
  72. public GeoServerRESTStructuredGridCoverageReaderManager(URL restURL, String username,
  73. String password) throws IllegalArgumentException {
  74. super(restURL, username, password);
  75. }
  76. /**
  77. * Create a new ImageMosaic with the provided configuration provided as a zip file.
  78. *
  79. * <p>
  80. * This call configures all the coverages contained in the ImageMosaic.
  81. *
  82. * @param workspace the GeoServer workspace
  83. * @param coverageStore the GeoServer coverageStore
  84. * @param the absolute path to the file to upload
  85. *
  86. * @return <code>true</code> if the call succeeds or <code>false</code> otherwise.
  87. * @since geoserver-2.4.0, geoserver-mng-1.6.0
  88. */
  89. public boolean create(String workspace, String coverageStore, String path) {
  90. return create(workspace, coverageStore, path, ConfigureCoveragesOption.ALL);
  91. }
  92. /**
  93. * Create a new ImageMosaic with the provided configuration provided as a zip file.
  94. *
  95. * <p>
  96. * With the options configure we can decide whether or not to configure or not the coverages contained in the ImageMosaic.
  97. *
  98. * @param workspace the GeoServer workspace
  99. * @param coverageStore the GeoServer coverageStore
  100. * @param the absolute path to the file to upload
  101. * @param configureOpt tells GeoServer whether to configure all coverages in this mosaic (ALL) or none of them (NONE).
  102. *
  103. * @return <code>true</code> if the call succeeds or <code>false</code> otherwise.
  104. * @since geoserver-2.4.0, geoserver-mng-1.6.0
  105. */
  106. public boolean create(String workspace, String coverageStore, String path, ConfigureCoveragesOption configureOpt) {
  107. // checks
  108. checkString(workspace);
  109. checkString(coverageStore);
  110. checkString(path);
  111. final File zipFile= new File(path);
  112. if(!zipFile.exists()||!zipFile.isFile()||!zipFile.canRead()){
  113. throw new IllegalArgumentException("The provided pathname does not point to a valide zip file: "+path);
  114. }
  115. // is it a zip?
  116. ZipFile zip=null;
  117. try{
  118. zip= new ZipFile(zipFile);
  119. zip.getName();
  120. }catch (Exception e) {
  121. LOGGER.trace(e.getLocalizedMessage(),e.getStackTrace());
  122. throw new IllegalArgumentException("The provided pathname does not point to a valide zip file: "+path);
  123. }finally{
  124. if(zip!=null){
  125. try {
  126. zip.close();
  127. } catch (IOException e) {
  128. // swallow
  129. LOGGER.trace(e.getLocalizedMessage(),e.getStackTrace());
  130. }
  131. }
  132. }
  133. // create URL
  134. StringBuilder ss=HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores/",
  135. coverageStore, "/file.imagemosaic");
  136. switch(configureOpt){
  137. case ALL:
  138. break;
  139. case NONE:
  140. ss.append("?configure=none");
  141. break;
  142. default:
  143. throw new IllegalArgumentException("Unrecognized COnfigureOption: "+configureOpt);
  144. }
  145. String sUrl = ss.toString();
  146. // POST request
  147. String result = HTTPUtils.put(sUrl, zipFile, "application/zip", gsuser, gspass);
  148. return result != null;
  149. }
  150. /**
  151. * Create a store or harvest the coverage from the provided <b>external</b> path.
  152. *
  153. * @param workspace the GeoServer workspace
  154. * @param coverageStore the GeoServer coverageStore
  155. * @param format the format of the file to upload
  156. * @param the absolute path to the file to upload
  157. *
  158. * @return <code>true</code> if the call succeeds or <code>false</code> otherwise.
  159. * @since geoserver-2.4.0, geoserver-mng-1.6.0
  160. */
  161. public boolean harvestExternal(String workspace, String coverageStore, String format,
  162. String path) {
  163. // checks
  164. checkString(workspace);
  165. checkString(coverageStore);
  166. checkString(format);
  167. checkString(path);
  168. // create URL
  169. String sUrl = HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores/",
  170. coverageStore, "/", UploadMethod.EXTERNAL.toString(), ".", format).toString();
  171. // POST request
  172. String result = HTTPUtils.post(sUrl, "file://" + path, "text/plain", gsuser, gspass);
  173. return result != null;
  174. }
  175. /**
  176. * Check the provided string for not being null or empty.
  177. *
  178. * <p>
  179. * It throws an exception in case the string is either null or empty.
  180. *
  181. * @param string the {@link String} to be checked
  182. */
  183. private static void checkString(String string) {
  184. if (string == null) {
  185. throw new NullPointerException("Provided string is is null!");
  186. }
  187. if (string.length() <= 0) {
  188. throw new IllegalArgumentException("Provided string is is empty!");
  189. }
  190. }
  191. /**
  192. * Remove granules from a structured coverage, by providing a CQL filter.
  193. *
  194. * @param workspace the GeoServer workspace
  195. * @param coverageStore the GeoServer coverageStore
  196. * @param coverage the name of the target coverage from which we are going to remove
  197. * @param filter the absolute path to the file to upload
  198. *
  199. * @return <code>null</code> in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}.
  200. *
  201. * @throws MalformedURLException
  202. * @throws UnsupportedEncodingException
  203. *
  204. * @since geoserver-2.4.0, geoserver-mng-1.6.0
  205. */
  206. public boolean removeGranulesByCQL(final String workspace, String coverageStore,
  207. String coverage, String filter) throws UnsupportedEncodingException {
  208. // checks
  209. checkString(workspace);
  210. checkString(coverage);
  211. checkString(filter);
  212. checkString(coverageStore);
  213. // does it exist?
  214. RESTStructuredCoverageGranulesList granulesList = null;
  215. try {
  216. granulesList = getGranules(workspace, coverageStore, coverage, filter, null, 1);
  217. } catch (MalformedURLException e) {
  218. if (LOGGER.isTraceEnabled()) {
  219. LOGGER.trace(e.getMessage(), e);
  220. }
  221. } catch (UnsupportedEncodingException e) {
  222. if (LOGGER.isTraceEnabled()) {
  223. LOGGER.trace(e.getMessage(), e);
  224. }
  225. }
  226. if (granulesList == null || granulesList.isEmpty()) {
  227. if (LOGGER.isTraceEnabled()) {
  228. LOGGER.trace("Granules for filter: " + filter + " does not exist for coverage "
  229. + coverage);
  230. }
  231. return true; // nothing to remove
  232. }
  233. // method
  234. String sUrl = HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores",
  235. "/", coverageStore, "/coverages/", coverage, "/index/granules?filter=",
  236. URLEncoder.encode(filter, "UTF-8")).toString();
  237. if (!HTTPUtils.delete(sUrl, gsuser, gspass)) {
  238. return false;
  239. }
  240. // does it exist?
  241. granulesList = null;
  242. try {
  243. granulesList = getGranules(workspace, coverageStore, coverage, filter, null, 1);
  244. } catch (MalformedURLException e) {
  245. if (LOGGER.isTraceEnabled()) {
  246. LOGGER.trace(e.getMessage(), e);
  247. }
  248. } catch (UnsupportedEncodingException e) {
  249. if (LOGGER.isTraceEnabled()) {
  250. LOGGER.trace(e.getMessage(), e);
  251. }
  252. }
  253. if (granulesList == null || granulesList.isEmpty()) {
  254. return true; // nothing to remove
  255. }
  256. return false;
  257. }
  258. /**
  259. * Remove a granule from a structured coverage by id.
  260. *
  261. * @param workspace the GeoServer workspace
  262. * @param coverageStore the GeoServer coverageStore
  263. * @param coverage the name of the target coverage from which we are going to remove
  264. * @param filter the absolute path to the file to upload
  265. *
  266. * @return <code>null</code> in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}.
  267. *
  268. * @throws MalformedURLException
  269. * @throws UnsupportedEncodingException
  270. *
  271. * @since geoserver-2.4.0, geoserver-mng-1.6.0
  272. */
  273. public boolean removeGranuleById(final String workspace, String coverageStore, String coverage,
  274. String granuleId) {
  275. // checks
  276. checkString(workspace);
  277. checkString(coverage);
  278. checkString(granuleId);
  279. checkString(coverageStore);
  280. // does it exist?
  281. RESTStructuredCoverageGranulesList granule = null;
  282. try {
  283. granule = getGranuleById(workspace, coverageStore, coverage, granuleId);
  284. } catch (MalformedURLException e) {
  285. if (LOGGER.isTraceEnabled()) {
  286. LOGGER.trace(e.getMessage(), e);
  287. }
  288. } catch (UnsupportedEncodingException e) {
  289. if (LOGGER.isTraceEnabled()) {
  290. LOGGER.trace(e.getMessage(), e);
  291. }
  292. }
  293. if (granule == null) {
  294. if (LOGGER.isTraceEnabled()) {
  295. LOGGER.trace("Granule for id: " + granuleId + " does not exist for coverage "
  296. + coverage);
  297. }
  298. return true; // nothing to remove
  299. }
  300. // delete
  301. String sUrl = HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores",
  302. "/", coverageStore, "/coverages/", coverage, "/index/granules/", granuleId)
  303. .toString();
  304. if (!HTTPUtils.delete(sUrl, gsuser, gspass)) {
  305. return false;
  306. }
  307. // has it been canceled?
  308. // does it exist?
  309. granule = null;
  310. try {
  311. granule = getGranuleById(workspace, coverageStore, coverage, granuleId);
  312. } catch (MalformedURLException e) {
  313. if (LOGGER.isTraceEnabled()) {
  314. LOGGER.trace(e.getMessage(), e);
  315. }
  316. } catch (UnsupportedEncodingException e) {
  317. if (LOGGER.isTraceEnabled()) {
  318. LOGGER.trace(e.getMessage(), e);
  319. }
  320. }
  321. if (granule == null) {
  322. return true;
  323. }
  324. return false;
  325. }
  326. /**
  327. * Get information about the schema of the index for a structured coverage.
  328. *
  329. * @param workspace the GeoServer workspace
  330. * @param coverageStore the GeoServer coverageStore
  331. * @param format the format of the file to upload
  332. *
  333. * @return <code>null</code> in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}.
  334. *
  335. * @throws MalformedURLException
  336. * @throws UnsupportedEncodingException
  337. *
  338. * @since geoserver-2.4.0, geoserver-mng-1.6.0
  339. */
  340. public RESTStructuredCoverageIndexSchema getGranuleIndexSchema(final String workspace,
  341. String coverageStore, String coverage) throws MalformedURLException {
  342. // checks
  343. checkString(workspace);
  344. checkString(coverage);
  345. checkString(coverageStore);
  346. // create URL and then call it
  347. String sUrl = HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores/",
  348. coverageStore, "/coverages/", coverage, "/index.xml").toString();
  349. String result = HTTPUtils.get(sUrl, gsuser, gspass);
  350. if (result != null) {
  351. return RESTStructuredCoverageIndexSchema.build(result);
  352. }
  353. return null;
  354. }
  355. /**
  356. * Get information about all the granules for a coverage with optional filter and paging.
  357. *
  358. * @param workspace the GeoServer workspace
  359. * @param coverageStore the GeoServer coverageStore
  360. * @param coverage the name of the target coverage
  361. *
  362. * @return <code>null</code> in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}.
  363. *
  364. * @throws MalformedURLException
  365. * @throws UnsupportedEncodingException
  366. *
  367. * @since geoserver-2.4.0, geoserver-mng-1.6.0
  368. */
  369. public RESTStructuredCoverageGranulesList getGranules(final String workspace,
  370. String coverageStore, String coverage)
  371. throws MalformedURLException, UnsupportedEncodingException {
  372. return getGranules(workspace, coverageStore, coverage, null, null, null);
  373. }
  374. /**
  375. * Get information about the granules for a coverage with optional filter and paging.
  376. *
  377. * @param workspace the GeoServer workspace
  378. * @param coverageStore the GeoServer coverageStore
  379. * @param coverage the name of the target coverage
  380. * @param filter the format of the file to upload, can be <code>null</code> to include all the granules
  381. * @param offset the start page, can be <code>null</code> or an integer
  382. * @param limit the dimension of the page, can be <code>null</code> or a positive integer
  383. *
  384. * @return <code>null</code> in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}.
  385. *
  386. * @throws MalformedURLException
  387. * @throws UnsupportedEncodingException
  388. *
  389. * @since geoserver-2.4.0, geoserver-mng-1.6.0
  390. */
  391. public RESTStructuredCoverageGranulesList getGranules(final String workspace,
  392. String coverageStore, String coverage, String filter, Integer offset, Integer limit)
  393. throws MalformedURLException, UnsupportedEncodingException {
  394. // checks
  395. checkString(workspace);
  396. checkString(coverage);
  397. checkString(coverageStore);
  398. // method
  399. boolean append = false;
  400. String sUrl = HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores/",
  401. coverageStore, "/coverages/", coverage, "/index/granules.xml").toString();
  402. if (filter != null && !filter.isEmpty()) {
  403. append = true;
  404. sUrl = HTTPUtils.append(sUrl, "?filter=", URLEncoder.encode(filter, "UTF-8"))
  405. .toString();
  406. }
  407. if (offset != null) {
  408. if (offset < 0)
  409. offset = 0;
  410. sUrl = HTTPUtils.append(sUrl, append ? "&offset=" : "?offset=", offset.toString())
  411. .toString();
  412. append = true;
  413. }
  414. if (limit != null) {
  415. if (limit < 1) {
  416. limit = 1;
  417. }
  418. sUrl = HTTPUtils.append(sUrl, append ? "&limit=" : "?limit=", limit.toString())
  419. .toString();
  420. append = true;
  421. }
  422. String result = HTTPUtils.get(sUrl, gsuser, gspass);
  423. if (result != null) {
  424. return RESTStructuredCoverageGranulesList.build(result);
  425. }
  426. return null;
  427. }
  428. /**
  429. * Get information about a granule for a structured coverage.
  430. *
  431. * @param workspace the GeoServer workspace
  432. * @param coverageStore the GeoServer coverageStore
  433. * @param format the format of the file to upload
  434. * @param the absolute path to the file to upload
  435. * @param id the ID of the granule to get information for
  436. *
  437. * @return <code>null</code> in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}.
  438. *
  439. * @throws MalformedURLException
  440. * @throws UnsupportedEncodingException
  441. *
  442. * @since geoserver-2.4.0, geoserver-mng-1.6.0
  443. */
  444. public RESTStructuredCoverageGranulesList getGranuleById(final String workspace,
  445. String coverageStore, String coverage, String id) throws MalformedURLException,
  446. UnsupportedEncodingException {
  447. // checks
  448. checkString(workspace);
  449. checkString(coverage);
  450. checkString(coverageStore);
  451. checkString(id);
  452. try {
  453. Integer.parseInt(id);
  454. } catch (NumberFormatException e) {
  455. throw new IllegalArgumentException(e);
  456. }
  457. // method
  458. String sUrl = HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores/",
  459. coverageStore, "/coverages/", coverage, "/index/granules/", id, ".xml").toString();
  460. String result = HTTPUtils.get(sUrl, gsuser, gspass);
  461. if (result != null) {
  462. return RESTStructuredCoverageGranulesList.build(result);
  463. }
  464. return null;
  465. }
  466. }