Skip to content

Commit

Permalink
Merge pull request #17 from sinri/latest
Browse files Browse the repository at this point in the history
3.2.4
  • Loading branch information
sinri authored Apr 15, 2024
2 parents f8f84ed + a0f0e45 commit 05e73b1
Show file tree
Hide file tree
Showing 44 changed files with 1,014 additions and 503 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A Java framework with VERT.X eco, for projects for web, job and more.
<dependency>
<groupId>io.github.sinri</groupId>
<artifactId>Keel</artifactId>
<version>3.1.9</version>
<version>3.2.2</version>
</dependency>
```

Expand Down
25 changes: 15 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vertxVersion>4.5.4</vertxVersion>
<commonmarkVersion>0.21.0</commonmarkVersion>
<jacksonVersion>2.15.3</jacksonVersion>
<poiVersion>5.2.5</poiVersion>
</properties>

<groupId>io.github.sinri</groupId>
<artifactId>Keel</artifactId>
<version>3.2.3-SNAPSHOT</version>
<!-- <version>3.2.2</version>-->
<!-- <version>3.2.4-SNAPSHOT</version>-->
<version>3.2.4</version>

<name>Keel</name>
<description>
Expand Down Expand Up @@ -44,6 +36,14 @@
</developer>
</developers>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vertxVersion>4.5.7</vertxVersion>
<commonmarkVersion>0.21.0</commonmarkVersion>
<jacksonVersion>2.15.3</jacksonVersion>
<poiVersion>5.2.5</poiVersion>
</properties>

<dependencies>
<dependency>
<groupId>io.vertx</groupId>
Expand Down Expand Up @@ -145,6 +145,11 @@
<artifactId>vertx-redis-client</artifactId>
<version>${vertxVersion}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-json-schema</artifactId>
<version>${vertxVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
Expand Down
68 changes: 51 additions & 17 deletions src/main/java/io/github/sinri/keel/core/KeelCronExpression.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.sinri.keel.core;


import javax.annotation.Nonnull;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -13,9 +15,9 @@ public class KeelCronExpression {
final Set<Integer> dayOptions = new HashSet<>();
final Set<Integer> monthOptions = new HashSet<>();
final Set<Integer> weekdayOptions = new HashSet<>();
private final String rawCronExpression;
private final @Nonnull String rawCronExpression;

public KeelCronExpression(String rawCronExpression) {
public KeelCronExpression(@Nonnull String rawCronExpression) {
this.rawCronExpression = rawCronExpression;

String[] parts = rawCronExpression.trim().split("\\s+");
Expand All @@ -36,24 +38,20 @@ public KeelCronExpression(String rawCronExpression) {
parseField(weekdayExpression, weekdayOptions, 0, 6);
}

public boolean match(Calendar currentCalendar) {
// currentCalendar := Calendar.getInstance();
int minute = currentCalendar.get(Calendar.MINUTE);
int hour = currentCalendar.get(Calendar.HOUR_OF_DAY);
int day = currentCalendar.get(Calendar.DAY_OF_MONTH);
int month = 1 + currentCalendar.get(Calendar.MONTH);// make JAN 1, ...
int weekday = currentCalendar.get(Calendar.DAY_OF_WEEK) - 1; // make sunday 0, ...

return minuteOptions.contains(minute)
&& hourOptions.contains(hour)
&& dayOptions.contains(day)
&& monthOptions.contains(month)
&& weekdayOptions.contains(weekday);
public boolean match(@Nonnull Calendar currentCalendar) {
ParsedCalenderElements parsedCalenderElements = new ParsedCalenderElements(currentCalendar);
return match(parsedCalenderElements);
}

private void parseField(String rawComponent, Set<Integer> optionSet, int min, int max) {
// System.out.println("parseField: " + rawComponent);
public boolean match(@Nonnull ParsedCalenderElements parsedCalenderElements) {
return minuteOptions.contains(parsedCalenderElements.minute)
&& hourOptions.contains(parsedCalenderElements.hour)
&& dayOptions.contains(parsedCalenderElements.day)
&& monthOptions.contains(parsedCalenderElements.month)
&& weekdayOptions.contains(parsedCalenderElements.weekday);
}

private void parseField(@Nonnull String rawComponent, @Nonnull Set<Integer> optionSet, int min, int max) {
if (rawComponent.equals("*")) {
for (int i = min; i <= max; i++) {
optionSet.add(i);
Expand Down Expand Up @@ -106,10 +104,46 @@ private void parseField(String rawComponent, Set<Integer> optionSet, int min, in
}
}

/**
* @since 3.2.4
*/
public static ParsedCalenderElements parseCalenderToElements(@Nonnull Calendar currentCalendar) {
return new ParsedCalenderElements(currentCalendar);
}

@Nonnull
public String getRawCronExpression() {
return rawCronExpression;
}

/**
* @since 3.2.4
*/
public static class ParsedCalenderElements {
public final int minute;
public final int hour;
public final int day;
public final int month;
public final int weekday;

// debug use
public final int second;

public ParsedCalenderElements(@Nonnull Calendar currentCalendar) {
minute = currentCalendar.get(Calendar.MINUTE);
hour = currentCalendar.get(Calendar.HOUR_OF_DAY);
day = currentCalendar.get(Calendar.DAY_OF_MONTH);
month = 1 + currentCalendar.get(Calendar.MONTH);// make JAN 1, ...
weekday = currentCalendar.get(Calendar.DAY_OF_WEEK) - 1; // make sunday 0, ...
second = currentCalendar.get(Calendar.SECOND);
}

@Override
public String toString() {
return "(" + second + ") " + minute + " " + hour + " " + day + " " + month + " " + weekday;
}
}

@Override
public String toString() {
return getRawCronExpression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/**
* @since 2.7
*/
@Deprecated(since = "3.2.4")
public class JsonArrayScheme extends JsonValueScheme<JsonArray> {
private final Map<Integer, JsonElementScheme<?>> indexedElementSchemeMap = new LinkedHashMap<>();
private JsonElementScheme<?> defaultElementScheme;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/**
* @since 2.7
*/
@Deprecated(since = "3.2.4")
public class JsonBooleanScheme extends JsonValueScheme<Boolean> {
private Boolean expected;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/**
* @since 2.7
*/
@Deprecated(since = "3.2.4")
public interface JsonElementScheme<T> extends JsonifiableEntity<JsonElementScheme<T>> {
static JsonElementScheme<?> fromJsonObject(JsonObject jsonObject) {
JsonElementSchemeType scheme_type = JsonElementSchemeType.valueOf(jsonObject.getString("scheme_type"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/**
* @since 2.7
*/
@Deprecated(since = "3.2.4")
public class JsonNullScheme extends JsonValueScheme<Object> {
public JsonNullScheme() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/**
* @since 2.7
*/
@Deprecated(since = "3.2.4")
public class JsonNumberScheme extends JsonValueScheme<Number> {
//private boolean withFractions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/**
* @since 2.7
*/
@Deprecated(since = "3.2.4")
public class JsonObjectScheme extends JsonValueScheme<JsonObject> {

private final Map<String, JsonElementScheme<?>> elementSchemeMap = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.annotation.Nonnull;

@Deprecated(since = "3.2.4")
public class JsonPlainScheme extends JsonValueScheme<Object> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static io.github.sinri.keel.helper.KeelHelpersInterface.KeelHelpers;

@Deprecated(since = "3.2.4")
public class JsonSchemeMismatchException extends Exception {
public static final String RuleSchemeError = "SchemeError";
public static final String RuleEmptyArrayNotAllowed = "EmptyArrayNotAllowed";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/**
* @since 2.7
*/
@Deprecated(since = "3.2.4")
public class JsonStringScheme extends JsonValueScheme<String> {

private Pattern pattern;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/**
* @since 2.7
*/
@Deprecated(since = "3.2.4")
abstract public class JsonValueScheme<T> implements JsonElementScheme<T> {

protected T digested;
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/io/github/sinri/keel/elasticsearch/ESApiMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.vertx.core.Future;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.DecodeException;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;
Expand Down Expand Up @@ -64,9 +66,7 @@ default Future<JsonObject> call(@Nonnull HttpMethod httpMethod, @Nonnull String
})
.compose(bufferHttpResponse -> {
int statusCode = bufferHttpResponse.statusCode();
JsonObject resp = bufferHttpResponse.bodyAsJsonObject();

if ((statusCode >= 300 || statusCode < 200) || resp == null) {
if ((statusCode >= 300 || statusCode < 200)) {
// this.getLogger().error(log -> {
// logRequestEnricher.handle(log);
// log.message("ES API Response Error")
Expand All @@ -86,6 +86,14 @@ default Future<JsonObject> call(@Nonnull HttpMethod httpMethod, @Nonnull String
requestBody
));
}
JsonObject resp;
try {
resp = bufferHttpResponse.bodyAsJsonObject();
} catch (DecodeException decodeException) {
// There are situations that use Json Array as the response body!
resp = new JsonObject()
.put("array", new JsonArray(bufferHttpResponse.bodyAsString()));
}
// this.getLogger().info(log -> {
// logRequestEnricher.handle(log);
// log.message("ES API Response Error")
Expand Down Expand Up @@ -208,6 +216,18 @@ public ESApiException(
this.requestBody = requestBody;
}

@Override
public String toString() {
return getClass().getName()
+ "{status_code: " + statusCode
+ ", response: " + response
+ ", http_method: " + httpMethod.name()
+ ", endpoint: " + endpoint
+ ", queries: " + queries
+ ", request_body: " + requestBody
+ "}";
}

public int getStatusCode() {
return statusCode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package io.github.sinri.keel.elasticsearch;

import io.github.sinri.keel.facade.KeelConfiguration;
import io.github.sinri.keel.facade.configuration.KeelConfigElement;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;

import static io.github.sinri.keel.facade.KeelInstance.Keel;

/**
* @since 3.0.7
*/
public class ElasticSearchConfig extends KeelConfiguration {
public class ElasticSearchConfig extends KeelConfigElement {

public ElasticSearchConfig(KeelConfiguration configuration) {
public ElasticSearchConfig(KeelConfigElement configuration) {
super(configuration);
}

Expand All @@ -34,32 +35,30 @@ public ElasticSearchConfig(String esKey) {
*/

public String username() {
return readString("username");
return readString("username", null);
}

public String password() {
return readString("password");
return readString("password", null);
}

public @Nonnull String clusterHost() {
return Objects.requireNonNull(readString("cluster", "host"));
return Objects.requireNonNull(readString(List.of("cluster", "host"), null));
}

public int clusterPort() {
String s = readString("cluster", "port");
return Integer.parseInt(Objects.requireNonNullElse(s, "9200"));
return readInteger(List.of("cluster", "port"), 9200);
}

public @Nonnull String clusterScheme() {
String s = readString("cluster", "scheme");
return Objects.requireNonNullElse(s, "http");
return Objects.requireNonNull(readString(List.of("cluster", "scheme"), "http"));
}

public @Nonnull String clusterApiUrl(@Nonnull String endpoint) {
return this.clusterScheme() + "://" + this.clusterHost() + ":" + this.clusterPort() + endpoint;
}

public @Nullable String opaqueId() {
return readString("opaqueId");
return readString("opaqueId", null);
}
}
14 changes: 7 additions & 7 deletions src/main/java/io/github/sinri/keel/email/smtp/KeelSmtpKit.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.sinri.keel.email.smtp;

import io.github.sinri.keel.facade.KeelConfiguration;
import io.vertx.core.Future;
import io.vertx.ext.mail.MailClient;
import io.vertx.ext.mail.MailConfig;
Expand Down Expand Up @@ -55,14 +54,15 @@ public KeelSmtpKit() {
* As of 3.0.6, only five property keys supported.
*/
private static MailConfig buildMailConfig(@Nonnull String smtpName) {
KeelConfiguration smtpConfiguration = Keel.getConfiguration().extract("email", "smtp", smtpName);
var smtpConfiguration = Keel.getConfiguration().extract("email", "smtp", smtpName);
Objects.requireNonNull(smtpConfiguration);

var mailConfig = new MailConfig();
mailConfig.setHostname(smtpConfiguration.readString("hostname"));
mailConfig.setPort(Objects.requireNonNull(smtpConfiguration.readAsInteger("port")));
mailConfig.setUsername(smtpConfiguration.readString("username"));
mailConfig.setPassword(smtpConfiguration.readString("password"));
mailConfig.setSsl("ON".equals(smtpConfiguration.readString("ssl")));
mailConfig.setHostname(smtpConfiguration.readString("hostname", null));
mailConfig.setPort(smtpConfiguration.readInteger("port", 25));
mailConfig.setUsername(smtpConfiguration.readString("username", null));
mailConfig.setPassword(smtpConfiguration.readString("password", null));
mailConfig.setSsl(smtpConfiguration.readBoolean("ssl", false));

return mailConfig;
}
Expand Down
Loading

0 comments on commit 05e73b1

Please sign in to comment.