Skip to content

Commit

Permalink
oxTrust issue #392
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhar16 committed Feb 28, 2017
1 parent b104fbd commit d27dba6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,99 +11,60 @@
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
* Action class to load data from LDIF file
*
* @author Shekhar Laad Date: 02.14.2011
* @author Shekhar L Date: 02.28.2017
*/
@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";
/**
*
*/
private static final long serialVersionUID = 8755036208872218664L;

@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;
Expand Down Expand Up @@ -141,6 +102,7 @@ public String importAttributes() throws Exception {

public void validateFileToImport() throws Exception {
removeFileDataToImport();
String dn=attributeService.getDnForAttribute(null);

if (uploadedFile == null) {
return;
Expand All @@ -149,7 +111,7 @@ public void validateFileToImport() throws Exception {
if (uploadedFile != null) {
//Table table;
InputStream is = new ByteArrayInputStream(this.fileData);
ResultCode result = ldifService.importLdifFileInLdap( is);
ResultCode result = ldifService.validateLdifFile(is , dn);
if(!result.equals(ResultCode.SUCCESS)){
log.info("LDIFReader --- : ");
removeFileDataToImport();
Expand All @@ -163,7 +125,6 @@ public void validateFileToImport() throws Exception {
}
}

@Restrict("#{s:hasPermission('import', 'person')}")
public void cancel() {
destroy();
}
Expand All @@ -186,15 +147,13 @@ 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 {
Expand Down Expand Up @@ -254,34 +213,4 @@ 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;
}
}

}
38 changes: 16 additions & 22 deletions server/src/main/java/org/gluu/oxtrust/ldap/service/LdifService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
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;
Expand All @@ -19,7 +17,6 @@
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;
Expand All @@ -29,15 +26,20 @@
import com.unboundid.ldif.LDIFReader;

/**
* Provides operations with persons
* Provides operations with LDIF files
*
* @author Yuriy Movchan Date: 10.13.2010
* @author Shekhar L Date: 02.28.2017
*/
@Scope(ScopeType.STATELESS)
@Name("ldifService")
@AutoCreate
public class LdifService implements Serializable{


/**
*
*/
private static final long serialVersionUID = 6690460114767359078L;

@Logger
private Log log;

Expand All @@ -46,24 +48,14 @@ public class LdifService implements Serializable{

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;

LDAPConnectionPool ldapConnectionPool = ldapEntryManager.getLdapOperationService().getConnectionPool();
LDAPConnection connection = null;
try {
connection = ldapOperationService.getConnection();
//ResultCode result = LdifDataUtility.instance().importLdifFileContent(connection, ldifFileContent);
try {
connection = ldapConnectionPool.getConnection();

LDIFReader importLdifReader = new LDIFReader(is);

Expand All @@ -72,26 +64,28 @@ public ResultCode importLdifFileInLdap(InputStream is) throws LDAPException{

} catch(Exception e ) {
log.info("LDIFReader --- : " + e.getMessage());
e.printStackTrace();

}finally {
if (connection != null) {
ldapOperationService.releaseConnection(connection);
ldapConnectionPool.releaseConnection(connection);
}
}
return result;

}

public ResultCode validateLdifFile(InputStream is) throws LDAPException{
public ResultCode validateLdifFile(InputStream is, String dn) throws LDAPException{
ResultCode result = ResultCode.UNAVAILABLE;
try {
LDIFReader validateLdifReader = new LDIFReader(is);
result = ldifDataUtility.validateLDIF(validateLdifReader);
result = ldifDataUtility.validateLDIF(validateLdifReader,dn);
log.info("LDIFReader successfully");
validateLdifReader.close();

} catch(Exception e ) {
log.info("LDIFReader --- : "+e.getMessage());
e.printStackTrace();

}
return result;
Expand Down

0 comments on commit d27dba6

Please sign in to comment.