Search in sources :

Example 16 with FetcherException

use of org.jabref.logic.importer.FetcherException in project jabref by JabRef.

the class GoogleScholar method performSearch.

@Override
public List<BibEntry> performSearch(String query) throws FetcherException {
    try {
        obtainAndModifyCookie();
        List<BibEntry> foundEntries = new ArrayList<>(10);
        URIBuilder uriBuilder = new URIBuilder(BASIC_SEARCH_URL);
        uriBuilder.addParameter("hl", "en");
        uriBuilder.addParameter("btnG", "Search");
        uriBuilder.addParameter("q", query);
        addHitsFromQuery(foundEntries, uriBuilder.toString());
        if (foundEntries.size() == 10) {
            uriBuilder.addParameter("start", "10");
            addHitsFromQuery(foundEntries, uriBuilder.toString());
        }
        return foundEntries;
    } catch (URISyntaxException e) {
        throw new FetcherException("Error while fetching from " + getName(), e);
    } catch (IOException e) {
        // java.io.IOException: Server returned HTTP response code: 503 for URL: https://ipv4.google.com/sorry/index?continue=https://scholar.google.com/scholar%3Fhl%3Den%26btnG%3DSearch%26q%3Dbpmn&hl=en&q=CGMSBI0NBDkYuqy9wAUiGQDxp4NLQCWbIEY1HjpH5zFJhv4ANPGdWj0
        if (e.getMessage().contains("Server returned HTTP response code: 503 for URL")) {
            throw new FetcherException("Fetching from Google Scholar failed.", Localization.lang("This might be caused by reaching the traffic limitation of Google Scholar (see 'Help' for details)."), e);
        } else {
            throw new FetcherException("Error while fetching from " + getName(), e);
        }
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FetcherException(org.jabref.logic.importer.FetcherException) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 17 with FetcherException

use of org.jabref.logic.importer.FetcherException in project jabref by JabRef.

the class MedlineFetcher method getPubMedIdsFromQuery.

/**
     * When using 'esearch.fcgi?db=<database>&term=<query>' we will get a list of IDs matching the query.
     * Input: Any text query (&term)
     * Output: List of UIDs matching the query
     *
     * @see <a href="https://www.ncbi.nlm.nih.gov/books/NBK25500/">www.ncbi.nlm.nih.gov/books/NBK25500/</a>
     */
private List<String> getPubMedIdsFromQuery(String query) throws FetcherException {
    boolean fetchIDs = false;
    boolean firstOccurrenceOfCount = false;
    List<String> idList = new ArrayList<>();
    try {
        URL ncbi = createSearchUrl(query);
        XMLInputFactory inputFactory = XMLInputFactory.newFactory();
        XMLStreamReader streamReader = inputFactory.createXMLStreamReader(ncbi.openStream());
        fetchLoop: while (streamReader.hasNext()) {
            int event = streamReader.getEventType();
            switch(event) {
                case XMLStreamConstants.START_ELEMENT:
                    if (streamReader.getName().toString().equals("Count")) {
                        firstOccurrenceOfCount = true;
                    }
                    if (streamReader.getName().toString().equals("IdList")) {
                        fetchIDs = true;
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    if (firstOccurrenceOfCount) {
                        numberOfResultsFound = Integer.parseInt(streamReader.getText());
                        firstOccurrenceOfCount = false;
                    }
                    if (fetchIDs) {
                        idList.add(streamReader.getText());
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    //Everything relevant is listed before the IdList. So we break the loop right after the IdList tag closes.
                    if (streamReader.getName().toString().equals("IdList")) {
                        break fetchLoop;
                    }
            }
            streamReader.next();
        }
        streamReader.close();
        return idList;
    } catch (IOException | URISyntaxException e) {
        throw new FetcherException("Unable to get PubMed IDs", Localization.lang("Unable to get PubMed IDs"), e);
    } catch (XMLStreamException e) {
        throw new FetcherException("Error while parsing ID list", Localization.lang("Error while parsing ID list"), e);
    }
}
Also used : FetcherException(org.jabref.logic.importer.FetcherException) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 18 with FetcherException

use of org.jabref.logic.importer.FetcherException in project jabref by JabRef.

the class MrDLibFetcher method makeServerRequest.

/**
     * Contact the server with the title of the selected item
     *
     * @param query: The query holds the title of the selected entry. Used to make a query to the MDL Server
     * @return Returns the server response. This is an XML document as a String.
     */
private String makeServerRequest(String queryByTitle) throws FetcherException {
    try {
        URLDownload urlDownload = new URLDownload(constructQuery(queryByTitle));
        urlDownload.bypassSSLVerification();
        String response = urlDownload.asString();
        //Conversion of < and >
        response = response.replaceAll("&gt;", ">");
        response = response.replaceAll("&lt;", "<");
        return response;
    } catch (IOException e) {
        throw new FetcherException("Problem downloading", e);
    }
}
Also used : FetcherException(org.jabref.logic.importer.FetcherException) IOException(java.io.IOException) URLDownload(org.jabref.logic.net.URLDownload)

Aggregations

FetcherException (org.jabref.logic.importer.FetcherException)18 IOException (java.io.IOException)12 BibEntry (org.jabref.model.entry.BibEntry)12 URISyntaxException (java.net.URISyntaxException)7 URL (java.net.URL)6 ArrayList (java.util.ArrayList)6 ParserResult (org.jabref.logic.importer.ParserResult)5 DOI (org.jabref.model.entry.identifier.DOI)5 URIBuilder (org.apache.http.client.utils.URIBuilder)4 URLDownload (org.jabref.logic.net.URLDownload)4 StringReader (java.io.StringReader)3 MalformedURLException (java.net.MalformedURLException)3 Optional (java.util.Optional)3 ParseException (org.jabref.logic.importer.ParseException)3 BufferedReader (java.io.BufferedReader)2 HttpURLConnection (java.net.HttpURLConnection)2 URLConnection (java.net.URLConnection)2 List (java.util.List)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2