/*
 * Copyright (C) 2013 tarent AG
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package org.osiam.auth.login;

import org.osiam.auth.oauth_client.OsiamAuthServerClientProvider;
import org.osiam.auth.token.OsiamAccessTokenProvider;
import org.osiam.client.OsiamConnector;
import org.osiam.client.query.Query;
import org.osiam.client.query.QueryBuilder;
import org.osiam.resources.scim.SCIMSearchResult;
import org.osiam.resources.scim.UpdateUser;
import org.osiam.resources.scim.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class ResourceServerConnector {

    @Value("${org.osiam.resource-server.home}")
    private String resourceServerHome;

    @Value("${org.osiam.auth-server.home}")
    private String authServerHome;

    @Autowired
    private OsiamAccessTokenProvider osiamAccessTokenProvider;

    @Autowired
    private OsiamAuthServerClientProvider authServerClientProvider;

    public User getUserByUsername(final String userName) {
        Query query = new QueryBuilder().filter("userName eq \"" + userName + "\"").build();

        SCIMSearchResult<User> result = createOsiamConnector().searchUsers(query,
                osiamAccessTokenProvider.createAccessToken());

        if (result.getTotalResults() != 1) {
            return null;
        } else {
            return result.getResources().get(0);
        }
    }

    public User getUserById(final String id) {
        return createOsiamConnector().getUser(id, osiamAccessTokenProvider.createAccessToken());
    }

    public User createUser(User user) {
        return createOsiamConnector().createUser(user, osiamAccessTokenProvider.createAccessToken());
    }

    public User updateUser(String userId, UpdateUser user) {
        return createOsiamConnector().updateUser(userId, user, osiamAccessTokenProvider.createAccessToken());
    }

    public User searchUserByUserNameAndPassword(String userName, String hashedPassword) {
        Query query = new QueryBuilder().filter("userName eq \"" + userName + "\""
                + " and password eq \"" + hashedPassword + "\"").build();

        SCIMSearchResult<User> result = createOsiamConnector().searchUsers(
                query, osiamAccessTokenProvider.createAccessToken()
        );

        if (result.getTotalResults() != 1) {
            return null;
        } else {
            return result.getResources().get(0);
        }
    }

    private OsiamConnector createOsiamConnector() {
        return new OsiamConnector.Builder().
                setAuthServerEndpoint(authServerHome).
                setResourceServerEndpoint(resourceServerHome).
                setClientId(OsiamAuthServerClientProvider.AUTH_SERVER_CLIENT_ID).
                setClientSecret(authServerClientProvider.getClientSecret()).build();
    }
}

@@ -2061,24 +2061,144 @@
 tProvider;%0A%0A
+    static %7B%0A        OsiamConnector.setMaxConnections(40);%0A        OsiamConnector.setMaxConnectionsPerRoute(40);%0A    %7D%0A%0A
     public U
@@ -3767,17 +3767,16 @@
 uilder()
-.
 %0A       
@@ -3784,16 +3784,17 @@
         
+.
 setAuthS
@@ -3814,33 +3814,32 @@
 (authServerHome)
-.
 %0A               
@@ -3839,16 +3839,17 @@
         
+.
 setResou
@@ -3885,17 +3885,16 @@
 verHome)
-.
 %0A       
@@ -3894,32 +3894,33 @@
                 
+.
 setClientId(Osia
@@ -3967,17 +3967,16 @@
 IENT_ID)
-.
 %0A       
@@ -3984,16 +3984,17 @@
         
+.
 setClien
@@ -4044,16 +4044,115 @@
 ecret())
+%0A                .withReadTimeout(10000)%0A                .withConnectTimeout(5000)%0A                
 .build()

Increase timeouts and parallel connections to resource-server