Ported code to storagehub-application-persistence
This commit is contained in:
parent
f173af41fd
commit
65aaefa8d2
4
pom.xml
4
pom.xml
|
@ -50,7 +50,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.gcube.data-catalogue</groupId>
|
<groupId>org.gcube.data-catalogue</groupId>
|
||||||
<artifactId>gcat-api</artifactId>
|
<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>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.gcube.common</groupId>
|
<groupId>org.gcube.common</groupId>
|
||||||
|
@ -140,7 +140,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.gcube.data-publishing</groupId>
|
<groupId>org.gcube.data-publishing</groupId>
|
||||||
<artifactId>storagehub-application-persistence</artifactId>
|
<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>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.json</groupId>
|
<groupId>org.json</groupId>
|
||||||
|
|
|
@ -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.ArrayNode;
|
||||||
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
|
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import org.gcube.common.authorization.utils.manager.SecretManager;
|
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.CMItemStatus;
|
||||||
import org.gcube.gcat.api.moderation.Moderated;
|
import org.gcube.gcat.api.moderation.Moderated;
|
||||||
import org.gcube.gcat.moderation.thread.ModerationThread;
|
import org.gcube.gcat.moderation.thread.ModerationThread;
|
||||||
import org.gcube.gcat.moderation.thread.zulip.ZulipResponse.Result;
|
import org.gcube.gcat.moderation.thread.zulip.ZulipResponse.Result;
|
||||||
import org.gcube.gcat.social.SocialUsers;
|
import org.gcube.gcat.social.SocialUsers;
|
||||||
import org.gcube.gcat.utils.Constants;
|
import org.gcube.gcat.utils.Constants;
|
||||||
import org.gcube.storagehub.ApplicationMode;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -51,12 +51,13 @@ public class ZulipStream extends ModerationThread {
|
||||||
return new ZulipRestExecutor(zulipAuth.getEmail(), zulipAuth.getAPIKey(), zulipAuth.getSite());
|
return new ZulipRestExecutor(zulipAuth.getEmail(), zulipAuth.getAPIKey(), zulipAuth.getSite());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ZulipRestExecutor getGCatZulipRestExecutor() {
|
public ZulipRestExecutor getGCatZulipRestExecutor() throws Exception {
|
||||||
if(gCatZulipRestExecutor==null) {
|
if(gCatZulipRestExecutor==null) {
|
||||||
ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
|
SecretManager secretManager = SecretManager.instance.get();
|
||||||
applicationMode.start();
|
Secret secret = Constants.getCatalogueSecret();
|
||||||
|
secretManager.startSession(secret);
|
||||||
gCatZulipRestExecutor = getZulipRestExecutor();
|
gCatZulipRestExecutor = getZulipRestExecutor();
|
||||||
applicationMode.end();
|
secretManager.endSession();
|
||||||
}
|
}
|
||||||
return gCatZulipRestExecutor;
|
return gCatZulipRestExecutor;
|
||||||
}
|
}
|
||||||
|
|
|
@ -182,7 +182,7 @@ public class SocialPost extends Thread {
|
||||||
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(basePath);
|
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(basePath);
|
||||||
gxhttpStringRequest.from(Constants.CATALOGUE_NAME);
|
gxhttpStringRequest.from(Constants.CATALOGUE_NAME);
|
||||||
gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
|
gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
|
||||||
gxhttpStringRequest.setSecurityToken(Constants.getSecurityToken());
|
gxhttpStringRequest.setSecurityToken(Constants.getCatalogueSecurityToken());
|
||||||
gxhttpStringRequest.path(SOCIAL_SERVICE_WRITE_APPLICATION_POST_PATH);
|
gxhttpStringRequest.path(SOCIAL_SERVICE_WRITE_APPLICATION_POST_PATH);
|
||||||
|
|
||||||
HttpURLConnection httpURLConnection = gxhttpStringRequest.post(objectMapper.writeValueAsString(objectNode));
|
HttpURLConnection httpURLConnection = gxhttpStringRequest.post(objectMapper.writeValueAsString(objectNode));
|
||||||
|
|
|
@ -10,6 +10,8 @@ import javax.ws.rs.InternalServerErrorException;
|
||||||
import javax.ws.rs.WebApplicationException;
|
import javax.ws.rs.WebApplicationException;
|
||||||
|
|
||||||
import org.gcube.common.authorization.utils.manager.SecretManager;
|
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.KeycloakClientFactory;
|
||||||
import org.gcube.common.keycloak.model.TokenResponse;
|
import org.gcube.common.keycloak.model.TokenResponse;
|
||||||
|
|
||||||
|
@ -46,7 +48,7 @@ public class Constants {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static String getCatalogueApplicationToken() {
|
private static String getCatalogueApplicationToken() {
|
||||||
String context = SecretManager.instance.get().getContext();
|
String context = SecretManager.instance.get().getContext();
|
||||||
try {
|
try {
|
||||||
return applicationTokens.get(context);
|
return applicationTokens.get(context);
|
||||||
|
@ -82,8 +84,7 @@ public class Constants {
|
||||||
return tr.getAccessToken();
|
return tr.getAccessToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getCatalogueSecurityToken() throws Exception {
|
||||||
public static String getSecurityToken() throws Exception {
|
|
||||||
try {
|
try {
|
||||||
return getJWTAccessToken();
|
return getJWTAccessToken();
|
||||||
}catch (Exception e) {
|
}catch (Exception e) {
|
||||||
|
@ -91,4 +92,9 @@ public class Constants {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Secret getCatalogueSecret() throws Exception {
|
||||||
|
String securityToken = getCatalogueSecurityToken();
|
||||||
|
return SecretUtility.getSecretByTokenString(securityToken);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,12 @@ import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Map;
|
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.gxhttp.request.GXHTTPStringRequest;
|
||||||
import org.gcube.common.storagehub.client.dsl.FileContainer;
|
import org.gcube.common.storagehub.client.dsl.FileContainer;
|
||||||
import org.gcube.common.storagehub.model.Metadata;
|
import org.gcube.common.storagehub.model.Metadata;
|
||||||
import org.gcube.gcat.utils.Constants;
|
import org.gcube.gcat.utils.Constants;
|
||||||
import org.gcube.storagehub.ApplicationMode;
|
|
||||||
import org.gcube.storagehub.StorageHubManagement;
|
import org.gcube.storagehub.StorageHubManagement;
|
||||||
import org.glassfish.jersey.media.multipart.ContentDisposition;
|
import org.glassfish.jersey.media.multipart.ContentDisposition;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -43,9 +44,10 @@ public class CatalogueStorageHubManagement {
|
||||||
}
|
}
|
||||||
|
|
||||||
public URL ensureResourcePersistence(URL persistedURL, String itemID, String resourceID) throws Exception {
|
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 {
|
try {
|
||||||
applicationMode.start();
|
secretManager.startSession(secret);
|
||||||
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(persistedURL.toString());
|
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(persistedURL.toString());
|
||||||
gxhttpStringRequest.from(Constants.CATALOGUE_NAME);
|
gxhttpStringRequest.from(Constants.CATALOGUE_NAME);
|
||||||
gxhttpStringRequest.isExternalCall(true);
|
gxhttpStringRequest.isExternalCall(true);
|
||||||
|
@ -63,20 +65,21 @@ public class CatalogueStorageHubManagement {
|
||||||
logger.error("Error while trying to persists the resource", e);
|
logger.error("Error while trying to persists the resource", e);
|
||||||
throw e;
|
throw e;
|
||||||
} finally {
|
} finally {
|
||||||
applicationMode.end();
|
secretManager.endSession();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteResourcePersistence(String itemID, String resourceID, String mimeType) throws Exception {
|
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 {
|
try {
|
||||||
applicationMode.start();
|
secretManager.startSession(secret);
|
||||||
storageHubManagement = new StorageHubManagement();
|
storageHubManagement = new StorageHubManagement();
|
||||||
CatalogueMetadata catalogueMetadata = new CatalogueMetadata(itemID);
|
CatalogueMetadata catalogueMetadata = new CatalogueMetadata(itemID);
|
||||||
storageHubManagement.setCheckMetadata(catalogueMetadata);
|
storageHubManagement.setCheckMetadata(catalogueMetadata);
|
||||||
storageHubManagement.removePersistedFile(resourceID, mimeType);
|
storageHubManagement.removePersistedFile(resourceID, mimeType);
|
||||||
} finally {
|
} finally {
|
||||||
applicationMode.end();
|
secretManager.endSession();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,35 +100,38 @@ public class CatalogueStorageHubManagement {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void renameFile(String resourceID, String revisionID) throws Exception {
|
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 {
|
try {
|
||||||
applicationMode.start();
|
secretManager.startSession(secret);
|
||||||
FileContainer createdfile = storageHubManagement.getPersistedFile();
|
FileContainer createdfile = storageHubManagement.getPersistedFile();
|
||||||
createdfile.rename(resourceID);
|
createdfile.rename(resourceID);
|
||||||
internalAddRevisionID(resourceID, revisionID);
|
internalAddRevisionID(resourceID, revisionID);
|
||||||
} finally {
|
} finally {
|
||||||
applicationMode.end();
|
secretManager.endSession();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addRevisionID(String resourceID, String revisionID) throws Exception {
|
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 {
|
try {
|
||||||
applicationMode.start();
|
secretManager.startSession(secret);
|
||||||
internalAddRevisionID(resourceID, revisionID);
|
internalAddRevisionID(resourceID, revisionID);
|
||||||
} finally {
|
} finally {
|
||||||
applicationMode.end();
|
secretManager.endSession();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileContainer retrievePersistedFile(String id, String mimeType) throws Exception {
|
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 {
|
try {
|
||||||
applicationMode.start();
|
secretManager.startSession(secret);
|
||||||
return storageHubManagement.getPersistedFile(id, mimeType);
|
return storageHubManagement.getPersistedFile(id, mimeType);
|
||||||
} finally {
|
} finally {
|
||||||
applicationMode.end();
|
secretManager.endSession();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,8 +16,8 @@ public class SocialPostTest extends ContextTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToken() throws Exception {
|
public void testToken() throws Exception {
|
||||||
logger.debug("Application Token is {}", Constants.getCatalogueApplicationToken());
|
logger.debug("Application Token is {}", Constants.getCatalogueSecurityToken());
|
||||||
ContextTest.setContext(Constants.getCatalogueApplicationToken());
|
ContextTest.setContext(Constants.getCatalogueSecurityToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -11,9 +11,9 @@ public class ConstantsTest extends ContextTest {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ConstantsTest.class);
|
private static final Logger logger = LoggerFactory.getLogger(ConstantsTest.class);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetApplicationToken() {
|
public void testGetApplicationToken() throws Exception {
|
||||||
logger.debug("Application token for Context {} is {}", SecretManager.instance.get().getContext(),
|
logger.debug("Application token for Context {} is {}", SecretManager.instance.get().getContext(),
|
||||||
Constants.getCatalogueApplicationToken());
|
Constants.getCatalogueSecurityToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue