Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved handling of CSW GetCapabilities errors in POST request to try GET request #11

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading