1717package org .apache .cloudstack .network .element ;
1818
1919import java .io .IOException ;
20- import java .io .InputStreamReader ;
2120import java .io .UnsupportedEncodingException ;
2221import java .net .URI ;
2322import java .net .URISyntaxException ;
2423import java .util .Arrays ;
2524
26- import org .apache .http .HttpResponse ;
2725import org .apache .http .HttpStatus ;
26+ import org .apache .http .client .ClientProtocolException ;
2827import org .apache .http .client .HttpClient ;
28+ import org .apache .http .client .HttpResponseException ;
2929import org .apache .http .client .entity .UrlEncodedFormEntity ;
3030import org .apache .http .client .methods .HttpDelete ;
3131import org .apache .http .client .methods .HttpPost ;
3535import org .apache .http .client .params .CookiePolicy ;
3636import org .apache .http .entity .ContentType ;
3737import org .apache .http .entity .StringEntity ;
38+ import org .apache .http .impl .client .BasicResponseHandler ;
3839import org .apache .http .impl .client .DefaultHttpClient ;
3940import org .apache .http .impl .conn .PoolingClientConnectionManager ;
4041import org .apache .http .message .BasicNameValuePair ;
4142import org .apache .http .params .CoreConnectionPNames ;
4243import org .apache .log4j .Logger ;
4344
4445import com .google .gson .Gson ;
45- import com .google .gson .JsonIOException ;
46- import com .google .gson .JsonSyntaxException ;
4746import com .google .gson .annotations .SerializedName ;
4847
4948/**
@@ -74,7 +73,7 @@ protected HttpClient getHttpClient() { // for mock test
7473 return s_client ;
7574 }
7675
77- private HttpResponse innerExecuteMethod (HttpRequestBase req , String path ) {
76+ private String executeMethod (HttpRequestBase req , String path ) {
7877 try {
7978 URI base = new URI (apiUrl );
8079 req .setURI (new URI (base .getScheme (), base .getUserInfo (), base .getHost (),
@@ -83,23 +82,26 @@ private HttpResponse innerExecuteMethod(HttpRequestBase req, String path) {
8382 s_logger .error ("invalid API URL " + apiUrl + " path " + path , e );
8483 return null ;
8584 }
86- HttpResponse res = null ;
8785 try {
88- res = getHttpClient ().execute (req );
89- s_logger .info ("ssp api call:" + req + " status=" + res .getStatusLine ());
86+ String content = null ;
87+ try {
88+ content = getHttpClient ().execute (req , new BasicResponseHandler ());
89+ s_logger .info ("ssp api call: " + req );
90+ } catch (HttpResponseException e ) {
91+ s_logger .info ("ssp api call failed: " + req , e );
92+ if (e .getStatusCode () == HttpStatus .SC_UNAUTHORIZED && login ()) {
93+ req .reset ();
94+ content = getHttpClient ().execute (req , new BasicResponseHandler ());
95+ s_logger .info ("ssp api retry call: " + req );
96+ }
97+ }
98+ return content ;
99+ } catch (ClientProtocolException e ) { // includes HttpResponseException
100+ s_logger .error ("ssp api call failed: " + req , e );
90101 } catch (IOException e ) {
91102 s_logger .error ("ssp api call failed: " + req , e );
92103 }
93- return res ;
94- }
95-
96- private HttpResponse executeMethod (HttpRequestBase req , String path ) {
97- HttpResponse res = innerExecuteMethod (req , path );
98- if (res .getStatusLine ().getStatusCode () == HttpStatus .SC_UNAUTHORIZED && login ()) {
99- req .reset ();
100- res = innerExecuteMethod (req , path );
101- }
102- return res ;
104+ return null ;
103105 }
104106
105107 public boolean login () {
@@ -112,9 +114,7 @@ public boolean login() {
112114 s_logger .error ("invalid username or password" , e );
113115 return false ;
114116 }
115-
116- HttpResponse res = this .innerExecuteMethod (method , "/ws.v1/login" );
117- if (res != null && res .getStatusLine ().getStatusCode () == HttpStatus .SC_OK ) {
117+ if (executeMethod (method , "/ws.v1/login" ) != null ) {
118118 return true ;
119119 }
120120 return false ;
@@ -134,29 +134,14 @@ public TenantNetwork createTenantNetwork(String tenantUuid, String networkName)
134134
135135 HttpPost method = new HttpPost ();
136136 method .setEntity (new StringEntity (new Gson ().toJson (req ), ContentType .APPLICATION_JSON ));
137- HttpResponse res = executeMethod (method , "/ssp.v1/tenant-networks" );
138- if (res == null || res .getStatusLine ().getStatusCode () != HttpStatus .SC_CREATED ) {
139- return null ;
140- }
141- try {
142- return new Gson ().fromJson (new InputStreamReader (res .getEntity ().getContent ()),
143- TenantNetwork .class );
144- } catch (JsonSyntaxException e ) {
145- s_logger .error ("reading response body failed" , e );
146- } catch (JsonIOException e ) {
147- s_logger .error ("reading response body failed" , e );
148- } catch (IllegalStateException e ) {
149- s_logger .error ("reading response body failed" , e );
150- } catch (IOException e ) {
151- s_logger .error ("reading response body failed" , e );
152- }
153- return null ;
137+ return new Gson ().fromJson (
138+ executeMethod (method , "/ssp.v1/tenant-networks" ),
139+ TenantNetwork .class );
154140 }
155141
156142 public boolean deleteTenantNetwork (String tenantNetworkUuid ) {
157143 HttpDelete method = new HttpDelete ();
158- HttpResponse res = executeMethod (method , "/ssp.v1/tenant-networks/" + tenantNetworkUuid );
159- if (res != null && res .getStatusLine ().getStatusCode () == HttpStatus .SC_NO_CONTENT ) {
144+ if (executeMethod (method , "/ssp.v1/tenant-networks/" + tenantNetworkUuid ) != null ) {
160145 return true ;
161146 }
162147 return false ;
@@ -182,31 +167,14 @@ public TenantPort createTenantPort(String tenantNetworkUuid) {
182167
183168 HttpPost method = new HttpPost ();
184169 method .setEntity (new StringEntity (new Gson ().toJson (req ), ContentType .APPLICATION_JSON ));
185- HttpResponse res = executeMethod (method , "/ssp.v1/tenant-ports" );
186-
187- if (res == null || res .getStatusLine ().getStatusCode () != HttpStatus .SC_CREATED ) {
188- return null ;
189- }
190- try {
191- return new Gson ().fromJson (new InputStreamReader (res .getEntity ().getContent ()),
192- TenantPort .class );
193- } catch (JsonSyntaxException e ) {
194- s_logger .error ("reading response body failed" , e );
195- } catch (JsonIOException e ) {
196- s_logger .error ("reading response body failed" , e );
197- } catch (IllegalStateException e ) {
198- s_logger .error ("reading response body failed" , e );
199- } catch (IOException e ) {
200- s_logger .error ("reading response body failed" , e );
201- }
202- return null ;
170+ return new Gson ().fromJson (
171+ executeMethod (method , "/ssp.v1/tenant-ports" ),
172+ TenantPort .class );
203173 }
204174
205175 public boolean deleteTenantPort (String tenantPortUuid ) {
206176 HttpDelete method = new HttpDelete ();
207- HttpResponse res = executeMethod (method , "/ssp.v1/tenant-ports/" + tenantPortUuid );
208-
209- if (res != null && res .getStatusLine ().getStatusCode () == HttpStatus .SC_NO_CONTENT ) {
177+ if (executeMethod (method , "/ssp.v1/tenant-ports/" + tenantPortUuid ) != null ) {
210178 return true ;
211179 }
212180 return false ;
@@ -223,22 +191,8 @@ public TenantPort updateTenantVifBinding(String portUuid, String hypervisorIpAdd
223191
224192 HttpPut method = new HttpPut ();
225193 method .setEntity (new StringEntity (new Gson ().toJson (req ), ContentType .APPLICATION_JSON ));
226- HttpResponse res = executeMethod (method , "/ssp.v1/tenant-ports/" + portUuid );
227- if (res == null || res .getStatusLine ().getStatusCode () != HttpStatus .SC_OK ) {
228- return null ;
229- }
230- try {
231- return new Gson ().fromJson (new InputStreamReader (res .getEntity ().getContent ()),
232- TenantPort .class );
233- } catch (JsonSyntaxException e ) {
234- s_logger .error ("reading response body failed" , e );
235- } catch (JsonIOException e ) {
236- s_logger .error ("reading response body failed" , e );
237- } catch (IllegalStateException e ) {
238- s_logger .error ("reading response body failed" , e );
239- } catch (IOException e ) {
240- s_logger .error ("reading response body failed" , e );
241- }
242- return null ;
194+ return new Gson ().fromJson (
195+ executeMethod (method , "/ssp.v1/tenant-ports/" + portUuid ),
196+ TenantPort .class );
243197 }
244198}
0 commit comments