Skip to content

Commit

Permalink
[GH-309] Allow running in development mode without configuring mail s…
Browse files Browse the repository at this point in the history
…erver.
  • Loading branch information
ledsoft committed Nov 13, 2024
1 parent 44541bf commit f3f6daf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 47 deletions.
11 changes: 9 additions & 2 deletions src/main/java/cz/cvut/kbss/termit/service/mail/Postman.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import cz.cvut.kbss.termit.exception.PostmanException;
import cz.cvut.kbss.termit.exception.ValidationException;
import cz.cvut.kbss.termit.util.Utils;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
Expand Down Expand Up @@ -65,7 +66,12 @@ public Postman(Environment env, @Autowired(required = false) JavaMailSender mail

@PostConstruct
public void postConstruct() {
if(mailSender == null) {
if (mailSender == null) {
if (Utils.isDevelopmentProfile(env.getActiveProfiles())) {
LOG.warn(
"Mail server not configured but running in development mode. Will not be able to send messages.");
return;
}
throw new ValidationException("Mail server not configured.");
}
}
Expand All @@ -86,7 +92,8 @@ public void sendMessage(Message message) {

final MimeMessage mail = mailSender.createMimeMessage();
final MimeMessageHelper helper = new MimeMessageHelper(mail, true);
helper.setFrom(new InternetAddress(sender != null ? sender : senderUsername, FROM_NICKNAME, StandardCharsets.UTF_8.toString()));
helper.setFrom(new InternetAddress(sender != null ? sender : senderUsername, FROM_NICKNAME,
StandardCharsets.UTF_8.toString()));
helper.setTo(message.getRecipients().toArray(new String[]{}));
helper.setSubject(message.getSubject());
helper.setText(message.getContent(), true);
Expand Down
45 changes: 17 additions & 28 deletions src/main/java/cz/cvut/kbss/termit/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ public class Constants {
"Notation", "Example", "References")
);


/**
* the maximum amount of data to buffer when sending messages to a WebSocket session
*/
public static final int WEBSOCKET_SEND_BUFFER_SIZE_LIMIT = Integer.MAX_VALUE;

/**
* Set the maximum time allowed in milliseconds after the WebSocket connection is established
* and before the first sub-protocol message is received.
*/
public static final int WEBSOCKET_TIME_TO_FIRST_MESSAGE = 15 * 1000 /* 15s */;

/**
* Development Spring profile.
*/
public static final String DEVELOPMENT_PROFILE = "development";

private Constants() {
throw new AssertionError();
}
Expand Down Expand Up @@ -247,32 +264,4 @@ private QueryParams() {
throw new AssertionError();
}
}

public static final class DebouncingGroups {

/**
* Text analysis of all terms in specific vocabulary
*/
public static final String TEXT_ANALYSIS_VOCABULARY_TERMS_ALL_DEFINITIONS = "TEXT_ANALYSIS_VOCABULARY_TERMS_ALL_DEFINITIONS";

/**
* Text analysis of all vocabularies
*/
public static final String TEXT_ANALYSIS_VOCABULARY = "TEXT_ANALYSIS_VOCABULARY";

private DebouncingGroups() {
throw new AssertionError();
}
}

/**
* the maximum amount of data to buffer when sending messages to a WebSocket session
*/
public static final int WEBSOCKET_SEND_BUFFER_SIZE_LIMIT = Integer.MAX_VALUE;

/**
* Set the maximum time allowed in milliseconds after the WebSocket connection is established
* and before the first sub-protocol message is received.
*/
public static final int WEBSOCKET_TIME_TO_FIRST_MESSAGE = 15 * 1000 /* 15s */;
}
43 changes: 26 additions & 17 deletions src/main/java/cz/cvut/kbss/termit/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -194,13 +195,20 @@ public static String getVocabularyIri(final Set<String> conceptUris, String term
if (conceptUris.isEmpty()) {
throw new IllegalArgumentException("No namespace candidate.");
}

final Iterator<String> i = conceptUris.iterator();

final String conceptUri = i.next();
final String namespace = extractNamespace(termSeparator, conceptUri);
for (final String s : conceptUris) {
if (!s.startsWith(namespace)) {
throw new IllegalArgumentException(
"Not all Concept IRIs have the same namespace: " + conceptUri + " vs. " + namespace);
}
}
return namespace;
}

private static String extractNamespace(String termSeparator, String conceptUri) {
final String separator;

if (conceptUri.lastIndexOf(termSeparator) > 0) {
separator = termSeparator;
} else if (conceptUri.lastIndexOf("#") > 0) {
Expand All @@ -210,16 +218,7 @@ public static String getVocabularyIri(final Set<String> conceptUris, String term
} else {
throw new IllegalArgumentException("The IRI does not have a proper format: " + conceptUri);
}

final String namespace = conceptUri.substring(0, conceptUri.lastIndexOf(separator));

for (final String s : conceptUris) {
if (!s.startsWith(namespace)) {
throw new IllegalArgumentException(
"Not all Concept IRIs have the same namespace: " + conceptUri + " vs. " + namespace);
}
}
return namespace;
return conceptUri.substring(0, conceptUri.lastIndexOf(separator));
}

/**
Expand Down Expand Up @@ -402,15 +401,25 @@ public static void pruneBlankTranslations(MultilingualString str) {

/**
* Converts the map into a string
* @return Empty string when the map is {@code null}, otherwise the String in format
* {@code {key=value, key=value}}
*
* @return Empty string when the map is {@code null}, otherwise the String in format {@code {key=value, key=value}}
*/
public static <A, B> String mapToString(Map<A, B> map) {
if (map == null) {
return "";
}
return map.keySet().stream()
.map(key -> key + "=" + map.get(key))
.collect(Collectors.joining(", ", "{", "}"));
.map(key -> key + "=" + map.get(key))
.collect(Collectors.joining(", ", "{", "}"));
}

/**
* Checks whether the {@code development} profile is active.
*
* @param activeProfiles Array of active profiles
* @return {@code true} if the {@code development} profile is active, {@code false} otherwise
*/
public static boolean isDevelopmentProfile(String[] activeProfiles) {
return Arrays.binarySearch(activeProfiles, Constants.DEVELOPMENT_PROFILE) != -1;
}
}

0 comments on commit f3f6daf

Please sign in to comment.