Skip to content

Commit

Permalink
feat: doc store update (#8009)
Browse files Browse the repository at this point in the history
* feat(documentstore): save document should return a handle to created asset or an identifier #7920

Signed-off-by: Yuriy Movchan <Yuriy.Movchan@gmail.com>

* feat: document store API should allow to specify file description #7981

Signed-off-by: Yuriy Movchan <Yuriy.Movchan@gmail.com>

* feat(documnt-store): should support document for all types required for jans asset mgt #7917

Signed-off-by: Yuriy Movchan <Yuriy.Movchan@gmail.com>

* feat(documnt-store): should support document for all types required for jans asset mgt #7917

Signed-off-by: Yuriy Movchan <Yuriy.Movchan@gmail.com>

* feat(documnt-store): should support document for all types required for jans asset mgt #7917

Signed-off-by: Yuriy Movchan <Yuriy.Movchan@gmail.com>

---------

Signed-off-by: Yuriy Movchan <Yuriy.Movchan@gmail.com>
  • Loading branch information
yurem committed Mar 8, 2024
1 parent 53ef338 commit 59d4cd5
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ public String saveMetadataFile(String metadataDir, String metadataFileName, Stri
logger.info("documentStoreService:{}, metadataFile:{}, localDocumentStoreService:{} ", documentStoreService,
metadataFile, localDocumentStoreService);
try {
boolean result = documentStoreService.saveDocumentStream(metadataFile, stream,
List.of("jans-server", documentStoreModuleName));
String result = documentStoreService.saveDocumentStream(metadataFile, null,
stream, List.of("jans-server", documentStoreModuleName));
logger.info("SAML file saving result:{}", result);

InputStream newFile = documentStoreService.readDocumentAsStream(metadataFile);
logger.info("SAML file read newFile:{}", newFile);

if (result) {
if (result != null) {
return metadataFile;
}
} catch (Exception ex) {
Expand Down Expand Up @@ -123,7 +123,7 @@ public boolean renameMetadata(String metadataPath, String destinationMetadataPat
logger.debug("Rename metadata file documentStoreService:{},metadataPath:{}, destinationMetadataPath:{}",
documentStoreService, metadataPath, destinationMetadataPath);
try {
return documentStoreService.renameDocument(metadataPath, destinationMetadataPath);
return documentStoreService.renameDocument(metadataPath, destinationMetadataPath) != null;
} catch (Exception ex) {
logger.error("Failed to rename metadata '{}' to '{}'", metadataPath, destinationMetadataPath, ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package io.jans.service.document.store.provider;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.apache.commons.codec.binary.Base64InputStream;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.jans.orm.PersistenceEntryManager;
import io.jans.service.document.store.conf.DBDocumentStoreConfiguration;
import io.jans.service.document.store.conf.DocumentStoreConfiguration;
Expand All @@ -10,14 +22,6 @@
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
* @author Shekhar L. on 29/10/2022
Expand All @@ -30,7 +34,7 @@ public class DBDocumentStoreProvider extends DocumentStoreProvider<DBDocumentSto

@Inject
private DBDocumentService documentService;

@Inject
private DocumentStoreConfiguration documentStoreConfiguration;

Expand All @@ -39,113 +43,119 @@ public class DBDocumentStoreProvider extends DocumentStoreProvider<DBDocumentSto
public DocumentStoreType getProviderType() {
return DocumentStoreType.DB;
}

@PostConstruct
public void init() {
this.dbDocumentStoreConfiguration = documentStoreConfiguration.getDbConfiguration();
}

public void configure(DocumentStoreConfiguration documentStoreConfiguration, PersistenceEntryManager persistenceEntryManager) {

public void configure(DocumentStoreConfiguration documentStoreConfiguration,
PersistenceEntryManager persistenceEntryManager) {
this.log = LoggerFactory.getLogger(DBDocumentStoreProvider.class);
this.documentStoreConfiguration = documentStoreConfiguration;
if(documentService == null) {
if (documentService == null) {
this.documentService = new DBDocumentService(persistenceEntryManager);
}
}

@Override
public void create() {

}

@Override
public void destroy() {

}

@Override
public boolean hasDocument(String DisplayName) {
log.debug("Has document: '{}'", DisplayName);
if (StringHelper.isEmpty(DisplayName)) {
public boolean hasDocument(String path) {
log.debug("Has document: '{}'", path);
if (StringHelper.isEmpty(path)) {
throw new IllegalArgumentException("Specified path should not be empty!");
}
}
Document oxDocument = null;
try {
oxDocument = documentService.getDocumentByDisplayName(DisplayName);
if(oxDocument != null) {
oxDocument = documentService.getDocumentByDisplayName(path);
if (oxDocument != null) {
return true;
}
} catch (Exception e) {
log.error("Failed to check if path '" + DisplayName + "' exists in repository", e);
log.error("Failed to check if path '" + path + "' exists in repository", e);
}

return false;
}

@Override
public boolean saveDocument(String name, String documentContent, Charset charset, List<String> moduleList) {
log.debug("Save document: '{}'", name);
public String saveDocument(String path, String description, String documentContent, Charset charset, List<String> moduleList) {
log.debug("Save document: '{}'", path);
Document oxDocument = new Document();
oxDocument.setDocument(documentContent);
oxDocument.setDisplayName(name);
oxDocument.setDisplayName(path);
try {
try {
oxDocument.setInum(documentService.generateInumForNewDocument());
String dn = "inum="+ oxDocument.getInum() +",ou=document,o=jans";
oxDocument.setInum(documentService.generateInumForNewDocument());
String dn = "inum=" + oxDocument.getInum() + ",ou=document,o=jans";
oxDocument.setDn(dn);
oxDocument.setDescription(name);
oxDocument.setDescription(description);
oxDocument.setJansEnabled(true);
oxDocument.setJansModuleProperty(moduleList);
oxDocument.setJansModuleProperty(moduleList);
documentService.addDocument(oxDocument);
return true;
return path;
} finally {
}
} catch (Exception ex) {
log.error("Failed to write document to file '{}'", name, ex);
log.error("Failed to write document to file '{}'", path, ex);
}

return false;
return null;
}

@Override
public boolean saveDocumentStream(String name, InputStream documentStream, List <String> moduleList) {
public String saveDocumentStream(String path, String description, InputStream documentStream, List<String> moduleList) {

Document oxDocument = new Document();
oxDocument.setDisplayName(name);
try {
oxDocument.setDisplayName(path);

try {
String documentContent = new String(documentStream.readAllBytes(), StandardCharsets.UTF_8);
oxDocument.setDocument(documentContent);
String inum = documentService.generateInumForNewDocument();
oxDocument.setInum(inum);
String dn = "inum="+ oxDocument.getInum() +",ou=document,o=jans";
oxDocument.setInum(inum);
String dn = "inum=" + oxDocument.getInum() + ",ou=document,o=jans";
oxDocument.setDn(dn);
oxDocument.setDescription(name);
oxDocument.setDescription(description);
oxDocument.setJansEnabled(true);
oxDocument.setJansModuleProperty(moduleList);
documentService.addDocument(oxDocument);
return true;
return path;
} catch (Exception e) {
log.error("Failed to write document from stream to file '{}'", name, e);
}
log.error("Failed to write document from stream to file '{}'", path, e);
}

return false;
return null;
}

@Override
public String saveBinaryDocumentStream(String path, String description, InputStream documentStream,
List<String> moduleList) {
return saveDocumentStream(path, description, new Base64InputStream(documentStream, true), moduleList);
}

@Override
public String readDocument(String name, Charset charset) {
log.debug("Read document: '{}'", name);
log.debug("Read document: '{}'", name);
Document oxDocument;
try {
oxDocument = documentService.getDocumentByDisplayName(name);
if(oxDocument != null) {
if (oxDocument != null) {
return oxDocument.getDocument();
}
} catch (Exception e) {
log.error("Failed to read document as stream from file '{}'", name, e);
}
return null;
return null;
}

@Override
Expand All @@ -162,47 +172,50 @@ public InputStream readDocumentAsStream(String name) {
}

@Override
public boolean renameDocument(String currentDisplayName, String destinationDisplayName) {
log.debug("Rename document: '{}' -> '{}'", currentDisplayName, destinationDisplayName);
public InputStream readBinaryDocumentAsStream(String path) {
return new Base64InputStream(readDocumentAsStream(path), false);
}

@Override
public String renameDocument(String currentPath, String destinationPath) {
log.debug("Rename document: '{}' -> '{}'", currentPath, destinationPath);
Document oxDocument;
try {
oxDocument = documentService.getDocumentByDisplayName(currentDisplayName);
oxDocument = documentService.getDocumentByDisplayName(currentPath);
if (oxDocument == null) {
log.error("Document doesn't Exist with the name '{}'", currentDisplayName);
return false;
log.error("Document doesn't Exist with the name '{}'", currentPath);
return null;
}
oxDocument.setDisplayName(destinationDisplayName);
oxDocument.setDisplayName(destinationPath);
documentService.updateDocument(oxDocument);
Document oxDocumentDestination = documentService.getDocumentByDisplayName(destinationDisplayName);
if(oxDocumentDestination == null) {
log.error("Failed to rename to destination file '{}'", destinationDisplayName);
return false;
Document oxDocumentDestination = documentService.getDocumentByDisplayName(destinationPath);
if (oxDocumentDestination == null) {
log.error("Failed to rename to destination file '{}'", destinationPath);
return null;
}
} catch (Exception e) {
log.error("Failed to rename to destination file '{}'", destinationDisplayName);
log.error("Failed to rename to destination file '{}'", destinationPath);
}
return true;

return destinationPath;
}

@Override
public boolean removeDocument(String inum) {
log.debug("Remove document: '{}'", inum);
public boolean removeDocument(String path) {
log.debug("Remove document: '{}'", path);
Document oxDocument;
try {
oxDocument = documentService.getDocumentByInum(inum);
if(oxDocument == null) {
log.error(" document not exist file '{}'", inum);
oxDocument = documentService.getDocumentByDisplayName(path);
if (oxDocument == null) {
log.error(" document not exist file '{}'", path);
return false;
}

documentService.removeDocument(oxDocument);
Document checkDocument = documentService.getDocumentByInum(inum);
if(checkDocument == null) {
return true;
}
return false;

return true;
} catch (Exception e) {
log.error("Failed to remove document file '{}'", inum, e);
log.error("Failed to remove document file '{}'", path, e);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ public interface DocumentStore<T> {
/**
* Save document into store
*/
boolean saveDocument(String path, String documentContent, Charset charset, List<String> moduleList);
String saveDocument(String path, String description, String documentContent, Charset charset, List<String> moduleList);

/**
* Save document stream into store
*/
boolean saveDocumentStream(String path, InputStream documentStream, List<String> moduleList);
String saveDocumentStream(String path, String description, InputStream documentStream, List<String> moduleList);

/**
* Save binary document stream into store
*/
String saveBinaryDocumentStream(String path, String description, InputStream documentStream, List<String> moduleList);

/**
* Load document from store
Expand All @@ -42,6 +47,11 @@ public interface DocumentStore<T> {
*/
public InputStream readDocumentAsStream(String path) ;

/**
* Load binary document from store as stream
*/
public InputStream readBinaryDocumentAsStream(String path);

/**
* Removes an object document from store
*/
Expand All @@ -50,7 +60,7 @@ public interface DocumentStore<T> {
/**
* Rename an object in document store
*/
boolean renameDocument(String currentPath, String destinationPath);
String renameDocument(String currentPath, String destinationPath);

public abstract DocumentStoreType getProviderType();

Expand Down
Loading

0 comments on commit 59d4cd5

Please sign in to comment.