-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jans-core): port Gluu ORM-based document store
Signed-off-by: shekhar16 <shekharlaad1609@gmail.com>
- Loading branch information
Showing
13 changed files
with
635 additions
and
17 deletions.
There are no files selected for viewing
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
20 changes: 20 additions & 0 deletions
20
...store/src/main/java/io/jans/service/document/store/conf/DBDocumentStoreConfiguration.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,20 @@ | ||
package io.jans.service.document.store.conf; | ||
|
||
import java.io.Serializable; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
/** | ||
* @author shekhar L. on 29/10/2022 | ||
*/ | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class DBDocumentStoreConfiguration implements Serializable { | ||
|
||
private static final long serialVersionUID = 3380170170265842538L; | ||
|
||
@Override | ||
public String toString() { | ||
return "DBDocumentStoreConfiguration []"; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,5 @@ | |
*/ | ||
@XmlEnum(String.class) | ||
public enum DocumentStoreType { | ||
LOCAL, JCA, WEB_DAV | ||
LOCAL, JCA, WEB_DAV, DB | ||
} |
213 changes: 213 additions & 0 deletions
213
...-store/src/main/java/io/jans/service/document/store/provider/DBDocumentStoreProvider.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,213 @@ | ||
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.util.ArrayList; | ||
import java.util.Base64; | ||
import java.util.List; | ||
|
||
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; | ||
import io.jans.service.document.store.conf.DocumentStoreType; | ||
import io.jans.service.document.store.service.DBDocumentService; | ||
import io.jans.service.document.store.service.OxDocument; | ||
import io.jans.util.StringHelper; | ||
import jakarta.annotation.PostConstruct; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
/** | ||
* @author Shekhar L. on 29/10/2022 | ||
*/ | ||
@ApplicationScoped | ||
public class DBDocumentStoreProvider extends DocumentStoreProvider<DBDocumentStoreProvider> { | ||
|
||
@Inject | ||
private Logger log; | ||
|
||
@Inject | ||
private DBDocumentService documentService; | ||
|
||
@Inject | ||
private DocumentStoreConfiguration documentStoreConfiguration; | ||
|
||
private DBDocumentStoreConfiguration dbDocumentStoreConfiguration; | ||
|
||
public DocumentStoreType getProviderType() { | ||
return DocumentStoreType.DB; | ||
} | ||
|
||
@PostConstruct | ||
public void init() { | ||
this.dbDocumentStoreConfiguration = documentStoreConfiguration.getDbConfiguration(); | ||
} | ||
|
||
public void configure(DocumentStoreConfiguration documentStoreConfiguration, PersistenceEntryManager persistenceEntryManager) { | ||
this.log = LoggerFactory.getLogger(DBDocumentStoreProvider.class); | ||
this.documentStoreConfiguration = documentStoreConfiguration; | ||
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)) { | ||
throw new IllegalArgumentException("Specified path should not be empty!"); | ||
} | ||
OxDocument oxDocument = null; | ||
try { | ||
oxDocument = documentService.getOxDocumentByDisplayName(DisplayName); | ||
} catch (Exception e) { | ||
log.error("Failed to check if path '" + DisplayName + "' exists in repository", e); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public boolean saveDocument(String name, String documentContent, Charset charset, List<String> moduleList) { | ||
log.debug("Save document: '{}'", name); | ||
OxDocument oxDocument = new OxDocument(); | ||
oxDocument.setDocument(documentContent); | ||
oxDocument.setDisplayName(name); | ||
try { | ||
try { | ||
oxDocument.setInum(documentService.generateInumForNewOxDocument()); | ||
String dn = "inum="+ oxDocument.getInum() +",ou=document,o=gluu"; | ||
oxDocument.setDn(dn); | ||
oxDocument.setDescription(name); | ||
oxDocument.setOxEnabled("true"); | ||
oxDocument.setOxModuleProperty(moduleList); | ||
documentService.addOxDocument(oxDocument); | ||
return true; | ||
} finally { | ||
} | ||
} catch (Exception ex) { | ||
log.error("Failed to write document to file '{}'", name, ex); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public boolean saveDocumentStream(String name, InputStream documentStream, List <String> moduleList) { | ||
|
||
OxDocument oxDocument = new OxDocument(); | ||
oxDocument.setDisplayName(name); | ||
|
||
try { | ||
String documentContent = Base64.getEncoder().encodeToString(IOUtils.toByteArray(documentStream)); | ||
oxDocument.setDocument(documentContent); | ||
String inum = documentService.generateInumForNewOxDocument(); | ||
oxDocument.setInum(inum); | ||
String dn = "inum="+ oxDocument.getInum() +",ou=document,o=gluu"; | ||
oxDocument.setDn(dn); | ||
oxDocument.setDescription(name); | ||
oxDocument.setOxEnabled("true"); | ||
oxDocument.setOxModuleProperty(moduleList); | ||
documentService.addOxDocument(oxDocument); | ||
return true; | ||
} catch (IOException e) { | ||
log.error("Failed to write document from stream to file '{}'", name, e); | ||
}catch (Exception e) { | ||
log.error("Failed to write document from stream to file '{}'", name, e); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
||
@Override | ||
public String readDocument(String name, Charset charset) { | ||
log.debug("Read document: '{}'", name); | ||
OxDocument oxDocument; | ||
try { | ||
oxDocument = documentService.getOxDocumentByDisplayName(name); | ||
if(oxDocument != null) { | ||
return oxDocument.getDocument(); | ||
} | ||
} catch (Exception e) { | ||
log.error("Failed to read document as stream from file '{}'", name, e); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public InputStream readDocumentAsStream(String name) { | ||
log.debug("Read document as stream: '{}'", name); | ||
String filecontecnt = readDocument(name, null); | ||
if (filecontecnt == null) { | ||
log.error("Document file '{}' isn't exist", name); | ||
return null; | ||
} | ||
|
||
InputStream InputStream = new ByteArrayInputStream(Base64.getDecoder().decode(filecontecnt)); | ||
return InputStream; | ||
} | ||
|
||
@Override | ||
public boolean renameDocument(String currentDisplayName, String destinationDisplayName) { | ||
log.debug("Rename document: '{}' -> '{}'", currentDisplayName, destinationDisplayName); | ||
OxDocument oxDocument; | ||
try { | ||
oxDocument = documentService.getOxDocumentByDisplayName(currentDisplayName); | ||
if (oxDocument == null) { | ||
log.error("Document doesn't Exist with the name '{}'", currentDisplayName); | ||
return false; | ||
} | ||
oxDocument.setDisplayName(destinationDisplayName); | ||
documentService.updateOxDocument(oxDocument); | ||
OxDocument oxDocumentDestination = documentService.getOxDocumentByDisplayName(destinationDisplayName); | ||
if(oxDocumentDestination == null) { | ||
log.error("Failed to rename to destination file '{}'", destinationDisplayName); | ||
return false; | ||
} | ||
} catch (Exception e) { | ||
log.error("Failed to rename to destination file '{}'", destinationDisplayName); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean removeDocument(String inum) { | ||
log.debug("Remove document: '{}'", inum); | ||
OxDocument oxDocument; | ||
try { | ||
oxDocument = documentService.getOxDocumentByInum(inum); | ||
if(oxDocument == null) { | ||
log.error(" document not exist file '{}'", inum); | ||
return false; | ||
} | ||
|
||
documentService.removeOxDocument(oxDocument); | ||
OxDocument checkOxDocument = documentService.getOxDocumentByInum(inum); | ||
if(checkOxDocument == null) { | ||
return true; | ||
} | ||
return false; | ||
} catch (Exception e) { | ||
log.error("Failed to remove document file '{}'", inum, e); | ||
} | ||
return false; | ||
} | ||
|
||
} |
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
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.