-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
319 additions
and
4 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/main/java/pl/kerpson/license/utilites/filter/JsonFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...java/pl/kerpson/license/utilites/modules/blacklist/operation/BlacklistGetIdOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...ain/java/pl/kerpson/license/utilites/modules/license/operation/LicenseGetIdOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...in/java/pl/kerpson/license/utilites/modules/license/operation/LicenseGetKeyOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.