-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
GeoServerTileLayer.java
1364 lines (1201 loc) · 49 KB
/
GeoServerTileLayer.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
/* (c) 2014 - 2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.gwc.layer;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Throwables.propagate;
import static org.geoserver.gwc.GWC.tileLayerName;
import static org.geoserver.ows.util.ResponseUtils.buildURL;
import static org.geoserver.ows.util.ResponseUtils.params;
import java.awt.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import com.google.common.collect.Iterables;
import org.geoserver.catalog.Catalog;
import org.geoserver.catalog.KeywordInfo;
import org.geoserver.catalog.LayerGroupInfo;
import org.geoserver.catalog.LayerInfo;
import org.geoserver.catalog.MetadataLinkInfo;
import org.geoserver.catalog.MetadataMap;
import org.geoserver.catalog.PublishedInfo;
import org.geoserver.catalog.ResourceInfo;
import org.geoserver.catalog.StyleInfo;
import org.geoserver.catalog.WorkspaceInfo;
import org.geoserver.gwc.GWC;
import org.geoserver.gwc.config.GWCConfig;
import org.geoserver.gwc.dispatch.GwcServiceDispatcherCallback;
import org.geoserver.ows.LocalWorkspace;
import org.geoserver.ows.Dispatcher;
import org.geoserver.ows.URLMangler;
import org.geoserver.ows.util.RequestUtils;
import org.geoserver.platform.GeoServerExtensions;
import org.geoserver.wms.GetLegendGraphicRequest;
import org.geoserver.wms.GetMapRequest;
import org.geoserver.wms.WMS;
import org.geoserver.wms.WebMap;
import org.geoserver.wms.capabilities.LegendSample;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
import org.geotools.util.logging.Logging;
import org.geowebcache.GeoWebCacheException;
import org.geowebcache.config.ConfigurationException;
import org.geowebcache.config.XMLGridSubset;
import org.geowebcache.config.legends.LegendInfo;
import org.geowebcache.config.legends.LegendInfoBuilder;
import org.geowebcache.conveyor.ConveyorTile;
import org.geowebcache.filter.parameters.ParameterException;
import org.geowebcache.filter.parameters.ParameterFilter;
import org.geowebcache.filter.request.RequestFilter;
import org.geowebcache.grid.BoundingBox;
import org.geowebcache.grid.GridSet;
import org.geowebcache.grid.GridSetBroker;
import org.geowebcache.grid.GridSubset;
import org.geowebcache.grid.OutsideCoverageException;
import org.geowebcache.grid.SRS;
import org.geowebcache.io.Resource;
import org.geowebcache.layer.ExpirationRule;
import org.geowebcache.layer.LayerListenerList;
import org.geowebcache.layer.MetaTile;
import org.geowebcache.layer.ProxyLayer;
import org.geowebcache.layer.TileLayer;
import org.geowebcache.layer.TileLayerListener;
import org.geowebcache.layer.meta.ContactInformation;
import org.geowebcache.layer.meta.LayerMetaInformation;
import org.geowebcache.layer.meta.MetadataURL;
import org.geowebcache.layer.updatesource.UpdateSourceDefinition;
import org.geowebcache.locks.LockProvider.Lock;
import org.geowebcache.mime.FormatModifier;
import org.geowebcache.mime.MimeException;
import org.geowebcache.mime.MimeType;
import org.geowebcache.util.GWCVars;
import org.geowebcache.util.ServletUtils;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import com.google.common.base.Throwables;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import org.vfny.geoserver.util.ResponseUtils;
public class GeoServerTileLayer extends TileLayer implements ProxyLayer {
private static final Logger LOGGER = Logging.getLogger(GeoServerTileLayer.class);
private final GeoServerTileLayerInfo info;
public static final String GWC_SEED_INTERCEPT_TOKEN = "GWC_SEED_INTERCEPT";
public static final ThreadLocal<WebMap> WEB_MAP = new ThreadLocal<WebMap>();
private String configErrorMessage;
private Map<String, GridSubset> subSets;
private static LayerListenerList listeners = new LayerListenerList();
private final GridSetBroker gridSetBroker;
private Catalog catalog;
private String publishedId;
volatile private PublishedInfo publishedInfo;
private LegendSample legendSample;
private WMS wms;
public GeoServerTileLayer(final PublishedInfo publishedInfo, final GWCConfig configDefaults, final GridSetBroker gridsets) {
checkNotNull(publishedInfo, "publishedInfo");
checkNotNull(gridsets, "gridsets");
checkNotNull(configDefaults, "configDefaults");
this.gridSetBroker = gridsets;
this.publishedInfo = publishedInfo;
this.info = TileLayerInfoUtil.loadOrCreate(getPublishedInfo(), configDefaults);
}
public GeoServerTileLayer(final PublishedInfo publishedInfo, final GridSetBroker gridsets,
final GeoServerTileLayerInfo state) {
checkNotNull(publishedInfo, "publishedInfo");
checkNotNull(gridsets, "gridsets");
checkNotNull(state, "state");
this.gridSetBroker = gridsets;
this.publishedInfo = publishedInfo;
this.info = state;
TileLayerInfoUtil.checkAutomaticStyles(publishedInfo, state);
}
public GeoServerTileLayer(final Catalog catalog, final String publishedId,
final GWCConfig configDefaults, final GridSetBroker gridsets) {
checkNotNull(catalog, "catalog");
checkNotNull(publishedId, "publishedId");
checkNotNull(gridsets, "gridsets");
checkNotNull(configDefaults, "configDefaults");
this.gridSetBroker = gridsets;
this.catalog = catalog;
this.publishedId = publishedId;
this.info = TileLayerInfoUtil.loadOrCreate(getPublishedInfo(), configDefaults);
}
public GeoServerTileLayer(final Catalog catalog, final String publishedId,
final GridSetBroker gridsets, final GeoServerTileLayerInfo state) {
checkNotNull(catalog, "catalog");
checkNotNull(publishedId, "publishedId");
checkNotNull(gridsets, "gridsets");
checkNotNull(state, "state");
this.gridSetBroker = gridsets;
this.catalog = catalog;
this.publishedId = publishedId;
this.info = state;
}
@Override
public String getId() {
return info.getId();
}
@Override
public String getBlobStoreId(){
return info.getBlobStoreId();
}
@Override
public String getName() {
// getting the current gwc operation
String gwcOperation = GwcServiceDispatcherCallback.GWC_OPERATION.get();
// checking if we are in the context of a get capabilities request
if (gwcOperation != null && gwcOperation.equalsIgnoreCase("GetCapabilities")) {
// this is a get capabilities request, we need to check if we are in the context of virtual service
return getNoPrefixedNameIfVirtualService();
}
return info.getName();
}
private String getNoPrefixedNameIfVirtualService() {
// let's see if this a virtual service request
WorkspaceInfo localWorkspace = LocalWorkspace.get();
if (localWorkspace != null) {
// yes this is a virtual service request so removing the workspace prefix
return CatalogConfiguration.removeWorkspacePrefix(info.getName(), catalog);
}
// this a normal request so just returning the prefixed layer name
return info.getName();
}
void setConfigErrorMessage(String configErrorMessage) {
this.configErrorMessage = configErrorMessage;
}
public String getConfigErrorMessage() {
return configErrorMessage;
}
@Override
public List<ParameterFilter> getParameterFilters() {
return new ArrayList<ParameterFilter>(info.getParameterFilters());
}
public void resetParameterFilters() {
super.defaultParameterFilterValues = null;// reset default values
}
/**
* Returns whether this tile layer is enabled.
* <p>
* The layer is enabled if the following conditions apply:
* <ul>
* <li>Caching for this layer is enabled by configuration
* <li>Its backing {@link LayerInfo} or {@link LayerGroupInfo} is enabled and not errored (as
* per {@link LayerInfo#enabled()} {@link LayerGroupInfo}
* <li>The layer is not errored ({@link #getConfigErrorMessage() == null}
* </ul>
* The layer is enabled by configuration if: the {@code GWC.enabled} metadata property is set to
* {@code true} in it's corresponding {@link LayerInfo} or {@link LayerGroupInfo}
* {@link MetadataMap}, or there's no {@code GWC.enabled} property set at all but the global
* {@link GWCConfig#isCacheLayersByDefault()} is {@code true}.
* </p>
*
* @see org.geowebcache.layer.TileLayer#isEnabled()
*/
@Override
public boolean isEnabled() {
final boolean tileLayerInfoEnabled = info.isEnabled();
if (!tileLayerInfoEnabled) {
return false;
}
if (getConfigErrorMessage() != null) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("Layer " + getName() + "is not enabled due to config error: "
+ getConfigErrorMessage());
}
return false;
}
boolean geoserverLayerEnabled;
LayerInfo layerInfo = getLayerInfo();
if (layerInfo != null) {
geoserverLayerEnabled = layerInfo.enabled();
} else {
// LayerGroupInfo has no enabled property, so assume true
geoserverLayerEnabled = true;
}
return tileLayerInfoEnabled && geoserverLayerEnabled;
}
@Override
public void setEnabled(final boolean enabled) {
info.setEnabled(enabled);
}
/**
*
* @see org.geowebcache.layer.TileLayer#isQueryable()
* @see WMS#isQueryable(LayerGroupInfo)
* @see WMS#isQueryable(LayerInfo)
*/
@Override
public boolean isQueryable() {
boolean queryable = GWC.get().isQueryable(this);
return queryable;
}
private ReferencedEnvelope getLatLonBbox() throws IllegalStateException {
final CoordinateReferenceSystem wgs84LonFirst;
try {
final boolean longitudeFirst = true;
wgs84LonFirst = CRS.decode("EPSG:4326", longitudeFirst);
} catch (Exception e) {
throw new RuntimeException(e);
}
ReferencedEnvelope latLongBbox;
if (getLayerInfo() == null) {
LayerGroupInfo groupInfo = getLayerGroupInfo();
try {
ReferencedEnvelope bounds = groupInfo.getBounds();
boolean lenient = true;
latLongBbox = bounds.transform(wgs84LonFirst, lenient);
} catch (Exception e) {
String msg = "Can't get lat long bounds for layer group "
+ tileLayerName(groupInfo);
LOGGER.log(Level.WARNING, msg, e);
throw new IllegalStateException(msg, e);
}
} else {
ResourceInfo resourceInfo = getResourceInfo();
latLongBbox = resourceInfo.getLatLonBoundingBox();
if (null == latLongBbox) {
latLongBbox = new ReferencedEnvelope(wgs84LonFirst);
}
if (null == latLongBbox.getCoordinateReferenceSystem()) {
ReferencedEnvelope tmp = new ReferencedEnvelope(wgs84LonFirst);
tmp.init(latLongBbox.getMinX(), latLongBbox.getMaxX(), latLongBbox.getMinY(),
latLongBbox.getMaxY());
latLongBbox = tmp;
}
}
return latLongBbox;
}
/**
* @return the {@link LayerInfo} for this layer, or {@code null} if it's backed by a
* {@link LayerGroupInfo} instead
*
* @deprecated use getPublishedInfo instead
*/
@Deprecated
public LayerInfo getLayerInfo() {
PublishedInfo info = getPublishedInfo();
if (info instanceof LayerInfo) {
return (LayerInfo) info;
}
return null;
}
public PublishedInfo getPublishedInfo() {
if (publishedInfo == null) {
synchronized (this) {
if(publishedInfo == null) {
// see if it's a layer or a layer group
PublishedInfo work = catalog.getLayer(publishedId);
if (work == null) {
work = catalog.getLayerGroup(publishedId);
}
if (work != null) {
TileLayerInfoUtil.checkAutomaticStyles(work, info);
} else {
throw new IllegalStateException(
"Could not locate a layer or layer group with id "
+ publishedId
+ " within GeoServer configuration, the GWC configuration seems to be out of synch");
}
this.publishedInfo = work;
}
}
}
return publishedInfo;
}
/**
* @return the {@link LayerGroupInfo} for this layer, or {@code null} if it's backed by a
* {@link LayerInfo} instead
*
* @deprecated use getPublishedInfo instead
*/
@Deprecated
public LayerGroupInfo getLayerGroupInfo() {
PublishedInfo info = getPublishedInfo();
if (info instanceof LayerGroupInfo) {
return (LayerGroupInfo) info;
}
return null;
}
private ResourceInfo getResourceInfo() {
LayerInfo layerInfo = getLayerInfo();
return layerInfo == null ? null : layerInfo.getResource();
}
/**
* Overrides to return a dynamic view of the backing {@link LayerInfo} or {@link LayerGroupInfo}
* metadata adapted to GWC
*
* @see org.geowebcache.layer.TileLayer#getMetaInformation()
*/
@Override
public LayerMetaInformation getMetaInformation() {
LayerMetaInformation meta = null;
String title = getName();
String description = "";
List<String> keywords = Collections.emptyList();
List<ContactInformation> contacts = Collections.emptyList();
ResourceInfo resourceInfo = getResourceInfo();
if (resourceInfo != null) {
title = resourceInfo.getTitle();
description = resourceInfo.getAbstract();
keywords = new ArrayList<>();
for (KeywordInfo kw : resourceInfo.getKeywords()) {
keywords.add(kw.getValue());
}
} else {
LayerGroupInfo lg = getLayerGroupInfo();
if(lg != null) {
if (lg != null) {
if(lg.getTitle() != null) {
title = lg.getTitle();
}
if(lg.getAbstract() != null) {
description = lg.getAbstract();
}
}
}
}
meta = new LayerMetaInformation(title, description, keywords, contacts);
return meta;
}
/**
* The default style name for the layer, as advertised by its backing
* {@link LayerInfo#getDefaultStyle()}, or {@code null} if this tile layer is backed by a
* {@link LayerGroupInfo}.
* <p>
* As the default style is always cached, its name is not stored as part of this tile layer's
* {@link GeoServerTileLayerInfo}. Instead it's 'live' and retrieved from the current
* {@link LayerInfo} every time this method is invoked.
* </p>
*
* @see org.geowebcache.layer.TileLayer#getStyles()
* @see GeoServerTileLayerInfo#getDefaultStyle()
*/
@Override
public String getStyles() {
LayerGroupInfo layerGroupInfo = getLayerGroupInfo();
if (layerGroupInfo != null) {
// there's no such thing as default style for a layer group
return null;
}
LayerInfo layerInfo = getLayerInfo();
StyleInfo defaultStyle = layerInfo.getDefaultStyle();
if (defaultStyle == null) {
setConfigErrorMessage("Underlying GeoSever Layer has no default style");
return null;
}
return defaultStyle.prefixedName();
}
/**
* @see org.geowebcache.layer.TileLayer#getFeatureInfo
* @see GWC#dispatchOwsRequest
*/
@Override
public Resource getFeatureInfo(ConveyorTile convTile, BoundingBox bbox, int height, int width,
int x, int y) throws GeoWebCacheException {
Map<String, String> params = buildGetFeatureInfo(convTile, bbox, height, width, x, y);
Resource response;
try {
response = GWC.get().dispatchOwsRequest(params, (Cookie[]) null);
} catch (Exception e) {
throw new GeoWebCacheException(e);
}
return response;
}
private Map<String, String> buildGetFeatureInfo(ConveyorTile convTile, BoundingBox bbox,
int height, int width, int x, int y) {
Map<String, String> wmsParams = new HashMap<String, String>();
wmsParams.put("SERVICE", "WMS");
wmsParams.put("VERSION", "1.1.1");
wmsParams.put("REQUEST", "GetFeatureInfo");
wmsParams.put("LAYERS", getName());
wmsParams.put("STYLES", "");
wmsParams.put("QUERY_LAYERS", getName());
MimeType mimeType = convTile.getMimeType();
if (mimeType == null) {
mimeType = getMimeTypes().get(0);
}
wmsParams.put("FORMAT", mimeType.getFormat());
wmsParams.put("EXCEPTIONS", GetMapRequest.SE_XML);
wmsParams.put("INFO_FORMAT", convTile.getMimeType().getFormat());
GridSubset gridSubset = convTile.getGridSubset();
wmsParams.put("SRS", gridSubset.getSRS().toString());
wmsParams.put("HEIGHT", String.valueOf(height));
wmsParams.put("WIDTH", String.valueOf(width));
wmsParams.put("BBOX", bbox.toString());
wmsParams.put("X", String.valueOf(x));
wmsParams.put("Y", String.valueOf(y));
String featureCount;
{
Map<String, String> values = ServletUtils.selectedStringsFromMap(
convTile.servletReq.getParameterMap(),
convTile.servletReq.getCharacterEncoding(), "feature_count");
featureCount = values.get("feature_count");
}
if (featureCount != null) {
wmsParams.put("FEATURE_COUNT", featureCount);
}
Map<String, String> fullParameters = convTile.getFullParameters();
if (fullParameters.isEmpty()) {
fullParameters = getDefaultParameterFilters();
}
wmsParams.putAll(fullParameters);
return wmsParams;
}
@Override
public ConveyorTile getTile(ConveyorTile tile) throws GeoWebCacheException, IOException,
OutsideCoverageException {
MimeType mime = tile.getMimeType();
final List<MimeType> formats = getMimeTypes();
if (mime == null) {
mime = formats.get(0);
} else {
if (!formats.contains(mime)) {
throw new IllegalArgumentException(mime.getFormat()
+ " is not a supported format for " + getName());
}
}
final String tileGridSetId = tile.getGridSetId();
final GridSubset gridSubset = getGridSubset(tileGridSetId);
if (gridSubset == null) {
throw new IllegalArgumentException("Requested gridset not found: " + tileGridSetId);
}
final long[] gridLoc = tile.getTileIndex();
checkNotNull(gridLoc);
// Final preflight check, throws OutsideCoverageException if necessary
gridSubset.checkCoverage(gridLoc);
ConveyorTile returnTile;
int metaX;
int metaY;
if (mime.supportsTiling()) {
metaX = info.getMetaTilingX();
metaY = info.getMetaTilingY();
} else {
metaX = metaY = 1;
}
returnTile = getMetatilingReponse(tile, true, metaX, metaY);
sendTileRequestedEvent(returnTile);
return returnTile;
}
@Override
public void addLayerListener(final TileLayerListener listener) {
listeners.addListener(listener);
}
@Override
public boolean removeLayerListener(final TileLayerListener listener) {
listeners.removeListener(listener);
return true;
}
protected final void sendTileRequestedEvent(ConveyorTile tile) {
if (listeners != null) {
listeners.sendTileRequested(this, tile);
}
}
private ConveyorTile getMetatilingReponse(ConveyorTile tile, final boolean tryCache,
final int metaX, final int metaY) throws GeoWebCacheException, IOException {
final GridSubset gridSubset = getGridSubset(tile.getGridSetId());
final int zLevel = (int) tile.getTileIndex()[2];
tile.setMetaTileCacheOnly(!gridSubset.shouldCacheAtZoom(zLevel));
if (tryCache && tryCacheFetch(tile)) {
return finalizeTile(tile);
}
final GeoServerMetaTile metaTile = createMetaTile(tile, metaX, metaY);
Lock lock = null;
try {
/** ****************** Acquire lock ******************* */
lock = GWC.get().getLockProvider().getLock(buildLockKey(tile, metaTile));
// got the lock on the meta tile, try again
if (tryCache && tryCacheFetch(tile)) {
LOGGER.finest("--> " + Thread.currentThread().getName() + " returns cache hit for "
+ Arrays.toString(metaTile.getMetaGridPos()));
} else {
LOGGER.finer("--> " + Thread.currentThread().getName()
+ " submitting getMap request for meta grid location "
+ Arrays.toString(metaTile.getMetaGridPos()) + " on " + metaTile);
WebMap map;
try {
long requestTime = System.currentTimeMillis();
map = dispatchGetMap(tile, metaTile);
checkNotNull(map, "Did not obtain a WebMap from GeoServer's Dispatcher");
metaTile.setWebMap(map);
saveTiles(metaTile, tile, requestTime);
} catch (Exception e) {
Throwables.propagateIfInstanceOf(e, GeoWebCacheException.class);
throw new GeoWebCacheException("Problem communicating with GeoServer", e);
}
}
/** ****************** Return lock and response ****** */
} finally {
if(lock != null) {
lock.release();
}
metaTile.dispose();
}
return finalizeTile(tile);
}
private String buildLockKey(ConveyorTile tile, GeoServerMetaTile metaTile) {
StringBuilder metaKey = new StringBuilder();
final long[] tileIndex;
if(metaTile != null) {
tileIndex = metaTile.getMetaGridPos();
metaKey.append("gsmeta_");
} else {
tileIndex = tile.getTileIndex();
metaKey.append("tile_");
}
long x = tileIndex[0];
long y = tileIndex[1];
long z = tileIndex[2];
metaKey.append(tile.getLayerId());
metaKey.append("_").append(tile.getGridSetId());
metaKey.append("_").append(x).append("_").append(y).append("_").append(z);
if(tile.getParametersId() != null) {
metaKey.append("_").append(tile.getParametersId());
}
metaKey.append(".").append(tile.getMimeType().getFileExtension());
return metaKey.toString();
}
private WebMap dispatchGetMap(final ConveyorTile tile, final MetaTile metaTile)
throws Exception {
Map<String, String> params = buildGetMap(tile, metaTile);
WebMap map;
try {
HttpServletRequest actualRequest = tile.servletReq;
Cookie[] cookies = actualRequest == null ? null : actualRequest.getCookies();
GWC.get().dispatchOwsRequest(params, cookies);
map = WEB_MAP.get();
if (!(map instanceof WebMap)) {
throw new IllegalStateException("Expected: RenderedImageMap, got " + map);
}
} finally {
WEB_MAP.remove();
}
return map;
}
private GeoServerMetaTile createMetaTile(ConveyorTile tile, final int metaX, final int metaY) {
GeoServerMetaTile metaTile;
String tileGridSetId = tile.getGridSetId();
GridSubset gridSubset = getGridSubset(tileGridSetId);
MimeType responseFormat = tile.getMimeType();
FormatModifier formatModifier = null;
long[] tileGridPosition = tile.getTileIndex();
int gutter = responseFormat.isVector() ? 0 : info.getGutter();
metaTile = new GeoServerMetaTile(gridSubset, responseFormat, formatModifier,
tileGridPosition, metaX, metaY, gutter);
return metaTile;
}
private Map<String, String> buildGetMap(final ConveyorTile tile, final MetaTile metaTile)
throws ParameterException {
Map<String, String> params = new HashMap<String, String>();
final MimeType mimeType = tile.getMimeType();
final String gridSetId = tile.getGridSetId();
final GridSubset gridSubset = getGridSubset(gridSetId);
int width = metaTile.getMetaTileWidth();
int height = metaTile.getMetaTileHeight();
String srs = gridSubset.getSRS().toString();
String format = mimeType.getFormat();
BoundingBox bbox = metaTile.getMetaTileBounds();
params.put("SERVICE", "WMS");
params.put("VERSION", "1.1.1");
params.put("REQUEST", "GetMap");
params.put("LAYERS", getName());
params.put("SRS", srs);
params.put("FORMAT", format);
params.put("WIDTH", String.valueOf(width));
params.put("HEIGHT", String.valueOf(height));
params.put("BBOX", bbox.toString());
params.put("EXCEPTIONS", GetMapRequest.SE_XML);
params.put("STYLES", "");
params.put("TRANSPARENT", "true");
params.put(GWC_SEED_INTERCEPT_TOKEN, "true");
Map<String, String> filteredParams = tile.getFullParameters();
if (filteredParams.isEmpty()) {
filteredParams = getDefaultParameterFilters();
}
params.putAll(filteredParams);
return params;
}
private boolean tryCacheFetch(ConveyorTile tile) {
int expireCache = this.getExpireCache((int) tile.getTileIndex()[2]);
if (expireCache != GWCVars.CACHE_DISABLE_CACHE) {
try {
return tile.retrieve(expireCache * 1000L);
} catch (GeoWebCacheException gwce) {
LOGGER.info(gwce.getMessage());
tile.setErrorMsg(gwce.getMessage());
return false;
}
}
return false;
}
private ConveyorTile finalizeTile(ConveyorTile tile) {
if (tile.getStatus() == 0 && !tile.getError()) {
tile.setStatus(200);
}
if (tile.servletResp != null) {
setExpirationHeader(tile.servletResp, (int) tile.getTileIndex()[2]);
setTileIndexHeader(tile);
}
tile.setTileLayer(this);
return tile;
}
/**
* @param tile
*/
private void setTileIndexHeader(ConveyorTile tile) {
tile.servletResp.addHeader("geowebcache-tile-index", Arrays.toString(tile.getTileIndex()));
}
@Override
public ConveyorTile getNoncachedTile(ConveyorTile tile) throws GeoWebCacheException {
try {
return getMetatilingReponse(tile, false, 1, 1);
} catch (IOException e) {
throw new GeoWebCacheException(e);
}
}
@Override
public ConveyorTile doNonMetatilingRequest(ConveyorTile tile) throws GeoWebCacheException {
try {
return getMetatilingReponse(tile, true, 1, 1);
} catch (IOException e) {
throw new GeoWebCacheException(e);
}
}
@Override
public void seedTile(ConveyorTile tile, boolean tryCache) throws GeoWebCacheException,
IOException {
// Ignore a seed call on a tile that's outside the cached grid levels range
final GridSubset gridSubset = getGridSubset(tile.getGridSetId());
final int zLevel = (int) tile.getTileIndex()[2];
if (!gridSubset.shouldCacheAtZoom(zLevel)) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("Ignoring seed call on tile " + tile
+ " as it's outside the cacheable zoom level range");
}
return;
}
int metaX = info.getMetaTilingX();
int metaY = info.getMetaTilingY();
if (!tile.getMimeType().supportsTiling()) {
metaX = metaY = 1;
}
getMetatilingReponse(tile, tryCache, metaX, metaY);
}
/**
* @see org.geowebcache.layer.TileLayer#getGridSubsets()
*/
@Override
public synchronized Set<String> getGridSubsets() {
checkGridSubsets();
return new HashSet<String>(subSets.keySet());
}
@Override
public GridSubset getGridSubset(final String gridSetId) {
checkGridSubsets();
return subSets.get(gridSetId);
}
private synchronized void checkGridSubsets() {
if (this.subSets == null) {
ReferencedEnvelope latLongBbox = getLatLonBbox();
try {
this.subSets = getGrids(latLongBbox, gridSetBroker);
} catch (ConfigurationException e) {
String msg = "Can't create grids for '" + getName() + "': " + e.getMessage();
LOGGER.log(Level.WARNING, msg, e);
setConfigErrorMessage(msg);
}
}
}
@Override
public synchronized GridSubset removeGridSubset(String gridSetId) {
checkGridSubsets();
final GridSubset oldValue = this.subSets.remove(gridSetId);
Set<XMLGridSubset> gridSubsets = new HashSet<XMLGridSubset>(info.getGridSubsets());
for (Iterator<XMLGridSubset> it = gridSubsets.iterator(); it.hasNext();) {
if (it.next().getGridSetName().equals(gridSetId)) {
it.remove();
break;
}
}
info.setGridSubsets(gridSubsets);
return oldValue;
}
@Override
public void addGridSubset(GridSubset gridSubset) {
XMLGridSubset gridSubsetInfo = new XMLGridSubset(gridSubset);
Set<XMLGridSubset> gridSubsets = new HashSet<XMLGridSubset>(info.getGridSubsets());
gridSubsets.add(gridSubsetInfo);
info.setGridSubsets(gridSubsets);
this.subSets = null;
}
private Map<String, GridSubset> getGrids(final ReferencedEnvelope latLonBbox,
final GridSetBroker gridSetBroker) throws ConfigurationException {
Set<XMLGridSubset> cachedGridSets = info.getGridSubsets();
if (cachedGridSets.size() == 0) {
return Collections.emptyMap();
}
Map<String, GridSubset> grids = new HashMap<String, GridSubset>(2);
for (XMLGridSubset gridSubset : cachedGridSets) {
final String gridSetId = gridSubset.getGridSetName();
final GridSet gridSet = gridSetBroker.get(gridSetId);
if (gridSet == null) {
LOGGER.info("No GWC GridSet named '" + gridSetId + "' exists.");
continue;
}
BoundingBox extent = gridSubset.getExtent();
if (null == extent) {
try {
SRS srs = gridSet.getSrs();
try {
extent = getBounds(srs);
} catch (RuntimeException cantComputeBounds) {
final String msg = "Can't compute bounds for tile layer " + getName()
+ " in CRS " + srs + ". Assuming full GridSet bounds. ("
+ cantComputeBounds.getMessage() + ")";
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, msg, cantComputeBounds);
} else {
LOGGER.warning(msg);
}
extent = gridSet.getBounds();
}
BoundingBox maxBounds = gridSet.getBounds();
BoundingBox intersection = maxBounds.intersection(extent);
extent = intersection;
} catch (RuntimeException e) {
LOGGER.log(Level.WARNING,
"Error computing layer bounds, assuming whole GridSet bounds", e);
extent = gridSet.getOriginalExtent();
}
}
gridSubset.setExtent(extent);
GridSubset gridSubSet = gridSubset.getGridSubSet(gridSetBroker);
grids.put(gridSetId, gridSubSet);
}
return grids;
}
private BoundingBox getBounds(final SRS srs) {
CoordinateReferenceSystem targetCrs;
try {
final String epsgCode = srs.toString();
final boolean longitudeFirst = true;
targetCrs = CRS.decode(epsgCode, longitudeFirst);
checkNotNull(targetCrs);
} catch (Exception e) {
throw propagate(e);
}
ReferencedEnvelope nativeBounds;
if (getLayerInfo() != null) {
// projection policy for these bounds are already taken care of by the geoserver
// configuration
nativeBounds = getLayerInfo().getResource().getNativeBoundingBox();
} else {
nativeBounds = getLayerGroupInfo().getBounds();
}
checkState(nativeBounds != null, getName(), " has no native bounds set");
Envelope transformedBounds;
// try reprojecting directly
try {
transformedBounds = nativeBounds.transform(targetCrs, true, 10000);
} catch (Exception e) {
// no luck, try the expensive way
final Geometry targetAov = GWC.getAreaOfValidityAsGeometry(targetCrs, gridSetBroker);
if (null == targetAov) {
String msg = "Can't compute tile layer bouds out of resource native bounds for CRS "
+ srs;
LOGGER.log(Level.WARNING, msg, e);
throw new IllegalArgumentException(msg, e);
}
LOGGER.log(Level.FINE, "Can't compute tile layer bouds out of resource "
+ "native bounds for CRS " + srs, e);
final CoordinateReferenceSystem nativeCrs = nativeBounds.getCoordinateReferenceSystem();
try {
ReferencedEnvelope targetAovBounds = new ReferencedEnvelope(
targetAov.getEnvelopeInternal(), targetCrs);
// transform target AOV in target CRS to native CRS
ReferencedEnvelope targetAovInNativeCrs = targetAovBounds.transform(nativeCrs,
true, 10000);
// get the intersection between the target aov in native crs and native layer bounds
Envelope intersection = targetAovInNativeCrs.intersection(nativeBounds);
ReferencedEnvelope clipped = new ReferencedEnvelope(intersection, nativeCrs);
// transform covered area in native crs to target crs
transformedBounds = clipped.transform(targetCrs, true, 10000);
} catch (Exception e1) {
throw propagate(e1);
}
}
BoundingBox targetBbox = new BoundingBox(transformedBounds.getMinX(),
transformedBounds.getMinY(), transformedBounds.getMaxX(),
transformedBounds.getMaxY());
return targetBbox;
}
public GeoServerTileLayerInfo getInfo() {
return info;
}
/**
* @see org.geowebcache.layer.TileLayer#getUpdateSources()
*/
@Override
public List<UpdateSourceDefinition> getUpdateSources() {
return Collections.emptyList();
}
/**
* @see org.geowebcache.layer.TileLayer#useETags()
*/
@Override
public boolean useETags() {
return false;
}
/**
* @see org.geowebcache.layer.TileLayer#getFormatModifiers()
*/
@Override
public List<FormatModifier> getFormatModifiers() {
return Collections.emptyList();