WfsGetCapabilitiesRequest.java

  1. package de.turnertech.ows.servlet;

  2. import java.io.IOException;
  3. import java.util.Map.Entry;

  4. import javax.xml.stream.XMLOutputFactory;
  5. import javax.xml.stream.XMLStreamWriter;

  6. import de.turnertech.ows.common.ExceptionCode;
  7. import de.turnertech.ows.common.OwsContext;
  8. import de.turnertech.ows.common.OwsRequestContext;
  9. import de.turnertech.ows.common.RequestHandler;
  10. import de.turnertech.ows.gml.Envelope;
  11. import de.turnertech.ows.gml.FeatureType;
  12. import de.turnertech.ows.parameter.WfsVersionValue;
  13. import de.turnertech.ows.srs.SpatialReferenceSystem;
  14. import de.turnertech.ows.srs.SpatialReferenceSystemFormat;
  15. import de.turnertech.ows.srs.SpatialReferenceSystemRepresentation;
  16. import jakarta.servlet.ServletException;
  17. import jakarta.servlet.http.HttpServletRequest;
  18. import jakarta.servlet.http.HttpServletResponse;

  19. public class WfsGetCapabilitiesRequest implements RequestHandler {

  20.     @Override
  21.     public void handleRequest(HttpServletRequest request, HttpServletResponse response, OwsContext owsContext, OwsRequestContext requestContext) throws ServletException, IOException {
  22.         response.setContentType(RequestHandler.CONTENT_XML);
  23.         XMLStreamWriter out = null;
  24.         WfsVersionValue requestedVersion = WfsVersionValue.V2_0_2;
  25.         String port;
  26.         if("http".equalsIgnoreCase(request.getScheme()) && request.getServerPort() == 80 || "https".equalsIgnoreCase(request.getScheme()) && request.getServerPort() == 443) {
  27.             port = "";
  28.         } else {
  29.             port = ":" + Integer.toString(request.getServerPort());
  30.         }
  31.         String currentUrl = request.getScheme() + "://" + request.getServerName() + port + request.getContextPath() + request.getServletPath();

  32.         if(requestContext.getOwsVersion() != null) {
  33.             requestedVersion = requestContext.getOwsVersion();
  34.         }
  35.         SpatialReferenceSystemFormat srsFormat = requestedVersion == WfsVersionValue.V2_0_0 ? SpatialReferenceSystemFormat.URN : SpatialReferenceSystemFormat.URI;

  36.         try {
  37.             out = XMLOutputFactory.newInstance().createXMLStreamWriter(response.getOutputStream(), "UTF-8");
  38.             out.writeStartDocument("UTF-8", "1.0");
  39.             out.writeStartElement("WFS_Capabilities");
  40.             out.writeAttribute("service", "WFS");
  41.             out.writeAttribute("version", requestedVersion.toString());
  42.             out.writeDefaultNamespace(OwsContext.WFS_URI);
  43.             out.writeNamespace(owsContext.getXmlNamespacePrefix(OwsContext.XSI_URI), OwsContext.XSI_URI);
  44.             out.writeNamespace(owsContext.getXmlNamespacePrefix(OwsContext.GML_URI), OwsContext.GML_URI);
  45.             out.writeNamespace(owsContext.getXmlNamespacePrefix(OwsContext.FES_URI), OwsContext.FES_URI);
  46.             out.writeNamespace(owsContext.getXmlNamespacePrefix(OwsContext.OWS_URI), OwsContext.OWS_URI);
  47.             out.writeNamespace(owsContext.getXmlNamespacePrefix(OwsContext.XSD_URI), OwsContext.XSD_URI);
  48.             out.writeNamespace(owsContext.getXmlNamespacePrefix(OwsContext.XLINK_URI), OwsContext.XLINK_URI);
  49.             out.writeAttribute(OwsContext.XSI_URI, "schemaLocation", "http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd http://www.opengis.net/ows/1.1 http://schemas.opengis.net/ows/1.1.0/owsAll.xsd http://www.opengis.net/fes/2.0 http://schemas.opengis.net/filter/2.0/filterAll.xsd");

  50.             out.writeStartElement(OwsContext.OWS_URI, "ServiceIdentification");
  51.                 out.writeStartElement(OwsContext.OWS_URI, "Title");
  52.                     out.writeCharacters(owsContext.getWfsCapabilities().getServiceTitle());
  53.                 out.writeEndElement();
  54.                 out.writeStartElement(OwsContext.OWS_URI, "Abstract");
  55.                     out.writeCharacters(owsContext.getWfsCapabilities().getServiceAbstract());
  56.                 out.writeEndElement();
  57.                 out.writeStartElement(OwsContext.OWS_URI, "Keywords");
  58.                     for(String keyword : owsContext.getWfsCapabilities().getKeywords()) {
  59.                         out.writeStartElement(OwsContext.OWS_URI, "Keyword");
  60.                             out.writeCharacters(keyword);
  61.                         out.writeEndElement();
  62.                     }
  63.                     out.writeStartElement(OwsContext.OWS_URI, "Type");
  64.                         out.writeCharacters("String");
  65.                     out.writeEndElement();
  66.                 out.writeEndElement();
  67.                 out.writeStartElement(OwsContext.OWS_URI, "ServiceType");
  68.                     out.writeCharacters(owsContext.getWfsCapabilities().getServiceType().toString());
  69.                 out.writeEndElement();
  70.                 for(WfsVersionValue serviceTypeVersion : owsContext.getWfsCapabilities().getServiceTypeVersions()) {
  71.                     out.writeStartElement(OwsContext.OWS_URI, "ServiceTypeVersion");
  72.                         out.writeCharacters(serviceTypeVersion.toString());
  73.                     out.writeEndElement();
  74.                 }
  75.                 out.writeStartElement(OwsContext.OWS_URI, "Fees");
  76.                     out.writeCharacters(owsContext.getWfsCapabilities().getFees());
  77.                 out.writeEndElement();
  78.                 out.writeStartElement(OwsContext.OWS_URI, "AccessConstraints");
  79.                     out.writeCharacters(owsContext.getWfsCapabilities().getAccessConstraints());
  80.                 out.writeEndElement();
  81.             out.writeEndElement();

  82.             out.writeStartElement(OwsContext.OWS_URI, "OperationsMetadata");
  83.                 out.writeStartElement(OwsContext.OWS_URI, "Operation");
  84.                 out.writeAttribute("name", "GetCapabilities");
  85.                     out.writeStartElement(OwsContext.OWS_URI, "DCP");
  86.                         out.writeStartElement(OwsContext.OWS_URI, "HTTP");
  87.                             out.writeEmptyElement(OwsContext.OWS_URI, "Get");
  88.                             out.writeAttribute(OwsContext.XLINK_URI, "href", currentUrl + "?");
  89.                             out.writeEmptyElement(OwsContext.OWS_URI, "Post");
  90.                             out.writeAttribute(OwsContext.XLINK_URI, "href", currentUrl);
  91.                         out.writeEndElement();
  92.                     out.writeEndElement();


  93.                     out.writeStartElement(OwsContext.OWS_URI, "Parameter");
  94.                     out.writeAttribute("name", "AcceptVersions");
  95.                         out.writeStartElement(OwsContext.OWS_URI, "AllowedValues");
  96.                             for(WfsVersionValue allowedVersion : owsContext.getWfsCapabilities().getServiceTypeVersions()) {
  97.                                 out.writeStartElement(OwsContext.OWS_URI, "Value");
  98.                                     out.writeCharacters(allowedVersion.toString());
  99.                                 out.writeEndElement();
  100.                             }
  101.                         out.writeEndElement();
  102.                     out.writeEndElement();
  103.                 out.writeEndElement();
  104.                 out.writeStartElement(OwsContext.OWS_URI, "Operation");
  105.                 out.writeAttribute("name", "DescribeFeatureType");
  106.                     out.writeStartElement(OwsContext.OWS_URI, "DCP");
  107.                         out.writeStartElement(OwsContext.OWS_URI, "HTTP");
  108.                             out.writeEmptyElement(OwsContext.OWS_URI, "Get");
  109.                             out.writeAttribute(OwsContext.XLINK_URI, "href", currentUrl + "?");
  110.                             out.writeEmptyElement(OwsContext.OWS_URI, "Post");
  111.                             out.writeAttribute(OwsContext.XLINK_URI, "href", currentUrl);
  112.                         out.writeEndElement();
  113.                     out.writeEndElement();
  114.                 out.writeEndElement();
  115.                 out.writeStartElement(OwsContext.OWS_URI, "Operation");
  116.                 out.writeAttribute("name", "GetFeature");
  117.                     out.writeStartElement(OwsContext.OWS_URI, "DCP");
  118.                         out.writeStartElement(OwsContext.OWS_URI, "HTTP");
  119.                             out.writeEmptyElement(OwsContext.OWS_URI, "Get");
  120.                             out.writeAttribute(OwsContext.XLINK_URI, "href", currentUrl + "?");
  121.                             out.writeEmptyElement(OwsContext.OWS_URI, "Post");
  122.                             out.writeAttribute(OwsContext.XLINK_URI, "href", currentUrl);
  123.                         out.writeEndElement();
  124.                     out.writeEndElement();
  125.                 out.writeEndElement();
  126.                 out.writeStartElement(OwsContext.OWS_URI, "Operation");
  127.                 out.writeAttribute("name", "Transaction");
  128.                     out.writeStartElement(OwsContext.OWS_URI, "DCP");
  129.                         out.writeStartElement(OwsContext.OWS_URI, "HTTP");
  130.                             out.writeEmptyElement(OwsContext.OWS_URI, "Post");
  131.                             out.writeAttribute(OwsContext.XLINK_URI, "href", currentUrl);
  132.                         out.writeEndElement();
  133.                     out.writeEndElement();
  134.                     out.writeStartElement(OwsContext.OWS_URI, "Parameter");
  135.                     out.writeAttribute("name", "AcceptVersions");
  136.                         out.writeStartElement(OwsContext.OWS_URI, "AllowedValues");
  137.                             for(WfsVersionValue allowedVersion : owsContext.getWfsCapabilities().getServiceTypeVersions()) {
  138.                                 out.writeStartElement(OwsContext.OWS_URI, "Value");
  139.                                     out.writeCharacters(allowedVersion.toString());
  140.                                 out.writeEndElement();
  141.                             }
  142.                         out.writeEndElement();
  143.                     out.writeEndElement();
  144.                 out.writeEndElement();
  145.             out.writeEndElement();


  146.             out.writeStartElement(OwsContext.WFS_URI, "FeatureTypeList");
  147.             for(FeatureType featureType : owsContext.getWfsCapabilities().getFeatureTypes()) {
  148.                 out.writeStartElement(OwsContext.WFS_URI, "FeatureType");
  149.                     out.writeStartElement(OwsContext.WFS_URI, "Name");
  150.                         out.writeCharacters(owsContext.getXmlNamespacePrefix(featureType.getNamespace()) + ":" + featureType.getName());
  151.                     out.writeEndElement();
  152.                     if(featureType.getTitle() != null) {
  153.                         out.writeStartElement(OwsContext.WFS_URI, "Title");
  154.                             out.writeCharacters(featureType.getTitle());
  155.                         out.writeEndElement();
  156.                     }
  157.                     if(featureType.getDescription() != null) {
  158.                         out.writeStartElement(OwsContext.WFS_URI, "Abstract");
  159.                             out.writeCharacters(featureType.getDescription());
  160.                         out.writeEndElement();
  161.                     }
  162.                     if(featureType.getSrs() == null) {
  163.                         out.writeEmptyElement(OwsContext.WFS_URI, "NoCRS");
  164.                     } else {
  165.                         out.writeStartElement(OwsContext.WFS_URI, "DefaultCRS");
  166.                             out.writeCharacters(new SpatialReferenceSystemRepresentation(featureType.getSrs(), srsFormat).toString());
  167.                         out.writeEndElement();
  168.                         for(SpatialReferenceSystem srs : SpatialReferenceSystem.values()) {
  169.                             if(srs != featureType.getSrs()) {
  170.                                 out.writeStartElement(OwsContext.WFS_URI, "OtherCRS");
  171.                                     out.writeCharacters(new SpatialReferenceSystemRepresentation(srs, srsFormat).toString());
  172.                                 out.writeEndElement();
  173.                             }
  174.                         }
  175.                     }

  176.                     Envelope boundingBox = owsContext.getModelProvider().getModel(featureType).getBoundingBox();
  177.                     if(boundingBox != null) {
  178.                         out.writeStartElement(OwsContext.OWS_URI, "WGS84BoundingBox");
  179.                             out.writeStartElement(OwsContext.OWS_URI, "LowerCorner");
  180.                                 out.writeCharacters(Double.toString(boundingBox.getWest()) + " " + Double.toString(boundingBox.getSouth()));
  181.                             out.writeEndElement();
  182.                             out.writeStartElement(OwsContext.OWS_URI, "UpperCorner");
  183.                                 out.writeCharacters(Double.toString(boundingBox.getEast()) + " " + Double.toString(boundingBox.getNorth()));
  184.                             out.writeEndElement();
  185.                         out.writeEndElement();
  186.                     }

  187.                 out.writeEndElement();
  188.             }
  189.             out.writeEndElement();

  190.             out.writeStartElement(OwsContext.FES_URI, "Filter_Capabilities");
  191.                 out.writeStartElement(OwsContext.FES_URI, "Conformance");
  192.                     for(Entry<String, Boolean> entry : owsContext.getWfsCapabilities().getFilterConformance().entrySet()) {
  193.                         out.writeStartElement(OwsContext.FES_URI, "Constraint");
  194.                             out.writeAttribute(OwsContext.WFS_URI, "name", entry.getKey());
  195.                             out.writeEmptyElement(OwsContext.OWS_URI, "NoValues");
  196.                             out.writeStartElement(OwsContext.OWS_URI, "Constraint");
  197.                                 out.writeCharacters(entry.getValue().toString().toUpperCase());
  198.                             out.writeEndElement();
  199.                         out.writeEndElement();
  200.                     }
  201.                 out.writeEndElement();
  202.             out.writeEndElement();

  203.             out.writeEndElement();
  204.             out.writeEndDocument();
  205.         } catch (Exception e) {
  206.             response.sendError(500, ErrorServlet.encodeMessage(ExceptionCode.OPERATION_PROCESSING_FAILED.toString(), "GetCapabilities", "XML Constrction of the response failed."));
  207.         }
  208.     }
  209. }