Skip to content

Commit

Permalink
Update jersey-client to 2.27
Browse files Browse the repository at this point in the history
  • Loading branch information
arteymix committed Oct 30, 2022
1 parent a9bffd5 commit 0a8ccf0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion aspiredb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>3.0.8</version>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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.
Expand Down Expand Up @@ -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<String, String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -56,25 +56,28 @@ public class NeurocartaQueryServiceImpl implements NeurocartaQueryService {

private static String sendRequest( String urlSuffix, MultivaluedMap<String, String> 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
Expand All @@ -90,13 +93,13 @@ private static String sendRequest( String urlSuffix, MultivaluedMap<String, Stri
public Collection<NeurocartaPhenotypeValueObject> fetchAllPhenotypes()
throws NeurocartaServiceException, BioMartServiceException {

String result = sendRequest( LOAD_PHENOTYPES_URL_SUFFIX, new MultivaluedMapImpl() );
String result = sendRequest( LOAD_PHENOTYPES_URL_SUFFIX, new MultivaluedHashMap<>() );

Collection<NeurocartaPhenotypeValueObject> 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 );
Expand All @@ -121,7 +124,7 @@ public Collection<NeurocartaPhenotypeValueObject> fetchAllPhenotypes()
@Override
public Collection<GeneValueObject> fetchGenesAssociatedWithPhenotype( String phenotypeUri )
throws NeurocartaServiceException, BioMartServiceException {
MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
queryParams.add( "editableOnly", "false" );
queryParams.add( "phenotypes", phenotypeUri );
// Example result :
Expand All @@ -132,7 +135,7 @@ public Collection<GeneValueObject> 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() );
Expand All @@ -157,15 +160,15 @@ public Collection<GeneValueObject> fetchGenesAssociatedWithPhenotype( String phe
public Map<String, GeneValueObject> findPhenotypeGenes( String phenotypeUri ) throws NeurocartaServiceException,
BioMartServiceException {

MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
queryParams.add( "editableOnly", "false" );
queryParams.add( "phenotypes", phenotypeUri );

String result = sendRequest( FIND_GENES_URL_SUFFIX, queryParams );
Map<String, String> 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 );
Expand Down

0 comments on commit 0a8ccf0

Please sign in to comment.