|
| 1 | +package com.sintef.featureserver.rs.wmts; |
| 2 | + |
| 3 | +import com.sintef.featureserver.netcdf.NetCdfManager; |
| 4 | +import com.sintef.featureserver.util.VelocityUtil; |
| 5 | +import com.sintef.featureserver.wmts.GoogleMapsCompatibleTileMatrixSet; |
| 6 | +import com.sintef.featureserver.wmts.TileMatrix; |
| 7 | +import java.io.IOException; |
| 8 | +import java.net.URI; |
| 9 | +import javax.ws.rs.GET; |
| 10 | +import javax.ws.rs.Path; |
| 11 | +import javax.ws.rs.Produces; |
| 12 | +import javax.ws.rs.core.Context; |
| 13 | +import javax.ws.rs.core.Response; |
| 14 | +import javax.ws.rs.core.UriBuilder; |
| 15 | +import javax.ws.rs.core.UriInfo; |
| 16 | +import org.apache.velocity.Template; |
| 17 | +import org.apache.velocity.VelocityContext; |
| 18 | +import org.apache.velocity.app.VelocityEngine; |
| 19 | +import ucar.unidata.geoloc.LatLonPoint; |
| 20 | +import ucar.unidata.geoloc.LatLonRect; |
| 21 | + |
| 22 | +/** |
| 23 | + * Serves the Capabilities document as prescribed by the WMTS spec. |
| 24 | + * The spec only defines an XML encoding for this document. |
| 25 | + * The path to this document is also defined by the spec. |
| 26 | + * @author arve |
| 27 | + */ |
| 28 | + |
| 29 | +@Path("WMTS/1.0.0/WMTSCapabilities.xml") |
| 30 | +public class CapabilityResource { |
| 31 | + private final NetCdfManager netCdfManager; |
| 32 | + private final VelocityEngine velocityEngine; |
| 33 | + private final UriInfo uriInfo; |
| 34 | + |
| 35 | + public CapabilityResource( |
| 36 | + @Context final NetCdfManager netCdfManager, |
| 37 | + @Context final UriInfo uriInfo, |
| 38 | + @Context final VelocityEngine velocityEngine) { |
| 39 | + this.netCdfManager = netCdfManager; |
| 40 | + this.velocityEngine = velocityEngine; |
| 41 | + this.uriInfo = uriInfo; |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + @GET |
| 46 | + @Produces("text/xml") |
| 47 | + public Response getCapabilities() throws IOException { |
| 48 | + |
| 49 | + |
| 50 | + final Template xmlTemplate |
| 51 | + = VelocityUtil.loadTemplate(velocityEngine, "capabilities.xml"); |
| 52 | + final VelocityContext context = new VelocityContext(); |
| 53 | + |
| 54 | + final URI wmtsBaseUrl = UriBuilder. |
| 55 | + fromUri(uriInfo.getBaseUri()) |
| 56 | + .path("WMTS") |
| 57 | + .build(); |
| 58 | + final LatLonRect boundingBox = netCdfManager.getBoundingBox(); |
| 59 | + final LatLonPoint upperLeft = boundingBox.getUpperLeftPoint(); |
| 60 | + final LatLonPoint lowerRight = boundingBox.getLowerRightPoint(); |
| 61 | + final GoogleMapsCompatibleTileMatrixSet tileMatrixSet |
| 62 | + = new GoogleMapsCompatibleTileMatrixSet( |
| 63 | + netCdfManager.getResolution(), |
| 64 | + netCdfManager.getBoundingBox()); |
| 65 | + |
| 66 | + context.put("WmtsBaseUrl", wmtsBaseUrl); |
| 67 | + context.put("upperLeftPoint", upperLeft); |
| 68 | + context.put("lowerRightPoint", lowerRight); |
| 69 | + context.put("tileMatrixSet", tileMatrixSet); |
| 70 | + final String xmlString = VelocityUtil.renderTemplate(xmlTemplate, context); |
| 71 | + return Response.ok(xmlString).build(); |
| 72 | + } |
| 73 | +} |
0 commit comments