Fixed parsing error response from KorAP API and updated tests.
Change-Id: I26ff028c701760ee2345bf41e1f50affd444fbc6
diff --git a/ChangeLog b/ChangeLog
index 2aaaf03..697b74c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,6 @@
+20/11/2020
+ - Fixed parsing error response from KorAP API and updated tests
+
20/12/2019
- Added dynamic acquisition of resource info using KorAP API.
diff --git a/pom.xml b/pom.xml
index 1a655ea..809b3ba 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
<groupId>de.mannheim.ids</groupId>
<artifactId>KorapSRU</artifactId>
- <version>1.0.3-SNAPSHOT</version>
+ <version>1.0.4-SNAPSHOT</version>
<packaging>war</packaging>
<name>KorapSRU</name>
diff --git a/src/main/java/de/ids_mannheim/korap/sru/KorapClient.java b/src/main/java/de/ids_mannheim/korap/sru/KorapClient.java
index 347aaae..fccc24a 100644
--- a/src/main/java/de/ids_mannheim/korap/sru/KorapClient.java
+++ b/src/main/java/de/ids_mannheim/korap/sru/KorapClient.java
@@ -178,18 +178,20 @@
try {
result = objectMapper.readValue(jsonStream, KorapResult.class);
}
- catch (IOException e) {
- throw new IOException("Failed processing response.");
- }
+// catch (IOException e) {
+// throw new IOException("Failed processing response.");
+// }
finally {
jsonStream.close();
}
}
- catch (IOException e) {
- throw new IOException("Failed executing HTTP request.", e);
- }
+// catch (IOException e) {
+// throw new IOException("Failed executing HTTP request.", e);
+// }
finally {
- response.close();
+ if (response != null) {
+ response.close();
+ }
}
return result;
@@ -225,23 +227,35 @@
InputStream is = response.getEntity().getContent();
JsonNode node = objectMapper.readTree(is);
- String message = node.get("error").textValue();
- String[] errorItems;
- if (message.contains("SRU diagnostic")) {
- errorItems = message.split(":", 2);
- errorItems[0] = errorItems[0].replace("SRU diagnostic ", "");
- errorItems[1] = errorItems[1].trim();
+ node = node.at("/errors");
+ String errorCode="", errorMessage="";
+ for (int i=0; i<node.size(); i++){
+ errorCode = node.get(i).get(0).asText();
+ errorMessage= node.get(i).get(1).asText();
+ if (errorCode.contains("SRU diagnostic")){
+ errorCode = errorCode.replace("SRU diagnostic ", "");
+ }
+ // see de.ids_mannheim.korap.query.serialize.util.StatusCodes
+ else if (errorCode.equals("301")){
+ errorCode="7"; // Mandatory parameter not supplied
+ }
+ else if (errorCode.equals("302") || errorCode.equals("303")
+ || errorCode.equals("304") || errorCode.equals("305")) {
+ errorCode = "10"; // Query syntax error
+ }
+ else if (errorCode.equals("399")){
+ errorCode="47"; // Cannot process query; reason unknown
+ }
+ else if (errorCode.equals("306") || errorCode.equals("307") ){
+ errorCode = "6"; // Unsupported parameter value
+ }
+ else{
+ errorCode = "1"; // General system error
+ }
}
- else if (message.contains("not a supported query language")) {
- errorItems = new String[] { "4",
- "KorAP does not support the query language." };
- }
- else {
- errorItems = new String[] { "1", message };
- }
-
- throw new HttpResponseException(Integer.parseInt(errorItems[0]),
- errorItems[1]);
+ System.out.println(errorCode +" "+errorMessage);
+ throw new HttpResponseException(Integer.parseInt(errorCode),
+ errorMessage);
}
/**
diff --git a/src/main/java/de/ids_mannheim/korap/sru/KorapSRUSearchResultSet.java b/src/main/java/de/ids_mannheim/korap/sru/KorapSRUSearchResultSet.java
index 25133f0..2a711b1 100644
--- a/src/main/java/de/ids_mannheim/korap/sru/KorapSRUSearchResultSet.java
+++ b/src/main/java/de/ids_mannheim/korap/sru/KorapSRUSearchResultSet.java
@@ -171,6 +171,7 @@
saxParser.parse(is, annotationHandler);
}
catch (SAXException | IOException | URISyntaxException e) {
+ logger.error(e.getMessage());
throw new XMLStreamException(e);
}
@@ -200,6 +201,7 @@
if (dataviews.contains("adv")) {
helper.writeAdvancedDataView(writer);
}
+ helper.reset();
}
/**
diff --git a/src/test/java/de/ids_mannheim/korap/test/FCSQLRequestTest.java b/src/test/java/de/ids_mannheim/korap/test/FCSQLRequestTest.java
index ffb7675..8e56007 100644
--- a/src/test/java/de/ids_mannheim/korap/test/FCSQLRequestTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/FCSQLRequestTest.java
@@ -2,8 +2,10 @@
import static org.junit.Assert.assertEquals;
+import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
@@ -62,8 +64,9 @@
builder.addParameters(params);
URI uri = builder.build();
- assertEquals(korapSruUri + "?operation=startRetrieve&query="
- + "%5Btt%3Alemma%3D%22.*bar%22%5D&queryType=fcs",
+ assertEquals(
+ korapSruUri + "?operation=startRetrieve&query="
+ + "%5Btt%3Alemma%3D%22.*bar%22%5D&queryType=fcs",
uri.toString());
HttpGet request = new HttpGet(uri);
@@ -81,7 +84,7 @@
assertEquals("134", nodeList.item(0).getTextContent());
response.close();
}
-
+
@Test
public void testLemmaRegex () throws URISyntaxException, IOException,
SAXException, ParserConfigurationException {
@@ -95,8 +98,7 @@
URI uri = builder.build();
assertEquals(korapSruUri + "?operation=startRetrieve&query=%5Blemma"
- + "%3D%22.*bar%22%5D&queryType=fcs",
- uri.toString());
+ + "%3D%22.*bar%22%5D&queryType=fcs", uri.toString());
HttpGet request = new HttpGet(uri);
CloseableHttpClient client = HttpClients.createDefault();
@@ -114,4 +116,43 @@
response.close();
}
+ @Test
+ public void testUnsupportedLayer () throws URISyntaxException, IOException,
+ SAXException, ParserConfigurationException {
+ List<NameValuePair> params = new ArrayList<NameValuePair>();
+ params.add(new BasicNameValuePair("operation", "startRetrieve"));
+ params.add(new BasicNameValuePair("query", "[unknown=\"Feuer\"]"));
+ params.add(new BasicNameValuePair("queryType", "fcs"));
+
+ URIBuilder builder = new URIBuilder(korapSruUri);
+ builder.addParameters(params);
+
+ URI uri = builder.build();
+ assertEquals(
+ korapSruUri + "?operation=startRetrieve&query="
+ + "%5Bword%3D%22Feuer%22%5D&queryType=fcs",
+ uri.toString());
+
+ HttpGet request = new HttpGet(uri);
+ CloseableHttpClient client = HttpClients.createDefault();
+ CloseableHttpResponse response = null;
+ response = client.execute(request);
+
+ assertEquals(200, response.getStatusLine().getStatusCode());
+
+ InputStream is = response.getEntity().getContent();
+// BufferedReader br = new BufferedReader(new InputStreamReader(is));
+// String line = null;
+// while ((line=br.readLine())!=null){
+// System.out.println(line);
+// }
+
+ Document document = documentBuilder.parse(is);
+ NodeList nodeList = document.getElementsByTagName("diag:message");
+ assertEquals(
+ "status code: 6, reason phrase: Layer unknown is unsupported.",
+ nodeList.item(0).getTextContent());
+ response.close();
+ }
+
}
diff --git a/src/test/java/de/ids_mannheim/korap/test/KorapClientTest.java b/src/test/java/de/ids_mannheim/korap/test/KorapClientTest.java
index 26e3311..dee9022 100644
--- a/src/test/java/de/ids_mannheim/korap/test/KorapClientTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/KorapClientTest.java
@@ -6,13 +6,11 @@
import java.net.URISyntaxException;
import org.apache.http.client.HttpResponseException;
-import org.junit.Ignore;
import org.junit.Test;
-import com.fasterxml.jackson.databind.JsonNode;
-
import de.ids_mannheim.korap.sru.KorapClient;
import de.ids_mannheim.korap.sru.KorapMatch;
+import de.ids_mannheim.korap.sru.KorapResource;
import de.ids_mannheim.korap.sru.KorapResult;
import de.ids_mannheim.korap.sru.QueryLanguage;
@@ -35,7 +33,7 @@
private KorapMatch match;
public KorapClientTest () {
- c = new KorapClient("http://localhost:8089/api/", 25, 50);
+ c = new KorapClient("http://localhost:8089/api/v1.0", 25, 50);
}
@Test
@@ -92,12 +90,11 @@
}
@Test
- @Ignore
public void testRetrieveResource () throws HttpResponseException, Exception {
- JsonNode resources = c.retrieveResources();
-
- //assertEquals(1, resources.size());
- assertEquals("Wikipedia 2015", resources.get(0).get("name").asText());
- assertEquals("Deutsche Wikipedia 2015", resources.get(0).get("description").asText());
+ KorapResource[] resources = c.retrieveResources();
+ assertEquals(3, resources.length);
+ assertEquals("WPD17", resources[0].getResourceId());
+ assertEquals("WDD17", resources[1].getResourceId());
+ assertEquals("WUD17", resources[2].getResourceId());
}
}