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

integrate JabRef online #7796

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ application {
mainModule.set('org.jabref')
}

// TODO: Ugly workaround to temporarily ignore build errors to dependencies of latex2unicode
// These should be removed, as well as the files in the lib folder, as soon as they have valid module names
// TODO: Ugly workaround to temporarily ignore build errors (split packages or non-valid module names)
// These should be removed, as well as the files in the lib folder, as soon as these issues are fixed
modularity.patchModule("test", "fastparse_2.12-1.0.0.jar")
modularity.patchModule("test2", "fastparse-utils_2.12-1.0.0.jar")
modularity.patchModule("test3", "sourcecode_2.12-0.1.4.jar")
modularity.patchModule("apollo.runtime", "apollo-api-2.5.8.jar")


// Workaround for https://github.com/openjfx/javafx-gradle-plugin/issues/89
// See also https://github.com/java9-modularity/gradle-modules-plugin/issues/165
Expand Down Expand Up @@ -206,6 +208,11 @@ dependencies {
implementation (group: 'com.microsoft.azure', name: 'applicationinsights-logging-log4j2', version: '2.4.1') {
exclude module: "log4j-core"
}

implementation ("com.apollographql.apollo:apollo-runtime:2.5.8") {
exclude module: "apollo-normalized-cache"
exclude module: "apollo-api"
}

implementation 'com.vladsch.flexmark:flexmark:0.62.2'
implementation 'com.vladsch.flexmark:flexmark-ext-gfm-strikethrough:0.62.2'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.jabref.logic.remote.online;

public class JabrefOnlineException extends Exception {
}
112 changes: 112 additions & 0 deletions src/main/java/org/jabref/logic/remote/online/JabrefOnlineService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.jabref.logic.remote.online;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;

import org.jabref.logic.remote.online.dto.EntryDto;
import org.jabref.logic.remote.online.dto.FieldDto;
import org.jabref.logic.remote.online.dto.GetByIdGraphQLQuery;
import org.jabref.logic.remote.online.dto.GraphQLGetByIdResponseData;
import org.jabref.logic.remote.online.dto.GraphQLResponseDto;
import org.jabref.logic.remote.online.dto.GraphQLSaveResponseData;
import org.jabref.logic.remote.online.dto.SaveEntryGraphQLQuery;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.InternalField;
import org.jabref.model.entry.field.UnknownField;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JabrefOnlineService {

private static final String JABREF_ONLINE = "https://jabref-online.herokuapp.com/api";
private static final ObjectMapper MAPPER = new ObjectMapper();

public JabrefOnlineService() {
}

public BibEntry getSharedEntry(String id) throws JabrefOnlineException {
try {
URL url = new URL(JABREF_ONLINE);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("content-type", "application/json");
connection.setRequestMethod("POST");
connection.setDoOutput(true);

GetByIdGraphQLQuery getByIdGraphQLQuery = new GetByIdGraphQLQuery(id);

try (OutputStream os = connection.getOutputStream()) {
byte[] input = MAPPER.writeValueAsString(getByIdGraphQLQuery).getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}

EntryDto entryDto;
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}

GraphQLResponseDto<GraphQLGetByIdResponseData> graphQLResponseDto = MAPPER.readValue(response.toString(), new TypeReference<>() {
});
entryDto = graphQLResponseDto.getData().getEntryDto();
}

BibEntry bibEntry = new BibEntry();
for (FieldDto field : entryDto.getFields()) {
Field fieldFromDto = new UnknownField(field.getField());
bibEntry.setField(fieldFromDto, field.getValue());
}

bibEntry.setCitationKey(entryDto.getCitationKey());
return bibEntry;
} catch (IOException e) {
throw new JabrefOnlineException();
}
}

public String save(BibEntry entry) throws JabrefOnlineException {
try {
URL url = new URL(JABREF_ONLINE);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("content-Type", "application/json");
connection.setRequestMethod("POST");
connection.setDoOutput(true);

List<FieldDto> fieldDtos = entry.getFields().stream()
.filter(field -> !field.getName().equals(InternalField.KEY_FIELD.getName()))
.filter(field -> !field.getName().equals("entrytype"))
.map(field -> new FieldDto(field.getName(), entry.getField(field).orElse("")))
.collect(Collectors.toList());
EntryDto entryDto = new EntryDto(entry.getType().getName(), entry.getCitationKey().orElse(""), fieldDtos);
SaveEntryGraphQLQuery saveEntryGraphQLQuery = new SaveEntryGraphQLQuery(entryDto);

try (OutputStream os = connection.getOutputStream()) {
byte[] input = MAPPER.writeValueAsString(saveEntryGraphQLQuery).getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}

try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}

GraphQLResponseDto<GraphQLSaveResponseData> graphQLResponseDto = MAPPER.readValue(response.toString(), new TypeReference<>() {});
return graphQLResponseDto.getData().getAddUserDocumentRaw().getId();
}
} catch (IOException e) {
throw new JabrefOnlineException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jabref.logic.remote.online.dto;

public class AddEntryResponse {

private String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}
31 changes: 31 additions & 0 deletions src/main/java/org/jabref/logic/remote/online/dto/EntryDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jabref.logic.remote.online.dto;

import java.util.List;

public class EntryDto {

private String type;
private String citationKey;
private List<FieldDto> fields;

public EntryDto() {
}

public EntryDto(String type, String citationKey, List<FieldDto> fields) {
this.type = type;
this.citationKey = citationKey;
this.fields = fields;
}

public String getType() {
return type;
}

public String getCitationKey() {
return citationKey;
}

public List<FieldDto> getFields() {
return fields;
}
}
31 changes: 31 additions & 0 deletions src/main/java/org/jabref/logic/remote/online/dto/FieldDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jabref.logic.remote.online.dto;

public class FieldDto {

private String field;
private String value;

public FieldDto() {
}

public FieldDto(String field, String value) {
this.field = field;
this.value = value;
}

public String getField() {
return field;
}

public void setField(String field) {
this.field = field;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.jabref.logic.remote.online.dto;

import java.util.HashMap;
import java.util.Map;

import org.jabref.logic.remote.online.dto.EntryDto;
import org.jabref.logic.remote.online.dto.GraphQLQuery;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class GetByIdGraphQLQuery extends GraphQLQuery {
private static final String ID_GRAPH_QL_FIELD = "id";
private static final String OPERATION_NAME = "getDocumentById";

private static final String QUERY = "query getDocumentById($id: ID!) {" +
"getUserDocumentRaw(id: $id) {" +
" type" +
" citationKey" +
" fields { field, value }" +
" }" +
"}";

protected Map<String, String> variables = new HashMap<>();

public GetByIdGraphQLQuery(String id) {
super(OPERATION_NAME, QUERY);
variables.put(ID_GRAPH_QL_FIELD, id);
}

@JsonIgnore
public String getId() {
return variables.get(ID_GRAPH_QL_FIELD);
}

@JsonIgnore
public void setId(String id) {
variables.put(ID_GRAPH_QL_FIELD, id);
}

public Map<String, String> getVariables() {
return variables;
}

public void setVariables(Map<String, String> variables) {
this.variables = variables;
}

public static class Variable {
private EntryDto entryDto;

public Variable(EntryDto entryDto) {
this.entryDto = entryDto;
}

public EntryDto getEntryDto() {
return entryDto;
}

public void setEntryDto(EntryDto entryDto) {
this.entryDto = entryDto;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jabref.logic.remote.online.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

public class GraphQLGetByIdResponseData {

@JsonProperty("getUserDocumentRaw")
private EntryDto entryDto;

public EntryDto getEntryDto() {
return entryDto;
}

public void setEntryDto(EntryDto entryDto) {
this.entryDto = entryDto;
}
}
27 changes: 27 additions & 0 deletions src/main/java/org/jabref/logic/remote/online/dto/GraphQLQuery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jabref.logic.remote.online.dto;

import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

public abstract class GraphQLQuery {

protected final String operationName;
protected final String query;
protected Map<String, Object> variables;

public GraphQLQuery(String operationName, String query) {
this.operationName = operationName;
this.query = query;
}

public String getOperationName() {
return operationName;
}

public String getQuery() {
return query;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jabref.logic.remote.online.dto;

public class GraphQLResponseDto<T> {

private T data;

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jabref.logic.remote.online.dto;

public class GraphQLSaveResponseData {

private AddEntryResponse addUserDocumentRaw;

public AddEntryResponse getAddUserDocumentRaw() {
return addUserDocumentRaw;
}

public void setAddUserDocumentRaw(AddEntryResponse addUserDocumentRaw) {
this.addUserDocumentRaw = addUserDocumentRaw;
}
}
Loading