Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jans-core): port Gluu ORM-based document store #2506 #2581

Merged
merged 3 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions jans-core/document-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
<artifactId>jans-orm-util</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.jans</groupId>
<artifactId>jans-orm-core</artifactId>
<version>${project.version}</version>
</dependency>



<!-- CDI -->
<dependency>
Expand Down
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 []";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public class DocumentStoreConfiguration implements Serializable {

private JcaDocumentStoreConfiguration jcaConfiguration;

private WebDavDocumentStoreConfiguration webDavConfiguration;
private WebDavDocumentStoreConfiguration webDavConfiguration;

private DBDocumentStoreConfiguration dbConfiguration;

public DocumentStoreType getDocumentStoreType() {
return documentStoreType;
Expand Down Expand Up @@ -61,9 +63,17 @@ public void setWebDavConfiguration(WebDavDocumentStoreConfiguration webDavConfig
this.webDavConfiguration = webDavConfiguration;
}

public DBDocumentStoreConfiguration getDbConfiguration() {
return dbConfiguration;
}

public void setDbConfiguration(DBDocumentStoreConfiguration dbConfiguration) {
this.dbConfiguration = dbConfiguration;
}

@Override
public String toString() {
return "DocumentStoreConfiguration [documentStoreType=" + documentStoreType + ", localConfiguration=" + localConfiguration
+ ", jcaConfiguration=" + jcaConfiguration + ", webDavConfiguration=" + webDavConfiguration + "]";
+ ", jcaConfiguration=" + jcaConfiguration + ", webDavConfiguration=" + webDavConfiguration + ", dbConfiguration=" + dbConfiguration + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
*/
@XmlEnum(String.class)
public enum DocumentStoreType {
LOCAL, JCA, WEB_DAV
LOCAL, JCA, WEB_DAV, DB
}
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IOException is redundant because it has exactly same statements as super Exception.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something is wrong here. In case of exception, we got further in line 187 and return true even if it's not renamed.
Also we should log exception for easier troubleshooting.

}
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

import io.jans.service.document.store.conf.DocumentStoreType;

Expand All @@ -24,12 +25,12 @@ public interface DocumentStore<T> {
/**
* Save document into store
*/
boolean saveDocument(String path, String documentContent, Charset charset);
boolean saveDocument(String path, String documentContent, Charset charset, List<String> moduleList);

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

/**
* Load document from store
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.jcr.version.VersionException;
import java.io.*;
import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.*;

/**
Expand Down Expand Up @@ -129,7 +130,7 @@ public boolean hasDocument(String path) {
}

@Override
public boolean saveDocument(String path, String documentContent, Charset charset) {
public boolean saveDocument(String path, String documentContent, Charset charset, List<String> moduleList) {
log.debug("Save document: '{}'", path);

String normalizedPath = getNormalizedPath(path);
Expand All @@ -153,7 +154,7 @@ public boolean saveDocument(String path, String documentContent, Charset charset
}

@Override
public boolean saveDocumentStream(String path, InputStream documentStream) {
public boolean saveDocumentStream(String path, InputStream documentStream, List<String> moduleList) {
log.debug("Save document from stream: '{}'", path);

String normalizedPath = getNormalizedPath(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
Expand Down Expand Up @@ -99,7 +100,7 @@ public boolean hasDocument(String path) {
}

@Override
public boolean saveDocument(String path, String documentContent, Charset charset) {
public boolean saveDocument(String path, String documentContent, Charset charset, List<String> moduleList) {
log.debug("Save document: '{}'", path);

File file = buildFilePath(path);
Expand All @@ -120,7 +121,7 @@ public boolean saveDocument(String path, String documentContent, Charset charset
}

@Override
public boolean saveDocumentStream(String path, InputStream documentStream) {
public boolean saveDocumentStream(String path, InputStream documentStream, List<String> moduleList) {
log.debug("Save document from stream: '{}'", path);

File file = buildFilePath(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
Expand Down Expand Up @@ -70,13 +71,13 @@ public boolean hasDocument(String path) {
}

@Override
public boolean saveDocument(String path, String documentContent, Charset charset) {
public boolean saveDocument(String path, String documentContent, Charset charset, List<String> moduleList) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean saveDocumentStream(String path, InputStream documentStream) {
public boolean saveDocumentStream(String path, InputStream documentStream, List<String> moduleList) {
// TODO Auto-generated method stub
return false;
}
Expand Down
Loading