Skip to content

Commit 6b29bbc

Browse files
committed
Get rid of httpclient
1 parent f6821ce commit 6b29bbc

3 files changed

Lines changed: 58 additions & 112 deletions

File tree

pom.xml

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,6 @@
116116
<version>1.14.0</version>
117117
</dependency>
118118

119-
<!-- HTTP client (optional, used for AnnotatorClient) -->
120-
<dependency>
121-
<groupId>org.apache.httpcomponents</groupId>
122-
<artifactId>httpcore</artifactId>
123-
<version>4.4.16</version>
124-
<optional>true</optional>
125-
</dependency>
126-
<dependency>
127-
<groupId>org.apache.httpcomponents</groupId>
128-
<artifactId>httpclient</artifactId>
129-
<version>4.5.14</version>
130-
<optional>true</optional>
131-
<exclusions>
132-
<exclusion>
133-
<groupId>commons-logging</groupId>
134-
<artifactId>commons-logging</artifactId>
135-
</exclusion>
136-
<exclusion>
137-
<groupId>commons-codec</groupId>
138-
<artifactId>commons-codec</artifactId>
139-
</exclusion>
140-
</exclusions>
141-
</dependency>
142-
143119
<!-- R engine -->
144120
<dependency>
145121
<groupId>org.rosuda.REngine</groupId>

src/ubic/basecode/ontology/ncbo/AnnotatorClient.java

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
import org.apache.commons.lang3.StringUtils;
44
import org.apache.commons.lang3.exception.ExceptionUtils;
5-
import org.apache.http.HttpResponse;
6-
import org.apache.http.client.HttpClient;
7-
import org.apache.http.client.methods.HttpGet;
8-
import org.apache.http.impl.client.HttpClientBuilder;
95
import org.slf4j.Logger;
106
import org.slf4j.LoggerFactory;
117
import org.w3c.dom.Document;
@@ -21,14 +17,15 @@
2117
import java.io.IOException;
2218
import java.io.InputStream;
2319
import java.net.ConnectException;
20+
import java.net.URL;
2421
import java.util.Collection;
2522
import java.util.TreeSet;
2623
import java.util.regex.Matcher;
2724
import java.util.regex.Pattern;
2825

2926
/**
3027
* Use the NCBO annotator to find ontology terms matching strings.
31-
*
28+
*
3229
*/
3330
public class AnnotatorClient {
3431

@@ -49,7 +46,7 @@ public class AnnotatorClient {
4946
private static String ontologies = HP_ONTOLOGY + "," + DOID_ONTOLOGY;
5047

5148
/**
52-
*
49+
*
5350
* @param term
5451
* @return
5552
* @throws ParserConfigurationException
@@ -58,7 +55,7 @@ public class AnnotatorClient {
5855
* @throws Exception
5956
*/
6057
public static Collection<AnnotatorResponse> findTerm( String term )
61-
throws IOException, ParserConfigurationException, IllegalStateException, SAXException {
58+
throws IOException, ParserConfigurationException, IllegalStateException, SAXException {
6259
if ( StringUtils.isBlank( API_KEY ) ) {
6360
throw new IllegalStateException( "NCBO ncbo.api.key needs to be configured" );
6461
}
@@ -70,18 +67,16 @@ public static Collection<AnnotatorResponse> findTerm( String term )
7067
if ( StringUtils.isBlank( termClean ) ) return responsesFound;
7168

7269
String url = ANNOTATOR_URL + "apikey=" + API_KEY + "&max_level=0&ontologies=" + ontologies
73-
+ "&format=xml&text=" + termClean;
70+
+ "&format=xml&text=" + termClean;
7471

7572
if ( log.isDebugEnabled() ) log.debug( "request url: " + url );
7673

7774
int tries = 0;
7875

79-
HttpClient httpclient = HttpClientBuilder.create().build();
80-
HttpResponse response = null;
76+
InputStream response = null;
8177
while ( response == null && tries < MAX_TRIES ) {
8278
try {
83-
HttpGet httpGet = new HttpGet( url );
84-
response = httpclient.execute( httpGet );
79+
response = new URL( url ).openStream();
8580
} catch ( IOException e ) {
8681
try {
8782
Thread.sleep( 10000 ); // long wait...
@@ -99,7 +94,7 @@ public static Collection<AnnotatorResponse> findTerm( String term )
9994

10095
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
10196
DocumentBuilder builder = factory.newDocumentBuilder();
102-
Document document = builder.parse( response.getEntity().getContent() );
97+
Document document = builder.parse( response );
10398
NodeList nodes = document.getElementsByTagName( "annotation" );
10499

105100
// for each response receive, populate the return objects
@@ -124,7 +119,7 @@ public static Collection<AnnotatorResponse> findTerm( String term )
124119
Integer to = new Integer( infoE.getElementsByTagName( "to" ).item( 0 ).getTextContent() );
125120

126121
AnnotatorResponse annotatorResponse = new AnnotatorResponse( valueUri, matchType, txtMatched, from, to,
127-
ontologyUsed, termClean );
122+
ontologyUsed, termClean );
128123

129124
responsesFound.add( annotatorResponse );
130125
}
@@ -135,7 +130,7 @@ public static Collection<AnnotatorResponse> findTerm( String term )
135130

136131
/**
137132
* FIXME only knows about HP and DOID
138-
*
133+
*
139134
* @param url
140135
* @return
141136
*/
@@ -153,10 +148,10 @@ private static String findOntologyUsed( String url ) {
153148

154149
/**
155150
* return the label associated with an conceptid. FIXME why are we doing things this way, it must be terribly slow
156-
*
151+
*
157152
* @param ontologyId what virtual ontology to use
158153
* @param identifier the identifier, knows about: OMIM, DOID, MESH
159-
* @return the label for that term, example : ABCD syndrome
154+
* @return the label for that term, example : ABCD syndrome
160155
*/
161156
public static String findLabelForIdentifier( String ontologyId, String identifier ) {
162157

@@ -173,43 +168,32 @@ public static String findLabelForIdentifier( String ontologyId, String identifie
173168

174169
// http://data.bioontology.org/ontologies/OMIM
175170

176-
// http://data.bioontology.org/ontologies/OMIM/classes/http%3A%2F%2Fpurl.bioontology.org%2Fontology%2FOMIM%2F600374
177-
178-
for ( int i = 0; i < MAX_TRIES; i++ ) {
179-
try {
180-
String url;
181-
182-
// These Ontology identifiers could potentially be retrieved, using OntologyLookup
183-
switch ( ontologyId ) {
184-
case "OMIM":
185-
case "MESH":
186-
url = "http://data.bioontology.org/ontologies/" + ontologyId
187-
+ "/classes/http%3A%2F%2Fpurl.bioontology.org%2Fontology%2FMESH%2F"
188-
+ identifier
189-
+ "/?apikey=" + API_KEY + "&format=xml";
190-
break;
191-
case "DOID":
192-
url = "http://data.bioontology.org/ontologies/" + ontologyId + "/classes/http%3A%2F%2Fpurl.obolibrary.org%2Fobo%2F"
193-
+ identifier
194-
+ "/?apikey=" + API_KEY + "&format=xml";
195-
break;
196-
default:
197-
throw new IllegalArgumentException( "Don't know how to deal with " + ontologyId );
198-
}
199-
200-
log.debug( url );
201-
202-
HttpClient httpclient = HttpClientBuilder.create().build();
171+
// http://data.bioontology.org/ontologies/OMIM/classes/http%3A%2F%2Fpurl.bioontology.org%2Fontology%2FOMIM%2F600374
172+
173+
String url;
174+
175+
// These Ontology identifiers could potentially be retrieved, using OntologyLookup
176+
switch ( ontologyId ) {
177+
case "OMIM":
178+
case "MESH":
179+
url = "http://data.bioontology.org/ontologies/" + ontologyId
180+
+ "/classes/http%3A%2F%2Fpurl.bioontology.org%2Fontology%2FMESH%2F"
181+
+ identifier
182+
+ "/?apikey=" + API_KEY + "&format=xml";
183+
break;
184+
case "DOID":
185+
url = "http://data.bioontology.org/ontologies/" + ontologyId + "/classes/http%3A%2F%2Fpurl.obolibrary.org%2Fobo%2F"
186+
+ identifier
187+
+ "/?apikey=" + API_KEY + "&format=xml";
188+
break;
189+
default:
190+
throw new IllegalArgumentException( "Don't know how to deal with " + ontologyId );
191+
}
203192

204-
HttpGet httpGet = new HttpGet( url );
205-
HttpResponse response = httpclient.execute( httpGet );
206-
207-
// term was not found
208-
if ( response.getStatusLine().getStatusCode() == 404 ) {
209-
log.debug( "404 returned, the term: " + identifier + " was not found" );
210-
return null;
211-
}
193+
log.debug( url );
212194

195+
for ( int i = 0; i < MAX_TRIES; i++ ) {
196+
try ( InputStream response = new URL( url ).openStream() ) {
213197
return findLabel( response );
214198
} catch ( ConnectException ce ) {
215199
try {
@@ -227,10 +211,10 @@ public static String findLabelForIdentifier( String ontologyId, String identifie
227211
/**
228212
* using the response return the label associated with the request
229213
*/
230-
private static String findLabel( HttpResponse response ) throws Exception {
214+
private static String findLabel( InputStream response ) throws Exception {
231215
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
232216
DocumentBuilder builder = factory.newDocumentBuilder();
233-
try ( InputStream content = response.getEntity().getContent() ) {
217+
try ( InputStream content = response ) {
234218
Document document = builder.parse( content );
235219
NodeList nodes = document.getElementsByTagName( "prefLabel" );
236220
if ( nodes == null ) {

src/ubic/basecode/ontology/ncbo/OmimAnnotatorClient.java

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
package ubic.basecode.ontology.ncbo;
22

3-
import java.util.Collection;
4-
import java.util.HashSet;
5-
import java.util.Map;
6-
7-
import javax.xml.parsers.DocumentBuilder;
8-
import javax.xml.parsers.DocumentBuilderFactory;
9-
103
import org.apache.commons.lang3.StringUtils;
114
import org.apache.commons.lang3.exception.ExceptionUtils;
12-
import org.apache.http.HttpResponse;
13-
import org.apache.http.client.HttpClient;
14-
import org.apache.http.client.methods.HttpGet;
15-
import org.apache.http.impl.client.HttpClientBuilder;
165
import org.slf4j.Logger;
176
import org.slf4j.LoggerFactory;
187
import org.w3c.dom.Document;
198
import org.w3c.dom.Element;
209
import org.w3c.dom.Node;
2110
import org.w3c.dom.NodeList;
22-
2311
import ubic.basecode.util.Configuration;
2412

13+
import javax.xml.parsers.DocumentBuilder;
14+
import javax.xml.parsers.DocumentBuilderFactory;
15+
import java.io.InputStream;
16+
import java.net.URL;
17+
import java.util.Collection;
18+
import java.util.HashSet;
19+
import java.util.Map;
20+
2521
/**
2622
* to use the OMIM api
2723
* TODO Document Me
28-
*
24+
*
2925
* @author Nicolas
3026
*/
3127
public class OmimAnnotatorClient {
@@ -39,16 +35,16 @@ public class OmimAnnotatorClient {
3935

4036
/**
4137
* Giving a set of OMIM id return for each id its list of related publication
42-
*
38+
*
4339
* @param omimIds the omimIds we want to find the publication
4440
* @param mimToPubmeds the results, pass by reference for multiple calls, limit of size 10
4541
*/
4642
public static Map<Long, Collection<Long>> findLinkedPublications( Collection<Long> omimIds,
47-
Map<Long, Collection<Long>> mimToPubmeds ) throws InterruptedException {
43+
Map<Long, Collection<Long>> mimToPubmeds ) throws InterruptedException {
4844

4945
if ( omimIds.size() > 10 || omimIds.isEmpty() ) {
5046
throw new IllegalArgumentException( "Size of the Omim ids must be between 1 and 10, Size Found: "
51-
+ omimIds.size() );
47+
+ omimIds.size() );
5248
}
5349

5450
// From OMIM documentation : The rate of requests is currently limited to 4 requests per second.
@@ -57,35 +53,25 @@ public static Map<Long, Collection<Long>> findLinkedPublications( Collection<Lon
5753

5854
String omimIdsAsString = StringUtils.removeEnd( StringUtils.join( omimIds, "," ), "," );
5955

60-
try {
56+
String url = OMIM_API_URL + "?mimNumber=" + omimIdsAsString + "&format=xml&include=referenceList&apiKey="
57+
+ OMIM_API_KEY;
6158

62-
String url = OMIM_API_URL + "?mimNumber=" + omimIdsAsString + "&format=xml&include=referenceList&apiKey="
63-
+ OMIM_API_KEY;
64-
65-
log.info( "request url: " + url );
66-
67-
HttpClient httpclient = HttpClientBuilder.create().build();
68-
HttpGet httpGet = new HttpGet( url );
69-
HttpResponse response = httpclient.execute( httpGet );
70-
71-
// term was not found
72-
if ( response.getStatusLine().getStatusCode() == 404 ) {
73-
throw new Exception( "404 returned" );
74-
}
59+
log.info( "request url: " + url );
7560

61+
try ( InputStream response = new URL( url ).openConnection().getInputStream() ) {
7662
return findPublications( response, mimToPubmeds );
7763
} catch ( Exception e ) {
7864
log.error( ExceptionUtils.getStackTrace( e ) );
7965
return null;
8066
}
8167
}
8268

83-
private static Map<Long, Collection<Long>> findPublications( HttpResponse response,
84-
Map<Long, Collection<Long>> mimToPubmeds ) throws Exception {
69+
private static Map<Long, Collection<Long>> findPublications( InputStream response,
70+
Map<Long, Collection<Long>> mimToPubmeds ) throws Exception {
8571

8672
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
8773
DocumentBuilder builder = factory.newDocumentBuilder();
88-
Document document = builder.parse( response.getEntity().getContent() );
74+
Document document = builder.parse( response );
8975
NodeList nodes = document.getElementsByTagName( "reference" );
9076

9177
// for each response receive, populate the return objects
@@ -95,7 +81,7 @@ private static Map<Long, Collection<Long>> findPublications( HttpResponse respon
9581
Element eElement = ( Element ) nNode;
9682

9783
if ( eElement.getElementsByTagName( "pubmedID" ).item( 0 ) != null
98-
&& eElement.getElementsByTagName( "mimNumber" ).item( 0 ) != null ) {
84+
&& eElement.getElementsByTagName( "mimNumber" ).item( 0 ) != null ) {
9985

10086
Long pubmedID = new Long( eElement.getElementsByTagName( "pubmedID" ).item( 0 ).getTextContent() );
10187
Long mimNumber = new Long( eElement.getElementsByTagName( "mimNumber" ).item( 0 ).getTextContent() );

0 commit comments

Comments
 (0)