2020package org .apache .cloudstack .storage .datastore .util ;
2121
2222import com .cloud .agent .api .Answer ;
23+ import com .cloud .utils .Pair ;
2324import com .cloud .utils .exception .CloudRuntimeException ;
2425import com .google .gson .Gson ;
2526import com .google .gson .annotations .SerializedName ;
26- import com .sun .jersey .api .client .Client ;
27- import com .sun .jersey .api .client .ClientResponse ;
28- import com .sun .jersey .api .client .WebResource ;
29- import com .sun .jersey .api .client .config .ClientConfig ;
30- import com .sun .jersey .api .client .config .DefaultClientConfig ;
31- import com .sun .jersey .core .util .MultivaluedMapImpl ;
3227import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
3328import org .apache .cloudstack .utils .security .SSLUtils ;
3429import org .apache .cloudstack .utils .security .SecureSSLSocketFactory ;
3530import org .apache .http .auth .InvalidCredentialsException ;
3631import org .apache .log4j .Logger ;
32+ import org .glassfish .jersey .client .ClientConfig ;
33+ import org .glassfish .jersey .client .ClientResponse ;
3734
3835import javax .naming .ServiceUnavailableException ;
3936import javax .net .ssl .HostnameVerifier ;
4340import javax .net .ssl .SSLSession ;
4441import javax .net .ssl .TrustManager ;
4542import javax .net .ssl .X509TrustManager ;
43+ import javax .ws .rs .client .Client ;
44+ import javax .ws .rs .client .ClientBuilder ;
45+ import javax .ws .rs .client .WebTarget ;
4646import javax .ws .rs .core .MediaType ;
47- import javax .ws .rs .core .MultivaluedMap ;
4847import javax .ws .rs .core .UriBuilder ;
4948import java .net .ConnectException ;
5049import java .security .InvalidParameterException ;
5150import java .security .SecureRandom ;
5251import java .security .cert .X509Certificate ;
52+ import java .util .ArrayList ;
5353import java .util .HashMap ;
54+ import java .util .List ;
5455
5556public class ElastistorUtil {
5657
@@ -960,7 +961,7 @@ static interface ElastiCenterCommand {
960961 * Returns null if there are query parameters associated with the
961962 * command
962963 */
963- public MultivaluedMap < String , String > getCommandParameters ();
964+ public List < Pair < String , String > > getCommandParameters ();
964965
965966 /*
966967 * Adds new key-value pair to the query paramters lists.
@@ -978,7 +979,7 @@ static interface ElastiCenterCommand {
978979 private static class BaseCommand implements ElastiCenterCommand {
979980
980981 private String commandName = null ;
981- private MultivaluedMap < String , String > commandParameters = null ;
982+ private List < Pair < String , String > > commandParameters = null ;
982983 private Object responseObject = null ;
983984
984985 /*
@@ -1003,16 +1004,16 @@ public boolean validate() {
10031004 }
10041005
10051006 @ Override
1006- public MultivaluedMap < String , String > getCommandParameters () {
1007+ public List < Pair < String , String > > getCommandParameters () {
10071008 return commandParameters ;
10081009 }
10091010
10101011 @ Override
10111012 public void putCommandParameter (String key , String value ) {
10121013 if (null == commandParameters ) {
1013- commandParameters = new MultivaluedMapImpl ();
1014+ commandParameters = new ArrayList <> ();
10141015 }
1015- commandParameters .add (key , value );
1016+ commandParameters .add (new Pair <>( key , value ) );
10161017 }
10171018
10181019 @ Override
@@ -1134,7 +1135,7 @@ public Object executeCommand(ElastiCenterCommand cmd) throws Throwable {
11341135 return executeCommand (cmd .getCommandName (), cmd .getCommandParameters (), cmd .getResponseObject ());
11351136 }
11361137
1137- public Object executeCommand (String command , MultivaluedMap < String , String > params , Object responeObj ) throws Throwable {
1138+ public Object executeCommand (String command , List < Pair < String , String > > params , Object responeObj ) throws Throwable {
11381139
11391140 if (!initialized ) {
11401141 throw new IllegalStateException ("Error : ElastiCenterClient is not initialized." );
@@ -1145,25 +1146,27 @@ public Object executeCommand(String command, MultivaluedMap<String, String> para
11451146 }
11461147
11471148 try {
1148- ClientConfig config = new DefaultClientConfig ();
1149- Client client = Client .create (config );
1150- WebResource webResource = client .resource (UriBuilder .fromUri (restprotocol + elastiCenterAddress + restpath ).build ());
1151-
1152- MultivaluedMap <String , String > queryParams = new MultivaluedMapImpl ();
1153- queryParams .add (queryparamapikey , apiKey );
1154- queryParams .add (queryparamresponse , responseType );
1155-
1156- queryParams .add (queryparamcommand , command );
1149+ ClientConfig config = new ClientConfig ();
1150+ Client client = ClientBuilder .newClient (config );
1151+ WebTarget webResource = client .target (UriBuilder .fromUri (restprotocol + elastiCenterAddress + restpath ).build ())
1152+ .queryParam (queryparamapikey , apiKey )
1153+ .queryParam (queryparamresponse , responseType )
1154+ .queryParam (queryparamcommand , command );
11571155
11581156 if (null != params ) {
1159- for (String key : params . keySet () ) {
1160- queryParams . add ( key , params . getFirst ( key ));
1157+ for (Pair < String , String > pair : params ) {
1158+ webResource = webResource . queryParam ( pair . first (), pair . second ( ));
11611159 }
11621160 }
11631161 if (debug ) {
1164- System .out .println ("Command Sent " + command + " : " + queryParams );
1162+ List <Pair <String , String >> qryParams = new ArrayList <>();
1163+ qryParams .add (new Pair <>(queryparamapikey , apiKey ));
1164+ qryParams .add (new Pair <>(queryparamresponse , responseType ));
1165+ qryParams .add (new Pair <>(queryparamcommand , command ));
1166+ qryParams .addAll (params );
1167+ System .out .println ("Command Sent " + command + " : " + params );
11651168 }
1166- ClientResponse response = webResource .queryParams ( queryParams ). accept (MediaType .APPLICATION_JSON ).get (ClientResponse .class );
1169+ ClientResponse response = webResource .request (MediaType .APPLICATION_JSON ).get (ClientResponse .class );
11671170
11681171 if (response .getStatus () >= 300 ) {
11691172 if (debug )
@@ -1178,7 +1181,7 @@ public Object executeCommand(String command, MultivaluedMap<String, String> para
11781181 throw new ServiceUnavailableException ("Internal Error. Please contact your ElastiCenter Administrator." );
11791182 }
11801183 } else if (null != responeObj ) {
1181- String jsonResponse = response .getEntity (String .class );
1184+ String jsonResponse = String . valueOf ( response .readEntity (String .class ) );
11821185 if (debug ) {
11831186 System .out .println ("Command Response : " + jsonResponse );
11841187 }
0 commit comments