Skip to content

Commit

Permalink
Merge branch 'apply-sdk-java' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ren2003u committed Oct 18, 2023
2 parents 9ab9bc2 + c99af6f commit 49f61b7
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 10 deletions.
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.github.ren2003u</groupId>
<artifactId>platform-sdk-java</artifactId>
<version>1.0.3</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>io.github.ren2003u</groupId>-->
<!-- <artifactId>platform-sdk-java</artifactId>-->
<!-- <version>1.0.2</version>-->
<!-- </dependency>-->
<!-- Mockito core dependency -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.11.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import space.ao.services.support.OperationUtils;
import space.ao.services.support.StringUtils;
import space.ao.services.support.log.Logged;
import space.ao.services.support.platform.PlatformClient;
import space.ao.services.support.platform.PlatformRegistryServiceRestClient;
import space.ao.services.support.platform.PlatformUtils;
import space.ao.services.support.platform.info.registry.*;
Expand All @@ -40,6 +41,8 @@

import java.net.URI;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@ApplicationScoped
Expand All @@ -55,6 +58,9 @@ public class PlatformRegistryService {
TempRegistryInfoRepository tempRegistryInfoRepository;
static final Logger LOG = Logger.getLogger("app.log");

@Inject
PlatformClient platformClient;

@Inject
@RestClient
PlatformRegistryServiceRestClient platformRegistryServiceRestClient;
Expand Down Expand Up @@ -91,8 +97,7 @@ public PlatformInfo setPlatform(String ssplatformUrl){
@Transactional
public UserRegistryResult registryUser(String requestId, UserRegistryInfo userRegistryInfo, Boolean platformRegistry) {
if (Boolean.TRUE.equals(platformRegistry) && platformUtils.isRegistryPlatformAvailable(requestId)) {
var boxRegKey = platformUtils.createRegistryBoxRegKey(requestId);
return platformRegistryServiceRestClient.platformRegistryUser(userRegistryInfo, requestId, boxRegKey, properties.boxUuid());
return platformClient.registerUser(requestId, userRegistryInfo);
} else {
TempRegistryInfoEntity tempRegistryInfoEntity = new TempRegistryInfoEntity();
tempRegistryInfoEntity.setRequestId(requestId);
Expand All @@ -105,7 +110,6 @@ public UserRegistryResult registryUser(String requestId, UserRegistryInfo userRe
LOG.warnv("registry user failed, Unable to connect to the platform, delay registration to connectable platform, userRegistryInfo: {0}", userRegistryInfo);
return new UserRegistryResult(properties.boxUuid(), userRegistryInfo.userId(), null, RegistryTypeEnum.USER_ADMIN.getName(), userRegistryInfo.clientUUID());
}

}

/**
Expand All @@ -118,9 +122,8 @@ public UserRegistryResult registryUser(String requestId, UserRegistryInfo userRe
**/
@Logged
public void platformRegistryUserReset(String requestId, String aoid) {
var boxRegKey = platformUtils.createRegistryBoxRegKey(requestId);
try {
platformRegistryServiceRestClient.platformResetUser(requestId, boxRegKey, properties.boxUuid(), aoid);
platformClient.deleteUser(requestId, aoid);
} catch (WebApplicationException e) {
if(Objects.equals(Status.NOT_FOUND.getStatusCode(), e.getResponse().getStatusInfo().getStatusCode())){
LOG.errorv("platform Registry User Reset: {0}", utils.getErrorInfoFromException(e));
Expand All @@ -144,10 +147,8 @@ public void platformRegistryUserReset(String requestId, String aoid) {
@Logged
@Transactional
public ClientRegistryResult registryClient(String requestId, ClientRegistryInfo clientRegistryInfo, String aoid) {

if(Boolean.TRUE.equals(utils.getEnableInternetAccess()) && platformUtils.isRegistryPlatformAvailable(requestId)){
var boxRegKey = platformUtils.createRegistryBoxRegKey(requestId);
return platformRegistryServiceRestClient.platformRegistryClient(clientRegistryInfo, requestId, boxRegKey, properties.boxUuid(), aoid);
return platformClient.registerClient(requestId, clientRegistryInfo, aoid);
} else {
TempRegistryInfoEntity tempRegistryInfoEntity = new TempRegistryInfoEntity();
tempRegistryInfoEntity.setRequestId(requestId);
Expand Down Expand Up @@ -175,9 +176,8 @@ public ClientRegistryResult registryClient(String requestId, ClientRegistryInfo
@Logged
public void platformRegistryClientReset(String requestId, String aoid, String clientUUID) {
if(Boolean.TRUE.equals(utils.getEnableInternetAccess()) && platformUtils.isRegistryPlatformAvailable(requestId)){
var boxRegKey = platformUtils.createRegistryBoxRegKey(requestId);
try {
platformRegistryServiceRestClient.platformRestClient(requestId, boxRegKey, properties.boxUuid(), aoid, clientUUID);
platformClient.deleteClient(requestId, aoid, clientUUID);
} catch (WebApplicationException e){
LOG.errorv("platform Registry Client Reset: {0}", utils.getErrorInfoFromException(e));
if(Objects.equals(Status.NOT_FOUND.getStatusCode(), e.getResponse().getStatusInfo().getStatusCode())){
Expand Down
146 changes: 146 additions & 0 deletions src/main/java/space/ao/services/support/platform/PlatformClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package space.ao.services.support.platform;

import io.github.ren2003u.authentication.model.ObtainBoxRegKeyResponse;
import io.github.ren2003u.client.Client;
import io.github.ren2003u.domain.errorHandle.ApiResponse;
import io.github.ren2003u.register.model.RegisterClientResponse;
import io.github.ren2003u.register.model.RegisterUserResponse;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import space.ao.services.config.ApplicationProperties;
import space.ao.services.support.platform.info.registry.ClientRegistryInfo;
import space.ao.services.support.platform.info.registry.ClientRegistryResult;
import space.ao.services.support.platform.info.registry.UserRegistryInfo;
import space.ao.services.support.platform.info.registry.UserRegistryResult;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@ApplicationScoped
public class PlatformClient {

@Inject
ApplicationProperties properties;

private String host;
private Client client;

private void init() {
if (this.client == null) {
this.host = properties.ssplatformUrl();
this.client = new Client(host, null);
}
}
private static final Logger LOG = LoggerFactory.getLogger(PlatformClient.class);

// Cache variables
private String cachedBoxRegKey = null;
private LocalDateTime lastFetchedTime = null;
private static final Duration CACHE_DURATION = Duration.ofMinutes(5); // Cache duration of 5 minutes

// public PlatformClient() {
// this.client = new Client(host, null);
// }

public UserRegistryResult registerUser(String requestId, UserRegistryInfo userRegistryInfo) {
init();
try {
// Obtain BoxRegKey
String boxRegKey = obtainBoxRegKey(requestId);
if (boxRegKey == null) {
LOG.error("Failed to obtain BoxRegKey for requestId: {}", requestId);
return null;
}

// Register User
ApiResponse<RegisterUserResponse> response = client.registerUser(properties.boxUuid(), userRegistryInfo.userId(), userRegistryInfo.subdomain(), userRegistryInfo.userType(), userRegistryInfo.clientUUID(), requestId, boxRegKey);
if (response.getError() != null) {
LOG.error("Error registering user: {}", response.getError().getMessage());
return null;
}
return new UserRegistryResult(response.getData().getBoxUUID(), response.getData().getUserId(), response.getData().getUserDomain(), response.getData().getUserType(), response.getData().getClientUUID());
} catch (Exception e) {
LOG.error("Failed to register user", e);
return null;
}
}
public ClientRegistryResult registerClient(String requestId, ClientRegistryInfo clientRegistryInfo, String userId) {
try {
// Obtain BoxRegKey
String boxRegKey = obtainBoxRegKey(requestId);
if (boxRegKey == null) {
LOG.error("Failed to obtain BoxRegKey for requestId: {}", requestId);
return null;
}

// Register Client
ApiResponse<RegisterClientResponse> response = client.registerClient(properties.boxUuid(), userId, clientRegistryInfo.clientUUID(), clientRegistryInfo.clientType(), requestId, boxRegKey);
if (response.getError() != null) {
LOG.error("Error registering client: {}", response.getError().getMessage());
return null;
}
return new ClientRegistryResult(response.getData().getBoxUUID(), response.getData().getUserId(), response.getData().getClientUUID(), response.getData().getClientType());
} catch (Exception e) {
LOG.error("Failed to register client", e);
return null;
}
}

private String obtainBoxRegKey(String requestId) {
try {
// Check if the cache is still valid
if (cachedBoxRegKey != null && lastFetchedTime != null && Duration.between(lastFetchedTime, LocalDateTime.now()).compareTo(CACHE_DURATION) <= 0) {
return cachedBoxRegKey;
}

ApiResponse<ObtainBoxRegKeyResponse> response = client.obtainBoxRegKey(properties.boxUuid(), List.of("10001"), requestId);
if (response.getError() != null) {
LOG.error("Error obtaining BoxRegKey: {}", response.getError().getMessage());
return null;
}

// Update cache
cachedBoxRegKey = response.getData().getTokenResults().get(0).getBoxRegKey();
lastFetchedTime = LocalDateTime.now();

return cachedBoxRegKey;
} catch (Exception e) {
LOG.error("Failed to obtain BoxRegKey", e);
return null;
}
}
public void deleteUser(String requestId, String userId) {
try {
// Obtain BoxRegKey
String boxRegKey = obtainBoxRegKey(requestId);
if (boxRegKey == null) {
LOG.error("Failed to obtain BoxRegKey for requestId: {}", requestId);
return;
}

// Delete User
client.deleteUser(properties.boxUuid(), userId, requestId, boxRegKey);
} catch (Exception e) {
LOG.error("Failed to delete user with userId: {}", userId, e);
}
}
public void deleteClient(String requestId, String userId, String clientUUID) {
try {
// Obtain BoxRegKey
String boxRegKey = obtainBoxRegKey(requestId);
if (boxRegKey == null) {
LOG.error("Failed to obtain BoxRegKey for requestId: {}", requestId);
return;
}

// Delete Client
client.deleteClient(properties.boxUuid(), userId, clientUUID, requestId, boxRegKey);
} catch (Exception e) {
LOG.error("Failed to delete client with clientUUID: {}", clientUUID, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.io.CharStreams;
import io.quarkus.test.InjectMock;
import io.quarkus.test.Mock;

import java.io.IOException;
Expand All @@ -27,7 +28,11 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import org.jboss.logging.Logger;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import space.ao.services.account.member.service.PlatformRegistryService;
import space.ao.services.config.ApplicationProperties;
import space.ao.services.support.OperationUtils;
import space.ao.services.support.platform.info.ability.PlatformApiResults;
Expand All @@ -45,6 +50,9 @@
import space.ao.services.support.service.ServiceError;
import space.ao.services.support.service.ServiceOperationException;

import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;

@Mock
@ApplicationScoped
@RestClient
Expand All @@ -59,6 +67,12 @@ public class MockPlatformRegistryServiceRestClient implements PlatformRegistrySe
OperationUtils utils;
@Inject
SecurityUtils securityUtils;

@Inject
PlatformRegistryService platformRegistryService;



@Override
public TokenCreateResults createTokens(TokenInfo tokenInfo, String reqId) {
if(securityUtils.getSecurityProvider().verifySignUsingBoxPublicKey(reqId,
Expand Down

0 comments on commit 49f61b7

Please sign in to comment.