Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions src/main/java/com/uci/adapter/app/config/AppConfiguration1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.uci.utils.BotService;
import com.uci.utils.dto.BotServiceParams;
import com.uci.utils.CampaignService;
import io.fusionauth.client.FusionAuthClient;

import okhttp3.OkHttpClient;
import java.time.Duration;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
Expand All @@ -19,6 +21,8 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
Expand All @@ -28,36 +32,36 @@

@Configuration
@EnableAutoConfiguration
public class AppConfiguration1 {
public class AppConfiguration {

@Value("${campaign.url}")
public String CAMPAIGN_URL;
private String campaignUrl;

@Value("${fusionauth.url}")
public String FUSIONAUTH_URL;
private String fusionAuthUrl;

@Value("${fusionauth.key}")
public String FUSIONAUTH_KEY;

private String fusionAuthKey;

@Autowired
public Cache<Object, Object> cache;
private Cache<Object, Object> cache;

// Default RestTemplate
@Bean
@Qualifier("rest")
public RestTemplate getRestTemplate() {
return new RestTemplate();
}

// RestTemplate with Basic Auth
@Bean
@Qualifier("custom")
public RestTemplate getCustomTemplate(RestTemplateBuilder builder) {
Credentials credentials = new UsernamePasswordCredentials("test", "abcd1234");
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, credentials);

HttpClient httpClient = HttpClients
.custom()
HttpClient httpClient = HttpClients.custom()
.setDefaultCredentialsProvider(credentialsProvider)
.disableAuthCaching()
.build();
Expand All @@ -67,6 +71,7 @@ public RestTemplate getCustomTemplate(RestTemplateBuilder builder) {
.build();
}

// JSON-enabled RestTemplate
@Bean
@Qualifier("json")
public RestTemplate getJSONRestTemplate() {
Expand All @@ -75,28 +80,28 @@ public RestTemplate getJSONRestTemplate() {
.build();
}

@Bean
public BotServiceParams getBotServiceParams() {
return new BotServiceParams();
}

// FusionAuth client
@Bean
public FusionAuthClient getFAClient() {
return new FusionAuthClient(FUSIONAUTH_KEY, FUSIONAUTH_URL);
return new FusionAuthClient(fusionAuthKey, fusionAuthUrl);
}

// BotService using WebClient and FusionAuth
@Bean
public BotService getBotService() {
WebClient webClient = WebClient.builder()
.baseUrl(CAMPAIGN_URL)
.baseUrl(campaignUrl)
.build();

return new BotService(webClient, getFAClient(), cache, getBotServiceParams());
return new BotService(webClient, getFAClient(), cache);
}

// Optional: Provide default in-memory Caffeine cache if not injected externally
@Bean
public OkHttpClient getOkHttpClient() {
return new OkHttpClient().newBuilder().build();
public Cache<Object, Object> caffeineCache() {
return Caffeine.newBuilder()
.expireAfterWrite(Duration.ofMinutes(30))
.maximumSize(1000)
.build();
}

}
Loading