Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.directory.api.dsmlv2.DsmlDecorator;
import org.apache.directory.api.dsmlv2.request.AddRequestDsml;
import org.apache.directory.api.dsmlv2.request.BatchRequestDsml;
Expand All @@ -44,12 +45,14 @@
import org.apache.directory.api.ldap.model.entry.Value;
import org.apache.directory.api.ldap.model.exception.LdapException;
import org.apache.directory.api.ldap.model.exception.LdapURLEncodingException;
import org.apache.directory.api.ldap.model.message.Control;
import org.apache.directory.api.ldap.model.message.LdapResult;
import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
import org.apache.directory.api.ldap.model.message.Response;
import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
import org.apache.directory.api.ldap.model.message.SearchResultDone;
import org.apache.directory.api.ldap.model.message.SearchResultDoneImpl;
import org.apache.directory.api.ldap.model.message.controls.PagedResults;
import org.apache.directory.api.ldap.model.url.LdapUrl;
import org.apache.directory.studio.common.core.jobs.StudioProgressMonitor;
import org.apache.directory.studio.connection.core.Connection;
Expand Down Expand Up @@ -210,8 +213,8 @@ public void run( StudioProgressMonitor monitor )

monitor.worked( 1 );

// Writing the DSML string to the final destination file.
if ( dsmlExportString != null )
// Writing the DSML string to the final destination file, but not a truncated one after a cancellation
if ( dsmlExportString != null && !monitor.isCanceled() )
{
try ( FileOutputStream fos = new FileOutputStream( exportDsmlFilename ) )
{
Expand Down Expand Up @@ -246,7 +249,7 @@ private String processAsDsmlResponse( StudioSearchResultEnumeration sre, StudioP
// Creating the batch reponse
BatchResponseDsml batchResponse = new BatchResponseDsml();

processAsDsmlResponse( sre, batchResponse, monitor, searchParameter );
processAsDsmlResponse( sre, batchResponse, monitor, searchParameter, browserConnection );

// Returning the associated DSML
return batchResponse.toDsml();
Expand All @@ -267,27 +270,54 @@ private String processAsDsmlResponse( StudioSearchResultEnumeration sre, StudioP
*/
public static void processAsDsmlResponse( StudioSearchResultEnumeration sre, BatchResponseDsml batchResponse,
StudioProgressMonitor monitor, SearchParameter searchParameter ) throws LdapException
{
processAsDsmlResponse( sre, batchResponse, monitor, searchParameter, null );
}


/**
* Processes the {@link StudioSearchResultEnumeration} as a DSML response. If a browser connection
* is given, all the pages of a paged search are processed.
*
* @param sre the search result enumeration
* @param batchResponse the batch response
* @param monitor the monitor
* @param searchParameter the search parameter
* @param browserConnection the browser connection used to fetch the next pages, may be <code>null</code>
* @throws LdapException
*/
private static void processAsDsmlResponse( StudioSearchResultEnumeration sre, BatchResponseDsml batchResponse,
StudioProgressMonitor monitor, SearchParameter searchParameter, IBrowserConnection browserConnection )
throws LdapException
{
// Creating and adding the search response
SearchResponseDsml sr = new SearchResponseDsml( codec );
batchResponse.addResponse( sr );

// Remembering the cookie, the pagination below must leave the given search parameter untouched
byte[] cookie = getPagedResultsCookie( searchParameter );

try
{
int count = 0;

if ( !monitor.errorsReported() )
{
// Creating and adding a search result entry or reference for each result
while ( sre.hasMore() )
while ( sre != null && !monitor.isCanceled() && !monitor.errorsReported() )
{
Entry entry = sre.next().getEntry();
sr.addResponse( convertSearchResultToDsml( entry ) );
while ( sre.hasMore() )
{
Entry entry = sre.next().getEntry();
sr.addResponse( convertSearchResultToDsml( entry ) );

count++;
monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__export_progress,
new String[]
{ Integer.toString( count ) } ) );
count++;
monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__export_progress,
new String[]
{ Integer.toString( count ) } ) );
}

sre = nextPage( sre, browserConnection, searchParameter, monitor );
}
}
}
Expand All @@ -303,6 +333,11 @@ public static void processAsDsmlResponse( StudioSearchResultEnumeration sre, Bat
monitor.reportError( e );
}
}
finally
{
// restoring, the search parameter of the caller must stay untouched
applyPagedResultsCookie( searchParameter, cookie );
}

// Creating and adding a search result done at the end of the results
SearchResultDone srd = new SearchResultDoneImpl();
Expand All @@ -329,6 +364,90 @@ public static void processAsDsmlResponse( StudioSearchResultEnumeration sre, Bat
}


/**
* Fetches the next page of a paged search.
*
* @param sre the exhausted search result enumeration
* @param browserConnection the browser connection, may be <code>null</code>
* @param searchParameter the search parameter holding the PagedResults request control
* @param monitor the monitor
* @return the enumeration of the next page, or <code>null</code> if there is no further page
*/
private static StudioSearchResultEnumeration nextPage( StudioSearchResultEnumeration sre,
IBrowserConnection browserConnection, SearchParameter searchParameter, StudioProgressMonitor monitor )
{
if ( browserConnection == null || sre == null )
{
return null;
}

for ( Control responseControl : sre.getResponseControls() )
{
if ( responseControl instanceof PagedResults )
{
byte[] cookie = ( ( PagedResults ) responseControl ).getCookie();
// without a PagedResults request control the cookie cannot be passed, stop instead of searching again
if ( ArrayUtils.isNotEmpty( cookie ) && applyPagedResultsCookie( searchParameter, cookie ) )
{
return SearchRunnable.search( browserConnection, searchParameter, monitor );
}
}
}

return null;
}


/**
* Gets the cookie of the PagedResults request control.
*
* @param searchParameter the search parameter, may be <code>null</code>
* @return the cookie, or <code>null</code> if there is no PagedResults request control
*/
private static byte[] getPagedResultsCookie( SearchParameter searchParameter )
{
if ( searchParameter != null )
{
for ( Control control : searchParameter.getControls() )
{
if ( control instanceof PagedResults )
{
return ( ( PagedResults ) control ).getCookie();
}
}
}

return null;
}


/**
* Sets the cookie of the PagedResults request controls.
*
* @param searchParameter the search parameter, may be <code>null</code>
* @param cookie the cookie to set
* @return <code>true</code> if at least one PagedResults request control was found
*/
private static boolean applyPagedResultsCookie( SearchParameter searchParameter, byte[] cookie )
{
boolean found = false;

if ( searchParameter != null )
{
for ( Control control : searchParameter.getControls() )
{
if ( control instanceof PagedResults )
{
( ( PagedResults ) control ).setCookie( cookie );
found = true;
}
}
}

return found;
}


/**
* Converts the given {@link SearchResult} to a {@link SearchResultEntryDsml}.
*
Expand Down Expand Up @@ -423,23 +542,31 @@ private String processAsDsmlRequest( StudioSearchResultEnumeration sre, StudioPr
// Creating the batch request
BatchRequestDsml batchRequest = new BatchRequestDsml();

// Remembering the cookie, the pagination below must leave the given search parameter untouched
byte[] cookie = getPagedResultsCookie( searchParameter );

try
{
int count = 0;

if ( !monitor.errorsReported() )
{
// Creating and adding an add request for each result
while ( sre.hasMore() )
while ( sre != null && !monitor.isCanceled() && !monitor.errorsReported() )
{
Entry entry = sre.next().getEntry();
AddRequestDsml arDsml = convertToAddRequestDsml( entry );
batchRequest.addRequest( arDsml );

count++;
monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__export_progress,
new String[]
{ Integer.toString( count ) } ) );
while ( sre.hasMore() )
{
Entry entry = sre.next().getEntry();
AddRequestDsml arDsml = convertToAddRequestDsml( entry );
batchRequest.addRequest( arDsml );

count++;
monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__export_progress,
new String[]
{ Integer.toString( count ) } ) );
}

sre = nextPage( sre, browserConnection, searchParameter, monitor );
}
}
}
Expand All @@ -455,6 +582,11 @@ private String processAsDsmlRequest( StudioSearchResultEnumeration sre, StudioPr
monitor.reportError( e );
}
}
finally
{
// restoring, the search parameter of the caller must stay untouched
applyPagedResultsCookie( searchParameter, cookie );
}

// Returning the associated DSML
return batchRequest.toDsml();
Expand Down
Loading