Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

caffeine cache changes #13

Open
wants to merge 3 commits into
base: release-4.5.0
Choose a base branch
from
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
17 changes: 14 additions & 3 deletions src/main/java/com/uci/adapter/app/config/AppConfiguration1.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package com.uci.adapter.app.config;

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.CampaignService;
import io.fusionauth.client.FusionAuthClient;

import java.time.Duration;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 Down Expand Up @@ -67,6 +75,9 @@ public RestTemplate getJSONRestTemplate() {

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

@Autowired
public Cache<Object, Object> cache;

@Bean
public FusionAuthClient getFAClient() {
Expand All @@ -75,10 +86,10 @@ public FusionAuthClient getFAClient() {

@Bean
public BotService getBotService() {

WebClient webClient = WebClient.builder()
WebClient webClient = WebClient.builder()
.baseUrl(CAMPAIGN_URL)
.build();
return new BotService(webClient, getFAClient());

return new BotService(webClient, getFAClient(), cache);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Function;

@Slf4j
Expand Down Expand Up @@ -50,6 +51,11 @@ public Mono<XMessage> convertMessageToXMsg(Object message) throws JAXBException,
XMessage.MessageType messageType= XMessage.MessageType.TEXT;
//Todo: How to get Button choices from normal text
from.setUserID(webMessage.getFrom());

/* To use later in outbound reply message's message id & to */
messageIdentifier.setChannelMessageId(webMessage.getMessageId());
messageIdentifier.setReplyId(webMessage.getTo());

XMessage x = XMessage.builder()
.to(to)
.from(from)
Expand All @@ -68,6 +74,7 @@ public Mono<XMessage> convertMessageToXMsg(Object message) throws JAXBException,
public Mono<XMessage> processOutBoundMessageF(XMessage xMsg) throws Exception {
log.info("Sending message to transport socket :: " + xMsg.toXML());
OutboundMessage outboundMessage = getOutboundMessage(xMsg);
log.info("Sending final xmessage to transport socket :: " + xMsg.toXML());
// String url = PropertiesCache.getInstance().getProperty("SUNBIRD_OUTBOUND");
String url = "http://transport-socket.ngrok.samagra.io/botMsg/adapterOutbound";
return SunbirdWebService.getInstance().
Expand Down Expand Up @@ -113,10 +120,50 @@ public XMessage callOutBoundAPI(XMessage xMsg) throws Exception{
// return sc;
// }

private OutboundMessage getOutboundMessage(XMessage xMsg) {
SunbirdMessage sunbirdMessage = SunbirdMessage.builder().title(
xMsg.getPayload().getText() + renderMessageChoices(xMsg.getPayload().getButtonChoices())).choices(xMsg.getPayload().getButtonChoices()).build();
return OutboundMessage.builder().message(sunbirdMessage).build();
private OutboundMessage getOutboundMessage(XMessage xMsg) throws JAXBException {
SunbirdMessage sunbirdMessage = SunbirdMessage.builder()
.title(getTextMessage(xMsg))
.choices(this.getButtonChoices(xMsg))
.build();
return OutboundMessage.builder()
.message(sunbirdMessage)
.to(xMsg.getMessageId().getReplyId())
.messageId(xMsg.getMessageId().getChannelMessageId())
.build();
}

/**
* Get Simplified Text Message
* @param xMsg
* @return String
*/
private String getTextMessage(XMessage xMsg) {
XMessagePayload payload = xMsg.getPayload();
String text = payload.getText().replace("__", "");
text = text.replace("\n\n", "");
payload.setText(text);
return text;
}

/**
* Get Button Choices with calculated keys
* @param xMsg
* @return ArrayList of ButtonChoices
*/
private ArrayList<ButtonChoice> getButtonChoices(XMessage xMsg) {
ArrayList<ButtonChoice> choices = xMsg.getPayload().getButtonChoices();
if(choices != null) {
choices.forEach(c -> {
String[] a = c.getText().split(" ");
if(a[0] != null && !a[0].isEmpty()) {
String key = a[0].toString();

c.setKey(key);
c.setText(c.getText().replaceFirst(key, "").trim());
}
});
}
return choices;
}

private String renderMessageChoices(ArrayList<ButtonChoice> buttonChoices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
@Builder
public class OutboundMessage {
private SunbirdMessage message;
private String to;
private String messageId;
}