From b104fbd6f9f4effb8499056f3fb8e1608381e2ec Mon Sep 17 00:00:00 2001 From: shekhar16 Date: Tue, 28 Feb 2017 18:07:03 +0530 Subject: [PATCH] oxTrust/issues/392 : Added code to import ldif file. --- .../oxtrust/action/AttributeImportAction.java | 287 ++++++++++++++++++ .../oxtrust/ldap/service/LdifService.java | 102 +++++++ .../incl/attribute/attributeImportForm.xhtml | 73 +++++ .../webapp/WEB-INF/incl/layout/leftmenu.xhtml | 7 + .../webapp/attribute/attributeImport.page.xml | 63 ++++ .../webapp/attribute/attributeImport.xhtml | 33 ++ 6 files changed, 565 insertions(+) create mode 100644 server/src/main/java/org/gluu/oxtrust/action/AttributeImportAction.java create mode 100644 server/src/main/java/org/gluu/oxtrust/ldap/service/LdifService.java create mode 100644 server/src/main/webapp/WEB-INF/incl/attribute/attributeImportForm.xhtml create mode 100644 server/src/main/webapp/attribute/attributeImport.page.xml create mode 100644 server/src/main/webapp/attribute/attributeImport.xhtml diff --git a/server/src/main/java/org/gluu/oxtrust/action/AttributeImportAction.java b/server/src/main/java/org/gluu/oxtrust/action/AttributeImportAction.java new file mode 100644 index 000000000..637aa7570 --- /dev/null +++ b/server/src/main/java/org/gluu/oxtrust/action/AttributeImportAction.java @@ -0,0 +1,287 @@ +/* + * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text. + * + * Copyright (c) 2014, Gluu + */ + +package org.gluu.oxtrust.action; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Serializable; + +import org.gluu.oxtrust.ldap.load.conf.ImportPersonConfiguration; +import org.gluu.oxtrust.ldap.service.AttributeService; +import org.gluu.oxtrust.ldap.service.ExcelService; +import org.gluu.oxtrust.ldap.service.IPersonService; +import org.gluu.oxtrust.ldap.service.LdifService; +import org.gluu.oxtrust.service.external.ExternalUpdateUserService; +import org.gluu.oxtrust.util.OxTrustConstants; +import org.gluu.site.ldap.persistence.LdapEntryManager; +import org.gluu.site.ldap.persistence.LdifDataUtility; +import org.jboss.seam.ScopeType; +import org.jboss.seam.annotations.Destroy; +import org.jboss.seam.annotations.In; +import org.jboss.seam.annotations.Logger; +import org.jboss.seam.annotations.Name; +import org.jboss.seam.annotations.Out; +import org.jboss.seam.annotations.Scope; +import org.jboss.seam.annotations.security.Restrict; +import org.jboss.seam.faces.FacesMessages; +import org.jboss.seam.international.StatusMessage.Severity; +import org.jboss.seam.international.StatusMessages; +import org.jboss.seam.log.Log; +import org.richfaces.event.FileUploadEvent; +import org.richfaces.model.UploadedFile; +import org.xdi.config.oxtrust.ApplicationConfiguration; +import org.xdi.model.GluuAttribute; + +import com.unboundid.ldap.sdk.ResultCode; +import com.unboundid.ldif.LDIFReader; + +/** + * Action class for load data from LDIF file + * + * @author Shekhar Laad Date: 02.14.2011 + */ +@Name("attributeImportAction") +@Scope(ScopeType.CONVERSATION) +@Restrict("#{identity.loggedIn}") +public class AttributeImportAction implements Serializable { + + private static final long serialVersionUID = -1270460481895022468L; + + public static final String PERSON_PASSWORD_ATTRIBUTE = "userPassword"; + + @Logger + private Log log; + + @In + StatusMessages statusMessages; + + @In + private IPersonService personService; + + @In + private LdifService ldifService; + + @In + private AttributeService attributeService; + + @In(value = "#{oxTrustConfiguration.applicationConfiguration}") + private ApplicationConfiguration applicationConfiguration; + + @In + private ExternalUpdateUserService externalUpdateUserService; + + @In + private transient ExcelService excelService; + + @In + private FacesMessages facesMessages; + + @In + private transient ImportPersonConfiguration importPersonConfiguration; + + @In + protected LdapEntryManager ldapEntryManager; + + @In(create = true) + @Out(scope = ScopeType.CONVERSATION) + private CustomAttributeAction customAttributeAction; + + private UploadedFile uploadedFile; + private FileDataToImport fileDataToImport; + private byte[] fileData; + + private boolean isInitialized; + LdifDataUtility ldifDataUtility; + + public String init() { + if (this.isInitialized) { + return OxTrustConstants.RESULT_SUCCESS; + } + + ldifDataUtility = LdifDataUtility.instance(); + this.fileDataToImport = new FileDataToImport(); + + this.isInitialized = true; + + return OxTrustConstants.RESULT_SUCCESS; + } + + public String importAttributes() throws Exception { + if (!fileDataToImport.isReady()) { + return OxTrustConstants.RESULT_FAILURE; + } + + if (uploadedFile != null) { + //Table table; + InputStream is = new ByteArrayInputStream(this.fileData); + + ResultCode result = ldifService.importLdifFileInLdap( is); + + if(!result.equals(ResultCode.SUCCESS)){ + removeFileDataToImport(); + this.fileDataToImport.setReady(false); + facesMessages.add(Severity.ERROR, "Invalid LDIF File. import Failed"); + return OxTrustConstants.RESULT_FAILURE; + } + + is.close(); + this.fileDataToImport.setReady(true); + this.fileDataToImport.setIs(is); + } + + removeFileToImport(); + facesMessages.add(Severity.INFO,"LDIF File is successfully Added"); + return OxTrustConstants.RESULT_SUCCESS; + } + + public void validateFileToImport() throws Exception { + removeFileDataToImport(); + + if (uploadedFile == null) { + return; + } + + if (uploadedFile != null) { + //Table table; + InputStream is = new ByteArrayInputStream(this.fileData); + ResultCode result = ldifService.importLdifFileInLdap( is); + if(!result.equals(ResultCode.SUCCESS)){ + log.info("LDIFReader --- : "); + removeFileDataToImport(); + this.fileDataToImport.setReady(false); + facesMessages.add(Severity.ERROR, "Invalid LDIF File. validation Failed"); + return; + } + is.close(); + this.fileDataToImport.setReady(true); + this.fileDataToImport.setIs(is); + } + } + + @Restrict("#{s:hasPermission('import', 'person')}") + public void cancel() { + destroy(); + } + + @Destroy + public void destroy() { + removeFileDataToImport(); + removeFileToImport(); + } + + public UploadedFile getUploadedFile() { + return uploadedFile; + } + + public FileDataToImport getFileDataToImport() { + return this.fileDataToImport; + } + + public void removeFileDataToImport() { + this.fileDataToImport.reset(); + } + + @Restrict("#{s:hasPermission('import', 'person')}") + public void uploadFile(FileUploadEvent event) { + removeFileToImport(); + + this.uploadedFile = event.getUploadedFile(); + this.fileData = this.uploadedFile.getData(); + } + + @Restrict("#{s:hasPermission('import', 'person')}") + public void removeFileToImport() { + if (uploadedFile != null) { + try { + uploadedFile.delete(); + } catch (IOException ex) { + log.error("Failed to remove temporary file", ex); + } + + this.uploadedFile = null; + } + removeFileDataToImport(); + } + + + public static class FileDataToImport implements Serializable { + + private static final long serialVersionUID = 7334362213305310293L; + + private String fileName; + private InputStream is; + private boolean ready; + + public FileDataToImport() { + } + + public FileDataToImport(InputStream is) { + this.is = is; + } + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public boolean isReady() { + return ready; + } + + public void setReady(boolean ready) { + this.ready = ready; + } + + public void reset() { + this.fileName = null; + this.is = null; + this.ready = false; + } + + public InputStream getIs() { + return is; + } + + public void setIs(InputStream is) { + this.is = is; + } + } + + public static class ImportAttribute implements Serializable { + + private static final long serialVersionUID = -5640983196565086530L; + + private GluuAttribute attribute; + private int col; + + public ImportAttribute(int col, GluuAttribute attribute) { + this.col = col; + this.attribute = attribute; + } + + public int getCol() { + return col; + } + + public void setCol(int col) { + this.col = col; + } + + public GluuAttribute getAttribute() { + return attribute; + } + + public void setAttribute(GluuAttribute attribute) { + this.attribute = attribute; + } + } + +} diff --git a/server/src/main/java/org/gluu/oxtrust/ldap/service/LdifService.java b/server/src/main/java/org/gluu/oxtrust/ldap/service/LdifService.java new file mode 100644 index 000000000..0cd21a95f --- /dev/null +++ b/server/src/main/java/org/gluu/oxtrust/ldap/service/LdifService.java @@ -0,0 +1,102 @@ +/* + * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text. + * + * Copyright (c) 2014, Gluu + */ + +package org.gluu.oxtrust.ldap.service; + +import java.io.InputStream; +import java.io.Serializable; + +import org.gluu.oxtrust.util.OxTrustConstants; +import org.gluu.site.ldap.OperationsFacade; +import org.gluu.site.ldap.persistence.LdapEntryManager; +import org.gluu.site.ldap.persistence.LdifDataUtility; +import org.jboss.seam.ScopeType; +import org.jboss.seam.annotations.AutoCreate; +import org.jboss.seam.annotations.In; +import org.jboss.seam.annotations.Logger; +import org.jboss.seam.annotations.Name; +import org.jboss.seam.annotations.Scope; +import org.jboss.seam.international.StatusMessage.Severity; +import org.jboss.seam.log.Log; + +import com.unboundid.ldap.sdk.LDAPConnection; +import com.unboundid.ldap.sdk.LDAPConnectionPool; +import com.unboundid.ldap.sdk.LDAPException; +import com.unboundid.ldap.sdk.ResultCode; +import com.unboundid.ldif.LDIFReader; + +/** + * Provides operations with persons + * + * @author Yuriy Movchan Date: 10.13.2010 + */ +@Scope(ScopeType.STATELESS) +@Name("ldifService") +@AutoCreate +public class LdifService implements Serializable{ + + @Logger + private Log log; + + @In + private LdapEntryManager ldapEntryManager; + + LdifDataUtility ldifDataUtility = LdifDataUtility.instance(); + + @In + private IGroupService groupService; + + private transient OperationsFacade ldapOperationService; + + public boolean destroy() { + boolean destroyResult = this.ldapOperationService.destroy(); + + return destroyResult; + } + + public ResultCode importLdifFileInLdap(InputStream is) throws LDAPException{ + ResultCode result = ResultCode.UNAVAILABLE; + + LDAPConnection connection = null; + try { + connection = ldapOperationService.getConnection(); + //ResultCode result = LdifDataUtility.instance().importLdifFileContent(connection, ldifFileContent); + + LDIFReader importLdifReader = new LDIFReader(is); + + result = ldifDataUtility.importLdifFile(connection,importLdifReader); + importLdifReader.close(); + + } catch(Exception e ) { + log.info("LDIFReader --- : " + e.getMessage()); + + }finally { + if (connection != null) { + ldapOperationService.releaseConnection(connection); + } + } + return result; + + } + + public ResultCode validateLdifFile(InputStream is) throws LDAPException{ + ResultCode result = ResultCode.UNAVAILABLE; + try { + LDIFReader validateLdifReader = new LDIFReader(is); + result = ldifDataUtility.validateLDIF(validateLdifReader); + log.info("LDIFReader successfully"); + validateLdifReader.close(); + + } catch(Exception e ) { + log.info("LDIFReader --- : "+e.getMessage()); + + } + return result; + + } + + +} diff --git a/server/src/main/webapp/WEB-INF/incl/attribute/attributeImportForm.xhtml b/server/src/main/webapp/WEB-INF/incl/attribute/attributeImportForm.xhtml new file mode 100644 index 000000000..9c79b8cf3 --- /dev/null +++ b/server/src/main/webapp/WEB-INF/incl/attribute/attributeImportForm.xhtml @@ -0,0 +1,73 @@ + + + + + + + + +
+
+
+
+ + + + File to import + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
+
+
+ +
\ No newline at end of file diff --git a/server/src/main/webapp/WEB-INF/incl/layout/leftmenu.xhtml b/server/src/main/webapp/WEB-INF/incl/layout/leftmenu.xhtml index 4ffeba2dd..e19b8691d 100644 --- a/server/src/main/webapp/WEB-INF/incl/layout/leftmenu.xhtml +++ b/server/src/main/webapp/WEB-INF/incl/layout/leftmenu.xhtml @@ -79,6 +79,13 @@ +