Skip to content

Commit

Permalink
Fix medline fetcher by using https
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr authored and matthiasgeiger committed Nov 2, 2016
1 parent 8898697 commit c29cd55
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- [koppor/#97] (https://github.com/koppor/jabref/issues/97): When importing preferences, the explorer will start where the preferences are last exported
- [koppor#5](https://github.com/koppor/jabref/issues/5) When entries are found while dropping a pdf with xmp meta data the found entries will be displayed in the import dialog
- [koppor#61](https://github.com/koppor/jabref/issues/61) Display gray background text in "Author" and "Editor" field to assist newcomers
- Updated Vietnam translation
- Updated Vietnamese translation
- Added greyed-out suggestion for `year`/`date`/`url` fields
- [#1908](https://github.com/JabRef/jabref/issues/1908) Add a shortcut for check integrity <kbd>CTRL</kbd>+<kbd>F8</kbd>

### Fixed
- Fixed [#2228](https://github.com/JabRef/jabref/issues/2228): Fixed Medline fetcher no longer working. The fetcher now uses `https` for fetching
- Fixed [#2089](https://github.com/JabRef/jabref/issues/2089): Fixed faulty cite key generation
- Fixed [#2092](https://github.com/JabRef/jabref/issues/2092): "None"-button in date picker clears the date field
- Fixed [#1993](https://github.com/JabRef/jabref/issues/1993): Various optimizations regarding search performance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ public class MedlineFetcher implements IdBasedParserFetcher, SearchBasedFetcher
private static final Log LOGGER = LogFactory.getLog(MedlineFetcher.class);

private static final int NUMBER_TO_FETCH = 50;
private static final String ID_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi";
private static final String SEARCH_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi";

private int numberOfResultsFound;


/**
* Replaces all commas in a given string with " AND "
*
Expand Down Expand Up @@ -72,37 +75,36 @@ private List<String> getPubMedIdsFromQuery(String query) throws FetcherException
XMLInputFactory inputFactory = XMLInputFactory.newFactory();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(ncbi.openStream());

fetchLoop:
while (streamReader.hasNext()) {
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;
}
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();
}
Expand All @@ -111,7 +113,8 @@ private List<String> getPubMedIdsFromQuery(String query) throws FetcherException
} 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);
throw new FetcherException("Error while parsing ID list", Localization.lang("Error while parsing ID list"),
e);
}
}

Expand All @@ -127,7 +130,7 @@ public HelpFile getHelpPage() {

@Override
public URL getURLForID(String identifier) throws URISyntaxException, MalformedURLException, FetcherException {
URIBuilder uriBuilder = new URIBuilder("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi");
URIBuilder uriBuilder = new URIBuilder(ID_URL);
uriBuilder.addParameter("db", "pubmed");
uriBuilder.addParameter("retmode", "xml");
uriBuilder.addParameter("id", identifier);
Expand Down Expand Up @@ -163,7 +166,8 @@ public List<BibEntry> performSearch(String query) throws FetcherException {
return Collections.emptyList();
}
if (numberOfResultsFound > NUMBER_TO_FETCH) {
LOGGER.info(numberOfResultsFound + " results found. Only 50 relevant results will be fetched by default.");
LOGGER.info(
numberOfResultsFound + " results found. Only 50 relevant results will be fetched by default.");
}

//pass the list of ids to fetchMedline to download them. like a id fetcher for mutliple ids
Expand All @@ -175,7 +179,7 @@ public List<BibEntry> performSearch(String query) throws FetcherException {

private URL createSearchUrl(String term) throws URISyntaxException, MalformedURLException {
term = replaceCommaWithAND(term);
URIBuilder uriBuilder = new URIBuilder("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi");
URIBuilder uriBuilder = new URIBuilder(SEARCH_URL);
uriBuilder.addParameter("db", "pubmed");
uriBuilder.addParameter("sort", "relevance");
uriBuilder.addParameter("retmax", String.valueOf(NUMBER_TO_FETCH));
Expand Down Expand Up @@ -204,9 +208,11 @@ private List<BibEntry> fetchMedline(List<String> ids) throws FetcherException {
resultList.forEach(this::doPostCleanup);
return resultList;
} catch (URISyntaxException | MalformedURLException e) {
throw new FetcherException("Error while generating fetch URL", Localization.lang("Error while generating fetch URL"), e);
throw new FetcherException("Error while generating fetch URL",
Localization.lang("Error while generating fetch URL"), e);
} catch (IOException e) {
throw new FetcherException("Error while fetching from Medline", Localization.lang("Error while fetching from %0", "Medline"), e);
throw new FetcherException("Error while fetching from Medline",
Localization.lang("Error while fetching from %0", "Medline"), e);
}
}

Expand Down

0 comments on commit c29cd55

Please sign in to comment.