Skip to content

Commit

Permalink
[GEOS-7962] Add a WMS vendor FORMAT parameter to force the return of …
Browse files Browse the repository at this point in the history
…WMS 1.1.1 capabilities in text/xml
  • Loading branch information
Nuno Oliveira committed Feb 7, 2017
1 parent 46af947 commit caa7cbd
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 11 deletions.
3 changes: 3 additions & 0 deletions doc/en/user/source/services/wms/reference.rst
Expand Up @@ -116,6 +116,9 @@ They are fully documented in the :ref:`wms_vendor_parameters` section.
* - ``namespace``
- No
- limits response to layers in a given namespace
* - ``format``
- No
- request the capabilities document in a certain format


A example GetCapabilities request is:
Expand Down
11 changes: 11 additions & 0 deletions doc/en/user/source/services/wms/vendor.rst
Expand Up @@ -293,3 +293,14 @@ or empty if the default method has to be used for the related layer.

The parameter allows to override the global WMS Raster Rendering Options setting (see :ref:`WMS Settings <services_webadmin_wms>` for more info), on a layer by layer basis.

format
------

The ``format`` parameter can be used to request capabilities documents in a certain format. If the requested format is not supported the default format will be used.

An example request:

http://localhost:8080/geoserver/ows?service=wms&version=1.1.1&request=GetCapabilities&format=text/xml

.. note::
Currently this parameter can only be used to request WMS 1.1.1 capabilities documents encoded in ``text/xml``, if used with other WMS versions or other formats it will have no effect.
Expand Up @@ -11,7 +11,9 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
Expand Down Expand Up @@ -76,6 +78,8 @@
*/
public class GetCapabilitiesResponse extends BaseCapabilitiesResponse {

private static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger(GetCapabilitiesResponse.class);

private WMS wms;

/**
Expand All @@ -84,7 +88,7 @@ public class GetCapabilitiesResponse extends BaseCapabilitiesResponse {
* check of internal DTD elements shall be added to the output document
*/
public GetCapabilitiesResponse(final WMS wms) {
super(GetCapabilitiesTransformer.class,GetCapabilitiesTransformer.WMS_CAPS_MIME);
super(GetCapabilitiesTransformer.class,GetCapabilitiesTransformer.WMS_CAPS_DEFAULT_MIME);
this.wms = wms;
}

Expand Down Expand Up @@ -262,4 +266,41 @@ private String getInternalDTDDeclaration(final GetCapabilitiesRequest request) {
return fullDTDDeclaration;
}

@Override
public String getMimeType(Object value, Operation operation) throws ServiceException {
// check that we have a valid value (same check as the super method)
if (value == null || !value.getClass().isAssignableFrom(super.getBinding())) {
// this is not good (same error message as the super method)
String message = String.format("%s/%s",
value == null ? "null" : value.getClass().getName(), operation.getId());
throw new IllegalArgumentException(message);
}
// search for the get capabilities object
GetCapabilitiesRequest request = null;
for (Object parameter : operation.getParameters()) {
if (parameter instanceof GetCapabilitiesRequest) {
// we found our request
request = (GetCapabilitiesRequest) parameter;
}
}
if (request == null) {
// unlikely but no get capabilities request was found, fall back to the default behavior
return super.getMimeType(value, operation);
}
// let's see if we have the format parameter
String format = request.getRawKvp().get("FORMAT");
if (format == null || format.isEmpty()) {
// no format parameter, fall back to default behavior
return super.getMimeType(value, operation);
}
// if the requested format is a valid mime type return it
for (String mimeType : GetCapabilitiesTransformer.WMS_CAPS_AVAIL_MIME) {
if (format.equalsIgnoreCase(mimeType)) {
// the format parameter value maps to a valid mime type, returning the associate mime type
return mimeType;
}
}
// the requested format is not supported, throw an exception
throw new RuntimeException(String.format("The request format '%s' is not supported.", format));
}
}
Expand Up @@ -84,8 +84,6 @@
import org.xml.sax.ContentHandler;
import org.xml.sax.helpers.AttributesImpl;

import sun.security.action.GetBooleanAction;

import com.google.common.collect.Iterables;
import com.vividsolutions.jts.geom.Envelope;
import org.geoserver.wfs.json.JSONType;
Expand All @@ -100,8 +98,13 @@
* org.geoserver.platform.Operation)
*/
public class GetCapabilitiesTransformer extends TransformerBase {
/** fixed MIME type for the returned capabilities document */
public static final String WMS_CAPS_MIME = "application/vnd.ogc.wms_xml";
/** default MIME type for the returned capabilities document */
public static final String WMS_CAPS_DEFAULT_MIME = "application/vnd.ogc.wms_xml";

// available MIME types for the returned capabilities document
public static final String[] WMS_CAPS_AVAIL_MIME = {
WMS_CAPS_DEFAULT_MIME, "text/xml"
};

/** the WMS supported exception formats */
static final String[] EXCEPTION_FORMATS = { "application/vnd.ogc.se_xml",
Expand Down Expand Up @@ -473,7 +476,11 @@ private void handleRequest() {
start("Request");

start("GetCapabilities");
element("Format", WMS_CAPS_MIME);

// add all the supported MIME types for the capabilities document
for (String mimeType : WMS_CAPS_AVAIL_MIME) {
element("Format", mimeType);
}

// build the service URL and make sure it ends with &
String serviceUrl = buildURL(request.getBaseUrl(), "wms", params("SERVICE", "WMS"),
Expand Down
Expand Up @@ -9,10 +9,11 @@
import static org.custommonkey.xmlunit.XMLAssert.assertXpathExists;
import static org.custommonkey.xmlunit.XMLAssert.assertXpathNotExists;
import static org.custommonkey.xmlunit.XMLUnit.newXpathEngine;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import static org.junit.Assert.*;
import java.util.ArrayList;
import static org.hamcrest.CoreMatchers.is;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import org.custommonkey.xmlunit.XMLUnit;
Expand All @@ -38,8 +39,11 @@
import org.geoserver.wfs.json.JSONType;
import org.geoserver.wms.WMSInfo;
import org.geoserver.wms.WMSTestSupport;
import org.geoserver.wms.capabilities.GetCapabilitiesTransformer;
import org.geotools.data.Base64;
import org.geotools.referencing.CRS;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletResponse;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -83,14 +87,55 @@ protected void onSetUp(SystemTestData testData) throws Exception {
setupOpaqueGroup(catalog);
}


@Test
public void testCapabilities() throws Exception {
Document dom = dom(get("wms?request=getCapabilities&version=1.1.1"), false);
Element e = dom.getDocumentElement();
assertEquals("WMT_MS_Capabilities", e.getLocalName());
}

@Test
/**
* Tests the behavior of the format vendor parameter.
*/
public void testCapabilitiesFormat() throws Exception {
// the default and only wms standard conform mime type is application/vnd.ogc.wms_xml
MockHttpServletResponse response = getAsServletResponse("wms?request=getCapabilities&version=1.1.1");
assertThat(response.getStatus(), is(200));
assertThat(response.getContentType(), is("application/vnd.ogc.wms_xml"));
// request with the supported ime type text/xml
response = getAsServletResponse("wms?request=getCapabilities&version=1.1.1&format=text/xml");
assertThat(response.getStatus(), is(200));
assertThat(response.getContentType(), is("text/xml"));
// using an invalid mime type should throw an exception
response = getAsServletResponse("wms?request=getCapabilities&version=1.1.1&format=invalid");
assertThat(response.getStatus(), is(200));
assertThat(response.getContentType(), is("application/vnd.ogc.se_xml"));
// using an empty mime type should fall back to the default mime type
response = getAsServletResponse("wms?request=getCapabilities&version=1.1.1&format=");
assertThat(response.getStatus(), is(200));
assertThat(response.getContentType(), is("application/vnd.ogc.wms_xml"));
}

@Test
/**
* Test that all the supported mime types for the get capabilities
* operation are listed.
*/
public void testGetCapabilities() throws Exception {
// request a wms 1.1.1 capabilities document
MockHttpServletResponse response = getAsServletResponse("wms?request=getCapabilities&version=1.1.1");
assertThat(response.getStatus(), is(200));
assertThat(response.getContentType(), is("application/vnd.ogc.wms_xml"));
// parse the result as a xml document
InputStream content = new ByteArrayInputStream(response.getContentAsByteArray());
Document document = dom(content, true);
// check that all the available mime types are present
for(String mimeType : GetCapabilitiesTransformer.WMS_CAPS_AVAIL_MIME) {
assertXpathExists(String.format("//GetCapabilities[Format='%s']", mimeType), document);
}
}

@Test
public void testGetCapsContainsNoDisabledTypes() throws Exception {

Expand Down

0 comments on commit caa7cbd

Please sign in to comment.