Skip to content

Commit f9f28d6

Browse files
fsteegsol
authored andcommitted
2 parents 78dc48b + 68ce911 commit f9f28d6

File tree

5 files changed

+57
-31
lines changed

5 files changed

+57
-31
lines changed

app/controllers/HomeController.java

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ public Result authority(String id, String format) {
178178
Format responseFormat = Accept.formatFor(format, request().acceptedTypes());
179179
if (responseFormat == null || responseFormat == Accept.Format.JSON_LINES
180180
|| format != null && format.contains(":")) {
181-
return unsupportedMediaType(String.format("Unsupported for single resource: format=%s, accept=%s", format,
182-
request().acceptedTypes()));
181+
return unsupportedMediaType(views.html.error.render(id, String.format(
182+
"Unsupported for single resource: format=%s, accept=%s", format, request().acceptedTypes())));
183183
}
184184
try {
185185
switch (responseFormat) {
@@ -199,7 +199,7 @@ public Result authority(String id, String format) {
199199
}
200200
} catch (Exception e) {
201201
Logger.error("Could not create response", e);
202-
return internalServerError(e.getMessage());
202+
return internalServerError(views.html.error.render(id, e.getMessage()));
203203
}
204204
}
205205

@@ -297,31 +297,37 @@ public Result search(String q, String filter, int from, int size, String format)
297297
Format responseFormat = Accept.formatFor(format, request().acceptedTypes());
298298
if (responseFormat == null || Stream.of(RdfFormat.values()).map(RdfFormat::getParam)
299299
.anyMatch(f -> f.equals(responseFormat.queryParamString))) {
300-
return unsupportedMediaType(
301-
String.format("Unsupported for search: format=%s, accept=%s", format, request().acceptedTypes()));
300+
return unsupportedMediaType(views.html.error.render(q,
301+
String.format("Unsupported for search: format=%s, accept=%s", format, request().acceptedTypes())));
302302
}
303303
String queryString = (q == null || q.isEmpty()) ? "*" : q;
304-
SearchResponse response = index.query(queryString, filter, from, size);
305-
response().setHeader("Access-Control-Allow-Origin", "*");
306-
String[] formatAndConfig = format == null ? new String[] {} : format.split(":");
307-
boolean returnSuggestions = formatAndConfig.length == 2;
308-
if (returnSuggestions) {
309-
List<Map<String, Object>> hits = Arrays.asList(response.getHits().getHits()).stream()
310-
.map(hit -> hit.getSource()).collect(Collectors.toList());
311-
return withCallback(toSuggestions(Json.toJson(hits), formatAndConfig[1]));
312-
}
313-
switch (responseFormat) {
314-
case HTML: {
315-
return htmlSearch(q, filter, from, size, responseFormat.queryParamString, response);
316-
}
317-
case JSON_LINES: {
318-
response().setHeader("Content-Disposition",
319-
String.format("attachment; filename=\"lobid-gnd-bulk-%s.jsonl\"", System.currentTimeMillis()));
320-
return jsonLines(queryString, filter, response);
321-
}
322-
default: {
323-
return ok(returnAsJson(q, response)).as(config("index.content"));
324-
}
304+
try {
305+
SearchResponse response = index.query(queryString, filter, from, size);
306+
response().setHeader("Access-Control-Allow-Origin", "*");
307+
String[] formatAndConfig = format == null ? new String[] {} : format.split(":");
308+
boolean returnSuggestions = formatAndConfig.length == 2;
309+
if (returnSuggestions) {
310+
List<Map<String, Object>> hits = Arrays.asList(response.getHits().getHits()).stream()
311+
.map(hit -> hit.getSource()).collect(Collectors.toList());
312+
return withCallback(toSuggestions(Json.toJson(hits), formatAndConfig[1]));
313+
}
314+
switch (responseFormat) {
315+
case HTML: {
316+
return htmlSearch(q, filter, from, size, responseFormat.queryParamString, response);
317+
}
318+
case JSON_LINES: {
319+
response().setHeader("Content-Disposition",
320+
String.format("attachment; filename=\"lobid-gnd-bulk-%s.jsonl\"", System.currentTimeMillis()));
321+
return jsonLines(queryString, filter, response);
322+
}
323+
default: {
324+
return ok(returnAsJson(q, response)).as(config("index.content"));
325+
}
326+
}
327+
} catch (Throwable t) {
328+
String message = t.getMessage() + (t.getCause() != null ? ", cause: " + t.getCause().getMessage() : "");
329+
Logger.error("Error: {}", message);
330+
return internalServerError(views.html.error.render(q, "Error: " + message));
325331
}
326332
}
327333

@@ -361,7 +367,7 @@ public ByteString next() {
361367

362368
private Result htmlSearch(String q, String type, int from, int size, String format, SearchResponse response) {
363369
return ok(views.html.search.render(q, type, from, size, returnAsJson(q, response),
364-
response.getHits().getTotalHits()));
370+
response == null ? 0 : response.getHits().getTotalHits()));
365371
}
366372

367373
private static Result withCallback(final String json) {
@@ -434,6 +440,9 @@ public static String currentUri() {
434440
}
435441

436442
private static String returnAsJson(String q, SearchResponse queryResponse) {
443+
if (queryResponse == null) {
444+
return Json.newObject().toString();
445+
}
437446
List<Map<String, Object>> hits = Arrays.asList(queryResponse.getHits().getHits()).stream()
438447
.map(hit -> hit.getSource()).collect(Collectors.toList());
439448
ObjectNode object = Json.newObject();

app/views/error.scala.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@* Copyright 2018 Fabian Steeg, hbz. Licensed under the GPLv2 *@
2+
3+
@(q: String, message: String)
4+
5+
@main(q, "Error") {
6+
<div class="panel panel-danger footer">
7+
<div class='panel-heading'>Fehler</div>
8+
<div class='panel-body'>@message</div>
9+
</div>
10+
}

test/controllers/AcceptIntegrationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public static Collection<Object[]> data() {
4949
{ fakeRequest(GET, "/gnd/search?q=*").header("Accept", "*/*"), /*->*/ "application/json" },
5050
{ fakeRequest(GET, "/gnd/search?q=*&format="), /*->*/ "application/json" },
5151
{ fakeRequest(GET, "/gnd/search?q=*&format=json"), /*->*/ "application/json" },
52-
{ fakeRequest(GET, "/gnd/search?q=*&format=whatever"), /*->*/ "text/plain" },
53-
{ fakeRequest(GET, "/gnd/search?q=*").header("Accept", "text/plain"), /*->*/ "text/plain" },
52+
{ fakeRequest(GET, "/gnd/search?q=*&format=whatever"), /*->*/ "text/html" },
53+
{ fakeRequest(GET, "/gnd/search?q=*").header("Accept", "text/plain"), /*->*/ "text/html" },
5454
// search, bulk format: JSON lines
5555
{ fakeRequest(GET, "/gnd/search?q=*").header("Accept", "application/x-jsonlines"), /*->*/ "application/x-jsonlines" },
5656
{ fakeRequest(GET, "/gnd/search?format=jsonl"), /*->*/ "application/x-jsonlines" },
@@ -65,8 +65,8 @@ public static Collection<Object[]> data() {
6565
{ fakeRequest(GET, "/gnd/118820591"), /*->*/ "application/json" },
6666
{ fakeRequest(GET, "/gnd/118820591?format="), /*->*/ "application/json" },
6767
{ fakeRequest(GET, "/gnd/118820591?format=json"), /*->*/ "application/json" },
68-
{ fakeRequest(GET, "/gnd/118820591?format=whatever"), /*->*/ "text/plain" },
69-
{ fakeRequest(GET, "/gnd/118820591?format=whatever").header("Accept", "text/html"), /*->*/ "text/plain" },
68+
{ fakeRequest(GET, "/gnd/118820591?format=whatever"), /*->*/ "text/html" },
69+
{ fakeRequest(GET, "/gnd/118820591?format=whatever").header("Accept", "text/html"), /*->*/ "text/html" },
7070
{ fakeRequest(GET, "/gnd/118820591").header("Accept", "text/plain"), /*->*/ "application/n-triples" },
7171
// get, other formats as query param:
7272
{ fakeRequest(GET, "/gnd/118820591?format=html"), /*->*/ "text/html" },

test/controllers/HomeControllerTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static Collection<Object[]> data() {
3333
{ routes.HomeController.search("*", "", 0, 10, "json").toString(), Status.OK },
3434
{ routes.HomeController.search("*", "", 0, 10, "jsonl").toString(), Status.OK },
3535
{ routes.HomeController.search("*", "", 0, 10, "json:suggest").toString(), Status.OK },
36+
{ routes.HomeController.search("++test", "", 0, 10, "html").toString(), Status.INTERNAL_SERVER_ERROR },
3637
{ routes.HomeController.search("*", "", 0, 10, "jsonfoo").toString(), Status.UNSUPPORTED_MEDIA_TYPE },
3738
{ routes.HomeController.search("*", "", 0, 10, "ttl").toString(), Status.UNSUPPORTED_MEDIA_TYPE },
3839
{ routes.HomeController.search("*", "", 0, 10, "rdf").toString(), Status.UNSUPPORTED_MEDIA_TYPE },

test/modules/IndexQueryTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.FileNotFoundException;
88
import java.util.Set;
99

10+
import org.elasticsearch.action.search.SearchPhaseExecutionException;
1011
import org.elasticsearch.action.search.SearchResponse;
1112
import org.junit.Assert;
1213
import org.junit.Test;
@@ -122,4 +123,9 @@ public void testNoStemming() {
122123
Assert.assertEquals(1, index.query("namenlosen").getHits().getTotalHits());
123124
}
124125

126+
@Test(expected = SearchPhaseExecutionException.class)
127+
public void testInvalidQuery() {
128+
index.query("++test");
129+
}
130+
125131
}

0 commit comments

Comments
 (0)