Skip to content

Commit

Permalink
Merge pull request #394 from hwbllmnn/fix-wfs-interceptor
Browse files Browse the repository at this point in the history
Fix WFS interceptor
  • Loading branch information
weskamm authored Aug 19, 2020
2 parents 0aced55 + 777a55f commit 061ed76
Showing 1 changed file with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,23 @@ private void interceptGetCapabilities110And200(Document doc, String baseUrl, Str
@Override
public Response interceptGetCapabilities(MutableHttpServletRequest request, Response response) {
LOG.debug("Intercepting WFS GetCapabilities response");
String endpoint = request.getParameterIgnoreCase("CUSTOM_ENDPOINT");
if (endpoint == null) {
return null;
}
String proto = request.getHeader("x-forwarded-proto");
String requestHost = request.getHeader("x-forwarded-host");
// fallbacks in case SHOGun is not behind a reverse proxy
if (StringUtils.isEmpty(proto)) {
proto = request.getScheme();
}
if (StringUtils.isEmpty(requestHost)) {
requestHost = request.getHeader("x-forwarded-for");
}
if (StringUtils.isEmpty(requestHost)) {
requestHost = request.getServerName();
}
String baseUrl = proto + "://" + requestHost + request.getContextPath() + "/geoserver.action";
String baseUrl = proto + "://" + requestHost + request.getContextPath() + "/geoserver.action/" + endpoint;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
Expand Down Expand Up @@ -134,7 +141,7 @@ public Response interceptGetCapabilities(MutableHttpServletRequest request, Resp
private void removeLayers(Document doc) throws XPathExpressionException {
// get all layers allowed for this user in order to filter out not allowed ones
List<Layer> layers = layerService.findAll();
List<String> layerNames = new ArrayList<String>();
List<String> layerNames = new ArrayList<>();
for (Layer layer : layers) {
if (layer.getSource() instanceof ImageWmsLayerDataSource) {
ImageWmsLayerDataSource source = (ImageWmsLayerDataSource) layer.getSource();
Expand All @@ -150,14 +157,22 @@ private void removeLayers(Document doc) throws XPathExpressionException {
addNamespace("wfs", "http://www.opengis.net/wfs"));
NodeList list = (NodeList) xpath.compile("//wfs:FeatureType/wfs:Name").evaluate(doc.getDocumentElement(), NODESET);
List<Element> toRemove = new ArrayList<>();
determineFeatureTypeNodesToRemove(layerNames, list, toRemove);
xpath.setNamespaceContext(CommonNamespaces.getNamespaceContext().
addNamespace("wfs", "http://www.opengis.net/wfs/2.0"));
list = (NodeList) xpath.compile("//wfs:FeatureType/wfs:Name").evaluate(doc.getDocumentElement(), NODESET);
determineFeatureTypeNodesToRemove(layerNames, list, toRemove);
toRemove.forEach(element -> element.getParentNode().removeChild(element));
}

private void determineFeatureTypeNodesToRemove(List<String> layerNames, NodeList list, List<Element> toRemove) {
for (int i = 0; i < list.getLength(); ++i) {
Element name = (Element) list.item(i);
String str = name.getTextContent();
if (!layerNames.contains(str)) {
toRemove.add((Element) name.getParentNode());
}
}
toRemove.forEach(element -> element.getParentNode().removeChild(element));
}

@Override
Expand Down Expand Up @@ -185,7 +200,7 @@ public Response interceptDescribeFeatureType(MutableHttpServletRequest request,
private void removeFeatureTypes(Document doc) throws XPathExpressionException {
// get all layers allowed for this user in order to filter out not allowed ones
List<Layer> layers = layerService.findAll();
List<String> layerNames = new ArrayList<String>();
List<String> layerNames = new ArrayList<>();
for (Layer layer : layers) {
if (layer.getSource() instanceof ImageWmsLayerDataSource) {
ImageWmsLayerDataSource source = (ImageWmsLayerDataSource) layer.getSource();
Expand Down Expand Up @@ -219,15 +234,15 @@ private void removeFeatureTypes(Document doc) throws XPathExpressionException {
String str = name.getAttribute("name");
str = str.substring(0, str.lastIndexOf("Type"));
if (!layerNames.contains(str)) {
toRemove.add((Element) name);
toRemove.add(name);
}
}
list = (NodeList) xpath.compile("/xsd:schema/xsd:element").evaluate(doc.getDocumentElement(), NODESET);
for (int i = 0; i < list.getLength(); ++i) {
Element name = (Element) list.item(i);
String str = name.getAttribute("name");
if (!layerNames.contains(str)) {
toRemove.add((Element) name);
toRemove.add(name);
}
}
toRemove.forEach(element -> element.getParentNode().removeChild(element));
Expand Down

0 comments on commit 061ed76

Please sign in to comment.