Skip to content
Merged
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.umutayb</groupId>
<artifactId>Utilities</artifactId>
<version>1.6.8</version>
<version>1.6.9</version>
<packaging>jar</packaging>

<name>Java-Utilities</name>
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/api_assured/ApiUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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));
}
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/utils/FileUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -108,6 +118,22 @@ public static Optional<Boolean> 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.
*
Expand Down
43 changes: 33 additions & 10 deletions src/main/java/utils/email/EmailUtilities.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/**
Expand All @@ -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();
Expand All @@ -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.
Expand All @@ -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);
Expand Down