This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
Commit 8bacf47e authored by Michael Breu's avatar Michael Breu :speech_balloon:
Browse files

Intermediate commit: Message Service

parent e880b540
2 merge requests!188Merging Peer Reviewing et. al to Master,!164211 peer reviewing functionality
......@@ -43,8 +43,8 @@ public class GitLabRepository {
return new GitLabApi(accessInfo.get().getGitLabAccessIssuer(), TokenType.OAUTH2_ACCESS, accessInfo.get().getAccessToken());
}
return new GitLabApi(applicationProperties.getGitLab().getUrl(), TokenType.PRIVATE, applicationProperties.getGitLab().getGeneralAccessToken());
}
private Optional<String> getAccessTokenOfCurrentUser() {
Optional<String> accessTokenO = tokenProvider.getGitLabAccessToken();
return accessTokenO;
......
package at.ac.uibk.gitsearch.service;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.annotation.PostConstruct;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.client.ClientConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import at.ac.uibk.gitsearch.config.ApplicationProperties;
import at.ac.uibk.gitsearch.repository.gitlab.GitLabRepository;
/**
* this service provides messages. To make things simple, we use gitlab to
* manage the (broadcast) messages.
*
* @author Michael Breu
*
*/
@Service
public class MessageService {
public static class BroadCastMessage {
public String message;
public Date starts_at;
public Date ends_at;
public String color; // :"#E75E40",
public String font; // #FFFFFF",
public int id; // 1,
public boolean active; // false,
public String target_path; // "*/welcome",
public String broadcast_type; // "banner",
public boolean dismissable; // false
}
private static final Logger log = LoggerFactory.getLogger(MessageService.class);
@Autowired
protected ApplicationProperties applicationProperties;
BroadCastMessage[] messages = null;
public BroadCastMessage[] getMessages() {
return messages;
}
private static final long PLUGIN_RECHECK_TIMEOUT = 10 * 60 * 1000L; // every 10 Minutes
@PostConstruct
public void startReloadTimer() {
Timer check = new Timer("MessageReloadTimer");
check.schedule(new MessageLoaderTask(), 0, PLUGIN_RECHECK_TIMEOUT);
}
public class MessageLoaderTask extends TimerTask {
public void run() {
ClientConfig restClientConfig = new ClientConfig();
restClientConfig.register(JacksonJsonProvider.class);
Client client = ClientBuilder.newClient(restClientConfig);
WebTarget target = client.target(applicationProperties.getGitLab().getUrl() + "/api/v4/broadcast_messages");
try {
messages = target.request().accept(MediaType.APPLICATION_JSON).get(BroadCastMessage[].class);
} catch (Exception e) {
log.warn("Cannot load broadcast messages from {}", e.getMessage());
}
}
}
/**
* this is for testing mainly.
*/
public void triggerReload() {
new MessageLoaderTask().run();
}
}
package at.ac.uibk.gitsearch.service.management;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.boot.actuate.health.HealthContributor;
import org.springframework.boot.actuate.health.HealthContributorRegistry;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
import org.springframework.boot.actuate.health.Status;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import at.ac.uibk.gitsearch.config.ApplicationProperties;
......@@ -29,6 +22,8 @@ import at.ac.uibk.gitsearch.service.PluginManagementService.PluginConfigWrapper;
//@Endpoint(id = "pluginHealth")
public class PluginHealthCheckRegistry {
private static final Logger log = LoggerFactory.getLogger(PluginHealthCheckRegistry.class);
@Autowired
protected PluginManagementService pluginManagementService;
@Autowired
......@@ -38,7 +33,12 @@ public class PluginHealthCheckRegistry {
@PostConstruct
protected void registerPluginHealth() {
for (String configURL : applicationProperties.getRegisteredPlugins()) {
final List<String> registeredPlugins = applicationProperties.getRegisteredPlugins();
if(registeredPlugins==null) {
log.warn("No plugins defined?");
return;
}
for (String configURL : registeredPlugins) {
healthContributorRegistry.registerContributor(configURL,
new PluginHealthCheck(configURL, pluginManagementService));
}
......
package at.ac.uibk.gitsearch.service;
import java.io.IOException;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import at.ac.uibk.gitsearch.GitsearchApp;
import at.ac.uibk.gitsearch.service.MessageService.BroadCastMessage;
@SpringBootTest(classes = GitsearchApp.class)
public class MessageServiceIT {
@Autowired
MessageService messageService;
@Test
public void loadMessages() throws JsonParseException, JsonMappingException, IOException {
BroadCastMessage[] messages = messageService.getMessages();
if(messages==null) {
// give it more time (or trigger reload)
messageService.triggerReload();
messages = messageService.getMessages();
}
Assert.assertNotNull("This may fail, due to timing problems", messages);
Assert.assertThat("This may fail, if there are not messages", messages, Matchers.arrayWithSize(2));
}
}
......@@ -142,5 +142,8 @@ application:
highlight-pre: <mark><strong>
highlight-post: </strong></mark>
gitlab:
url: http://dev-exchange.codeability.org
mainGroup: exchange
url: https://sharing.codeability-austria.uibk.ac.at/
generalAccessToken: zPxPmJE3UXAZJpBzxqej
registeredPlugins:
- "http://localhost:8081/api/sharing/config" # may not be reachable for testing!
- "http://localhost:8082/api/sharingPluginConfig" # may not be reachable for testing!
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment