Skip to content

Commit

Permalink
oxTrust issue #392 : changes to export ldif
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhar16 committed Mar 14, 2017
1 parent 1b22ebb commit cb9fa09
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@
import static org.jboss.seam.ScopeType.CONVERSATION;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.gluu.oxtrust.ldap.service.AttributeService;
import org.gluu.oxtrust.ldap.service.LdifService;
import org.gluu.oxtrust.util.OxTrustConstants;
import org.gluu.site.ldap.persistence.exception.LdapMappingException;
import org.jboss.seam.annotations.In;
Expand All @@ -24,6 +33,9 @@
import org.xdi.model.GluuAttribute;
import org.xdi.model.GluuUserRole;

import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.SearchResultEntry;

/**
* Action class for displaying attributes
*
Expand All @@ -45,8 +57,29 @@ public class AttributeInventoryAction implements Serializable {

@In
private AttributeService attributeService;

@In(value = "#{facesContext.externalContext}")
private ExternalContext extCtx;

@In(value = "#{facesContext}")
FacesContext facesContext;

private List<GluuAttribute> activeAttributeList;

@In
private LdifService ldifService;


private Map<String, Boolean> checked = new HashMap<String, Boolean>();


public Map<String, Boolean> getChecked() {
return checked;
}

public void setChecked(Map<String, Boolean> checked) {
this.checked = checked;
}

@Restrict("#{s:hasPermission('attribute', 'access')}")
public String start() {
Expand Down Expand Up @@ -111,5 +144,30 @@ public List<GluuAttribute> getActiveAttributeList() {
public void setActiveAttributeList(List<GluuAttribute> activeAttributeList) {
this.activeAttributeList = activeAttributeList;
}



public void submit() {
List<String> checkedItems = new ArrayList<String>();

for (GluuAttribute item : activeAttributeList) {
if (checked.get(item.getInum())) {
checkedItems.add(item.getInum());
}
}
log.info("the selections are : {0}", checkedItems.size());
HttpServletResponse response = (HttpServletResponse) extCtx.getResponse();
response.setContentType("text/plain");
response.addHeader("Content-disposition", "attachment; filename=\"attributes.ldif\"");
try {
ServletOutputStream os = response.getOutputStream();
ldifService.exportLDIFFile(checkedItems,os);
os.flush();
os.close();
facesContext.responseComplete();
} catch (Exception e) {
log.error("\nFailure : " + e.toString() + "\n");
}
checked.clear();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,34 @@

package org.gluu.oxtrust.ldap.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;

import org.gluu.site.ldap.persistence.LdapEntryManager;
import org.gluu.site.ldap.persistence.LdifDataUtility;
import org.gluu.site.ldap.persistence.exception.LdapMappingException;
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.log.Log;
import org.xdi.model.GluuAttribute;

import com.unboundid.ldap.sdk.Entry;
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.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.unboundid.ldif.LDIFReader;
import com.unboundid.ldif.LDIFRecord;
import com.unboundid.ldif.LDIFWriter;

/**
* Provides operations with LDIF files
Expand All @@ -43,6 +53,9 @@ public class LdifService implements Serializable {

@In
private LdapEntryManager ldapEntryManager;

@In
private AttributeService attributeService;

public ResultCode importLdifFileInLdap(InputStream is) throws LDAPException {
ResultCode result = ResultCode.UNAVAILABLE;
Expand Down Expand Up @@ -79,5 +92,34 @@ public ResultCode validateLdifFile(InputStream is, String dn) throws LDAPExcepti
return result;

}

public void exportLDIFFile(List<String> checkedItems, OutputStream output)
throws LDAPException {
List<SearchResultEntry> result = null;
LDAPConnection connection = ldapEntryManager.getLdapOperationService().getConnection();
try {
LdifDataUtility ldifDataUtility = LdifDataUtility.instance();
result = ldifDataUtility.getAttributeResultEntryLDIF(connection,checkedItems, attributeService.getDnForAttribute(null));
} catch (Exception ex) {
log.error("Failed to export ldif file: ", ex);
} finally {
ldapEntryManager.getLdapOperationService().releaseConnection(connection);
}

if (result != null && result.size() > 0) {
// Write all of the matching entries to LDIF.
LDIFWriter ldifWriter;
try {
ldifWriter = new LDIFWriter(output);
for (SearchResultEntry entry : result) {
ldifWriter.writeEntry(entry);
}

ldifWriter.close();
} catch (IOException e) {
throw new LdapMappingException("Error writing to file, try again", e);
}
}
}

}
28 changes: 28 additions & 0 deletions server/src/main/webapp/attribute/attributeExport.page.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<page xmlns="http://jboss.org/schema/seam/pages"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/schema/seam/pages http://jboss.org/schema/seam/pages-2.3.xsd"
no-conversation-view-id="/home.xhtml" login-required="true"
view-id="/attribute/attributeExport.xhtml">

<restrict>#{s:hasPermission('attribute', 'access')}</restrict>

<action execute="#{attributeInventoryAction.start}" if="#{attributeInventoryAction.attributeList == null}" on-postback="false" />

<rewrite pattern="/attribute/export" />

<navigation from-action="#{attributeInventoryAction.start}">
<rule if-outcome="success">
<begin-conversation join="true" flush-mode="manual" />
<render view-id="/attribute/attributeExport.xhtml" />
</rule>

<rule if-outcome="failure">
<end-conversation />
<redirect view-id="/home.xhtml">
<message severity="ERROR">Failed to load attributes</message>
</redirect>
</rule>
</navigation>

</page>
141 changes: 141 additions & 0 deletions server/src/main/webapp/attribute/attributeExport.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.org/schema/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:richext="http://java.sun.com/jsf/composite/richext"
template="/WEB-INF/incl/layout/newtemplate.xhtml">

<ui:define name="body">

<section class="content-header">

<h1 style="color: #333;">
Import/Export Attributes<small><i class="fa fa-caret-right"
style="color: #333 !important;"></i> Export Attribute</small>
</h1>
<ol class="breadcrumb">
<li><a href="#{request.contextPath}/home"><i class="fa fa-home"></i></a></li>
<li class="organization/configuration"><a href="#{request.contextPath}/attribute/export">
Export Attribute</a></li>
</ol>
</section>
<section class= "content">
<div id="formArea">
<h:panelGroup columns="1" border="0" cellpadding="0" cellspacing="0"
width="100%">
<h:form id="attributesFormId">

<s:link view="/attribute/attributeImport.xhtml"
styleClass="label label-success" value="Import Attribute LDIF" propagation="none" style="margin-left:5px;padding:6px;"/>

<div class="row" style="padding-top:20px">
<div class="col-xs-12">
<div class="nav-tabs-custom" style="margin-bottom: 0px;">
<ul class="nav nav-tabs">

<li class="active"><a href="#attribuiteInventory"
data-toggle="tab">Export Attribute </a></li>
</ul>
<div class="tab-content">
<!-- System Configuration -->
<div class="tab-pane active" id="attributeInventory">

<div style="display: inline-block; width: 75% !important;">
<h:panelGroup>

<div class="row">
<div id="update" runat="server" visible="false">
<!-- general form elements -->
<div class="box box-primary" style="width:132%;">

<!-- /.box-header -->
<!-- form start -->
<div class="box-body">
<div class="table-responsive" style="overflow: auto">



<s:fragment
rendered="#{attributeInventoryAction.attributeList.size eq 0}">
<h:outputText value="No Attributes Found" />
</s:fragment>
<h:dataTable sortMode="single"
styleClass="table table-hover rt"
value="#{attributeInventoryAction.attributeList}"
var="attr" id="attributesListId" width="100%"
rendered="#{attributeInventoryAction.attributeList.size gt 0}">
<h:column sortBy="#{attr.displayName}"
class="col-xs-12">
<f:facet name="header" >
<div style="text-align: center;">Export</div>
</f:facet>
<h:selectBooleanCheckbox value="#{attributeInventoryAction.checked[attr.inum]}" />
</h:column>

<h:column sortBy="#{attr.displayName}"
class="col-xs-12">
<f:facet name="header" >
<div style="text-align: center;"> Display Name</div>
</f:facet>
<s:link view="/attribute/updateAttribute.xhtml"
value="#{attr.displayName}" propagation="none">
<f:param name="inum" value="#{attr.inum}" />
</s:link>
</h:column>

<h:column sortBy="#{attr.name}" >
<f:facet name="header">
<div style="text-align: center;"> Name</div>
</f:facet>
<h:outputText value="#{attr.name}" class="col-xs-12" />
</h:column>

<h:column sortBy="#{attr.origin}" class="col-xs-12">
<f:facet name="header">
<div style="text-align: center;"> Origin</div>
</f:facet>

<h:outputText value="#{attr.origin}"
class="col-xs-12" />
</h:column>

<h:column>
<f:facet name="header">
<div style="text-align: center;"> Description </div>
</f:facet>
<h:outputText class="col-xs-12"
value="#{attr.description}" />
</h:column>

<h:column>
<f:facet name="header">
<div style="text-align: center;"> Status</div>
</f:facet>
<h:outputText class="col-xs-1" value="#{attr.status}" />
</h:column>
</h:dataTable>
</div>
</div>
</div>
</div>
</div>
</h:panelGroup>
</div>
</div>
</div>
</div>
</div>
</div>
<h:commandButton styleClass="btn btn-primary" value="Export" action="#{attributeInventoryAction.submit}"/>
</h:form>

</h:panelGroup>
</div>
</section>
</ui:define>
</ui:composition>

0 comments on commit cb9fa09

Please sign in to comment.