Skip to content

Commit

Permalink
Merge pull request #11 from geonetwork/cswharvester-capabilitieserror…
Browse files Browse the repository at this point in the history
…handling

Improved handling of CSW GetCapabilities errors in POST request to try GET request
  • Loading branch information
josegar74 authored Oct 13, 2023
2 parents 6251ae7 + 12884ff commit 6163f21
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions services/csw-harvester/src/main/java/geocat/csw/csw/CSWEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,50 @@ public static boolean isXML(String doc) {

//----
public String GetCapabilities(String url) throws Exception {
String result;

try {
String result = GetCapabilitiesPOST(url);
if (!isXML(result)) {
throw new Exception("URL did not return XML!");
}
result = tryGetCapabilities(url, true);
} catch (Exception ex) {
result = tryGetCapabilities(url, false);
}

if (result.contains("<ExceptionReport")) {
throw new Exception("GetCapabilities exception: " + result);
}
return result;
}

return result;
} catch (Exception e) {
return GetCapabilitiesGET(url);
private String tryGetCapabilities(String url, boolean usePost) throws Exception {
String result = usePost?GetCapabilitiesPOST(url):GetCapabilitiesGET(url);
checkCapabilitiesResponse(result);

return result;
}

private void checkCapabilitiesResponse(String capabilitiesResponse) throws Exception {
if (!isXML(capabilitiesResponse)) {
throw new Exception("URL did not return XML!");
}

if (capabilitiesResponse.contains("<ows:ExceptionReport") ||
capabilitiesResponse.contains("<ExceptionReport")) {
throw new Exception("GetCapabilities exception: " + capabilitiesResponse);
}

if (!capabilitiesResponse.contains("<csw:Capabilities") ||
capabilitiesResponse.contains("<Capabilities")) {
throw new Exception("GetCapabilities response is not valid: " + capabilitiesResponse);
}
}

protected String GetCapabilitiesPOST(String url) throws Exception {
HttpResult result = retriever.retrieveXML("POST", url, GETCAP_XML, null, null);
if (result.getHttpCode() == 500)
if (result.getHttpCode() == 500) {
throw new Exception("attempting to get Cap with POST gives 500");
}

if (result.getHttpCode() == 404) {
throw new Exception("attempting to get Cap with POST gives 404");
}

return new String(result.getData());
}

Expand Down

0 comments on commit 6163f21

Please sign in to comment.