129 lines
7.0 KiB
Java
129 lines
7.0 KiB
Java
package eu.eudat.logic.proxy.fetching;
|
|
|
|
import com.jayway.jsonpath.DocumentContext;
|
|
import eu.eudat.logic.proxy.config.DataUrlConfiguration;
|
|
import eu.eudat.logic.proxy.config.ExternalUrlCriteria;
|
|
import eu.eudat.logic.proxy.fetching.entities.Results;
|
|
import net.minidev.json.JSONArray;
|
|
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class RemoteFetcherUtils {
|
|
|
|
public static Results getFromJsonWithSource(DocumentContext jsonContext, DataUrlConfiguration jsonDataPath) {
|
|
return new Results(jsonContext.read(jsonDataPath.getPath()
|
|
+ "[" + jsonDataPath.getFieldsUrlConfiguration().getName() + "," + jsonDataPath.getFieldsUrlConfiguration().getDescription()
|
|
+ "," + jsonDataPath.getFieldsUrlConfiguration().getUri() + "," + jsonDataPath.getFieldsUrlConfiguration().getId()
|
|
+ "," + jsonDataPath.getFieldsUrlConfiguration().getSource() + "]"),
|
|
new HashMap<>(1, 1));
|
|
}
|
|
|
|
public static Results getFromJsonWithParsedData(DocumentContext jsonContext, DataUrlConfiguration jsonDataPath) {
|
|
Results results = new Results(jsonContext.read(jsonDataPath.getPath()
|
|
+ "[" + jsonDataPath.getFieldsUrlConfiguration().getName()
|
|
+ "," + jsonDataPath.getFieldsUrlConfiguration().getId() + "]"),
|
|
new HashMap<>(1, 1));
|
|
List<Map<String, String>> fixedResults = results.getResults().stream().map(item -> {
|
|
for (int i = 0; i < 2; i++) {
|
|
String id;
|
|
if (i == 0) {
|
|
id = jsonDataPath.getFieldsUrlConfiguration().getId().replace("'", "");
|
|
} else {
|
|
id = jsonDataPath.getFieldsUrlConfiguration().getName().replace("'", "");
|
|
}
|
|
if (!(item.get(id) instanceof String)) {
|
|
Object obj = item.get(id);
|
|
if (obj instanceof JSONArray) {
|
|
JSONArray jarr = (JSONArray) obj;
|
|
if (jarr.get(0) instanceof String) {
|
|
item.put(id, jarr.get(0).toString());
|
|
} else {
|
|
for (int j = 0; j < jarr.size(); j++) {
|
|
mapToMap(id, (Map<String, String>)jarr.get(j), item, i == 1);
|
|
}
|
|
}
|
|
} else {
|
|
if (obj instanceof Map) {
|
|
mapToMap(id, (Map<String, String>) obj, item, i == 1);
|
|
} else if (obj != null){
|
|
item.put(id, obj.toString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return item;
|
|
}).collect(Collectors.toList());
|
|
|
|
return new Results(fixedResults, new HashMap<>(1, 1));
|
|
}
|
|
|
|
public static Results getFromJsonWithRecursiveFetching(DocumentContext jsonContext, DataUrlConfiguration jsonDataPath, RemoteFetcher remoteFetcher, String requestBody, String requestType) {
|
|
Results results = new Results(jsonContext.read(jsonDataPath.getPath()
|
|
+ "[" + jsonDataPath.getFieldsUrlConfiguration().getPath()
|
|
+ "," + jsonDataPath.getFieldsUrlConfiguration().getHost() + "]"),
|
|
new HashMap<>(1, 1));
|
|
|
|
List<Map<String, String>> multiResults = results.getResults().stream().map(result -> {
|
|
ExternalUrlCriteria externalUrlCriteria = new ExternalUrlCriteria();
|
|
externalUrlCriteria.setPath(result.get("path"));
|
|
externalUrlCriteria.setHost(result.get("host"));
|
|
String replacedPath = remoteFetcher.replaceCriteriaOnUrl(jsonDataPath.getUrlConfiguration().getUrl(), externalUrlCriteria, jsonDataPath.getUrlConfiguration().getFirstpage());
|
|
return remoteFetcher.getResultsFromUrl(replacedPath, jsonDataPath.getUrlConfiguration().getData(), jsonDataPath.getUrlConfiguration().getData().getPath(), jsonDataPath.getUrlConfiguration().getContentType(), requestBody, requestType);
|
|
}).filter(Objects::nonNull).map(results1 -> results1.getResults().get(0)).collect(Collectors.toList());
|
|
return new Results(multiResults, new HashMap<>(1, 1));
|
|
}
|
|
|
|
public static Results getFromJsonWithType(DocumentContext jsonContext, DataUrlConfiguration jsonDataPath) {
|
|
List<Map<String, Object>> tempRes = jsonContext.read(jsonDataPath.getPath()
|
|
+ "[" + jsonDataPath.getFieldsUrlConfiguration().getId() + "," + jsonDataPath.getFieldsUrlConfiguration().getName()
|
|
+ "," + jsonDataPath.getFieldsUrlConfiguration().getTypes() + "," + jsonDataPath.getFieldsUrlConfiguration().getUri() + "]");
|
|
List<Map<String, String>> finalRes = new ArrayList<>();
|
|
tempRes.forEach(map -> {
|
|
Map<String, String> resMap = new HashMap<>();
|
|
map.forEach((key, value) -> {
|
|
if (key.equals(jsonDataPath.getFieldsUrlConfiguration().getTypes().substring(1, jsonDataPath.getFieldsUrlConfiguration().getTypes().length() - 1))) {
|
|
resMap.put("tags", ((JSONArray) value).toJSONString());
|
|
} else if (key.equals(jsonDataPath.getFieldsUrlConfiguration().getUri().substring(1, jsonDataPath.getFieldsUrlConfiguration().getTypes().length() - 1))) {
|
|
resMap.put(key, ((JSONArray) value).toJSONString());
|
|
} else {
|
|
resMap.put(key, (String) value);
|
|
}
|
|
});
|
|
|
|
finalRes.add(resMap);
|
|
});
|
|
|
|
return new Results(finalRes,
|
|
new HashMap<>(1, 1));
|
|
}
|
|
|
|
public static Results getFromJsonWithFirstAndLastName(DocumentContext jsonContext, DataUrlConfiguration jsonDataPath) {
|
|
Results results = new Results(jsonContext.read(jsonDataPath.getPath()
|
|
+ "[" + jsonDataPath.getFieldsUrlConfiguration().getId() + "," + jsonDataPath.getFieldsUrlConfiguration().getFirstName()
|
|
+ "," + jsonDataPath.getFieldsUrlConfiguration().getLastName() + "]"),
|
|
new HashMap<>(1, 1));
|
|
results.getResults().stream().forEach(entry -> {
|
|
String name = entry.get(jsonDataPath.getFieldsUrlConfiguration().getFirstName().replace("'", "")) + " " + entry.get(jsonDataPath.getFieldsUrlConfiguration().getLastName().replace("'", ""));
|
|
entry.put("name", name);
|
|
entry.remove(jsonDataPath.getFieldsUrlConfiguration().getFirstName().replace("'", ""));
|
|
entry.remove(jsonDataPath.getFieldsUrlConfiguration().getLastName().replace("'", ""));
|
|
});
|
|
return results;
|
|
}
|
|
|
|
private static void mapToMap(String key, Map<String, String> source, Map<String, String> destination, boolean isTitle) {
|
|
if (source != null) {
|
|
String content = source.get("content");
|
|
/*if (isTitle) {
|
|
String classId = source.get("classid");
|
|
if (classId.equals("main title")) {
|
|
destination.put(key, content);
|
|
}
|
|
} else {*/
|
|
destination.put(key, content);
|
|
// }
|
|
}
|
|
}
|
|
}
|