Ported code to storagehub-application-persistence

This commit is contained in:
Luca Frosini 2021-12-16 12:33:49 +01:00
parent f173af41fd
commit 65aaefa8d2
7 changed files with 44 additions and 31 deletions

View File

@ -50,7 +50,7 @@
<dependency>
<groupId>org.gcube.data-catalogue</groupId>
<artifactId>gcat-api</artifactId>
<version>[2.0.0, 3.0.0-SNAPSHOT)</version>
<version>[2.0.0-SNAPSHOT,3.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
@ -140,7 +140,7 @@
<dependency>
<groupId>org.gcube.data-publishing</groupId>
<artifactId>storagehub-application-persistence</artifactId>
<version>[2.0.0-SNAPSHOT,3.0.0-SNAPSHOT)</version>
<version>[3.0.0-SNAPSHOT,4.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.json</groupId>

View File

@ -8,13 +8,13 @@ import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.gcat.api.moderation.CMItemStatus;
import org.gcube.gcat.api.moderation.Moderated;
import org.gcube.gcat.moderation.thread.ModerationThread;
import org.gcube.gcat.moderation.thread.zulip.ZulipResponse.Result;
import org.gcube.gcat.social.SocialUsers;
import org.gcube.gcat.utils.Constants;
import org.gcube.storagehub.ApplicationMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -51,12 +51,13 @@ public class ZulipStream extends ModerationThread {
return new ZulipRestExecutor(zulipAuth.getEmail(), zulipAuth.getAPIKey(), zulipAuth.getSite());
}
public ZulipRestExecutor getGCatZulipRestExecutor() {
public ZulipRestExecutor getGCatZulipRestExecutor() throws Exception {
if(gCatZulipRestExecutor==null) {
ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
applicationMode.start();
SecretManager secretManager = SecretManager.instance.get();
Secret secret = Constants.getCatalogueSecret();
secretManager.startSession(secret);
gCatZulipRestExecutor = getZulipRestExecutor();
applicationMode.end();
secretManager.endSession();
}
return gCatZulipRestExecutor;
}

View File

@ -182,7 +182,7 @@ public class SocialPost extends Thread {
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(basePath);
gxhttpStringRequest.from(Constants.CATALOGUE_NAME);
gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
gxhttpStringRequest.setSecurityToken(Constants.getSecurityToken());
gxhttpStringRequest.setSecurityToken(Constants.getCatalogueSecurityToken());
gxhttpStringRequest.path(SOCIAL_SERVICE_WRITE_APPLICATION_POST_PATH);
HttpURLConnection httpURLConnection = gxhttpStringRequest.post(objectMapper.writeValueAsString(objectNode));

View File

@ -10,6 +10,8 @@ import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.WebApplicationException;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.model.TokenResponse;
@ -46,7 +48,7 @@ public class Constants {
}
@Deprecated
public static String getCatalogueApplicationToken() {
private static String getCatalogueApplicationToken() {
String context = SecretManager.instance.get().getContext();
try {
return applicationTokens.get(context);
@ -82,8 +84,7 @@ public class Constants {
return tr.getAccessToken();
}
public static String getSecurityToken() throws Exception {
public static String getCatalogueSecurityToken() throws Exception {
try {
return getJWTAccessToken();
}catch (Exception e) {
@ -91,4 +92,9 @@ public class Constants {
}
}
public static Secret getCatalogueSecret() throws Exception {
String securityToken = getCatalogueSecurityToken();
return SecretUtility.getSecretByTokenString(securityToken);
}
}

View File

@ -4,11 +4,12 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.common.storagehub.client.dsl.FileContainer;
import org.gcube.common.storagehub.model.Metadata;
import org.gcube.gcat.utils.Constants;
import org.gcube.storagehub.ApplicationMode;
import org.gcube.storagehub.StorageHubManagement;
import org.glassfish.jersey.media.multipart.ContentDisposition;
import org.slf4j.Logger;
@ -43,9 +44,10 @@ public class CatalogueStorageHubManagement {
}
public URL ensureResourcePersistence(URL persistedURL, String itemID, String resourceID) throws Exception {
ApplicationMode applicationMode = new ApplicationMode(Constants.getSecurityToken());
SecretManager secretManager = SecretManager.instance.get();
Secret secret = Constants.getCatalogueSecret();
try {
applicationMode.start();
secretManager.startSession(secret);
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(persistedURL.toString());
gxhttpStringRequest.from(Constants.CATALOGUE_NAME);
gxhttpStringRequest.isExternalCall(true);
@ -63,20 +65,21 @@ public class CatalogueStorageHubManagement {
logger.error("Error while trying to persists the resource", e);
throw e;
} finally {
applicationMode.end();
secretManager.endSession();
}
}
public void deleteResourcePersistence(String itemID, String resourceID, String mimeType) throws Exception {
ApplicationMode applicationMode = new ApplicationMode(Constants.getSecurityToken());
SecretManager secretManager = SecretManager.instance.get();
Secret secret = Constants.getCatalogueSecret();
try {
applicationMode.start();
secretManager.startSession(secret);
storageHubManagement = new StorageHubManagement();
CatalogueMetadata catalogueMetadata = new CatalogueMetadata(itemID);
storageHubManagement.setCheckMetadata(catalogueMetadata);
storageHubManagement.removePersistedFile(resourceID, mimeType);
} finally {
applicationMode.end();
secretManager.endSession();
}
}
@ -97,35 +100,38 @@ public class CatalogueStorageHubManagement {
}
public void renameFile(String resourceID, String revisionID) throws Exception {
ApplicationMode applicationMode = new ApplicationMode(Constants.getSecurityToken());
SecretManager secretManager = SecretManager.instance.get();
Secret secret = Constants.getCatalogueSecret();
try {
applicationMode.start();
secretManager.startSession(secret);
FileContainer createdfile = storageHubManagement.getPersistedFile();
createdfile.rename(resourceID);
internalAddRevisionID(resourceID, revisionID);
} finally {
applicationMode.end();
secretManager.endSession();
}
}
public void addRevisionID(String resourceID, String revisionID) throws Exception {
ApplicationMode applicationMode = new ApplicationMode(Constants.getSecurityToken());
SecretManager secretManager = SecretManager.instance.get();
Secret secret = Constants.getCatalogueSecret();
try {
applicationMode.start();
secretManager.startSession(secret);
internalAddRevisionID(resourceID, revisionID);
} finally {
applicationMode.end();
secretManager.endSession();
}
}
public FileContainer retrievePersistedFile(String id, String mimeType) throws Exception {
ApplicationMode applicationMode = new ApplicationMode(Constants.getSecurityToken());
SecretManager secretManager = SecretManager.instance.get();
Secret secret = Constants.getCatalogueSecret();
try {
applicationMode.start();
secretManager.startSession(secret);
return storageHubManagement.getPersistedFile(id, mimeType);
} finally {
applicationMode.end();
secretManager.endSession();
}
}

View File

@ -16,8 +16,8 @@ public class SocialPostTest extends ContextTest {
@Test
public void testToken() throws Exception {
logger.debug("Application Token is {}", Constants.getCatalogueApplicationToken());
ContextTest.setContext(Constants.getCatalogueApplicationToken());
logger.debug("Application Token is {}", Constants.getCatalogueSecurityToken());
ContextTest.setContext(Constants.getCatalogueSecurityToken());
}
@Test

View File

@ -11,9 +11,9 @@ public class ConstantsTest extends ContextTest {
private static final Logger logger = LoggerFactory.getLogger(ConstantsTest.class);
@Test
public void testGetApplicationToken() {
public void testGetApplicationToken() throws Exception {
logger.debug("Application token for Context {} is {}", SecretManager.instance.get().getContext(),
Constants.getCatalogueApplicationToken());
Constants.getCatalogueSecurityToken());
}
}