Skip to content

Commit

Permalink
Create json filter.
Browse files Browse the repository at this point in the history
Add method get by id and key.
  • Loading branch information
kerpsondev committed Jul 31, 2024
1 parent 630dcc2 commit 605703f
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 4 deletions.
50 changes: 50 additions & 0 deletions src/main/java/pl/kerpson/license/utilites/filter/JsonFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package pl.kerpson.license.utilites.filter;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import org.jetbrains.annotations.Nullable;

public class JsonFilter {

private final Predicate<JsonObject> filter;

public static JsonFilter createFilter(Predicate<JsonObject> filter) {
return new JsonFilter(filter);
}

private JsonFilter(Predicate<JsonObject> filter) {
this.filter = filter;
}

public <T> List<T> applyToCollection(JsonArray jsonArray, Function<JsonObject, T> function) {
List<T> list = new ArrayList<>();
for (JsonElement element : jsonArray.asList()) {
JsonObject jsonObject = element.getAsJsonObject();
if (!this.filter.test(jsonObject)) {
continue;
}

list.add(function.apply(jsonObject));
}

return list;
}

public <T> @Nullable T applySingle(JsonArray jsonArray, Function<JsonObject, T> function) {
for (JsonElement element : jsonArray.asList()) {
JsonObject jsonObject = element.getAsJsonObject();
if (!this.filter.test(jsonObject)) {
continue;
}

return function.apply(jsonObject);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pl.kerpson.license.utilites.modules.blacklist.basic.Blacklist;
import pl.kerpson.license.utilites.modules.blacklist.operation.BlacklistCreateOperation;
import pl.kerpson.license.utilites.modules.blacklist.operation.BlacklistGetAllOperation;
import pl.kerpson.license.utilites.modules.blacklist.operation.BlacklistGetIdOperation;
import pl.kerpson.license.utilites.modules.blacklist.operation.BlacklistUpdateOperation;

class BlacklistModuleImpl implements BlacklistModule {
Expand Down Expand Up @@ -37,7 +38,7 @@ public Operation<OperationResult<Boolean>> delete(int id) {

@Override
public Operation<OperationResult<Blacklist>> get(int id) {
throw new IllegalStateException("Operation currently is disabled");
return new BlacklistGetIdOperation(BLACKLISTS_ALL_URL, this.secrets, id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import pl.kerpson.license.utilites.filter.JsonFilter;
import pl.kerpson.license.utilites.modules.blacklist.basic.Blacklist.Type;

public final class BlacklistReader {
Expand All @@ -26,6 +27,12 @@ public static List<Blacklist> readBlacklist(HttpResponse<String> response) {
return blacklists;
}

public static Blacklist readBlacklist(HttpResponse<String> response, JsonFilter filter) {
Gson gson = new Gson();
JsonArray jsonArray = gson.fromJson(response.body(), JsonArray.class);
return filter.applySingle(jsonArray, BlacklistReader::readBlacklist);
}

public static Blacklist readBlacklist(JsonObject jsonObject) {
return new BlacklistImpl(
jsonObject.get("id").getAsInt(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package pl.kerpson.license.utilites.modules.blacklist.operation;

import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
import pl.kerpson.license.utilites.MSecrets;
import pl.kerpson.license.utilites.exception.IllegalStatusException;
import pl.kerpson.license.utilites.filter.JsonFilter;
import pl.kerpson.license.utilites.http.HttpBuilder;
import pl.kerpson.license.utilites.modules.Operation;
import pl.kerpson.license.utilites.modules.OperationResult;
import pl.kerpson.license.utilites.modules.blacklist.basic.Blacklist;
import pl.kerpson.license.utilites.modules.blacklist.basic.BlacklistReader;

public class BlacklistGetIdOperation implements Operation<OperationResult<Blacklist>> {

private final String url;
private final MSecrets secrets;
private final int id;

public BlacklistGetIdOperation(String url, MSecrets secrets, int id) {
this.url = url;
this.secrets = secrets;
this.id = id;
}

private HttpBuilder prepareRequest() {
return HttpBuilder.get()
.url(this.url)
.bearer(this.secrets.getToken());
}

@Override
public OperationResult<Blacklist> complete() {
try {
HttpResponse<String> response = this.prepareRequest().sync();
if (response.statusCode() == 401) {
return new OperationResult<>(null, new IllegalStatusException(401, "You don't have permission!"));
}

JsonFilter jsonFilter = JsonFilter.createFilter(jsonObject -> jsonObject.get("id").getAsInt() == this.id);
return new OperationResult<>(BlacklistReader.readBlacklist(response, jsonFilter), null);
} catch (Exception exception) {
return new OperationResult<>(null, exception);
}
}

@Override
public CompletableFuture<OperationResult<Blacklist>> completeAsync() {
return prepareRequest().async()
.thenApply(response -> {
if (response.statusCode() == 401) {
return new OperationResult<Blacklist>(null, new IllegalStatusException(401, "You don't have permission!"));
}

JsonFilter jsonFilter = JsonFilter.createFilter(jsonObject -> jsonObject.get("id").getAsInt() == this.id);
return new OperationResult<>(BlacklistReader.readBlacklist(response, jsonFilter), null);
})
.exceptionally(exception -> new OperationResult<>(null, exception));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import pl.kerpson.license.utilites.modules.license.basic.License;
import pl.kerpson.license.utilites.modules.license.operation.LicenseCreateOperation;
import pl.kerpson.license.utilites.modules.license.operation.LicenseGetAllOperation;
import pl.kerpson.license.utilites.modules.license.operation.LicenseGetIdOperation;
import pl.kerpson.license.utilites.modules.license.operation.LicenseGetKeyOperation;
import pl.kerpson.license.utilites.modules.license.operation.LicenseUpdateOperation;

class LicenseModuleImpl implements LicenseModule {
Expand Down Expand Up @@ -37,12 +39,12 @@ public Operation<OperationResult<Boolean>> update(License license) {

@Override
public Operation<OperationResult<License>> get(String key) {
throw new IllegalStateException("Operation currently is disabled");
return new LicenseGetKeyOperation(LICENSES_ALL_URL, this.secrets, key);
}

@Override
public Operation<OperationResult<License>> get(int id) {
throw new IllegalStateException("Operation currently is disabled");
return new LicenseGetIdOperation(LICENSES_ALL_URL, this.secrets, id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import pl.kerpson.license.utilites.filter.JsonFilter;
import pl.kerpson.license.utilites.modules.license.basic.License.AddressInfo.Request;
import pl.kerpson.license.utilites.modules.license.basic.License.MachineInfo;
import pl.kerpson.license.utilites.modules.license.basic.LicenseImpl.AddressInfoImpl;
Expand Down Expand Up @@ -38,6 +39,12 @@ public static License readLicense(HttpResponse<String> response) {
return LicenseReader.readLicense(jsonObject);
}

public static License readLicense(HttpResponse<String> response, JsonFilter filter) {
Gson gson = new Gson();
JsonArray jsonArray = gson.fromJson(response.body(), JsonArray.class);
return filter.applySingle(jsonArray, LicenseReader::readLicense);
}

public static License readLicense(JsonObject jsonObject) {
return new LicenseImpl(
jsonObject.get("id").getAsInt(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package pl.kerpson.license.utilites.modules.license.operation;

import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
import pl.kerpson.license.utilites.MSecrets;
import pl.kerpson.license.utilites.exception.IllegalStatusException;
import pl.kerpson.license.utilites.filter.JsonFilter;
import pl.kerpson.license.utilites.http.HttpBuilder;
import pl.kerpson.license.utilites.modules.Operation;
import pl.kerpson.license.utilites.modules.OperationResult;
import pl.kerpson.license.utilites.modules.license.basic.License;
import pl.kerpson.license.utilites.modules.license.basic.LicenseReader;

public class LicenseGetIdOperation implements Operation<OperationResult<License>> {

private final String url;
private final MSecrets secrets;
private final int id;

public LicenseGetIdOperation(String url, MSecrets secrets, int id) {
this.url = url;
this.secrets = secrets;
this.id = id;
}

private HttpBuilder prepareRequest() {
return HttpBuilder.get()
.url(this.url)
.bearer(this.secrets.getToken());
}

@Override
public OperationResult<License> complete() {
try {
HttpResponse<String> response = this.prepareRequest().sync();
if (response.statusCode() == 401) {
return new OperationResult<>(null, new IllegalStatusException(401, "You don't have permission!"));
}

JsonFilter jsonFilter = JsonFilter.createFilter(jsonObject -> jsonObject.get("id").getAsInt() == this.id);
return new OperationResult<>(LicenseReader.readLicense(response, jsonFilter), null);
} catch (Exception exception) {
return new OperationResult<>(null, exception);
}
}

@Override
public CompletableFuture<OperationResult<License>> completeAsync() {
return prepareRequest().async()
.thenApply(response -> {
if (response.statusCode() == 401) {
return new OperationResult<License>(null, new IllegalStatusException(401, "You don't have permission!"));
}

JsonFilter jsonFilter = JsonFilter.createFilter(jsonObject -> jsonObject.get("id").getAsInt() == this.id);
return new OperationResult<>(LicenseReader.readLicense(response, jsonFilter), null);
})
.exceptionally(exception -> new OperationResult<>(null, exception));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package pl.kerpson.license.utilites.modules.license.operation;

import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
import pl.kerpson.license.utilites.MSecrets;
import pl.kerpson.license.utilites.exception.IllegalStatusException;
import pl.kerpson.license.utilites.filter.JsonFilter;
import pl.kerpson.license.utilites.http.HttpBuilder;
import pl.kerpson.license.utilites.modules.Operation;
import pl.kerpson.license.utilites.modules.OperationResult;
import pl.kerpson.license.utilites.modules.license.basic.License;
import pl.kerpson.license.utilites.modules.license.basic.LicenseReader;

public class LicenseGetKeyOperation implements Operation<OperationResult<License>> {

private final String url;
private final MSecrets secrets;
private final String key;

public LicenseGetKeyOperation(String url, MSecrets secrets, String key) {
this.url = url;
this.secrets = secrets;
this.key = key;
}

private HttpBuilder prepareRequest() {
return HttpBuilder.get()
.url(this.url)
.bearer(this.secrets.getToken());
}

@Override
public OperationResult<License> complete() {
try {
HttpResponse<String> response = this.prepareRequest().sync();
if (response.statusCode() == 401) {
return new OperationResult<>(null, new IllegalStatusException(401, "You don't have permission!"));
}

JsonFilter jsonFilter = JsonFilter.createFilter(jsonObject -> jsonObject.get("key").getAsString().equals(this.key));
return new OperationResult<>(LicenseReader.readLicense(response, jsonFilter), null);
} catch (Exception exception) {
return new OperationResult<>(null, exception);
}
}

@Override
public CompletableFuture<OperationResult<License>> completeAsync() {
return prepareRequest().async()
.thenApply(response -> {
if (response.statusCode() == 401) {
return new OperationResult<License>(null, new IllegalStatusException(401, "You don't have permission!"));
}

JsonFilter jsonFilter = JsonFilter.createFilter(jsonObject -> jsonObject.get("key").getAsString().equals(this.key));
return new OperationResult<>(LicenseReader.readLicense(response, jsonFilter), null);
})
.exceptionally(exception -> new OperationResult<>(null, exception));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pl.kerpson.license.utilites.modules.OperationResult;
import pl.kerpson.license.utilites.modules.product.basic.Product;
import pl.kerpson.license.utilites.modules.product.operation.ProductCreateOperation;
import pl.kerpson.license.utilites.modules.product.operation.ProductGetIdOperation;
import pl.kerpson.license.utilites.modules.product.operation.ProductUpdateOperation;
import pl.kerpson.license.utilites.modules.product.operation.ProductsGetAllOperation;

Expand Down Expand Up @@ -37,7 +38,7 @@ public Operation<OperationResult<Boolean>> update(Product product) {

@Override
public Operation<OperationResult<Product>> get(int id) {
throw new IllegalStateException("Operation currently is disabled");
return new ProductGetIdOperation(PRODUCTS_ALL_URL, this.secrets, id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import pl.kerpson.license.utilites.filter.JsonFilter;
import pl.kerpson.license.utilites.util.JsonUtil;

public final class ProductReader {
Expand All @@ -26,6 +27,12 @@ public static List<Product> readProducts(HttpResponse<String> response) {
return products;
}

public static Product readProduct(HttpResponse<String> response, JsonFilter filter) {
Gson gson = new Gson();
JsonArray jsonArray = gson.fromJson(response.body(), JsonArray.class);
return filter.applySingle(jsonArray, ProductReader::readProduct);
}

public static Product readProduct(JsonObject jsonObject) {
return new ProductImpl(
jsonObject.get("id").getAsInt(),
Expand Down
Loading

0 comments on commit 605703f

Please sign in to comment.