-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathGeoServerRESTReader.java
1193 lines (1081 loc) · 46 KB
/
GeoServerRESTReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GeoServer-Manager - Simple Manager Library for GeoServer
*
* Copyright (C) 2007,2013 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;
import it.geosolutions.geoserver.rest.decoder.RESTCoverage;
import it.geosolutions.geoserver.rest.decoder.RESTCoverageList;
import it.geosolutions.geoserver.rest.decoder.RESTCoverageStore;
import it.geosolutions.geoserver.rest.decoder.RESTCoverageStoreList;
import it.geosolutions.geoserver.rest.decoder.RESTDataStore;
import it.geosolutions.geoserver.rest.decoder.RESTDataStoreList;
import it.geosolutions.geoserver.rest.decoder.RESTFeatureType;
import it.geosolutions.geoserver.rest.decoder.RESTFeatureTypeList;
import it.geosolutions.geoserver.rest.decoder.RESTLayer;
import it.geosolutions.geoserver.rest.decoder.RESTLayer21;
import it.geosolutions.geoserver.rest.decoder.RESTLayerGroup;
import it.geosolutions.geoserver.rest.decoder.RESTLayerGroupList;
import it.geosolutions.geoserver.rest.decoder.RESTLayerList;
import it.geosolutions.geoserver.rest.decoder.RESTNamespace;
import it.geosolutions.geoserver.rest.decoder.RESTNamespaceList;
import it.geosolutions.geoserver.rest.decoder.RESTResource;
import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList;
import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageIndexSchema;
import it.geosolutions.geoserver.rest.decoder.RESTStyle;
import it.geosolutions.geoserver.rest.decoder.RESTStyleList;
import it.geosolutions.geoserver.rest.decoder.RESTWms;
import it.geosolutions.geoserver.rest.decoder.RESTWmsList;
import it.geosolutions.geoserver.rest.decoder.RESTWmsStore;
import it.geosolutions.geoserver.rest.decoder.RESTWmsStoreList;
import it.geosolutions.geoserver.rest.decoder.RESTWorkspaceList;
import it.geosolutions.geoserver.rest.decoder.about.GSVersionDecoder;
import it.geosolutions.geoserver.rest.manager.GeoServerRESTStructuredGridCoverageReaderManager;
import it.geosolutions.geoserver.rest.manager.GeoServerRESTStyleManager;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Connect to a GeoServer instance to read its data.
* <BR>Info are returned as <TT>Strings</TT> or, for complex data, as XML elements
* wrapped in proper parsers (e.g.: {@link RESTLayer}, {@link RESTCoverageStore}, ...).
*
* @author ETj (etj at geo-solutions.it)
*/
public class GeoServerRESTReader {
private final static Logger LOGGER = LoggerFactory.getLogger(GeoServerRESTReader.class);
private final String baseurl;
private String username;
private String password;
private GeoServerRESTStyleManager styleManager;
/**
* Creates a <TT>GeoServerRESTReader</TT> for a given GeoServer instance and
* no auth credentials.
* <P><B><I>Note that GeoServer 2.0 REST interface requires username/password credentials by
* default, if not otherwise configured. </I></B>.
*
* @param gsUrl the base GeoServer URL(e.g.: <TT>http://localhost:8080/geoserver</TT>)
*/
public GeoServerRESTReader(URL gsUrl) {
baseurl = init(gsUrl, null, null);
}
/**
* Creates a <TT>GeoServerRESTReader</TT> for a given GeoServer instance and
* no auth credentials.
* <P><B><I>Note that GeoServer 2.0 REST interface requires username/password credentials by
* default, if not otherwise configured. </I></B>.
*
* @param gsUrl the base GeoServer URL (e.g.: <TT>http://localhost:8080/geoserver</TT>)
*/
public GeoServerRESTReader(String gsUrl) throws MalformedURLException {
baseurl = init(gsUrl, null, null);
}
/**
* Creates a <TT>GeoServerRESTReader</TT> for a given GeoServer instance
* with the given auth credentials.
*
* @param gsUrl the base GeoServer URL (e.g.: <TT>http://localhost:8080/geoserver</TT>)
* @param username username auth credential
* @param password password auth credential
*/
public GeoServerRESTReader(String gsUrl, String username, String password) throws MalformedURLException {
baseurl = init(gsUrl, username, password);
}
/**
* Creates a <TT>GeoServerRESTReader</TT> for a given GeoServer instance
* with the given auth credentials.
*
* @param gsUrl the base GeoServer URL (e.g.: <TT>http://localhost:8080/geoserver</TT>)
* @param username username auth credential
* @param password password auth credential
*/
public GeoServerRESTReader(URL gsUrl, String username, String password) {
baseurl = init(gsUrl, username, password);
}
private String init(String gsUrl, String username, String password) throws MalformedURLException {
return init(new URL(gsUrl), username, password);
}
private String init(URL gsUrl, String username, String password) {
String restUrl = gsUrl.toExternalForm();
String cleanUrl = restUrl.endsWith("/") ?
restUrl.substring(0, restUrl.length()-1) :
restUrl;
this.username = username;
this.password = password;
styleManager = new GeoServerRESTStyleManager(gsUrl, username, password);
return cleanUrl;
}
private String load(String url) {
LOGGER.info("Loading from REST path " + url);
String response = HTTPUtils.get(baseurl + url, username, password);
return response;
}
private String loadFullURL(String url) {
LOGGER.info("Loading from REST path " + url);
String response = HTTPUtils.get(url, username, password);
return response;
}
/**
* Check if a GeoServer instance is running at the given URL.
* <BR>
* Return <TT>true</TT> if the configured GeoServer is up and replies to REST requests.
* <BR>
* Send a HTTP GET request to the configured URL.<BR>
* Return <TT>true</TT> if a HTTP 200 code (OK) is read from the HTTP response;
* any other response code, or connection error, will return a
* <TT>false</TT> boolean.
*
* @return true if a GeoServer instance was found at the configured URL.
*/
public boolean existGeoserver() {
return HTTPUtils.httpPing(baseurl + "/rest/", username, password);
}
/**
* Return the version of the target GeoServer
*/
public GSVersionDecoder getGeoserverVersion() {
final String url = "/rest/about/version.xml";
String xml = load(url);
if (xml == null) {
GSVersionDecoder v = new GSVersionDecoder();
v.getGeoServer().setVersion(GSVersionDecoder.VERSION.UNRECOGNIZED.toString());
return v;
} else {
return GSVersionDecoder.build(load(url));
}
}
//==========================================================================
//=== STYLES
//==========================================================================
/**
* Check if a Style exists in the configured GeoServer instance.
* @param styleName the name of the style to check for.
* @return <TT>true</TT> on HTTP 200, <TT>false</TT> on HTTP 404
* @throws RuntimeException if any other HTTP code than 200 or 404 was retrieved.
*/
public boolean existsStyle(String styleName) throws RuntimeException {
return styleManager.existsStyle(styleName);
}
/**
* Check if a Style exists in the configured GeoServer instance.
* @param styleName the name of the style to check for.
* @param quietOnNotFound if true, mute exception if false is returned
* @return <TT>true</TT> on HTTP 200, <TT>false</TT> on HTTP 404
* @throws RuntimeException if any other HTTP code than 200 or 404 was retrieved.
*/
public boolean existsStyle(String styleName, boolean quietOnNotFound) throws RuntimeException {
return styleManager.existsStyle(styleName, quietOnNotFound);
}
/**
* @see GeoServerRESTStyleManager#existsStyle(java.lang.String, java.lang.String)
* @since GeoServer 2.2
*/
public boolean existsStyle(String workspace, String styleName) throws RuntimeException {
return styleManager.existsStyle(workspace, styleName);
}
/**
* @see GeoServerRESTStyleManager#getStyle(java.lang.String)
* @since GeoServer 2.2
*/
public RESTStyle getStyle(String name) {
return styleManager.getStyle(name);
}
/**
* @see GeoServerRESTStyleManager#getStyle(java.lang.String, java.lang.String)
* @since GeoServer 2.2
*/
public RESTStyle getStyle(String workspace, String name) {
return styleManager.getStyle(workspace, name);
}
/**
* Get summary info about all Styles.
*
* @return summary info about Styles as a {@link RESTStyleList}
*/
public RESTStyleList getStyles() {
return styleManager.getStyles();
}
/**
* @see GeoServerRESTStyleManager#getStyles(java.lang.String)
* @since GeoServer 2.2
*/
public RESTStyleList getStyles(String workspace) {
return styleManager.getStyles(workspace);
}
/**
* Get the SLD body of a Style.
*/
public String getSLD(String styleName) {
return styleManager.getSLD(styleName);
}
/**
* @see GeoServerRESTStyleManager#getSLD(java.lang.String, java.lang.String)
* @since GeoServer 2.2
*/
public String getSLD(String workspace, String styleName) {
return styleManager.getSLD(workspace, styleName);
}
//==========================================================================
//=== DATASTORES
//==========================================================================
/**
* Get summary info about all DataStores in a WorkSpace.
*
* @param workspace The name of the workspace
*
* @return summary info about Datastores as a {@link RESTDataStoreList}
*/
public RESTDataStoreList getDatastores(String workspace) {
String url = "/rest/workspaces/" + workspace + "/datastores.xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving DS list from " + url);
}
return RESTDataStoreList.build(load(url));
}
/**
* Get detailed info about a given Datastore in a given Workspace.
*
* @param workspace The name of the workspace
* @param dsName The name of the Datastore
* @return DataStore details as a {@link RESTDataStore}
*/
public RESTDataStore getDatastore(String workspace, String dsName) {
String url = "/rest/workspaces/" + workspace + "/datastores/" + dsName + ".xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving DS from " + url);
}
String response = load(url);
// System.out.println("DATASTORE " + workspace+":"+dsName+"\n"+response);
return RESTDataStore.build(response);
}
/**
* Get detailed info about a FeatureType's Datastore.
*
* @param featureType the RESTFeatureType
* @return DataStore details as a {@link RESTDataStore}
*/
public RESTDataStore getDatastore(RESTFeatureType featureType) {
String url = featureType.getStoreUrl();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving DS from fullurl " + url);
}
String response = loadFullURL(url);
return RESTDataStore.build(response);
}
/**
* Checks if the selected DataStore is present
*
* @param workspace workspace of the datastore
* @param dsName name of the datastore
* @return boolean indicating if the datastore exists
*/
public boolean existsDatastore(String workspace, String dsName){
return existsDatastore(workspace, dsName, Util.DEFAULT_QUIET_ON_NOT_FOUND);
}
/**
* Checks if the selected DataStore is present. Parameter quietOnNotFound can be used for controlling the logging when 404 is returned.
*
* @param workspace workspace of the datastore
* @param dsName name of the datastore
* @param quietOnNotFound if true, no exception is logged
* @return boolean indicating if the datastore exists
*/
public boolean existsDatastore(String workspace, String dsName, boolean quietOnNotFound){
String url = baseurl + "/rest/workspaces/" + workspace + "/datastores/" + dsName + ".xml";
String composed = Util.appendQuietOnNotFound(quietOnNotFound, url);
return HTTPUtils.exists(composed, username, password);
}
//==========================================================================
//=== FEATURETYPES
//==========================================================================
/**
* Get detailed info about a FeatureType given the Layer where it's published with.
*
* @param layer A layer publishing the FeatureType
* @return FeatureType details as a {@link RESTCoverage}
*/
public RESTFeatureType getFeatureType(RESTLayer layer) {
if(layer.getType() != RESTLayer.Type.VECTOR)
throw new RuntimeException("Bad layer type for layer " + layer.getName());
String response = loadFullURL(layer.getResourceUrl());
return RESTFeatureType.build(response);
}
/**
* Checks if the selected FeatureType is present.
*
* @param workspace workspace of the datastore
* @param dsName name of the datastore
* @param ftName name of the featuretype
* @return boolean indicating if the featuretype exists
*/
public boolean existsFeatureType(String workspace, String dsName, String ftName){
return existsFeatureType(workspace, dsName, ftName, Util.DEFAULT_QUIET_ON_NOT_FOUND);
}
/**
* Checks if the selected FeatureType is present. Parameter quietOnNotFound can be used for controlling the logging when 404 is returned.
*
* @param workspace workspace of the datastore
* @param dsName name of the datastore
* @param ftName name of the featuretype
* @param quietOnNotFound if true, no exception is logged
* @return boolean indicating if the featuretype exists
*/
public boolean existsFeatureType(String workspace, String dsName, String ftName, boolean quietOnNotFound){
String url = baseurl + "/rest/workspaces/" + workspace + "/datastores/" + dsName + "/featuretypes/" + ftName +".xml";
String composed = Util.appendQuietOnNotFound(quietOnNotFound, url);
return HTTPUtils.exists(composed, username, password);
}
//==========================================================================
//=== COVERAGESTORES
//==========================================================================
/**
* Get summary info about all CoverageStores in a WorkSpace.
*
* @param workspace The name of the workspace
*
* @return summary info about CoverageStores as a {@link RESTDataStoreList}
*/
public RESTCoverageStoreList getCoverageStores(String workspace) {
String url = "/rest/workspaces/" + workspace + "/coveragestores.xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving CS list from " + url);
}
return RESTCoverageStoreList.build(load(url));
}
/**
* Get detailed info about a given CoverageStore in a given Workspace.
*
* @param workspace The name of the workspace
* @param csName The name of the CoverageStore
* @return CoverageStore details as a {@link RESTCoverageStore}
*/
public RESTCoverageStore getCoverageStore(String workspace, String csName) {
String url = "/rest/workspaces/" + workspace + "/coveragestores/" + csName + ".xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving CS from " + url);
}
return RESTCoverageStore.build(load(url));
}
/**
* Get detailed info about a Coverage's Datastore.
*
* @param coverage the RESTFeatureType
* @return CoverageStore details as a {@link RESTCoverageStore}
*/
public RESTCoverageStore getCoverageStore(RESTCoverage coverage) {
String url = coverage.getStoreUrl();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving CS from fullurl " + url);
}
String response = loadFullURL(url);
return RESTCoverageStore.build(response);
}
/**
* Checks if the selected Coverage store is present. Parameter quietOnNotFound can be used for controlling the logging when 404 is returned.
*
* @param workspace workspace of the coveragestore
* @param dsName name of the coveragestore
* @param quietOnNotFound if true, no exception is logged
* @return boolean indicating if the coveragestore exists
*/
public boolean existsCoveragestore(String workspace, String csName, boolean quietOnNotFound){
String url = baseurl + "/rest/workspaces/" + workspace + "/coveragestores/" + csName + ".xml";
String composed = Util.appendQuietOnNotFound(quietOnNotFound, url);
return HTTPUtils.exists(composed, username, password);
}
/**
* Checks if the selected Coverage store is present.
*
* @param workspace workspace of the coveragestore
* @param dsName name of the coveragestore
* @return boolean indicating if the coveragestore exists
*/
public boolean existsCoveragestore(String workspace, String csName){
return existsCoveragestore(workspace, csName, Util.DEFAULT_QUIET_ON_NOT_FOUND);
}
//==========================================================================
//=== COVERAGES
//==========================================================================
/**
* Get list of coverages (usually only one).
*
* @param workspace The name of the workspace
* @param csName The name of the CoverageStore
* @return Coverages list as a {@link RESTCoverageList}
*/
public RESTCoverageList getCoverages(String workspace, String csName) {
// restURL + "/rest/workspaces/" + workspace + "/coveragestores/" + coverageStore + "/coverages.xml";
String url = "/rest/workspaces/" + workspace + "/coveragestores/" + csName + "/coverages.xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving Covs from " + url);
}
return RESTCoverageList.build(load(url));
}
/**
* Get detailed info about a given Coverage.
*
* @param workspace The name of the workspace
* @param store The name of the CoverageStore
* @param name The name of the Coverage
* @return Coverage details as a {@link RESTCoverage}
*/
public RESTCoverage getCoverage(String workspace, String store, String name) {
String url = "/rest/workspaces/" + workspace + "/coveragestores/" + store + "/coverages/"+name+".xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving Coverage from " + url);
}
return RESTCoverage.build(load(url));
}
/**
* Checks if the selected Coverage is present. Parameter quietOnNotFound can be used for controlling the logging when 404 is returned.
*
* @param workspace workspace of the coveragestore
* @param dsName name of the coveragestore
* @param name name of the coverage
* @param quietOnNotFound if true, no exception is logged
* @return boolean indicating if the coverage exists
*/
public boolean existsCoverage(String workspace, String store, String name, boolean quietOnNotFound){
String url = baseurl + "/rest/workspaces/" + workspace + "/coveragestores/" + store + "/coverages/"+name+".xml";
String composed = Util.appendQuietOnNotFound(quietOnNotFound, url);
return HTTPUtils.exists(composed, username, password);
}
/**
* Checks if the selected Coverage is present.
*
* @param workspace workspace of the coveragestore
* @param store name of the coveragestore
* @param name name of the coverage
* @return boolean indicating if the coverage exists
*/
public boolean existsCoverage(String workspace, String store, String name){
return existsCoverage(workspace, store, name, Util.DEFAULT_QUIET_ON_NOT_FOUND);
}
/**
* Get detailed info about a Coverage given the Layer where it's published with.
*
* @param layer A layer publishing the CoverageStore
* @return Coverage details as a {@link RESTCoverage}
*/
public RESTCoverage getCoverage(RESTLayer layer) {
if(layer.getType() != RESTLayer.Type.RASTER)
throw new RuntimeException("Bad layer type for layer " + layer.getName());
String response = loadFullURL(layer.getResourceUrl());
return RESTCoverage.build(response);
}
//==========================================================================
//=== WMSSTORES
//==========================================================================
/**
* Get summary info about all WmsStore in a WorkSpace.
*
* @param workspace The name of the workspace
*
* @return summary info about CoverageStores as a {@link RESTWmsStoreList}
*/
public RESTWmsStoreList getWmsStores(String workspace) {
String url = "/rest/workspaces/" + workspace + "/wmsstores.xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving CS list from " + url);
}
return RESTWmsStoreList.build(load(url));
}
/**
* Get detailed info about a given WmsStore in a given Workspace.
*
* @param workspace The name of the workspace
* @param wsName The name of the WmsStore
* @return WmsStore details as a {@link RESTWmsStore}
*/
public RESTWmsStore getWmsStore(String workspace, String wsName) {
String url = "/rest/workspaces/" + workspace + "/wmsstores/" + wsName + ".xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving CS from " + url);
}
return RESTWmsStore.build(load(url));
}
/**
* Get detailed info about a Wms's Datastore.
*
* @param wms the RESTWms
* @return wmsStore details as a {@link RESTWmsStore}
*/
public RESTWmsStore getWmsStore(RESTWms wms) {
String url = wms.getStoreUrl();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving CS from fullurl " + url);
}
String response = loadFullURL(url);
return RESTWmsStore.build(response);
}
/**
* Checks if the selected Wms store is present. Parameter quietOnNotFound can be used for controlling the logging when 404 is returned.
*
* @param workspace workspace of the wmsstore
* @param wsName name of the wmsstore
* @param quietOnNotFound if true, no exception is logged
* @return boolean indicating if the wmsstore exists
*/
public boolean existsWmsstore(String workspace, String wsName, boolean quietOnNotFound){
String url = baseurl + "/rest/workspaces/" + workspace + "/wmsstores/" + wsName + ".xml";
String composed = Util.appendQuietOnNotFound(quietOnNotFound, url);
return HTTPUtils.exists(composed, username, password);
}
/**
* Checks if the selected wms store is present.
*
* @param workspace workspace of the wmsstore
* @param wsName name of the wmsstore
* @return boolean indicating if the wmsstore exists
*/
public boolean existsWmsstore(String workspace, String wsName){
return existsCoveragestore(workspace, wsName, Util.DEFAULT_QUIET_ON_NOT_FOUND);
}
//==========================================================================
//=== WMSS
//==========================================================================
/**
* Get list of wmss (usually only one).
*
* @param workspace The name of the workspace
* @param wsName The name of the WmsStore
* @return wms list as a {@link RESTWmsList}
*/
public RESTWmsList getWms(String workspace, String wsName) {
String url = "/rest/workspaces/" + workspace + "/wmsstores/" + wsName + "/wmslayers.xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving Wmss from " + url);
}
return RESTWmsList.build(load(url));
}
/**
* Get detailed info about a given Wms.
*
* @param workspace The name of the workspace
* @param store The name of the WmsStore
* @param name The name of the Wms
* @return wms details as a {@link RESTwms}
*/
public RESTWms getWms(String workspace, String store, String name) {
String url = "/rest/workspaces/" + workspace + "/wmsstores/" + store + "/wmslayers/"+name+".xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving Wmss from " + url);
}
return RESTWms.build(load(url));
}
/**
* Checks if the selected Wms is present. Parameter quietOnNotFound can be used for controlling the logging when 404 is returned.
*
* @param workspace workspace of the wmsstore
* @param wsName name of the wmsstore
* @param name name of the wms
* @param quietOnNotFound if true, no exception is logged
* @return boolean indicating if the coverage exists
*/
public boolean existsWms(String workspace, String store, String name, boolean quietOnNotFound){
String url = baseurl + "/rest/workspaces/" + workspace + "/wmsstores/" + store + "/wmslayers/"+name+".xml";
String composed = Util.appendQuietOnNotFound(quietOnNotFound, url);
return HTTPUtils.exists(composed, username, password);
}
/**
* Checks if the selected wms is present.
*
* @param workspace workspace of the wmsstore
* @param store name of the wmsstore
* @param name name of the wms
* @return boolean indicating if the coverage exists
*/
public boolean existsWms(String workspace, String store, String name){
return existsWms(workspace, store, name, Util.DEFAULT_QUIET_ON_NOT_FOUND);
}
/**
* Get detailed info about a Wms given the Layer where it's published with.
*
* @param layer A layer publishing the wmsStore
* @return Wms details as a {@link RESTWms}
*/
public RESTWms getWms(RESTLayer layer) {
String response = loadFullURL(layer.getResourceUrl());
return RESTWms.build(response);
}
//==========================================================================
//==========================================================================
/**
* Get detailed info about a Resource given the Layer where it's published with.
* The Resource can then be converted to RESTCoverage or RESTFeatureType
*
* @return Resource details as a {@link RESTResource}
*/
public RESTResource getResource(RESTLayer layer) {
String response = loadFullURL(layer.getResourceUrl());
return RESTResource.build(response);
}
//==========================================================================
//=== LAYERGROUPS
//==========================================================================
/**
* Get summary info about all LayerGroups in the given workspace.
*
* @param workspace name of the workspace
* @return summary info about LayerGroups as a {@link RESTLayerGroupList}
*/
public RESTLayerGroupList getLayerGroups(String workspace) {
String url;
if (workspace == null) {
url = "/rest/layergroups.xml";
} else {
url = "/rest/workspaces/" + workspace + "/layergroups.xml";
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving layergroups from " + url);
}
return RESTLayerGroupList.build(load(url));
}
/**
* Get detailed info about a given LayerGroup.
*
* @param workspace name of the workspace
* @param name the name of the LayerGroup
* @return LayerGroup details as a {@link RESTLayerGroup}
*/
public RESTLayerGroup getLayerGroup(String workspace, String name) {
String url;
if (workspace == null) {
url = "/rest/layergroups/" + name + ".xml";
} else {
url = "/rest/workspaces/" + workspace + "/layergroups/" + name + ".xml";
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving layergroup from " + url);
}
return RESTLayerGroup.build(load(url));
}
/**
* Get summary info about all LayerGroups.
*
* @return summary info about LayerGroups as a {@link RESTLayerGroupList}
*/
public RESTLayerGroupList getLayerGroups() {
return getLayerGroups(null);
}
/**
* Get detailed info about a given LayerGroup.
*
* @param name The name of the LayerGroup
* @return LayerGroup details as a {@link RESTLayerGroup}
*/
public RESTLayerGroup getLayerGroup(String name) {
return getLayerGroup(null, name);
}
/**
* Checks if the selected LayerGroup is present. Parameter quietOnNotFound can be used for controlling the logging when 404 is returned.
*
* @param workspace workspace of the LayerGroup
* @param name name of the LayerGroup
* @param quietOnNotFound if true, no exception is logged
* @return boolean indicating if the LayerGroup exists
*/
public boolean existsLayerGroup(String workspace, String name, boolean quietOnNotFound){
String url;
if (workspace == null) {
url = baseurl + "/rest/layergroups/" + name + ".xml";
} else {
url = baseurl + "/rest/workspaces/" + workspace + "/layergroups/" + name + ".xml";
}
String composed = Util.appendQuietOnNotFound(quietOnNotFound, url);
return HTTPUtils.exists(composed, username, password);
}
/**
* Checks if the selected LayerGroup is present.
*
* @param workspace workspace of the LayerGroup
* @param name name of the LayerGroup
* @return boolean indicating if the LayerGroup exists
*/
public boolean existsLayerGroup(String workspace, String name){
return existsLayerGroup(workspace, name, Util.DEFAULT_QUIET_ON_NOT_FOUND);
}
//==========================================================================
//=== LAYERS
//==========================================================================
/**
* Get summary info about all Layers.
*
* @return summary info about Layers as a {@link RESTLayerList}
*/
public RESTLayerList getLayers() {
String url = "/rest/layers.xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving layers from " + url);
}
return RESTLayerList.build(load(url));
}
/**
* Get summary info about layers from a given workspace.
*
* @return summary info about Layers as a {@link RESTLayerList}
*/
public RESTLayerList getLayers(String workspace) {
String url = "/rest/workspaces/" + workspace + "/layers.xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving layers from " + url);
}
return RESTLayerList.build(load(url));
}
/**
* Get summary info about all FeatureTypes of a workspace.
*
* @return summary info about Layers as a {@link RESTLayerList}
*/
public RESTFeatureTypeList getFeatureTypes(String workspace) {
String url = "/rest/workspaces/" + workspace + "/featuretypes.xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving featuretypes from " + url);
}
return RESTFeatureTypeList.build(load(url));
}
/**
* Get detailed info about a given Layer.
*
* @deprecated use {@link #getLayer(String, String)}
*
* @param name The name of the Layer
* @return Layer details as a {@link RESTLayer}
*/
public RESTLayer getLayer(String name) {
String url = "/rest/layers/" + name + ".xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving layer from " + url);
}
return RESTLayer.build(load(url));
}
/**
* Get detailed info about a given Layer.
*
* @param workspace the workspace name
* @param name the layer name
* @return a RESTLayer with layer information or null
*/
public RESTLayer getLayer(String workspace, String name) {
if (workspace == null || workspace.isEmpty())
throw new IllegalArgumentException("Workspace may not be null");
if (name == null || name.isEmpty())
throw new IllegalArgumentException("Layername may not be null");
String url = HTTPUtils.append("/rest/layers/",workspace,":",name,".xml").toString();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving layer from " + url);
}
RESTLayer layer = null;
if (this.getGeoserverVersion().getVersion()
.equals(GSVersionDecoder.VERSION.UNRECOGNIZED)) {
layer = RESTLayer21.build(load(url));
} else {
layer = RESTLayer.build(load(url));
}
return layer;
}
/**
* Checks if the selected Layer is present. Parameter quietOnNotFound can be used for controlling the logging when 404 is returned.
*
* @param workspace workspace of the Layer
* @param name name of the Layer
* @param quietOnNotFound if true, no exception is logged
* @return boolean indicating if the Layer exists
*/
public boolean existsLayer(String workspace, String name, boolean quietOnNotFound){
String url;
if (workspace == null) {
url = baseurl + "/rest/layers/" + name + ".xml";
} else {
url = baseurl + "/rest/layers/" + workspace + ":" + name + ".xml";
}
String composed = Util.appendQuietOnNotFound(quietOnNotFound, url);
return HTTPUtils.exists(composed, username, password);
}
/**
* Checks if the selected Layer is present.
*
* @param workspace workspace of the Layer
* @param name name of the Layer
* @return boolean indicating if the Layer exists
*/
public boolean existsLayer(String workspace, String name){
return existsLayerGroup(workspace, name, Util.DEFAULT_QUIET_ON_NOT_FOUND);
}
//==========================================================================
//=== NAMESPACES
//==========================================================================
/**
* Get a namespace.
*
* @param prefix namespace prefix.
*
* @return a RESTNamespace, or null if couldn't be created.
*/
public RESTNamespace getNamespace(String prefix) {
if (prefix == null || prefix.isEmpty()) {
throw new IllegalArgumentException(
"Namespace prefix cannot be null or empty");
}
String url = "/rest/namespaces/"+prefix+".xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Getting namespace from " + url);
}
return RESTNamespace.build(load(url));
}
/**
* Get summary info about all Namespaces.
*
* @return summary info about Namespaces as a {@link RESTNamespaceList}
*/
public RESTNamespaceList getNamespaces() {
String url = "/rest/namespaces.xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving namespaces from " + url);
}
return RESTNamespaceList.build(load(url));
}
/**
* Get the names of all the Namespaces.
* <BR>
* This is a shortcut call: These info could be retrieved using {@link #getNamespaces getNamespaces}
* @return the list of the names of all Namespaces.
*/
public List<String> getNamespaceNames() {
RESTNamespaceList list = getNamespaces();
List<String> names = new ArrayList<String>(list.size());
for (RESTNamespaceList.RESTShortNamespace item : list) {
names.add(item.getName());
}
return names;
}
/**
* Checks if the selected Namespace is present. Parameter quietOnNotFound can be used for controlling the logging when 404 is returned.
*
* @param prefix namespace prefix.
* @param quietOnNotFound if true, no exception is logged
* @return boolean indicating if the Namespace exists
*/
public boolean existsNamespace(String prefix, boolean quietOnNotFound) {
if (prefix == null || prefix.isEmpty()) {
throw new IllegalArgumentException("Namespace prefix cannot be null or empty");
}
String url = baseurl + "/rest/namespaces/" + prefix + ".xml";
String composed = Util.appendQuietOnNotFound(quietOnNotFound, url);
return HTTPUtils.exists(composed, username, password);
}
/**
* Checks if the selected Namespace is present.
*
* @param prefix namespace prefix.
* @return boolean indicating if the Namespace exists
*/
public boolean existsNamespace(String prefix){
return existsNamespace(prefix, Util.DEFAULT_QUIET_ON_NOT_FOUND);
}
//==========================================================================