Search in sources :

Example 1 with ArXivIdentifier

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);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) URIBuilder(org.apache.http.client.utils.URIBuilder) SAXException(org.xml.sax.SAXException) FetcherException(org.jabref.logic.importer.FetcherException) HttpURLConnection(java.net.HttpURLConnection) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArXivIdentifier(org.jabref.model.entry.identifier.ArXivIdentifier) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 FetcherException (org.jabref.logic.importer.FetcherException)1 ArXivIdentifier (org.jabref.model.entry.identifier.ArXivIdentifier)1 SAXException (org.xml.sax.SAXException)1