use of org.jabref.model.entry.identifier.ArXivIdentifier in project jabref by JabRef.
the class ArXiv method callApi.
/**
* Queries the API.
*
* If only {@code searchQuery} is given, then the API will return results for each article that matches the query.
* If only {@code ids} is given, then the API will return results for each article in the list.
* If both {@code searchQuery} and {@code ids} are given, then the API will return each article in
* {@code ids} that matches {@code searchQuery}. This allows the API to act as a results filter.
*
* @param searchQuery the search query used to find articles;
* <a href="http://arxiv.org/help/api/user-manual#query_details">details</a>
* @param ids a list of arXiv identifiers
* @param start the index of the first returned result (zero-based)
* @param maxResults the number of maximal results (has to be smaller than 2000)
* @return the response from the API as a XML document (Atom 1.0)
* @throws FetcherException if there was a problem while building the URL or the API was not accessible
*/
private Document callApi(String searchQuery, List<ArXivIdentifier> ids, int start, int maxResults) throws FetcherException {
if (maxResults > 2000) {
throw new IllegalArgumentException("The arXiv API limits the number of maximal results to be 2000");
}
try {
URIBuilder uriBuilder = new URIBuilder(API_URL);
// The arXiv API has problems with accents, so we remove them (i.e. Fréchet -> Frechet)
if (StringUtil.isNotBlank(searchQuery)) {
uriBuilder.addParameter("search_query", StringUtil.stripAccents(searchQuery));
}
if (!ids.isEmpty()) {
uriBuilder.addParameter("id_list", ids.stream().map(ArXivIdentifier::getNormalized).collect(Collectors.joining(",")));
}
uriBuilder.addParameter("start", String.valueOf(start));
uriBuilder.addParameter("max_results", String.valueOf(maxResults));
URL url = uriBuilder.build().toURL();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (connection.getResponseCode() == 400) {
// Bad request error from server, try to get more information
throw getException(builder.parse(connection.getErrorStream()));
} else {
return builder.parse(connection.getInputStream());
}
} catch (SAXException | ParserConfigurationException | IOException | URISyntaxException exception) {
throw new FetcherException("arXiv API request failed", exception);
}
}
Aggregations