diff --git a/pom.xml b/pom.xml index 1c5ffb8..15d2817 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.umutayb Utilities - 1.6.8 + 1.6.9 jar Java-Utilities diff --git a/src/main/java/api_assured/ApiUtilities.java b/src/main/java/api_assured/ApiUtilities.java index 67fc4dc..39002f4 100644 --- a/src/main/java/api_assured/ApiUtilities.java +++ b/src/main/java/api_assured/ApiUtilities.java @@ -20,7 +20,7 @@ @SuppressWarnings("unused") public abstract class ApiUtilities extends Caller { - public Printer log = new Printer(this.getClass()); + public static Printer log = new Printer(ApiUtilities.class); /** * Converts file to multipart @@ -29,7 +29,7 @@ public abstract class ApiUtilities extends Caller { * @param name desired name for the multipart * @return returns the multipart */ - public MultipartBody.Part getMultipartFromFile(File file, String name) { + public static MultipartBody.Part getMultipartFromFile(File file, String name) { RequestBody body = getRequestBodyFromFile(file); log.info("Creating multipart from " + file.getName() + " file"); return MultipartBody.Part.createFormData(name, file.getName(), body); @@ -43,7 +43,7 @@ public MultipartBody.Part getMultipartFromFile(File file, String name) { * @param mediaType desired media type * @return returns the multipart */ - public MultipartBody.Part getMultipartFromFile(File file, String name, String mediaType) { + public static MultipartBody.Part getMultipartFromFile(File file, String name, String mediaType) { RequestBody body = getRequestBodyFromFile(file, mediaType); log.info("Creating multipart from " + file.getName() + " file"); return MultipartBody.Part.createFormData(name, file.getName(), body); @@ -55,7 +55,7 @@ public MultipartBody.Part getMultipartFromFile(File file, String name, String me * @param file target file * @return returns the RequestBody */ - public RequestBody getRequestBodyFromFile(File file) { + public static RequestBody getRequestBodyFromFile(File file) { String mediaType; try { mediaType = Files.probeContentType(file.toPath()); @@ -72,7 +72,7 @@ public RequestBody getRequestBodyFromFile(File file) { * @param mediaType desired media type * @return returns the RequestBody */ - public RequestBody getRequestBodyFromFile(File file, String mediaType) { + public static RequestBody getRequestBodyFromFile(File file, String mediaType) { log.info("Generating request body from " + file.getName() + " file"); return RequestBody.create(file, MediaType.parse(mediaType)); } diff --git a/src/main/java/utils/FileUtilities.java b/src/main/java/utils/FileUtilities.java index f6779e9..6f2999d 100644 --- a/src/main/java/utils/FileUtilities.java +++ b/src/main/java/utils/FileUtilities.java @@ -88,9 +88,19 @@ public static String getAbsolutePath(String relativePath){ * @return The contents of the file as a string. */ public static String getString(String directory, String fileName) { - try {return new String(Files.readAllBytes(Paths.get(directory+"/"+fileName)));} + return getString(directory+"/"+fileName); + } + + /** + * Returns the contents of a file as a string. + * + * @param directory The directory where the file is located. + * @return The contents of the file as a string. + */ + public static String getString(String directory) { + try {return new String(Files.readAllBytes(Paths.get(directory)));} catch (IOException exception){ - Assert.fail(StringUtilities.markup(YELLOW, fileName+" not found!")); + Assert.fail(StringUtilities.markup(YELLOW, "File at '" + directory + "' not found!")); return null; } } @@ -108,6 +118,22 @@ public static Optional createIfAbsent(String pathname){ return Optional.empty(); } + /** + * Saves a content to a file. + * + * @param directory The directory where the file should be saved. + * @throws RuntimeException if an exception occurs while writing the file. + */ + public static void saveFile(String content, String directory){ + try { + FileWriter file = new FileWriter(directory); + file.write(String.valueOf(content)); + file.close(); + } + catch (Exception gamma){Assert.fail(String.valueOf(gamma));} + } + + /** * Writes a string to a file. * diff --git a/src/main/java/utils/email/EmailUtilities.java b/src/main/java/utils/email/EmailUtilities.java index d68f5e0..6c4ef31 100644 --- a/src/main/java/utils/email/EmailUtilities.java +++ b/src/main/java/utils/email/EmailUtilities.java @@ -1,10 +1,9 @@ package utils.email; import collections.Pair; +import context.ContextStore; import jakarta.mail.*; -import jakarta.mail.internet.InternetAddress; -import jakarta.mail.internet.MimeBodyPart; -import jakarta.mail.internet.MimeMessage; +import jakarta.mail.internet.*; import utils.DateUtilities; import utils.Printer; import utils.reflection.ReflectionUtilities; @@ -28,6 +27,7 @@ public EmailUtilities(String host) { } private static final Printer log = new Printer(EmailUtilities.class); + private final boolean keepLogs = Boolean.parseBoolean(ContextStore.get("keep-email-logs", "true")); private String host; /** @@ -37,11 +37,34 @@ public EmailUtilities(String host) { * @param content the content of the email * @param receiver the email address of the recipient * @param ID the username for authenticating with the SMTP server - * @param Password the password for authenticating with the SMTP server + * @param password the password for authenticating with the SMTP server * @param attachment the optional multipart attachment to include in the email * @return true if the email was sent successfully, false otherwise */ - public Boolean sendEmail(String subject, String content, String receiver, String ID, String Password, Multipart attachment) { + public Boolean sendEmail(String subject, String content, String receiver, String ID, String password, Multipart attachment) { + return this.sendEmail( + subject, + content, + "text/plain; charset=" + MimeUtility.quote("us-ascii", HeaderTokenizer.MIME), + receiver, + ID, + password, + attachment + ); + } + + /** + * Sends an email message with an optional attachment to the specified recipient. + * + * @param subject the subject of the email + * @param content the content of the email + * @param receiver the email address of the recipient + * @param ID the username for authenticating with the SMTP server + * @param password the password for authenticating with the SMTP server + * @param attachment the optional multipart attachment to include in the email + * @return true if the email was sent successfully, false otherwise + */ + public Boolean sendEmail(String subject, String content, String contentType, String receiver, String ID, String password, Multipart attachment) { // Get system properties Properties properties = new Properties(); @@ -55,12 +78,12 @@ public Boolean sendEmail(String subject, String content, String receiver, String // Get the Session object.// and pass username and password Session session = Session.getInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(ID, Password); + return new PasswordAuthentication(ID, password); } }); // Used to debug SMTP issues - session.setDebug(true); + session.setDebug(keepLogs); try { // Create a default MimeMessage object. @@ -76,13 +99,13 @@ protected PasswordAuthentication getPasswordAuthentication() { message.setSubject(subject); // Now set the actual message - message.setText(content + "\n"); + message.setContent(content, contentType); if (attachment != null) message.setContent(attachment); - log.info("Sending..."); + if (keepLogs) log.info("Sending..."); Transport.send(message);// Send message - log.success("Sent message successfully!"); + if (keepLogs) log.success("Sent message successfully!"); return true; } catch (MessagingException mex) { log.error(mex.getMessage(), mex);