You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by fm...@apache.org on 2012/06/20 15:50:00 UTC

svn commit: r1352100 - in /incubator/syncope/trunk: client/src/main/java/org/apache/syncope/client/http/ console/src/main/java/org/apache/syncope/console/pages/ console/src/main/resources/ core/src/main/java/org/apache/syncope/core/security/ core/src/t...

Author: fmartelli
Date: Wed Jun 20 13:49:59 2012
New Revision: 1352100

URL: http://svn.apache.org/viewvc?rev=1352100&view=rev
Log:
SYNCOPE-96 #comment preemptive authentication has been fixed

Added:
    incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/http/HttpClientParams.java   (with props)
Modified:
    incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/http/PreemptiveAuthHttpRequestFactory.java
    incubator/syncope/trunk/console/src/main/java/org/apache/syncope/console/pages/Login.java
    incubator/syncope/trunk/console/src/main/java/org/apache/syncope/console/pages/Logout.java
    incubator/syncope/trunk/console/src/main/resources/applicationContext.xml
    incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/security/SyncopeAuthenticationProvider.java
    incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/AbstractTest.java
    incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/AuthenticationTestITCase.java
    incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/ReportTestITCase.java
    incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/RoleTestITCase.java
    incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/UserRequestTestITCase.java
    incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/UserTestITCase.java
    incubator/syncope/trunk/core/src/test/resources/restClientContext.xml

Added: incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/http/HttpClientParams.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/http/HttpClientParams.java?rev=1352100&view=auto
==============================================================================
--- incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/http/HttpClientParams.java (added)
+++ incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/http/HttpClientParams.java Wed Jun 20 13:49:59 2012
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.syncope.client.http;
+
+import java.util.Map;
+import org.apache.http.params.SyncBasicHttpParams;
+
+public class HttpClientParams extends SyncBasicHttpParams {
+
+    public void setParameters(final Map<String, Object> parameters) {
+        clear();
+        
+        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
+            setParameter(entry.getKey(), entry.getValue());
+        }
+    }
+}
\ No newline at end of file

Propchange: incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/http/HttpClientParams.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/http/HttpClientParams.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/http/HttpClientParams.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/http/PreemptiveAuthHttpRequestFactory.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/http/PreemptiveAuthHttpRequestFactory.java?rev=1352100&r1=1352099&r2=1352100&view=diff
==============================================================================
--- incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/http/PreemptiveAuthHttpRequestFactory.java (original)
+++ incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/http/PreemptiveAuthHttpRequestFactory.java Wed Jun 20 13:49:59 2012
@@ -26,7 +26,9 @@ import org.apache.http.client.protocol.C
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.impl.auth.BasicScheme;
 import org.apache.http.impl.client.BasicAuthCache;
+import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.params.HttpParams;
+import org.apache.http.params.SyncBasicHttpParams;
 import org.apache.http.protocol.BasicHttpContext;
 import org.apache.http.protocol.HttpContext;
 import org.springframework.http.HttpMethod;
@@ -50,7 +52,7 @@ public class PreemptiveAuthHttpRequestFa
     public PreemptiveAuthHttpRequestFactory(final String host, final int port, final String scheme,
             final ClientConnectionManager conman, final HttpParams params) {
 
-        super();
+        super(new DefaultHttpClient(conman, params));
         targetHost = new HttpHost(host, port, scheme);
     }
 
@@ -61,13 +63,12 @@ public class PreemptiveAuthHttpRequestFa
     @Override
     protected HttpContext createHttpContext(final HttpMethod httpMethod, final URI uri) {
 
-        AuthCache authCache = new BasicAuthCache();
-        // Generate BASIC scheme object and add it to the local auth cache
-        BasicScheme basicAuth = new BasicScheme();
-        authCache.put(targetHost, basicAuth);
-
         // Add AuthCache to the execution context
         BasicHttpContext localcontext = new BasicHttpContext();
+
+        // Generate BASIC scheme object and add it to the local auth cache
+        AuthCache authCache = new BasicAuthCache();
+        authCache.put(targetHost, new BasicScheme());
         localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
 
         return localcontext;

Modified: incubator/syncope/trunk/console/src/main/java/org/apache/syncope/console/pages/Login.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/console/src/main/java/org/apache/syncope/console/pages/Login.java?rev=1352100&r1=1352099&r2=1352100&view=diff
==============================================================================
--- incubator/syncope/trunk/console/src/main/java/org/apache/syncope/console/pages/Login.java (original)
+++ incubator/syncope/trunk/console/src/main/java/org/apache/syncope/console/pages/Login.java Wed Jun 20 13:49:59 2012
@@ -109,16 +109,21 @@ public class Login extends WebPage {
 
             @Override
             public void onSubmit() {
-                String[] entitlements = authenticate(userIdField.getRawInput(), passwordField.getRawInput());
+                try {
+                    String[] entitlements = authenticate(userIdField.getRawInput(), passwordField.getRawInput());
 
-                if (entitlements == null) {
-                    error(getString("login-error"));
-                } else {
                     SyncopeSession.get().setUserId(userIdField.getRawInput());
                     SyncopeSession.get().setEntitlements(entitlements);
                     SyncopeSession.get().setCoreVersion(getCoreVersion());
 
                     setResponsePage(WelcomePage.class, parameters);
+                } catch (HttpClientErrorException e) {
+                    error(getString("login-error"));
+                    
+                    PreemptiveAuthHttpRequestFactory requestFactory =
+                            ((PreemptiveAuthHttpRequestFactory) restTemplate.getRequestFactory());
+
+                    ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().clear();
                 }
             }
         };
@@ -174,20 +179,14 @@ public class Login extends WebPage {
 
     private String[] authenticate(final String userId, final String password) {
         // 1. Set provided credentials to check
-        PreemptiveAuthHttpRequestFactory requestFactory = ((PreemptiveAuthHttpRequestFactory) restTemplate.
-                getRequestFactory());
+        PreemptiveAuthHttpRequestFactory requestFactory =
+                ((PreemptiveAuthHttpRequestFactory) restTemplate.getRequestFactory());
+
         ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
                 requestFactory.getAuthScope(), new UsernamePasswordCredentials(userId, password));
 
         // 2. Search authorizations for user specified by credentials
-        String[] entitlements = null;
-        try {
-            entitlements = restTemplate.getForObject(baseURL + "auth/entitlements.json", String[].class);
-        } catch (HttpClientErrorException e) {
-            LOG.error("While fetching user's entitlements", e);
-        }
-
-        return entitlements;
+        return restTemplate.getForObject(baseURL + "auth/entitlements.json", String[].class);
     }
 
     private boolean isSelfRegistrationAllowed() {

Modified: incubator/syncope/trunk/console/src/main/java/org/apache/syncope/console/pages/Logout.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/console/src/main/java/org/apache/syncope/console/pages/Logout.java?rev=1352100&r1=1352099&r2=1352100&view=diff
==============================================================================
--- incubator/syncope/trunk/console/src/main/java/org/apache/syncope/console/pages/Logout.java (original)
+++ incubator/syncope/trunk/console/src/main/java/org/apache/syncope/console/pages/Logout.java Wed Jun 20 13:49:59 2012
@@ -18,8 +18,12 @@
  */
 package org.apache.syncope.console.pages;
 
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.syncope.client.http.PreemptiveAuthHttpRequestFactory;
 import org.apache.wicket.request.mapper.parameter.PageParameters;
 import org.apache.syncope.console.SyncopeSession;
+import org.apache.wicket.spring.injection.annot.SpringBean;
+import org.springframework.web.client.RestTemplate;
 
 /**
  * Syncope Logout.
@@ -28,6 +32,9 @@ public class Logout extends BasePage {
 
     private static final long serialVersionUID = -2143007520243939450L;
 
+    @SpringBean
+    private RestTemplate restTemplate;
+
     public Logout(final PageParameters parameters) {
         super(parameters);
 
@@ -37,5 +44,10 @@ public class Logout extends BasePage {
         //        getRequestCycle().setRedirect(true);
 
         setResponsePage(getApplication().getHomePage());
+
+        PreemptiveAuthHttpRequestFactory requestFactory =
+                ((PreemptiveAuthHttpRequestFactory) restTemplate.getRequestFactory());
+
+        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().clear();
     }
 }

Modified: incubator/syncope/trunk/console/src/main/resources/applicationContext.xml
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/console/src/main/resources/applicationContext.xml?rev=1352100&r1=1352099&r2=1352100&view=diff
==============================================================================
--- incubator/syncope/trunk/console/src/main/resources/applicationContext.xml (original)
+++ incubator/syncope/trunk/console/src/main/resources/applicationContext.xml Wed Jun 20 13:49:59 2012
@@ -39,7 +39,7 @@ under the License.
   <context:component-scan base-package="org.apache.syncope.console.rest"/>
 
   <bean id="propertyConfigurer"
-          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
+        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="locations">
       <list>
         <value>classpath:configuration.properties</value>
@@ -64,12 +64,27 @@ under the License.
   <bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper"/>
         
   <bean id="mappingJacksonHttpMessageConverter"
-          class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
+        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
     <property name="objectMapper" ref="jacksonObjectMapper"/>
   </bean>
 
   <bean id="httpClientConnManager" class="org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager"/>
-  <bean id="httpClientParams" class="org.apache.http.params.SyncBasicHttpParams"/>
+  
+  <bean id="httpClientParams" class="org.apache.syncope.client.http.HttpClientParams">
+    <property name="parameters">
+      <map key-type="java.lang.String">
+        <entry>
+          <key>
+            <value>http.protocol.handle-authentication</value>
+          </key>
+          <value type="java.lang.Boolean">
+            false
+          </value>
+        </entry>
+      </map>
+    </property>
+  </bean>
+  
   <bean id="httpClientFactory" class="org.apache.syncope.client.http.PreemptiveAuthHttpRequestFactory">
     <constructor-arg value="${host}"/>
     <constructor-arg value="${port}"/>
@@ -82,7 +97,7 @@ under the License.
 
     <property name="errorHandler">
       <bean id="syncopeClientErrorHandler"
-                  class="org.apache.syncope.client.validation.SyncopeClientErrorHandler"/>
+            class="org.apache.syncope.client.validation.SyncopeClientErrorHandler"/>
     </property>
   </bean>
 

Modified: incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/security/SyncopeAuthenticationProvider.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/security/SyncopeAuthenticationProvider.java?rev=1352100&r1=1352099&r2=1352100&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/security/SyncopeAuthenticationProvider.java (original)
+++ incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/security/SyncopeAuthenticationProvider.java Wed Jun 20 13:49:59 2012
@@ -143,12 +143,7 @@ public class SyncopeAuthenticationProvid
 
             LOG.debug("User {} not authenticated", authentication.getPrincipal());
 
-            // By using HttpComponents version 4.2 the request is sent twice in case of exception (SYNCOPE-94) ...
-            // throw new BadCredentialsException("User " + authentication.getPrincipal() + " not authenticated");
-            
-            // ... this is the reason of the following code.
-            token = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(), null, null);
-            token.setDetails(authentication.getDetails());
+            throw new BadCredentialsException("User " + authentication.getPrincipal() + " not authenticated");
         }
 
         return token;

Modified: incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/AbstractTest.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/AbstractTest.java?rev=1352100&r1=1352099&r2=1352100&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/AbstractTest.java (original)
+++ incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/AbstractTest.java Wed Jun 20 13:49:59 2012
@@ -32,7 +32,7 @@ import org.springframework.web.client.Re
 import org.apache.syncope.client.http.PreemptiveAuthHttpRequestFactory;
 
 @RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(locations = { "classpath:restClientContext.xml", "classpath:testJDBCContext.xml" })
+@ContextConfiguration(locations = {"classpath:restClientContext.xml", "classpath:testJDBCContext.xml"})
 public abstract class AbstractTest {
 
     /**
@@ -52,11 +52,16 @@ public abstract class AbstractTest {
         return new RestTemplate();
     }
 
-    @Before
-    public void setupRestTemplate() {
-        PreemptiveAuthHttpRequestFactory requestFactory = ((PreemptiveAuthHttpRequestFactory) restTemplate
-                .getRequestFactory());
+    public void setupRestTemplate(final String uid, final String pwd) {
+        PreemptiveAuthHttpRequestFactory requestFactory =
+                ((PreemptiveAuthHttpRequestFactory) restTemplate.getRequestFactory());
+
         ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials("admin", "password"));
+                requestFactory.getAuthScope(), new UsernamePasswordCredentials(uid, pwd));
+    }
+
+    @Before
+    public void resetRestTemplate() {
+        setupRestTemplate("admin", "password");
     }
 }

Modified: incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/AuthenticationTestITCase.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/AuthenticationTestITCase.java?rev=1352100&r1=1352099&r2=1352100&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/AuthenticationTestITCase.java (original)
+++ incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/AuthenticationTestITCase.java Wed Jun 20 13:49:59 2012
@@ -24,12 +24,10 @@ import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.syncope.client.http.PreemptiveAuthHttpRequestFactory;
 import org.junit.Test;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.client.HttpClientErrorException;
-import org.apache.syncope.client.http.PreemptiveAuthHttpRequestFactory;
 import org.apache.syncope.client.search.AttributeCond;
 import org.apache.syncope.client.search.NodeCond;
 import org.apache.syncope.client.to.AttributeTO;
@@ -53,7 +51,8 @@ public class AuthenticationTestITCase ex
         assertFalse(allEntitlements.isEmpty());
 
         // 2. as admin, read own entitlements
-        super.setupRestTemplate();
+        super.resetRestTemplate();
+
         Set<String> adminEntitlements = new HashSet<String>(Arrays.asList(restTemplate.getForObject(BASE_URL
                 + "auth/entitlements.json", String[].class)));
 
@@ -99,10 +98,7 @@ public class AuthenticationTestITCase ex
         assertNotNull(schemaTO);
 
         // 4. read the schema created above (as user) - success
-        PreemptiveAuthHttpRequestFactory requestFactory = ((PreemptiveAuthHttpRequestFactory) restTemplate.
-                getRequestFactory());
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials(userTO.getUsername(), "password123"));
+        super.setupRestTemplate(userTO.getUsername(), "password123");
 
         schemaTO = restTemplate.getForObject(BASE_URL + "schema/user/read/authTestSchema.json", SchemaTO.class);
         assertNotNull(schemaTO);
@@ -118,7 +114,7 @@ public class AuthenticationTestITCase ex
         assertEquals(HttpStatus.FORBIDDEN, exception.getStatusCode());
 
         // reset admin credentials for restTemplate
-        super.setupRestTemplate();
+        super.resetRestTemplate();
 
         userTO = restTemplate.getForObject(BASE_URL + "user/read/{userId}.json", UserTO.class, userTO.getId());
 
@@ -142,16 +138,12 @@ public class AuthenticationTestITCase ex
         userTO = restTemplate.postForObject(BASE_URL + "user/create", userTO, UserTO.class);
         assertNotNull(userTO);
 
-        PreemptiveAuthHttpRequestFactory requestFactory = ((PreemptiveAuthHttpRequestFactory) restTemplate.
-                getRequestFactory());
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials(userTO.getUsername(), "password123"));
+        super.setupRestTemplate(userTO.getUsername(), "password123");
 
         UserTO readUserTO = restTemplate.getForObject(BASE_URL + "user/read/{userId}.json", UserTO.class, 1);
         assertNotNull(readUserTO);
 
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials("user2", "password"));
+        super.setupRestTemplate("user2", "password");
 
         SyncopeClientException exception = null;
         try {
@@ -163,7 +155,7 @@ public class AuthenticationTestITCase ex
         assertNotNull(exception);
 
         // reset admin credentials for restTemplate
-        super.setupRestTemplate();
+        super.resetRestTemplate();
     }
 
     @Test
@@ -181,10 +173,7 @@ public class AuthenticationTestITCase ex
         userTO = restTemplate.postForObject(BASE_URL + "user/create", userTO, UserTO.class);
         assertNotNull(userTO);
 
-        PreemptiveAuthHttpRequestFactory requestFactory = ((PreemptiveAuthHttpRequestFactory) restTemplate.
-                getRequestFactory());
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials(userTO.getUsername(), "password123"));
+        super.setupRestTemplate(userTO.getUsername(), "password123");
 
         AttributeCond isNullCond = new AttributeCond(AttributeCond.Type.ISNOTNULL);
         isNullCond.setSchema("loginDate");
@@ -200,20 +189,22 @@ public class AuthenticationTestITCase ex
         }
         assertTrue(userIds.contains(1L));
 
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials("user2", "password"));
+        super.setupRestTemplate("user2", "password");
+
+        matchedUsers =
+                Arrays.asList(restTemplate.postForObject(BASE_URL + "user/search", searchCondition, UserTO[].class));
 
-        matchedUsers = Arrays.asList(restTemplate.postForObject(BASE_URL + "user/search", searchCondition,
-                UserTO[].class));
         assertNotNull(matchedUsers);
+
         userIds = new HashSet<Long>(matchedUsers.size());
+
         for (UserTO user : matchedUsers) {
             userIds.add(user.getId());
         }
         assertFalse(userIds.contains(1L));
 
         // reset admin credentials for restTemplate
-        super.setupRestTemplate();
+        super.resetRestTemplate();
     }
 
     @Test
@@ -231,10 +222,7 @@ public class AuthenticationTestITCase ex
         userTO = restTemplate.postForObject(BASE_URL + "user/create", userTO, UserTO.class);
         assertNotNull(userTO);
 
-        PreemptiveAuthHttpRequestFactory requestFactory = ((PreemptiveAuthHttpRequestFactory) restTemplate.
-                getRequestFactory());
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials(userTO.getUsername(), "password123"));
+        super.setupRestTemplate(userTO.getUsername(), "password123");
 
         UserTO readUserTO =
                 restTemplate.getForObject(BASE_URL + "user/read/{userId}.json", UserTO.class, userTO.getId());
@@ -245,8 +233,7 @@ public class AuthenticationTestITCase ex
 
         // authentications failed ...
 
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials(userTO.getUsername(), "wrongpwd1"));
+        super.setupRestTemplate(userTO.getUsername(), "wrongpwd1");
 
         Throwable t = null;
 
@@ -268,15 +255,14 @@ public class AuthenticationTestITCase ex
         }
 
         // reset admin credentials for restTemplate
-        super.setupRestTemplate();
+        super.resetRestTemplate();
 
         readUserTO = restTemplate.getForObject(BASE_URL + "user/read/{userId}.json", UserTO.class, userTO.getId());
         assertNotNull(readUserTO);
         assertNotNull(readUserTO.getFailedLogins());
         assertEquals(Integer.valueOf(2), readUserTO.getFailedLogins());
 
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials(userTO.getUsername(), "password123"));
+        super.setupRestTemplate(userTO.getUsername(), "password123");
 
         readUserTO = restTemplate.getForObject(BASE_URL + "user/read/{userId}.json", UserTO.class, userTO.getId());
         assertNotNull(readUserTO);
@@ -299,10 +285,7 @@ public class AuthenticationTestITCase ex
         userTO = restTemplate.postForObject(BASE_URL + "user/create", userTO, UserTO.class);
         assertNotNull(userTO);
 
-        PreemptiveAuthHttpRequestFactory requestFactory = ((PreemptiveAuthHttpRequestFactory) restTemplate.
-                getRequestFactory());
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials(userTO.getUsername(), "password123"));
+        super.setupRestTemplate(userTO.getUsername(), "password123");
 
         userTO = restTemplate.getForObject(BASE_URL + "user/read/{userId}.json", UserTO.class, userTO.getId());
 
@@ -312,8 +295,7 @@ public class AuthenticationTestITCase ex
 
         // authentications failed ...
 
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials(userTO.getUsername(), "wrongpwd1"));
+        super.setupRestTemplate(userTO.getUsername(), "wrongpwd1");
 
         Throwable t = null;
 
@@ -345,7 +327,7 @@ public class AuthenticationTestITCase ex
         t = null;
 
         // reset admin credentials for restTemplate
-        super.setupRestTemplate();
+        super.resetRestTemplate();
 
         userTO = restTemplate.getForObject(BASE_URL + "user/read/{userId}.json", UserTO.class, userTO.getId());
 
@@ -354,8 +336,7 @@ public class AuthenticationTestITCase ex
         assertEquals(Integer.valueOf(3), userTO.getFailedLogins());
 
         // last authentication before suspension
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials(userTO.getUsername(), "wrongpwd1"));
+        super.setupRestTemplate(userTO.getUsername(), "wrongpwd1");
 
         try {
             restTemplate.getForObject(BASE_URL + "user/read/{userId}.json", UserTO.class, userTO.getId());
@@ -367,7 +348,7 @@ public class AuthenticationTestITCase ex
         t = null;
 
         // reset admin credentials for restTemplate
-        super.setupRestTemplate();
+        super.resetRestTemplate();
 
         userTO = restTemplate.getForObject(BASE_URL + "user/read/{userId}.json", UserTO.class, userTO.getId());
 
@@ -378,8 +359,7 @@ public class AuthenticationTestITCase ex
 
         // check for authentication
 
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials(userTO.getUsername(), "password123"));
+        super.setupRestTemplate(userTO.getUsername(), "password123");
 
         try {
             restTemplate.getForObject(BASE_URL + "user/read/{userId}.json", UserTO.class, userTO.getId());
@@ -392,15 +372,14 @@ public class AuthenticationTestITCase ex
         t = null;
 
         // reset admin credentials for restTemplate
-        super.setupRestTemplate();
+        super.resetRestTemplate();
 
         userTO = restTemplate.getForObject(BASE_URL + "user/reactivate/" + userTO.getId(), UserTO.class);
 
         assertNotNull(userTO);
         assertEquals("active", userTO.getStatus());
 
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials(userTO.getUsername(), "password123"));
+        super.setupRestTemplate(userTO.getUsername(), "password123");
 
         userTO = restTemplate.getForObject(BASE_URL + "user/read/{userId}.json", UserTO.class, userTO.getId());
 
@@ -438,10 +417,7 @@ public class AuthenticationTestITCase ex
         role1Admin = restTemplate.postForObject(BASE_URL + "user/create", role1Admin, UserTO.class);
         assertNotNull(role1Admin);
 
-        PreemptiveAuthHttpRequestFactory requestFactory = ((PreemptiveAuthHttpRequestFactory) restTemplate.
-                getRequestFactory());
-        ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
-                requestFactory.getAuthScope(), new UsernamePasswordCredentials(role1Admin.getUsername(), "password"));
+        super.setupRestTemplate(role1Admin.getUsername(), "password");
 
         // User with role 1, created by user with child role created above
         UserTO role1User = UserTestITCase.getSampleTO("syncope48user@apache.org");
@@ -453,6 +429,6 @@ public class AuthenticationTestITCase ex
         assertNotNull(role1User);
 
         // reset admin credentials for restTemplate
-        super.setupRestTemplate();
+        super.resetRestTemplate();
     }
 }

Modified: incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/ReportTestITCase.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/ReportTestITCase.java?rev=1352100&r1=1352099&r2=1352100&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/ReportTestITCase.java (original)
+++ incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/ReportTestITCase.java Wed Jun 20 13:49:59 2012
@@ -25,10 +25,17 @@ import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import org.apache.http.HttpHost;
 import org.apache.http.HttpResponse;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.client.AuthCache;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.protocol.ClientContext;
+import org.apache.http.impl.auth.BasicScheme;
+import org.apache.http.impl.client.BasicAuthCache;
 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.protocol.BasicHttpContext;
 import org.apache.http.util.EntityUtils;
 import org.junit.Test;
 import org.springframework.http.HttpStatus;
@@ -191,6 +198,17 @@ public class ReportTestITCase extends Ab
         // 1. XML (default)
 
         final HttpClient client = ((PreemptiveAuthHttpRequestFactory) restTemplate.getRequestFactory()).getHttpClient();
+        final AuthScope scope = ((PreemptiveAuthHttpRequestFactory) restTemplate.getRequestFactory()).getAuthScope();
+        final HttpHost targetHost = new HttpHost(scope.getHost(), scope.getPort(), scope.getScheme());
+
+
+        // Add AuthCache to the execution context
+        BasicHttpContext localcontext = new BasicHttpContext();
+
+        // Generate BASIC scheme object and add it to the local auth cache
+        AuthCache authCache = new BasicAuthCache();
+        authCache.put(targetHost, new BasicScheme());
+        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
 
         HttpResponse response = null;
 
@@ -207,7 +225,7 @@ public class ReportTestITCase extends Ab
             } catch (InterruptedException e) {
             }
 
-            response = client.execute(getMethod);
+            response = client.execute(targetHost, getMethod, localcontext);
 
             maxit--;
         } while ((response == null || response.getStatusLine().getStatusCode() != 200) && maxit > 0);
@@ -220,8 +238,7 @@ public class ReportTestITCase extends Ab
 
         // 2. HTML
         getMethod = new HttpGet(BASE_URL + "report/execution/export/" + postExecIds.iterator().next() + "?fmt=HTML");
-        response = ((PreemptiveAuthHttpRequestFactory) restTemplate.getRequestFactory()).getHttpClient().execute(
-                getMethod);
+        response = client.execute(targetHost, getMethod, localcontext);
         assertEquals(200, response.getStatusLine().getStatusCode());
 
         export = EntityUtils.toString(response.getEntity()).trim();
@@ -230,8 +247,7 @@ public class ReportTestITCase extends Ab
 
         // 3. PDF
         getMethod = new HttpGet(BASE_URL + "report/execution/export/" + postExecIds.iterator().next() + "?fmt=PDF");
-        response = ((PreemptiveAuthHttpRequestFactory) restTemplate.getRequestFactory()).getHttpClient().execute(
-                getMethod);
+        response = client.execute(targetHost, getMethod, localcontext);
         assertEquals(200, response.getStatusLine().getStatusCode());
 
         export = EntityUtils.toString(response.getEntity()).trim();
@@ -240,8 +256,7 @@ public class ReportTestITCase extends Ab
 
         // 4. RTF
         getMethod = new HttpGet(BASE_URL + "report/execution/export/" + postExecIds.iterator().next() + "?fmt=RTF");
-        response = ((PreemptiveAuthHttpRequestFactory) restTemplate.getRequestFactory()).getHttpClient().execute(
-                getMethod);
+        response = client.execute(targetHost, getMethod, localcontext);
         assertEquals(200, response.getStatusLine().getStatusCode());
 
         export = EntityUtils.toString(response.getEntity()).trim();

Modified: incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/RoleTestITCase.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/RoleTestITCase.java?rev=1352100&r1=1352099&r2=1352100&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/RoleTestITCase.java (original)
+++ incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/RoleTestITCase.java Wed Jun 20 13:49:59 2012
@@ -182,7 +182,7 @@ public class RoleTestITCase extends Abst
         assertFalse(roleTO.getAttributes().isEmpty());
 
         // restore admin authentication
-        super.setupRestTemplate();
+        super.resetRestTemplate();
     }
 
     @Test

Modified: incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/UserRequestTestITCase.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/UserRequestTestITCase.java?rev=1352100&r1=1352099&r2=1352100&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/UserRequestTestITCase.java (original)
+++ incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/UserRequestTestITCase.java Wed Jun 20 13:49:59 2012
@@ -95,7 +95,7 @@ public class UserRequestTestITCase exten
         assertNotNull(request);
 
         // 5. switch back to admin
-        super.setupRestTemplate();
+        super.resetRestTemplate();
 
         // 6. try to find user
         AttributeCond attrCond = new AttributeCond(AttributeCond.Type.EQ);
@@ -156,7 +156,7 @@ public class UserRequestTestITCase exten
         assertNotNull(request);
 
         // 6. switch back to admin
-        super.setupRestTemplate();
+        super.resetRestTemplate();
 
         // 7. user password has not changed yet
         Boolean verify = restTemplate.getForObject(BASE_URL + "user/verifyPassword/{username}.json?password="
@@ -204,7 +204,7 @@ public class UserRequestTestITCase exten
         assertNotNull(request);
 
         // 5. switch back to admin
-        super.setupRestTemplate();
+        super.resetRestTemplate();
 
         // 6. user still exists
         UserTO actual = restTemplate.getForObject(BASE_URL + "user/read/{userId}.json", UserTO.class, userTO.getId());

Modified: incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/UserTestITCase.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/UserTestITCase.java?rev=1352100&r1=1352099&r2=1352100&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/UserTestITCase.java (original)
+++ incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/UserTestITCase.java Wed Jun 20 13:49:59 2012
@@ -659,7 +659,7 @@ public class UserTestITCase extends Abst
         assertEquals("rejected", userTO.getStatus());
 
         // reset admin credentials for restTemplate
-        super.setupRestTemplate();
+        super.resetRestTemplate();
     }
 
     @Test

Modified: incubator/syncope/trunk/core/src/test/resources/restClientContext.xml
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/test/resources/restClientContext.xml?rev=1352100&r1=1352099&r2=1352100&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/test/resources/restClientContext.xml (original)
+++ incubator/syncope/trunk/core/src/test/resources/restClientContext.xml Wed Jun 20 13:49:59 2012
@@ -24,7 +24,22 @@ under the License.
                            http://www.springframework.org/schema/beans/spring-beans.xsd">
 
   <bean id="httpClientConnManager" class="org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager"/>
-  <bean id="httpClientParams" class="org.apache.http.params.SyncBasicHttpParams"/>
+  
+  <bean id="httpClientParams" class="org.apache.syncope.client.http.HttpClientParams">
+    <property name="parameters">
+      <map key-type="java.lang.String">
+        <entry>
+          <key>
+            <value>http.protocol.handle-authentication</value>
+          </key>
+          <value type="java.lang.Boolean">
+            false
+          </value>
+        </entry>
+      </map>
+    </property>
+  </bean>
+  
   <bean id="httpClientFactory" class="org.apache.syncope.client.http.PreemptiveAuthHttpRequestFactory">
     <constructor-arg value="localhost"/>
     <constructor-arg value="9080"/>
@@ -32,12 +47,13 @@ under the License.
     <constructor-arg ref="httpClientConnManager"/>
     <constructor-arg ref="httpClientParams"/>
   </bean>
+  
   <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
     <constructor-arg ref="httpClientFactory"/>
 
     <property name="errorHandler">
       <bean id="syncopeClientErrorHandler"
-                  class="org.apache.syncope.client.validation.SyncopeClientErrorHandler"/>
+            class="org.apache.syncope.client.validation.SyncopeClientErrorHandler"/>
     </property>
   </bean>