Skip to content

Commit

Permalink
improve structure
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenBloodDev committed Oct 4, 2023
1 parent 7ee10b2 commit 5499faf
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ bin/
.DS_Store

/API
/logs
/src/main/java/test
56 changes: 49 additions & 7 deletions src/main/java/pl/nightdev701/OpenAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,75 @@
import pl.nightdev701.key.UniqueValueKey;
import pl.nightdev701.key.ValueKey;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

public class OpenAPI {

/** encrypt and decrypt strings */
/**
* encrypt and decrypt strings
*/
public static CryptoManager getCryptoManager(String value) {
return new CryptoManager(value);
}

/** manage database connections and command easy */
/**
* manage database connections and command easy
*/
public static DatabaseConnector getDatabaseManager(DatabaseFormular formular, DatabaseType type) {
return new DatabaseConnector(formular, type);
}

/** request http and handle it */
/**
* request http and handle it
*/
public static HttpRequestHandler getRequestHandler(String url) {
return new HttpRequestHandler(url);
}

/** key to save values */
public static BaseKey getValueKey(String value){
/**
* key to save values
*/
public static BaseKey getValueKey(String value) {
return ValueKey.getKey(value);
}

/** key to save uuid */
public static BaseKey getValueKey(UUID value){
/**
* key to save uuid
*/
public static BaseKey getValueKey(UUID value) {
return UniqueValueKey.getKey(value);
}

/**
* print date and clock
*/
public static String getCurrentClockDate() {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
return sdf.format(date);
}

/**
* print only date
*/
public static String getCurrentDay() {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
Date date = new Date(System.currentTimeMillis());
return sdf.format(date);
}

/**
* show only clock
*/
public static String getClockTime() {
DateFormat dff = new SimpleDateFormat("HH:mm:ss");
Date today = Calendar.getInstance().getTime();
return dff.format(today);
}


}
4 changes: 3 additions & 1 deletion src/main/java/pl/nightdev701/base/BaseKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
*/

/** base for keys */
/**
* base for keys
*/
public interface BaseKey {

Object baseValue();
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/pl/nightdev701/crypto/CryptoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public CryptoManager(String encryptionKey) {
this.encryptionKey = encryptionKey;
}

/** encrypt data */
/**
* encrypt data
*/
public String encrypt(String plainText) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(this.encryptionKey.getBytes(StandardCharsets.UTF_8), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
Expand All @@ -32,7 +34,9 @@ public String encrypt(String plainText) throws Exception {
return Base64.getEncoder().encodeToString(encryptedBytes);
}

/** decrypt data */
/**
* decrypt data
*/
public String decrypt(String encryptedText) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(this.encryptionKey.getBytes(StandardCharsets.UTF_8), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
Expand Down
32 changes: 23 additions & 9 deletions src/main/java/pl/nightdev701/database/DatabaseConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType, A
}
}

/** connect to database */
/**
* connect to database
*/
public void connect() throws SQLException {
logger.log(Level.INFO,"Connect to database, with the type \"" + databaseType.name() + "\"!");
logger.log(Level.INFO, "Connect to database, with the type \"" + databaseType.name() + "\"!");
if (databaseType == DatabaseType.MYSQL) {
connection = DriverManager.getConnection("jdbc:mysql://" +
ip + ":" + port + "/" + database + "?autoReconnect=true" +
Expand All @@ -78,42 +80,54 @@ public void connect() throws SQLException {
logger.log(Level.INFO, "Connected to database!");
}

/** close connection */
/**
* close connection
*/
public void close() throws SQLException {
if (isConnected()) {
connection.close();
logger.log(Level.INFO,"Connection closed");
logger.log(Level.INFO, "Connection closed");
}
}

/** read value from database */
/**
* read value from database
*/
public Object getDatabaseStatement(String command, String data) throws SQLException {
PreparedStatement statement = connection.prepareStatement(command);
ResultSet result = statement.executeQuery();
result.next();
return result.getObject(data);
}

/** execute action to database */
/**
* execute action to database
*/
public void executeDatabaseStatement(String command) throws SQLException {
PreparedStatement statement = connection.prepareStatement(command);
statement.executeUpdate();
}

/** check if connected */
/**
* check if connected
*/
public boolean isConnected() throws SQLException {
if (connection == null) {
return false;
}
return !connection.isClosed();
}

/** get port */
/**
* get port
*/
public int getPort() {
return port;
}

/** set port */
/**
* set port
*/
public void setPort(int port) {
this.port = port;
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/pl/nightdev701/http/HttpRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public HttpRequestHandler(String url) {
this.url = url;
}

/** fire request */
/**
* fire request
*/
public void request() {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
Expand All @@ -31,7 +33,9 @@ public void request() {
}
}

/** read body */
/**
* read body
*/
public String getBody() {
return body;
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/pl/nightdev701/key/UniqueValueKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@

import java.util.UUID;

/** uuid key */
/**
* uuid key
*/
public class UniqueValueKey<T extends UUID> implements BaseKey {

T key;

/** create key */
/**
* create key
*/
private UniqueValueKey(T key) {
this.key = key;
}

/** read value */
/**
* read value
*/
public static UniqueValueKey<UUID> getKey(UUID key) {
return new UniqueValueKey<>(key);
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/pl/nightdev701/key/ValueKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

import pl.nightdev701.base.BaseKey;

/** string key */
/**
* string key
*/
public class ValueKey<T extends String> implements BaseKey {

T key;
Expand All @@ -23,16 +25,18 @@ private ValueKey(T key) {

/**
* create key
*
* @return key value
* */
*/
public static ValueKey<String> getKey(Object key) {
return new ValueKey<>(key.toString());
}

/**
* read value
*
* @return key value
* */
*/
@Override
public Object baseValue() {
return key;
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/pl/nightdev701/logger/standard/DefaultLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,43 @@
*/

import pl.nightdev701.OpenAPI;
import pl.nightdev701.logger.AbstractLogger;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DefaultLogger extends AbstractLogger {

@Override
public void log(Level level, String msg) {

File logFolder = new File("logs");

if (!logFolder.exists()) {
logFolder.mkdirs();
}

Logger logger = Logger.getLogger("openapi");
logger.log(level, msg);

String result = OpenAPI.getClockTime() + " | " + level + " | " + msg;

try {
File file = new File("logs/events-" + OpenAPI.getCurrentDay() + ".log");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8));
writer.write(result + "\n");
writer.flush();

System.out.println(result);
} catch (IOException e) {
System.err.println("Failed to log! " + e.getMessage());
e.printStackTrace();
System.out.println("(" + OpenAPI.getCurrentClockDate() + ") Log data: " + level + ", " + msg);
}

}

}

0 comments on commit 5499faf

Please sign in to comment.