From 1cf15cb01db5b45181301324daee82b9795b333b Mon Sep 17 00:00:00 2001 From: Andre Henn Date: Mon, 18 Nov 2024 12:36:51 +0100 Subject: [PATCH 1/2] fix: extract typename in WFS Transaction insert queries correctly --- .../interceptor/enumeration/OgcEnum.java | 2 +- .../servlet/MutableHttpServletRequest.java | 6 +- .../MutableHttpServletRequestTest.java | 154 ++++++++++++++++++ 3 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 shogun-gs-interceptor/src/test/java/de/terrestris/shogun/interceptor/servlet/MutableHttpServletRequestTest.java diff --git a/shogun-gs-interceptor/src/main/java/de/terrestris/shogun/interceptor/enumeration/OgcEnum.java b/shogun-gs-interceptor/src/main/java/de/terrestris/shogun/interceptor/enumeration/OgcEnum.java index caf77e36a..6c63b23b6 100644 --- a/shogun-gs-interceptor/src/main/java/de/terrestris/shogun/interceptor/enumeration/OgcEnum.java +++ b/shogun-gs-interceptor/src/main/java/de/terrestris/shogun/interceptor/enumeration/OgcEnum.java @@ -259,7 +259,7 @@ public String toString() { } /** - * A enum type for the allowed endPoint format. + * An enum type for the allowed endPoint format. */ public enum EndPoint { LAYERS("LAYERS"), diff --git a/shogun-gs-interceptor/src/main/java/de/terrestris/shogun/interceptor/servlet/MutableHttpServletRequest.java b/shogun-gs-interceptor/src/main/java/de/terrestris/shogun/interceptor/servlet/MutableHttpServletRequest.java index 516feee56..de658d782 100644 --- a/shogun-gs-interceptor/src/main/java/de/terrestris/shogun/interceptor/servlet/MutableHttpServletRequest.java +++ b/shogun-gs-interceptor/src/main/java/de/terrestris/shogun/interceptor/servlet/MutableHttpServletRequest.java @@ -166,13 +166,17 @@ public static String getRequestParameterValue(HttpServletRequest httpServletRequ if (StringUtils.isEmpty(value)) { value = OgcXmlUtil.getPathInDocument(document, "//@typeName | //@typeNames"); } + if (StringUtils.isEmpty(value)) { + log.debug("Check for first feature type in WFS-T insert request"); + value = OgcXmlUtil.getPathInDocument(document, "name(//Transaction/Insert/*[1])"); + } } } else { log.error("No body found in the request."); } } - log.trace("Found the request parameter value: " + value); + log.trace("Found the request parameter value: {}", value); return value; } diff --git a/shogun-gs-interceptor/src/test/java/de/terrestris/shogun/interceptor/servlet/MutableHttpServletRequestTest.java b/shogun-gs-interceptor/src/test/java/de/terrestris/shogun/interceptor/servlet/MutableHttpServletRequestTest.java new file mode 100644 index 000000000..ef972d15a --- /dev/null +++ b/shogun-gs-interceptor/src/test/java/de/terrestris/shogun/interceptor/servlet/MutableHttpServletRequestTest.java @@ -0,0 +1,154 @@ +package de.terrestris.shogun.interceptor.servlet; + +import de.terrestris.shogun.interceptor.enumeration.OgcEnum; +import de.terrestris.shogun.interceptor.exception.InterceptorException; +import jakarta.servlet.http.HttpServletRequest; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +class MutableHttpServletRequestTest { + + private final String featureTypeName = "TEST:MY_FEATURE_TYPE"; + private final String namespaceUrl = "http://localhost/TEST"; + + private HttpServletRequest request; + + @BeforeEach + public void setUp() { + request = Mockito.mock(HttpServletRequest.class); + } + + @Test + void extractWfsGetFeatureEndpointCorrectly() throws IOException, InterceptorException { + String xmlData = """ + + + + NUM1909 + + + """.formatted(featureTypeName, namespaceUrl); + + InputStream xmlInputStream = new ByteArrayInputStream(xmlData.getBytes()); + when(request.getInputStream()).thenReturn(new MockServletInputStream(xmlInputStream)); + + String requestEndPoint = MutableHttpServletRequest.getRequestParameterValue(request, OgcEnum.EndPoint.getAllValues()); + assertEquals(featureTypeName, requestEndPoint); + } + + @Test + void extractWfsTransactionUpdateEndpointCorrectly() throws IOException, InterceptorException { + String xmlData = """ + + + + + GEOMETRIE + + 373678.86587162 5606983.66695109 + + + + ID1909 + + + + + """.formatted(featureTypeName, namespaceUrl); + + InputStream xmlInputStream = new ByteArrayInputStream(xmlData.getBytes()); + when(request.getInputStream()).thenReturn(new MockServletInputStream(xmlInputStream)); + + String requestEndPoint = MutableHttpServletRequest.getRequestParameterValue(request, OgcEnum.EndPoint.getAllValues()); + assertEquals(featureTypeName, requestEndPoint); + } + + @Test + void extractWfsTransactionInsertEndpointCorrectly() throws IOException, InterceptorException { + String xmlData = """ + + + + <%s xmlns:TEST="%s"> + 1909 + + + + + 373740.32587162 5606976.66695109 + + + + + 373721.70587162 5606971.34695109 + + + + + + + + """.formatted(featureTypeName, namespaceUrl, featureTypeName); + + InputStream xmlInputStream = new ByteArrayInputStream(xmlData.getBytes()); + when(request.getInputStream()).thenReturn(new MockServletInputStream(xmlInputStream)); + + String requestEndPoint = MutableHttpServletRequest.getRequestParameterValue(request, OgcEnum.EndPoint.getAllValues()); + assertEquals(featureTypeName, requestEndPoint); + } + + private static class MockServletInputStream extends jakarta.servlet.ServletInputStream { + private final InputStream inputStream; + + public MockServletInputStream(InputStream inputStream) { + this.inputStream = inputStream; + } + + @Override + public int read() { + try { + return inputStream.read(); + } catch (Exception e) { + return -1; + } + } + + @Override + public boolean isFinished() { + return false; + } + + @Override + public boolean isReady() { + return true; + } + + @Override + public void setReadListener(jakarta.servlet.ReadListener readListener) { + } + } +} From ac7b4e1196306d690d1678a7ddaad5ae9184e333 Mon Sep 17 00:00:00 2001 From: Andre Henn Date: Mon, 18 Nov 2024 12:58:46 +0100 Subject: [PATCH 2/2] fix: adds missing lecense header --- .../servlet/MutableHttpServletRequestTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/shogun-gs-interceptor/src/test/java/de/terrestris/shogun/interceptor/servlet/MutableHttpServletRequestTest.java b/shogun-gs-interceptor/src/test/java/de/terrestris/shogun/interceptor/servlet/MutableHttpServletRequestTest.java index ef972d15a..938ed315e 100644 --- a/shogun-gs-interceptor/src/test/java/de/terrestris/shogun/interceptor/servlet/MutableHttpServletRequestTest.java +++ b/shogun-gs-interceptor/src/test/java/de/terrestris/shogun/interceptor/servlet/MutableHttpServletRequestTest.java @@ -1,3 +1,20 @@ +/* SHOGun, https://terrestris.github.io/shogun/ + * + * Copyright © 2020-present terrestris GmbH & Co. KG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package de.terrestris.shogun.interceptor.servlet; import de.terrestris.shogun.interceptor.enumeration.OgcEnum;