Skip to content

Commit f31eebd

Browse files
Fix errors in cayw endpoint (#13478)
* fix internal server issue upon request * fix content type according to the response * undo * remove throws and add default return of Formatter --------- Co-authored-by: iloveskittles <40355179+iloveskittles82@users.noreply.github.com>
1 parent 7efa8d2 commit f31eebd

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

jabsrv/src/main/java/org/jabref/http/server/cayw/CAYWResource.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import javafx.application.Platform;
2020

2121
import org.jabref.architecture.AllowedToUseAwt;
22+
import org.jabref.http.server.cayw.format.CAYWFormatter;
2223
import org.jabref.http.server.cayw.format.FormatterService;
2324
import org.jabref.http.server.cayw.gui.CAYWEntry;
2425
import org.jabref.http.server.cayw.gui.SearchDialog;
@@ -37,8 +38,6 @@
3738
import jakarta.ws.rs.BeanParam;
3839
import jakarta.ws.rs.GET;
3940
import jakarta.ws.rs.Path;
40-
import jakarta.ws.rs.Produces;
41-
import jakarta.ws.rs.core.MediaType;
4241
import jakarta.ws.rs.core.Response;
4342
import org.jspecify.annotations.Nullable;
4443
import org.slf4j.Logger;
@@ -64,14 +63,12 @@ public class CAYWResource {
6463
private ContextsToServe contextsToServe;
6564

6665
@GET
67-
@Produces(MediaType.TEXT_PLAIN)
6866
public Response getCitation(
6967
@BeanParam CAYWQueryParams queryParams
7068
) throws IOException, ExecutionException, InterruptedException {
7169
if (queryParams.isProbe()) {
7270
return Response.ok("ready").build();
7371
}
74-
7572
BibDatabaseContext databaseContext = getBibDatabaseContext(queryParams);
7673

7774
/* unused until DatabaseSearcher is fixed
@@ -111,17 +108,18 @@ public Response getCitation(
111108
}
112109

113110
// Format parameter handling
114-
String response = formatterService.format(queryParams, searchResults);
111+
CAYWFormatter formatter = formatterService.getFormatter(queryParams);
112+
String formattedResponse = formatter.format(queryParams, searchResults);
115113

116114
// Clipboard parameter handling
117115
if (queryParams.isClipboard()) {
118116
Toolkit toolkit = Toolkit.getDefaultToolkit();
119117
Clipboard systemClipboard = toolkit.getSystemClipboard();
120-
StringSelection strSel = new StringSelection(response);
118+
StringSelection strSel = new StringSelection(formattedResponse);
121119
systemClipboard.setContents(strSel, null);
122120
}
123121

124-
return Response.ok(response).build();
122+
return Response.ok(formattedResponse).type(formatter.getMediaType()).build();
125123
}
126124

127125
private BibDatabaseContext getBibDatabaseContext(CAYWQueryParams queryParams) throws IOException {
@@ -186,7 +184,7 @@ private BibDatabaseContext getDatabaseContextFromStream(InputStream inputStream)
186184
private synchronized void initializeGUI() {
187185
// TODO: Implement a better way to handle the window popup since this is a bit hacky.
188186
if (!initialized) {
189-
if (!contextsToServe.getContextsToServe().isEmpty()) {
187+
if (!contextsToServe.isEmpty()) {
190188
LOGGER.debug("Running inside JabRef UI, no need to initialize JavaFX for CAYW resource.");
191189
initialized = true;
192190
return;

jabsrv/src/main/java/org/jabref/http/server/cayw/format/BibLatexFormatter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.jabref.http.server.cayw.gui.CAYWEntry;
88
import org.jabref.model.entry.BibEntry;
99

10+
import jakarta.ws.rs.core.MediaType;
1011
import org.jvnet.hk2.annotations.Service;
1112

1213
@Service
@@ -17,6 +18,11 @@ public String getFormatName() {
1718
return "biblatex";
1819
}
1920

21+
@Override
22+
public MediaType getMediaType() {
23+
return MediaType.TEXT_PLAIN_TYPE;
24+
}
25+
2026
@Override
2127
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
2228
String command = queryParams.getCommand();

jabsrv/src/main/java/org/jabref/http/server/cayw/format/CAYWFormatter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import org.jabref.http.server.cayw.CAYWQueryParams;
66
import org.jabref.http.server.cayw.gui.CAYWEntry;
77

8+
import jakarta.ws.rs.core.MediaType;
9+
810
public interface CAYWFormatter {
911

1012
String getFormatName();
1113

14+
MediaType getMediaType();
15+
1216
String format(CAYWQueryParams caywQueryParams, List<CAYWEntry> caywEntries);
1317
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package org.jabref.http.server.cayw.format;
22

33
import java.util.HashMap;
4-
import java.util.List;
54
import java.util.Map;
65

76
import org.jabref.http.server.cayw.CAYWQueryParams;
8-
import org.jabref.http.server.cayw.gui.CAYWEntry;
97

108
import org.jvnet.hk2.annotations.Service;
119

1210
@Service
1311
public class FormatterService {
1412

13+
private static final String DEFAULT_FORMATTER = "biblatex";
1514
private final Map<String, CAYWFormatter> formatters;
1615

1716
public FormatterService() {
@@ -24,8 +23,7 @@ public void registerFormatter(CAYWFormatter formatter) {
2423
formatters.putIfAbsent(formatter.getFormatName(), formatter);
2524
}
2625

27-
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) throws IllegalArgumentException {
28-
CAYWFormatter formatter = formatters.get(queryParams.getFormat().toLowerCase());
29-
return formatter.format(queryParams, caywEntries);
26+
public CAYWFormatter getFormatter(CAYWQueryParams queryParams) {
27+
return formatters.getOrDefault(queryParams.getFormat().toLowerCase(), formatters.get(DEFAULT_FORMATTER));
3028
}
3129
}

jabsrv/src/main/java/org/jabref/http/server/cayw/format/SimpleJsonFormatter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.jabref.http.server.cayw.gui.CAYWEntry;
99

1010
import com.google.gson.Gson;
11+
import jakarta.ws.rs.core.MediaType;
1112
import org.jvnet.hk2.annotations.Service;
1213

1314
@Service
@@ -24,6 +25,11 @@ public String getFormatName() {
2425
return "simple-json";
2526
}
2627

28+
@Override
29+
public MediaType getMediaType() {
30+
return MediaType.APPLICATION_JSON_TYPE;
31+
}
32+
2733
@Override
2834
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
2935
List<SimpleJson> simpleJsons = caywEntries.stream()

0 commit comments

Comments
 (0)