org.opentestsystem.authoring.testspecbank.client.config.TestClientIntegratedConfigScanner.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.authoring.testspecbank.client.config.TestClientIntegratedConfigScanner.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/
package org.opentestsystem.authoring.testspecbank.client.config;

import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.springframework.aop.target.PoolingConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails;
import org.springframework.security.oauth2.common.AuthenticationScheme;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.client.RestTemplate;

import tds.shared.spring.configuration.WebConfiguration;

@Profile("test-config")
public class TestClientIntegratedConfigScanner {

    //just inject regular rest template for testing only
    @Bean
    public RestTemplate tsbRestTemplate() {
        return new RestTemplate();
    }

    @Bean
    public ResourceOwnerPasswordResourceDetails resourceDetails() {
        final ResourceOwnerPasswordResourceDetails resourceDetails = new ResourceOwnerPasswordResourceDetails();

        resourceDetails.setUsername("user-name");
        resourceDetails.setPassword("user-password");
        resourceDetails.setAccessTokenUri("https://sso-example.com/auth/oauth2/access_token?realm=/sbac");
        resourceDetails.setClientId("client-id");
        resourceDetails.setClientSecret("client-secret");
        resourceDetails.setGrantType("password");

        return resourceDetails;
    }

    @Bean
    public OAuth2RestTemplate oauthRestTemplate() {
        return new OAuth2RestTemplate(resourceDetails(), new DefaultOAuth2ClientContext());
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer() {

            @Override
            protected Properties mergeProperties() throws IOException {
                Properties props = super.mergeProperties();
                props.put("tsb.tsbUrl", "http://go.no.where");
                return props;
            }
        };
    }
}