Skip to content

Commit

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

Signed-off-by: Yuriy Movchan <Yuriy.Movchan@gmail.com>
  • Loading branch information
yurem committed Mar 8, 2024
1 parent 3828fa1 commit da4e4ea
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 140 deletions.
43 changes: 1 addition & 42 deletions jans-config-api/plugins/docs/kc-saml-plugin-swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ paths:
- SAML - Trust Relationship
summary: Get all Trust Relationship
description: Get all TrustRelationship.
operationId: get-trust-relationships
operationId: get-trust-relationship
responses:
"200":
description: Ok
Expand Down Expand Up @@ -830,47 +830,6 @@ paths:
security:
- oauth2:
- https://jans.io/oauth/config/saml.readonly
/kc/saml/trust-relationship/sp-metadata-file/{id}:
get:
tags:
- SAML - Trust Relationship
summary: Get TrustRelationship file metadata
description: Get TrustRelationship file metadata
operationId: get-trust-relationship-file-metadata
parameters:
- name: id
in: path
description: TrustRelationship inum
required: true
schema:
type: string
responses:
"200":
description: OK
content:
application/xml:
schema:
type: string
format: binary
"400":
description: Bad Request
content:
application/json:
schema:
$ref: '#/components/schemas/ApiError'
"401":
description: Unauthorized
"404":
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/ApiError'
"500":
description: Internal Server Error
security:
- oauth2:
- https://jans.io/oauth/config/saml.readonly
/kc/saml/trust-relationship/process-sp-meta-file:
post:
tags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class DBDocumentStoreProvider extends DocumentStoreProvider<DBDocumentSto

@Inject
private DBDocumentService documentService;

@Inject
private DocumentStoreConfiguration documentStoreConfiguration;

Expand All @@ -39,113 +39,113 @@ 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 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(path);
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, 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(path);
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 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 +162,45 @@ public InputStream readDocumentAsStream(String name) {
}

@Override
public boolean renameDocument(String currentDisplayName, String destinationDisplayName) {
log.debug("Rename document: '{}' -> '{}'", currentDisplayName, destinationDisplayName);
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,12 @@ public interface DocumentStore<T> {
/**
* Save document into store
*/
boolean saveDocument(String path, String documentContent, Charset charset, List<String> moduleList);
String saveDocument(String path, 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, InputStream documentStream, List<String> moduleList);

/**
* Load document from store
Expand All @@ -50,7 +50,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
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public boolean hasDocument(String path) {
}

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

String normalizedPath = getNormalizedPath(path);
Expand All @@ -142,19 +142,19 @@ public boolean saveDocument(String path, String documentContent, Charset charset
contentNode.setProperty("jcr:data", value);

session.save();
return true;
return path;
} finally {
closeSession(session);
}
} catch (RepositoryException ex) {
log.error("Failed to write document to file '{}'", path, ex);
}

return false;
return null;
}

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

String normalizedPath = getNormalizedPath(path);
Expand All @@ -166,15 +166,15 @@ public boolean saveDocumentStream(String path, InputStream documentStream, List<
contentNode.setProperty("jcr:data", value);

session.save();
return true;
return path;
} finally {
closeSession(session);
}
} catch (RepositoryException ex) {
log.error("Failed to write document from stream to file '{}'", path, ex);
}

return false;
return null;
}


Expand Down Expand Up @@ -243,7 +243,7 @@ public InputStream readDocumentAsStream(String path) {
}

@Override
public boolean renameDocument(String currentPath, String destinationPath) {
public String renameDocument(String currentPath, String destinationPath) {
log.debug("Rename document: '{}' -> '{}'", currentPath, destinationPath);

String normalizedCurrentPath = getNormalizedPath(currentPath);
Expand All @@ -258,15 +258,15 @@ public boolean renameDocument(String currentPath, String destinationPath) {
session.move(normalizedCurrentPath, normalizedDestinationPath);

session.save();
return true;
return destinationPath;
} finally {
closeSession(session);
}
} catch (RepositoryException ex) {
log.error("Failed to rename to destination file '{}'", destinationPath, ex);
}

return false;
return null;
}

@Override
Expand Down
Loading

0 comments on commit da4e4ea

Please sign in to comment.