@@ -66,9 +66,7 @@ public class WMTSGetCapabilities {
6666
6767 private GridSetBroker gsb ;
6868
69- private String baseUrl ;
70-
71- private String restBaseUrl ;
69+ private final WMTSUrls urls ;
7270
7371 private final Collection <WMTSExtension > extensions ;
7472
@@ -90,21 +88,31 @@ protected WMTSGetCapabilities(
9088 String contextPath ,
9189 URLMangler urlMangler ,
9290 Collection <WMTSExtension > extensions ) {
91+ this (tld , gsb , servReq , buildUrls (servReq , baseUrl , contextPath , urlMangler ), extensions );
92+ }
93+
94+ protected WMTSGetCapabilities (
95+ TileLayerDispatcher tld ,
96+ GridSetBroker gsb ,
97+ HttpServletRequest servReq ,
98+ WMTSUrls urls ,
99+ Collection <WMTSExtension > extensions ) {
93100 this .tld = tld ;
94101 this .gsb = gsb ;
102+ this .urls = urls ;
103+ this .extensions = extensions ;
104+ }
95105
106+ private static WMTSUrls buildUrls (
107+ HttpServletRequest servReq , String baseUrl , String contextPath , URLMangler urlMangler ) {
96108 String forcedBaseUrl =
97109 ServletUtils .stringFromMap (servReq .getParameterMap (), servReq .getCharacterEncoding (), "base_url" );
98-
99- if (forcedBaseUrl != null ) {
100- this .baseUrl = forcedBaseUrl ;
101- } else {
102- this .baseUrl = urlMangler .buildURL (baseUrl , contextPath , WMTSService .SERVICE_PATH );
103- }
104-
105- this .restBaseUrl = urlMangler .buildURL (baseUrl , contextPath , WMTSService .REST_PATH );
106-
107- this .extensions = extensions ;
110+ String effectiveBaseUrl = forcedBaseUrl != null ? forcedBaseUrl : baseUrl ;
111+ URLMangler .UrlAndParams service =
112+ urlMangler .buildURL (effectiveBaseUrl , contextPath , WMTSService .SERVICE_PATH , Collections .emptyMap ());
113+ URLMangler .UrlAndParams rest =
114+ urlMangler .buildURL (effectiveBaseUrl , contextPath , WMTSService .REST_PATH , Collections .emptyMap ());
115+ return new WMTSUrls (service .url (), service .queryParameters (), rest .url (), rest .queryParameters ());
108116 }
109117
110118 protected void writeResponse (HttpServletResponse response , RuntimeStats stats ) {
@@ -169,11 +177,18 @@ private String generateGetCapabilities(Charset encoding) {
169177 contents (xml );
170178
171179 xml .indentElement ("ServiceMetadataURL" )
172- .attribute ("xlink:href" , WMTSUtils .getKvpServiceMetadataURL (baseUrl ))
180+ .attribute (
181+ "xlink:href" ,
182+ WMTSUtils .appendQueryParameters (
183+ WMTSUtils .getKvpServiceMetadataURL (urls .serviceBaseUrl ()),
184+ urls .serviceQueryParameters ()))
173185 .endElement ();
174186
175187 xml .indentElement ("ServiceMetadataURL" )
176- .attribute ("xlink:href" , restBaseUrl + "/WMTSCapabilities.xml" )
188+ .attribute (
189+ "xlink:href" ,
190+ WMTSUtils .appendQueryParameters (
191+ urls .restBaseUrl () + "/WMTSCapabilities.xml" , urls .restQueryParameters ()))
177192 .endElement ();
178193
179194 xml .endElement ("Capabilities" );
@@ -365,9 +380,9 @@ private void serviceProvider(XMLBuilder xml, ServiceInformation servInfo) throws
365380 xml .endElement ();
366381 }
367382 } else {
368- appendTag (xml , "ows:ProviderName" , baseUrl , null );
383+ appendTag (xml , "ows:ProviderName" , urls . serviceBaseUrl () , null );
369384 xml .indentElement ("ows:ProviderSite" )
370- .attribute ("xlink:href" , baseUrl )
385+ .attribute ("xlink:href" , urls . serviceBaseUrl () )
371386 .endElement ();
372387 xml .indentElement ("ows:ServiceContact" );
373388 appendTag (xml , "ows:IndividualName" , "GeoWebCache User" , null );
@@ -379,9 +394,9 @@ private void serviceProvider(XMLBuilder xml, ServiceInformation servInfo) throws
379394
380395 private void operationsMetadata (XMLBuilder xml ) throws IOException {
381396 xml .indentElement ("ows:OperationsMetadata" );
382- operation (xml , "GetCapabilities" , baseUrl );
383- operation (xml , "GetTile" , baseUrl );
384- operation (xml , "GetFeatureInfo" , baseUrl );
397+ operation (xml , "GetCapabilities" , urls . serviceBaseUrl (), urls . serviceQueryParameters () );
398+ operation (xml , "GetTile" , urls . serviceBaseUrl (), urls . serviceQueryParameters () );
399+ operation (xml , "GetFeatureInfo" , urls . serviceBaseUrl (), urls . serviceQueryParameters () );
385400 // allow extension to inject their own metadata
386401 for (WMTSExtension extension : extensions ) {
387402 List <WMTSExtension .OperationMetadata > operationsMetaData = extension .getExtraOperationsMetadata ();
@@ -390,22 +405,27 @@ private void operationsMetadata(XMLBuilder xml) throws IOException {
390405 operation (
391406 xml ,
392407 operationMetadata .getName (),
393- operationMetadata .getBaseUrl () == null ? baseUrl : operationMetadata .getBaseUrl ());
408+ operationMetadata .getBaseUrl () == null
409+ ? urls .serviceBaseUrl ()
410+ : operationMetadata .getBaseUrl (),
411+ urls .serviceQueryParameters ());
394412 }
395413 }
396414 extension .encodedOperationsMetadata (xml );
397415 }
398416 xml .endElement ("ows:OperationsMetadata" );
399417 }
400418
401- private void operation (XMLBuilder xml , String operationName , String baseUrl ) throws IOException {
419+ private void operation (XMLBuilder xml , String operationName , String baseUrl , Map <String , String > queryParameters )
420+ throws IOException {
402421 xml .indentElement ("ows:Operation" ).attribute ("name" , operationName );
403422 xml .indentElement ("ows:DCP" );
404423 xml .indentElement ("ows:HTTP" );
405- if (baseUrl .contains ("?" )) {
406- xml .indentElement ("ows:Get" ).attribute ("xlink:href" , baseUrl + "&" );
424+ String href = WMTSUtils .appendQueryParameters (baseUrl , queryParameters );
425+ if (href .contains ("?" )) {
426+ xml .indentElement ("ows:Get" ).attribute ("xlink:href" , href + "&" );
407427 } else {
408- xml .indentElement ("ows:Get" ).attribute ("xlink:href" , baseUrl + "?" );
428+ xml .indentElement ("ows:Get" ).attribute ("xlink:href" , href + "?" );
409429 }
410430 xml .indentElement ("ows:Constraint" ).attribute ("name" , "GetEncoding" );
411431 xml .indentElement ("ows:AllowedValues" );
@@ -426,7 +446,7 @@ private void contents(XMLBuilder xml) throws IOException {
426446 if (!layer .isEnabled () || !layer .isAdvertised ()) {
427447 continue ;
428448 }
429- layer (xml , layer , baseUrl , usedGridsets );
449+ layer (xml , layer , urls . serviceBaseUrl () , usedGridsets );
430450 }
431451
432452 // only dump the gridsets actually used, as the OGC TMS spec introduced many default ones
@@ -488,7 +508,7 @@ private void layer(XMLBuilder xml, TileLayer layer, String baseurl, Set<GridSet>
488508
489509 layerGridSubSets (xml , layer , usedGridsets );
490510
491- layerResourceUrls (xml , layer , filters , restBaseUrl );
511+ layerResourceUrls (xml , layer , filters , urls . restBaseUrl (), urls . restQueryParameters () );
492512
493513 // allow extensions to contribute extra metadata to this layer
494514 for (WMTSExtension extension : extensions ) {
@@ -712,7 +732,12 @@ private void layerGridSubSets(XMLBuilder xml, TileLayer layer, Set<GridSet> used
712732 * For each layer discovers the available image formats, feature info formats and dimensions and produce the
713733 * necessary <ResourceURL> elements.
714734 */
715- private void layerResourceUrls (XMLBuilder xml , TileLayer layer , List <ParameterFilter > filters , String baseurl )
735+ private void layerResourceUrls (
736+ XMLBuilder xml ,
737+ TileLayer layer ,
738+ List <ParameterFilter > filters ,
739+ String baseurl ,
740+ Map <String , String > queryParameters )
716741 throws IOException {
717742 String baseTemplate = baseurl + "/" + layer .getName ();
718743 String commonTemplate = baseTemplate + "/{style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}" ;
@@ -728,21 +753,25 @@ private void layerResourceUrls(XMLBuilder xml, TileLayer layer, List<ParameterFi
728753 // Extracts image formats
729754 List <String > mimeFormats = WMTSUtils .getLayerFormats (layer );
730755 for (String format : mimeFormats ) {
731- String template = commonTemplate + "?format=" + format + commonDimensions ;
756+ String template = WMTSUtils .appendQueryParameters (
757+ commonTemplate + "?format=" + format + commonDimensions , queryParameters );
732758 layerResourceUrlsGen (xml , format , "tile" , template );
733759 }
734760 // Extracts feature info formats
735761 List <String > infoFormats = WMTSUtils .getInfoFormats (layer );
736762 for (String format : infoFormats ) {
737- String template = commonTemplate + "/{J}/{I}?format=" + format + commonDimensions ;
763+ String template = WMTSUtils .appendQueryParameters (
764+ commonTemplate + "/{J}/{I}?format=" + format + commonDimensions , queryParameters );
738765 layerResourceUrlsGen (xml , format , "FeatureInfo" , template );
739766 }
740767 if (layer instanceof TileJSONProvider provider ) {
741768 List <String > formatExtensions = WMTSUtils .getLayerFormatsExtensions (layer );
742769 String outputFormat = ApplicationMime .json .getFormat ();
743770 if (provider .supportsTileJSON ()) {
744771 for (String tileJsonFormat : formatExtensions ) {
745- String template = baseTemplate + "/{style}/tilejson/" + tileJsonFormat + "?format=" + outputFormat ;
772+ String template = WMTSUtils .appendQueryParameters (
773+ baseTemplate + "/{style}/tilejson/" + tileJsonFormat + "?format=" + outputFormat ,
774+ queryParameters );
746775 layerResourceUrlsGen (xml , outputFormat , "TileJSON" , template );
747776 }
748777 }
0 commit comments