Juan Hernandez has uploaded a new change for review.

Change subject: sdk: Introduce ApiBuilder
......................................................................

sdk: Introduce ApiBuilder

Currently the Api class has a collection of constructor, with different
combinations as parameters. Using this constructors renders code that is
difficult to read, as many of the parameters are usually null. For
example, the call to create a SSL connection without server certificate
validation looks like this:

  Api api = new Api(
    "https://fedora.example.com:443/ovirt-engine/api";,
    "admin@internal",
    "******",
    null,
    null,
    null,
    null,
    true,
    true,
    false,
    true
  );

The nulls and the magic values in this type of call make it hard to
understand what it is really doing. To simplify this kind of calls this
patch introduces a new ApiBuilder class that can be used with a fluent
style. For example, for the same use case:

  Api api = new ApiBuilder()
    .url("https://fedora.example.com:443/ovirt-engine/api";)
    .user("admin@internal")
    .password("******")
    .noHostVerification(true)
    .debug(true)
    .build();

This is much easier to read.

Change-Id: I251070c8888046bfbac90c16306168318528b464
Related-To: https://bugzilla.redhat.com/1145237
Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com>
---
M 
ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/ApiTemplate
M ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/Api.java
A ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/ApiBuilder.java
M 
ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/ConnectionsPoolBuilder.java
4 files changed, 604 insertions(+), 241 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-sdk-java 
refs/changes/03/33503/1

diff --git 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/ApiTemplate
 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/ApiTemplate
index 459f508..8ddedcf 100644
--- 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/ApiTemplate
+++ 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/ApiTemplate
@@ -46,15 +46,27 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site using
      *             HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password) throws 
ClientProtocolException,
             ServerException, IOException, UnsecuredConnectionAttemptError {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password).build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool).build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        this.initResources();
+        this(
+            url,
+            username,
+            password,
+            null, // sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            null, // noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            null, // filter,
+            null // debug,
+        );
     }
 
     /**
@@ -76,19 +88,28 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, String 
keyStorePath)
             throws ClientProtocolException, ServerException, IOException,
                    UnsecuredConnectionAttemptError {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
-                .keyStorePath(keyStorePath)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        this.initResources();
+        this(
+            url,
+            username,
+            password,
+            null, // sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            null, // noHostVerification,
+            keyStorePath,
+            null, // keyStorePassword,
+            null, // filter,
+            null // debug,
+        );
     }
 
     /**
@@ -114,21 +135,28 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, String 
keyStorePath,
             String keyStorePassword, Boolean filter) throws 
ClientProtocolException,
             ServerException, UnsecuredConnectionAttemptError, IOException {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
-                .keyStorePath(keyStorePath)
-                .keyStorePassword(keyStorePassword)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .filter(filter)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        initResources();
+        this(
+            url,
+            username,
+            password,
+            null, // sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            null, // noHostVerification,
+            keyStorePath,
+            keyStorePassword,
+            filter,
+            null // debug,
+        );
     }
 
     /**
@@ -147,18 +175,27 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String sessionid) throws ClientProtocolException, 
ServerException,
             IOException, UnsecuredConnectionAttemptError {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url).build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .sessionid(sessionid)
-                .persistentAuth(true)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        this.initResources();
+        this(
+            url,
+            null, // username,
+            null, // password,
+            sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            null, // noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            null, // filter,
+            null // debug,
+        );
     }
 
     /**
@@ -180,19 +217,28 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, boolean 
noHostVerification)
             throws ClientProtocolException, ServerException, 
UnsecuredConnectionAttemptError,
             IOException {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
-                .noHostVerification(noHostVerification)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        initResources();
+        this(
+            url,
+            username,
+            password,
+            null, // sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            null, // filter,
+            null // debug,
+        );
     }
 
     /**
@@ -213,21 +259,28 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String sessionid, boolean noHostVerification)
             throws ClientProtocolException, ServerException, 
UnsecuredConnectionAttemptError,
             IOException {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url)
-                .noHostVerification(noHostVerification)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .sessionid(sessionid)
-                .persistentAuth(true)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        initResources();
+        this(
+            url,
+            null, // username,
+            null, // password,
+            sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            null, // noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            null, // filter,
+            null // debug,
+        );
     }
 
     /**
@@ -251,20 +304,28 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, Boolean 
noHostVerification,
             Boolean filter) throws ClientProtocolException, ServerException,
             UnsecuredConnectionAttemptError, IOException {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
-                .noHostVerification(noHostVerification)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .filter(filter)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        initResources();
+        this(
+            url,
+            username,
+            password,
+            null, // sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            null, // noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            filter,
+            null // debug,
+        );
     }
 
     /**
@@ -299,26 +360,29 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, String sessionid, 
Integer port,
             Integer requestTimeout, Boolean persistentAuth, Boolean 
noHostVerification,
             Boolean filter, Boolean debug) throws ClientProtocolException, 
ServerException,
             UnsecuredConnectionAttemptError, IOException {
-
-        configureLog4J(debug);
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
-                .port(port)
-                .requestTimeout(requestTimeout)
-                .noHostVerification(noHostVerification)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .sessionid(sessionid)
-                .persistentAuth(persistentAuth)
-                .filter(filter)
-                .debug(debug)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        initResources();
+        this(
+            url,
+            username,
+            password,
+            sessionid,
+            port,
+            requestTimeout,
+            null, // sessionTimeout,
+            persistentAuth,
+            noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            filter,
+            debug
+        );
     }
 
     /**
@@ -355,27 +419,29 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, String sessionid, 
Integer port,
             Integer requestTimeout, Integer sessionTimeout, Boolean 
persistentAuth,
             Boolean noHostVerification, Boolean filter, Boolean debug) throws 
ClientProtocolException,
             ServerException, UnsecuredConnectionAttemptError, IOException {
-
-        configureLog4J(debug);
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
-                .port(port)
-                .requestTimeout(requestTimeout)
-                .sessionTimeout(sessionTimeout)
-                .noHostVerification(noHostVerification)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .sessionid(sessionid)
-                .persistentAuth(persistentAuth)
-                .filter(filter)
-                .debug(debug)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        initResources();
+        this(
+            url,
+            username,
+            password,
+            sessionid,
+            port,
+            requestTimeout,
+            sessionTimeout,
+            persistentAuth,
+            noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            filter,
+            debug
+        );
     }
 
     /**
@@ -415,18 +481,61 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured
      *             site using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, String sessionid,
             Integer port, Integer requestTimeout, Integer sessionTimeout,
             Boolean persistentAuth, String keyStorePath, String 
keyStorePassword,
             Boolean filter, Boolean debug) throws ClientProtocolException,
             ServerException, UnsecuredConnectionAttemptError, IOException {
+        this(
+            url,
+            username,
+            password,
+            sessionid,
+            port,
+            requestTimeout,
+            sessionTimeout,
+            persistentAuth,
+            null, // noHostVerification,
+            keyStorePath,
+            keyStorePassword,
+            filter,
+            debug
+        );
+    }
+
+    /**
+     * This method is intended for use by the {@link 
org.ovirt.engine.sdk.ApiBuilder} class, don't use it directly.
+     */
+    Api(
+        String url,
+        String username,
+        String password,
+        String sessionid,
+        Integer port,
+        Integer requestTimeout,
+        Integer sessionTimeout,
+        Boolean persistentAuth,
+        Boolean noHostVerification,
+        String keyStorePath,
+        String keyStorePassword,
+        Boolean filter,
+        Boolean debug
+    )
+    throws ServerException, UnsecuredConnectionAttemptError, IOException {
 
         configureLog4J(debug);
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
+        ConnectionsPool pool = new ConnectionsPoolBuilder()
+                .url(url)
+                .username(username)
+                .password(password)
                 .port(port)
                 .requestTimeout(requestTimeout)
                 .sessionTimeout(sessionTimeout)
+                .noHostVerification(noHostVerification)
                 .keyStorePath(keyStorePath)
                 .keyStorePassword(keyStorePassword)
                 .build();
@@ -441,16 +550,7 @@
     }
 
     /**
-     * Configures log4j
-     */
-    private void configureLog4J() {
-        configureLog4J(Boolean.FALSE);
-    }
-
-    /**
-     * Configures log4j
-     *
-     * @param debug
+     * Configures log4j.
      */
     private void configureLog4J(Boolean debug) {
         String patternLayout = "%d %-5p [%c] %m%n";
diff --git a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/Api.java 
b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/Api.java
index 2e534aa..c56b0d5 100644
--- a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/Api.java
+++ b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/Api.java
@@ -93,15 +93,27 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site using
      *             HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password) throws 
ClientProtocolException,
             ServerException, IOException, UnsecuredConnectionAttemptError {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password).build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool).build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        this.initResources();
+        this(
+            url,
+            username,
+            password,
+            null, // sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            null, // noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            null, // filter,
+            null // debug,
+        );
     }
 
     /**
@@ -123,19 +135,28 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, String 
keyStorePath)
             throws ClientProtocolException, ServerException, IOException,
                    UnsecuredConnectionAttemptError {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
-                .keyStorePath(keyStorePath)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        this.initResources();
+        this(
+            url,
+            username,
+            password,
+            null, // sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            null, // noHostVerification,
+            keyStorePath,
+            null, // keyStorePassword,
+            null, // filter,
+            null // debug,
+        );
     }
 
     /**
@@ -161,21 +182,28 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, String 
keyStorePath,
             String keyStorePassword, Boolean filter) throws 
ClientProtocolException,
             ServerException, UnsecuredConnectionAttemptError, IOException {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
-                .keyStorePath(keyStorePath)
-                .keyStorePassword(keyStorePassword)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .filter(filter)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        initResources();
+        this(
+            url,
+            username,
+            password,
+            null, // sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            null, // noHostVerification,
+            keyStorePath,
+            keyStorePassword,
+            filter,
+            null // debug,
+        );
     }
 
     /**
@@ -194,18 +222,27 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String sessionid) throws ClientProtocolException, 
ServerException,
             IOException, UnsecuredConnectionAttemptError {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url).build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .sessionid(sessionid)
-                .persistentAuth(true)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        this.initResources();
+        this(
+            url,
+            null, // username,
+            null, // password,
+            sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            null, // noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            null, // filter,
+            null // debug,
+        );
     }
 
     /**
@@ -227,19 +264,28 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, boolean 
noHostVerification)
             throws ClientProtocolException, ServerException, 
UnsecuredConnectionAttemptError,
             IOException {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
-                .noHostVerification(noHostVerification)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        initResources();
+        this(
+            url,
+            username,
+            password,
+            null, // sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            null, // filter,
+            null // debug,
+        );
     }
 
     /**
@@ -260,21 +306,28 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String sessionid, boolean noHostVerification)
             throws ClientProtocolException, ServerException, 
UnsecuredConnectionAttemptError,
             IOException {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url)
-                .noHostVerification(noHostVerification)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .sessionid(sessionid)
-                .persistentAuth(true)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        initResources();
+        this(
+            url,
+            null, // username,
+            null, // password,
+            sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            null, // noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            null, // filter,
+            null // debug,
+        );
     }
 
     /**
@@ -298,20 +351,28 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, Boolean 
noHostVerification,
             Boolean filter) throws ClientProtocolException, ServerException,
             UnsecuredConnectionAttemptError, IOException {
-
-        configureLog4J();
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
-                .noHostVerification(noHostVerification)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .filter(filter)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        initResources();
+        this(
+            url,
+            username,
+            password,
+            null, // sessionid,
+            null, // port,
+            null, // requestTimeout,
+            null, // sessionTimeout,
+            null, // persistentAuth,
+            null, // noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            filter,
+            null // debug,
+        );
     }
 
     /**
@@ -346,26 +407,29 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, String sessionid, 
Integer port,
             Integer requestTimeout, Boolean persistentAuth, Boolean 
noHostVerification,
             Boolean filter, Boolean debug) throws ClientProtocolException, 
ServerException,
             UnsecuredConnectionAttemptError, IOException {
-
-        configureLog4J(debug);
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
-                .port(port)
-                .requestTimeout(requestTimeout)
-                .noHostVerification(noHostVerification)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .sessionid(sessionid)
-                .persistentAuth(persistentAuth)
-                .filter(filter)
-                .debug(debug)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        initResources();
+        this(
+            url,
+            username,
+            password,
+            sessionid,
+            port,
+            requestTimeout,
+            null, // sessionTimeout,
+            persistentAuth,
+            noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            filter,
+            debug
+        );
     }
 
     /**
@@ -402,27 +466,29 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured site
      *             using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, String sessionid, 
Integer port,
             Integer requestTimeout, Integer sessionTimeout, Boolean 
persistentAuth,
             Boolean noHostVerification, Boolean filter, Boolean debug) throws 
ClientProtocolException,
             ServerException, UnsecuredConnectionAttemptError, IOException {
-
-        configureLog4J(debug);
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
-                .port(port)
-                .requestTimeout(requestTimeout)
-                .sessionTimeout(sessionTimeout)
-                .noHostVerification(noHostVerification)
-                .build();
-        HttpProxy httpProxy = new HttpProxyBuilder(pool)
-                .sessionid(sessionid)
-                .persistentAuth(persistentAuth)
-                .filter(filter)
-                .debug(debug)
-                .build();
-        this.proxy = new HttpProxyBroker(httpProxy);
-        initResources();
+        this(
+            url,
+            username,
+            password,
+            sessionid,
+            port,
+            requestTimeout,
+            sessionTimeout,
+            persistentAuth,
+            noHostVerification,
+            null, // keyStorePath,
+            null, // keyStorePassword,
+            filter,
+            debug
+        );
     }
 
     /**
@@ -462,18 +528,61 @@
      * @throws UnsecuredConnectionAttemptError
      *             Signals that attempt of connecting to SSL secured
      *             site using HTTP protocol has occurred.
+     *
+     * @deprecated Use the {@link ApiBuilder} class instead.
      */
+    @Deprecated
     public Api(String url, String username, String password, String sessionid,
             Integer port, Integer requestTimeout, Integer sessionTimeout,
             Boolean persistentAuth, String keyStorePath, String 
keyStorePassword,
             Boolean filter, Boolean debug) throws ClientProtocolException,
             ServerException, UnsecuredConnectionAttemptError, IOException {
+        this(
+            url,
+            username,
+            password,
+            sessionid,
+            port,
+            requestTimeout,
+            sessionTimeout,
+            persistentAuth,
+            null, // noHostVerification,
+            keyStorePath,
+            keyStorePassword,
+            filter,
+            debug
+        );
+    }
+
+    /**
+     * This method is intended for use by the {@link 
org.ovirt.engine.sdk.ApiBuilder} class, don't use it directly.
+     */
+    Api(
+        String url,
+        String username,
+        String password,
+        String sessionid,
+        Integer port,
+        Integer requestTimeout,
+        Integer sessionTimeout,
+        Boolean persistentAuth,
+        Boolean noHostVerification,
+        String keyStorePath,
+        String keyStorePassword,
+        Boolean filter,
+        Boolean debug
+    )
+    throws ServerException, UnsecuredConnectionAttemptError, IOException {
 
         configureLog4J(debug);
-        ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, 
password)
+        ConnectionsPool pool = new ConnectionsPoolBuilder()
+                .url(url)
+                .username(username)
+                .password(password)
                 .port(port)
                 .requestTimeout(requestTimeout)
                 .sessionTimeout(sessionTimeout)
+                .noHostVerification(noHostVerification)
                 .keyStorePath(keyStorePath)
                 .keyStorePassword(keyStorePassword)
                 .build();
@@ -488,16 +597,7 @@
     }
 
     /**
-     * Configures log4j
-     */
-    private void configureLog4J() {
-        configureLog4J(Boolean.FALSE);
-    }
-
-    /**
-     * Configures log4j
-     *
-     * @param debug
+     * Configures log4j.
      */
     private void configureLog4J(Boolean debug) {
         String patternLayout = "%d %-5p [%c] %m%n";
diff --git 
a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/ApiBuilder.java 
b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/ApiBuilder.java
new file mode 100644
index 0000000..0a3bc84
--- /dev/null
+++ b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/ApiBuilder.java
@@ -0,0 +1,186 @@
+//
+// Copyright (c) 2014 Red Hat, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package org.ovirt.engine.sdk;
+
+import java.io.IOException;
+
+import org.ovirt.engine.sdk.exceptions.ServerException;
+import org.ovirt.engine.sdk.exceptions.UnsecuredConnectionAttemptError;
+
+/**
+ * This class is intended to simplify the creation of a {@link 
org.ovirt.engine.sdk.Api} object. Instead of using a long
+ * list of constructor arguments, most of then with value {@code null},  this 
can be used with a fluent style, for
+ * example:
+ *
+ * <pre>
+ * Api api = new ApiBuilder()
+ *     .url("https://ovirt.example.com/ovirt-engine/api";)
+ *     .user("admin@internal")
+ *     .password("******")
+ *     .debug(true)
+ *     .build();
+ * </pre>
+ */
+@SuppressWarnings("unused")
+public class ApiBuilder {
+    private String url;
+    private String user;
+    private String password;
+    private String sessionId;
+    private Integer requestTimeout;
+    private Integer sessionTimeout;
+    private Boolean persistentAuth;
+    private Boolean noHostVerification;
+    private String keyStorePath;
+    private String keyStorePassword;
+    private Boolean filter;
+    private Boolean debug;
+
+    /**
+     * Sets the URL that will be used to build the API object, for example
+     * {@code https://ovirt.example.com/ovirt-engine/api}.
+     */
+    public ApiBuilder url(String url) {
+        this.url = url;
+        return this;
+    }
+
+    /**
+     * Sets the name of the user that will be used to authenticate to the 
server, for example {@code admin@internal}.
+     * This should usually be used together with the {@link #password(String)} 
method, as most authentication methods
+     * require both an user name and a password.
+     */
+    public ApiBuilder user(String user) {
+        this.user = user;
+        return this;
+    }
+
+    /**
+     * Sets the password that will be used to authenticate to the server.
+     */
+    public ApiBuilder password(String password) {
+        this.password = password;
+        return this;
+    }
+
+    /**
+     * Sets the session identifier that will be used when sending requests to 
the server. This is useful if a session
+     * has been established (and authenticated) before and you want to reuse 
it without authenticating again.
+     */
+    public ApiBuilder sessionId(String sessionId) {
+        this.sessionId = sessionId;
+        return this;
+    }
+
+    /**
+     * Sets the timeout (in seconds) using when waiting for responses from the 
server. If a request takes longer than
+     * this it will be cancelled and an exception will be thrown.
+     */
+    public ApiBuilder requestTimeout(Integer requestTimeout) {
+        this.requestTimeout = requestTimeout;
+        return this;
+    }
+
+    /**
+     * Sets the timeout (in minutes) of the session with the server. If the 
session is idle longer than this the server
+     * will destroy it.
+     */
+    public ApiBuilder sessionTimeout(Integer sessionTimeout) {
+        this.sessionTimeout = sessionTimeout;
+        return this;
+    }
+
+    /**
+     * Enables or disables persistent authentication using cookies. It is 
enabled by default, so calling this method is
+     * required only if you wish to disable it.
+     */
+    public ApiBuilder persistentAuth(Boolean persistentAuth) {
+        this.persistentAuth = persistentAuth;
+        return this;
+    }
+
+    /**
+     * Enables or disables verification of the host name inside the SSL 
certificate presented by the server. By default
+     * the certificate is verified, and the connection will be rejected if the 
host name isn't correct. If you want to
+     * disable this verification you need to explicitly pass {@code true} to 
this method.
+     */
+    public ApiBuilder noHostVerification(Boolean noHostVerification) {
+        this.noHostVerification = noHostVerification;
+        return this;
+    }
+
+    /**
+     * Sets the location of the file containing the CA certificates used to 
verify the certificate presented by the
+     * server. This must be combined with the {@link 
#keyStorePassword(String)} method.
+     */
+    public ApiBuilder keyStorePath(String keyStorePath) {
+        this.keyStorePath = keyStorePath;
+        return this;
+    }
+
+    /**
+     * Sets the password used to decrypt the key store file indicated by the 
{@link #keyStorePath} method.
+     */
+    public ApiBuilder keyStorePassword(String keyStorePassword) {
+        this.keyStorePassword = keyStorePassword;
+        return this;
+    }
+
+    /**
+     * If set to {@code true} enables the user mode API, where users will only 
see the objects that they have
+     * permissions for. By default this is {@code false}, which means that 
users will see all objects.
+     */
+    public ApiBuilder filter(Boolean filter) {
+        this.filter = filter;
+        return this;
+    }
+
+    /**
+     * Enables or disables debug output. By default debug output is disabled.
+     */
+    public ApiBuilder debug(Boolean debug) {
+        this.debug = debug;
+        return this;
+    }
+
+    /**
+     * Creates an API entry point object.
+     *
+     * @throws ServerException Signals that an API error has occurred
+     * @throws IOException Signals that an I/O exception of some sort has 
occurred
+     * @throws UnsecuredConnectionAttemptError Signals that attempt of 
connecting to SSL secured site using HTTP
+     *     protocol has occurred
+     */
+    public Api build() throws ServerException, 
UnsecuredConnectionAttemptError, IOException {
+        return new Api(
+            url,
+            user,
+            password,
+            sessionId,
+            null, // The port should be inside the URL, the builder doesn't 
support changing it.
+            requestTimeout,
+            sessionTimeout,
+            persistentAuth,
+            noHostVerification,
+            keyStorePath,
+            keyStorePassword,
+            filter,
+            debug
+        );
+    }
+}
+
diff --git 
a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/ConnectionsPoolBuilder.java
 
b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/ConnectionsPoolBuilder.java
index 5340a29..148443a 100644
--- 
a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/ConnectionsPoolBuilder.java
+++ 
b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/ConnectionsPoolBuilder.java
@@ -92,30 +92,7 @@
 
     private URL urlobj = null;
 
-    /**
-     * @param url
-     *            oVirt API url
-     * @param username
-     *            oVirt API user name
-     * @param password
-     *            oVirt API password
-     * 
-     * @throws MalformedURLException
-     */
-    public ConnectionsPoolBuilder(String url, String username, String 
password) throws MalformedURLException {
-        url(url);
-        username(username);
-        password(password);
-    }
-
-    /**
-     * @param url
-     *            oVirt API url
-     * 
-     * @throws MalformedURLException
-     */
-    public ConnectionsPoolBuilder(String url) throws MalformedURLException {
-        url(url);
+    public ConnectionsPoolBuilder() {
     }
 
     /**
@@ -125,7 +102,7 @@
      * 
      * @throws MalformedURLException
      */
-    private ConnectionsPoolBuilder url(String url) throws 
MalformedURLException {
+    public ConnectionsPoolBuilder url(String url) throws MalformedURLException 
{
         this.url = url;
         this.urlobj = createURL(url);
         return this;
@@ -135,7 +112,7 @@
      * @param username
      *            oVirt API user name
      */
-    private ConnectionsPoolBuilder username(String username) {
+    public ConnectionsPoolBuilder username(String username) {
         this.username = username;
         return this;
     }
@@ -144,7 +121,7 @@
      * @param password
      *            oVirt API password
      */
-    private ConnectionsPoolBuilder password(String password) {
+    public ConnectionsPoolBuilder password(String password) {
         this.password = password;
         return this;
     }


-- 
To view, visit http://gerrit.ovirt.org/33503
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I251070c8888046bfbac90c16306168318528b464
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine-sdk-java
Gerrit-Branch: master
Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to