From 0a8ccf021ba3cc53c7aba2ff32d7ba0ef760e3ef Mon Sep 17 00:00:00 2001 From: Guillaume Poirier-Morency Date: Sun, 30 Oct 2022 14:35:24 -0700 Subject: [PATCH] Update jersey-client to 2.27 --- aspiredb/pom.xml | 2 +- .../biomartquery/BioMartQueryServiceImpl.java | 27 +++++------ .../gemma/NeurocartaQueryServiceImpl.java | 45 ++++++++++--------- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/aspiredb/pom.xml b/aspiredb/pom.xml index 455e428a..fae43e04 100644 --- a/aspiredb/pom.xml +++ b/aspiredb/pom.xml @@ -85,7 +85,7 @@ org.glassfish.jersey.core jersey-client - 3.0.8 + 2.27 org.apache.commons diff --git a/aspiredb/src/main/java/ubc/pavlab/aspiredb/server/biomartquery/BioMartQueryServiceImpl.java b/aspiredb/src/main/java/ubc/pavlab/aspiredb/server/biomartquery/BioMartQueryServiceImpl.java index 5581002a..affb7634 100644 --- a/aspiredb/src/main/java/ubc/pavlab/aspiredb/server/biomartquery/BioMartQueryServiceImpl.java +++ b/aspiredb/src/main/java/ubc/pavlab/aspiredb/server/biomartquery/BioMartQueryServiceImpl.java @@ -22,11 +22,12 @@ import java.util.Map; import java.util.Timer; import java.util.TimerTask; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import javax.annotation.PostConstruct; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -37,6 +38,8 @@ import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.glassfish.jersey.client.ClientResponse; +import org.glassfish.jersey.client.JerseyClientBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -45,10 +48,7 @@ import ubc.pavlab.aspiredb.shared.GeneValueObject; import ubc.pavlab.aspiredb.shared.GenomicRange; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.core.util.MultivaluedMapImpl; +import javax.ws.rs.client.Client; /** * Simple wrapper that calls BioMart REST query service. @@ -84,27 +84,24 @@ public void run() { } private static String sendRequest( String xmlQueryString ) throws BioMartServiceException { - Client client = Client.create(); + Client client = JerseyClientBuilder.newBuilder() + .readTimeout( BIOMART_TIMEOUT_SECONDS, TimeUnit.SECONDS ) + .build(); - MultivaluedMap queryData = new MultivaluedMapImpl(); - queryData.add( "query", xmlQueryString ); + WebTarget resource = client.target( BIO_MART_URL ).queryParam( "query", xmlQueryString ); - WebResource resource = client.resource( BIO_MART_URL ).queryParams( queryData ); - client.setReadTimeout( 1000 * BIOMART_TIMEOUT_SECONDS ); - - ClientResponse response = resource.type( MediaType.APPLICATION_FORM_URLENCODED_TYPE ) - .get( ClientResponse.class ); + Response response = resource.request( MediaType.APPLICATION_FORM_URLENCODED_TYPE ).get(); // Check return code if ( Response.Status.fromStatusCode( response.getStatus() ).getFamily() != Response.Status.Family.SUCCESSFUL ) { String errorMessage = "Error occurred when accessing BioMart web service: " - + response.getEntity( String.class ); + + response.getEntity(); log.error( errorMessage ); throw new BioMartServiceException( errorMessage ); } - return response.getEntity( String.class ); + return ( String ) response.getEntity(); } @Autowired diff --git a/aspiredb/src/main/java/ubc/pavlab/aspiredb/server/gemma/NeurocartaQueryServiceImpl.java b/aspiredb/src/main/java/ubc/pavlab/aspiredb/server/gemma/NeurocartaQueryServiceImpl.java index 1df4fdb7..6e243a8a 100644 --- a/aspiredb/src/main/java/ubc/pavlab/aspiredb/server/gemma/NeurocartaQueryServiceImpl.java +++ b/aspiredb/src/main/java/ubc/pavlab/aspiredb/server/gemma/NeurocartaQueryServiceImpl.java @@ -14,12 +14,9 @@ */ package ubc.pavlab.aspiredb.server.gemma; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.core.util.MultivaluedMapImpl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.glassfish.jersey.client.JerseyClientBuilder; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -32,13 +29,16 @@ import ubc.pavlab.aspiredb.server.util.GemmaURLUtils; import ubc.pavlab.aspiredb.shared.GeneValueObject; import ubc.pavlab.aspiredb.shared.NeurocartaPhenotypeValueObject; -import ubic.basecode.ontology.model.OntologyTerm; import javax.annotation.PostConstruct; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import java.util.*; +import java.util.concurrent.TimeUnit; /** * Simple wrapper that calls Phenocarta REST web service. @@ -56,25 +56,28 @@ public class NeurocartaQueryServiceImpl implements NeurocartaQueryService { private static String sendRequest( String urlSuffix, MultivaluedMap queryParams ) throws NeurocartaServiceException { - Client client = Client.create(); - client.setReadTimeout( 1000 * REQUEST_TIMEOUT_SECONDS ); - client.setConnectTimeout( 1000 * REQUEST_TIMEOUT_SECONDS ); - WebResource resource = client.resource( GemmaURLUtils.makeWebServiceUrl( urlSuffix ) ).queryParams( - queryParams ); - - ClientResponse response = resource.type( MediaType.APPLICATION_FORM_URLENCODED_TYPE ) - .get( ClientResponse.class ); + Client client = JerseyClientBuilder.newBuilder() + .readTimeout( REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS ) + .connectTimeout( REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS ) + .build(); + WebTarget webTarget = client.target( GemmaURLUtils.makeWebServiceUrl( urlSuffix ) ); + queryParams.forEach( ( k, l ) -> { + webTarget.queryParam( k, l.toArray() ); + } ); + Response response = webTarget + .request( MediaType.APPLICATION_FORM_URLENCODED_TYPE ) + .get(); // Check return code if ( Response.Status.fromStatusCode( response.getStatus() ).getFamily() != Response.Status.Family.SUCCESSFUL ) { String errorMessage = "Error occurred when accessing Phenocarta web service: (" + GemmaURLUtils.makeWebServiceUrl( urlSuffix ) + ") " - + response.getEntity( String.class ); + + response.getEntity(); log.error( errorMessage ); throw new NeurocartaServiceException( errorMessage ); } - return response.getEntity( String.class ); + return ( String ) response.getEntity(); } @Autowired @@ -90,13 +93,13 @@ private static String sendRequest( String urlSuffix, MultivaluedMap fetchAllPhenotypes() throws NeurocartaServiceException, BioMartServiceException { - String result = sendRequest( LOAD_PHENOTYPES_URL_SUFFIX, new MultivaluedMapImpl() ); + String result = sendRequest( LOAD_PHENOTYPES_URL_SUFFIX, new MultivaluedHashMap<>() ); Collection neurocartaPhenotypes = new HashSet<>(); try { JSONObject jsonResult = new JSONObject( result ); - JSONArray phenotypes = (JSONArray) jsonResult.get( "data" ); + JSONArray phenotypes = ( JSONArray ) jsonResult.get( "data" ); for ( int i = 0; i < phenotypes.length(); i++ ) { JSONObject phen = phenotypes.getJSONObject( i ); @@ -121,7 +124,7 @@ public Collection fetchAllPhenotypes() @Override public Collection fetchGenesAssociatedWithPhenotype( String phenotypeUri ) throws NeurocartaServiceException, BioMartServiceException { - MultivaluedMap queryParams = new MultivaluedMapImpl(); + MultivaluedMap queryParams = new MultivaluedHashMap<>(); queryParams.add( "editableOnly", "false" ); queryParams.add( "phenotypes", phenotypeUri ); // Example result : @@ -132,7 +135,7 @@ public Collection fetchGenesAssociatedWithPhenotype( String phe try { JSONObject jsonResult = new JSONObject( result ); - JSONArray genes = (JSONArray) jsonResult.get( "data" ); + JSONArray genes = ( JSONArray ) jsonResult.get( "data" ); geneSymbols = new HashSet<>( genes.length() ); @@ -157,7 +160,7 @@ public Collection fetchGenesAssociatedWithPhenotype( String phe public Map findPhenotypeGenes( String phenotypeUri ) throws NeurocartaServiceException, BioMartServiceException { - MultivaluedMap queryParams = new MultivaluedMapImpl(); + MultivaluedMap queryParams = new MultivaluedHashMap<>(); queryParams.add( "editableOnly", "false" ); queryParams.add( "phenotypes", phenotypeUri ); @@ -165,7 +168,7 @@ public Map findPhenotypeGenes( String phenotypeUri ) th Map geneToURI = new HashMap<>(); try { JSONObject jsonResult = new JSONObject( result ); - JSONArray genes = (JSONArray) jsonResult.get( "data" ); + JSONArray genes = ( JSONArray ) jsonResult.get( "data" ); for ( int i = 0; i < genes.length(); i++ ) { JSONObject gene = genes.getJSONObject( i );