4141
4242import org .apache .commons .httpclient .ConnectTimeoutException ;
4343import org .apache .commons .httpclient .HttpClient ;
44+ import org .apache .commons .httpclient .HttpConnectionManager ;
4445import org .apache .commons .httpclient .HttpException ;
46+ import org .apache .commons .httpclient .HttpMethod ;
4547import org .apache .commons .httpclient .HttpMethodBase ;
4648import org .apache .commons .httpclient .HttpStatus ;
4749import org .apache .commons .httpclient .MultiThreadedHttpConnectionManager ;
@@ -71,9 +73,42 @@ public class NiciraNvpApi {
7173 private String _adminpass ;
7274
7375 private HttpClient _client ;
76+
77+ /* This factory method is protected so we can extend this
78+ * in the unittests.
79+ */
80+ protected HttpClient createHttpClient () {
81+ return new HttpClient (s_httpClientManager );
82+ }
83+
84+ protected HttpMethod createMethod (String type , String uri ) throws NiciraNvpApiException {
85+ String url ;
86+ try {
87+ url = new URL (_protocol , _host , "/ws.v1/login" ).toString ();
88+ } catch (MalformedURLException e ) {
89+ s_logger .error ("Unable to build Nicira API URL" , e );
90+ throw new NiciraNvpApiException ("Unable to build Nicira API URL" , e );
91+ }
92+
93+ if ("post" .equalsIgnoreCase (type )) {
94+ return new PostMethod (url );
95+ }
96+ else if ("get" .equalsIgnoreCase (type )) {
97+ return new GetMethod (url );
98+ }
99+ else if ("delete" .equalsIgnoreCase (type )) {
100+ return new DeleteMethod (url );
101+ }
102+ else if ("put" .equalsIgnoreCase (type )) {
103+ return new PutMethod (url );
104+ }
105+ else {
106+ throw new NiciraNvpApiException ("Requesting unknown method type" );
107+ }
108+ }
74109
75110 public NiciraNvpApi () {
76- _client = new HttpClient ( s_httpClientManager );
111+ _client = createHttpClient ( );
77112 _client .getParams ().setCookiePolicy (CookiePolicy .BROWSER_COMPATIBILITY );
78113
79114 try {
@@ -100,9 +135,15 @@ public void setAdminCredentials(String username, String password) {
100135 * The method returns false if the login failed or the connection could not be made.
101136 *
102137 */
103- private void login () throws NiciraNvpApiException {
138+ protected void login () throws NiciraNvpApiException {
104139 String url ;
105140
141+ if (_host == null || _host .isEmpty () ||
142+ _adminuser == null || _adminuser .isEmpty () ||
143+ _adminpass == null || _adminpass .isEmpty ()) {
144+ throw new NiciraNvpApiException ("Hostname/credentials are null or empty" );
145+ }
146+
106147 try {
107148 url = new URL (_protocol , _host , "/ws.v1/login" ).toString ();
108149 } catch (MalformedURLException e ) {
@@ -294,18 +335,16 @@ public NiciraNvpList<LogicalRouterPort> findLogicalRouterPortByGatewayServiceUui
294335 return executeRetrieveObject (new TypeToken <NiciraNvpList <LogicalRouterPort >>(){}.getType (), uri , params );
295336 }
296337
297- private <T > void executeUpdateObject (T newObject , String uri , Map <String ,String > parameters ) throws NiciraNvpApiException {
298- String url ;
299- try {
300- url = new URL (_protocol , _host , uri ).toString ();
301- } catch (MalformedURLException e ) {
302- s_logger .error ("Unable to build Nicira API URL" , e );
303- throw new NiciraNvpApiException ("Connection to NVP Failed" );
338+ protected <T > void executeUpdateObject (T newObject , String uri , Map <String ,String > parameters ) throws NiciraNvpApiException {
339+ if (_host == null || _host .isEmpty () ||
340+ _adminuser == null || _adminuser .isEmpty () ||
341+ _adminpass == null || _adminpass .isEmpty ()) {
342+ throw new NiciraNvpApiException ("Hostname/credentials are null or empty" );
304343 }
305344
306345 Gson gson = new Gson ();
307346
308- PutMethod pm = new PutMethod ( url );
347+ PutMethod pm = ( PutMethod ) createMethod ( "put" , uri );
309348 pm .setRequestHeader ("Content-Type" , "application/json" );
310349 try {
311350 pm .setRequestEntity (new StringRequestEntity (
@@ -325,18 +364,16 @@ private <T> void executeUpdateObject(T newObject, String uri, Map<String,String>
325364 pm .releaseConnection ();
326365 }
327366
328- private <T > T executeCreateObject (T newObject , Type returnObjectType , String uri , Map <String ,String > parameters ) throws NiciraNvpApiException {
329- String url ;
330- try {
331- url = new URL (_protocol , _host , uri ).toString ();
332- } catch (MalformedURLException e ) {
333- s_logger .error ("Unable to build Nicira API URL" , e );
334- throw new NiciraNvpApiException ("Unable to build Nicira API URL" , e );
367+ protected <T > T executeCreateObject (T newObject , Type returnObjectType , String uri , Map <String ,String > parameters ) throws NiciraNvpApiException {
368+ if (_host == null || _host .isEmpty () ||
369+ _adminuser == null || _adminuser .isEmpty () ||
370+ _adminpass == null || _adminpass .isEmpty ()) {
371+ throw new NiciraNvpApiException ("Hostname/credentials are null or empty" );
335372 }
336373
337374 Gson gson = new Gson ();
338375
339- PostMethod pm = new PostMethod ( url );
376+ PostMethod pm = ( PostMethod ) createMethod ( "post" , uri );
340377 pm .setRequestHeader ("Content-Type" , "application/json" );
341378 try {
342379 pm .setRequestEntity (new StringRequestEntity (
@@ -366,16 +403,14 @@ private <T> T executeCreateObject(T newObject, Type returnObjectType, String uri
366403 return result ;
367404 }
368405
369- private void executeDeleteObject (String uri ) throws NiciraNvpApiException {
370- String url ;
371- try {
372- url = new URL (_protocol , _host , uri ).toString ();
373- } catch (MalformedURLException e ) {
374- s_logger .error ("Unable to build Nicira API URL" , e );
375- throw new NiciraNvpApiException ("Unable to build Nicira API URL" , e );
406+ protected void executeDeleteObject (String uri ) throws NiciraNvpApiException {
407+ if (_host == null || _host .isEmpty () ||
408+ _adminuser == null || _adminuser .isEmpty () ||
409+ _adminpass == null || _adminpass .isEmpty ()) {
410+ throw new NiciraNvpApiException ("Hostname/credentials are null or empty" );
376411 }
377-
378- DeleteMethod dm = new DeleteMethod ( url );
412+
413+ DeleteMethod dm = ( DeleteMethod ) createMethod ( "delete" , uri );
379414 dm .setRequestHeader ("Content-Type" , "application/json" );
380415
381416 executeMethod (dm );
@@ -389,16 +424,14 @@ private void executeDeleteObject(String uri) throws NiciraNvpApiException {
389424 dm .releaseConnection ();
390425 }
391426
392- private <T > T executeRetrieveObject (Type returnObjectType , String uri , Map <String ,String > parameters ) throws NiciraNvpApiException {
393- String url ;
394- try {
395- url = new URL (_protocol , _host , uri ).toString ();
396- } catch (MalformedURLException e ) {
397- s_logger .error ("Unable to build Nicira API URL" , e );
398- throw new NiciraNvpApiException ("Unable to build Nicira API URL" , e );
427+ protected <T > T executeRetrieveObject (Type returnObjectType , String uri , Map <String ,String > parameters ) throws NiciraNvpApiException {
428+ if (_host == null || _host .isEmpty () ||
429+ _adminuser == null || _adminuser .isEmpty () ||
430+ _adminpass == null || _adminpass .isEmpty ()) {
431+ throw new NiciraNvpApiException ("Hostname/credentials are null or empty" );
399432 }
400433
401- GetMethod gm = new GetMethod ( url );
434+ GetMethod gm = ( GetMethod ) createMethod ( "get" , uri );
402435 gm .setRequestHeader ("Content-Type" , "application/json" );
403436 if (parameters != null && !parameters .isEmpty ()) {
404437 List <NameValuePair > nameValuePairs = new ArrayList <NameValuePair >(parameters .size ());
@@ -430,7 +463,7 @@ private <T> T executeRetrieveObject(Type returnObjectType, String uri, Map<Strin
430463 return returnValue ;
431464 }
432465
433- private void executeMethod (HttpMethodBase method ) throws NiciraNvpApiException {
466+ protected void executeMethod (HttpMethodBase method ) throws NiciraNvpApiException {
434467 try {
435468 _client .executeMethod (method );
436469 if (method .getStatusCode () == HttpStatus .SC_UNAUTHORIZED ) {
@@ -441,9 +474,11 @@ private void executeMethod(HttpMethodBase method) throws NiciraNvpApiException {
441474 }
442475 } catch (HttpException e ) {
443476 s_logger .error ("HttpException caught while trying to connect to the Nicira NVP Controller" , e );
477+ method .releaseConnection ();
444478 throw new NiciraNvpApiException ("API call to Nicira NVP Controller Failed" , e );
445479 } catch (IOException e ) {
446480 s_logger .error ("IOException caught while trying to connect to the Nicira NVP Controller" , e );
481+ method .releaseConnection ();
447482 throw new NiciraNvpApiException ("API call to Nicira NVP Controller Failed" , e );
448483 }
449484 }
0 commit comments