From 92ba44fcd83685337903c5db3964660f3d49a4f7 Mon Sep 17 00:00:00 2001 From: Manuel FLURY Date: Wed, 2 Dec 2026 17:31:19 +0100 Subject: [PATCH] DIRSTUDIO-1214: Follow the paged results cookie in the DSML export The DSML export only returned the first page of a paged search. When the response control carries a cookie, search again with that cookie, as the LDIF export already does. The cookie of the given search parameter is restored afterwards, so a second export does not resume in the middle of the entries. Pagination stops when no PagedResults request control can take the cookie, and a cancelled export writes no file instead of a truncated one. The test exports all entries as DSML request and as DSML response, without a paged results control and with page sizes smaller, equal and larger than the number of entries, checks that the cookie of the request control is left untouched, and checks that a cancelled export writes no file. --- .../core/jobs/ExportDsmlRunnable.java | 170 ++++++++++-- .../test/integration/core/ExportDsmlTest.java | 245 ++++++++++++++++++ 2 files changed, 396 insertions(+), 19 deletions(-) create mode 100644 tests/test.integration.core/src/main/java/org/apache/directory/studio/test/integration/core/ExportDsmlTest.java diff --git a/plugins/ldapbrowser.core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ExportDsmlRunnable.java b/plugins/ldapbrowser.core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ExportDsmlRunnable.java index 4381ff05f2..ad9c47b6bd 100644 --- a/plugins/ldapbrowser.core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ExportDsmlRunnable.java +++ b/plugins/ldapbrowser.core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ExportDsmlRunnable.java @@ -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; @@ -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; @@ -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 ) ) { @@ -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(); @@ -267,11 +270,33 @@ 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 null + * @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; @@ -279,15 +304,20 @@ public static void processAsDsmlResponse( StudioSearchResultEnumeration sre, Bat 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 ); } } } @@ -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(); @@ -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 null + * @param searchParameter the search parameter holding the PagedResults request control + * @param monitor the monitor + * @return the enumeration of the next page, or null 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 null + * @return the cookie, or null 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 null + * @param cookie the cookie to set + * @return true 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}. * @@ -423,6 +542,9 @@ 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; @@ -430,16 +552,21 @@ private String processAsDsmlRequest( StudioSearchResultEnumeration sre, StudioPr 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 ); } } } @@ -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(); diff --git a/tests/test.integration.core/src/main/java/org/apache/directory/studio/test/integration/core/ExportDsmlTest.java b/tests/test.integration.core/src/main/java/org/apache/directory/studio/test/integration/core/ExportDsmlTest.java new file mode 100644 index 0000000000..32341446cd --- /dev/null +++ b/tests/test.integration.core/src/main/java/org/apache/directory/studio/test/integration/core/ExportDsmlTest.java @@ -0,0 +1,245 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.directory.studio.test.integration.core; + + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.nio.file.Files; +import java.nio.file.Path; + +import org.apache.directory.api.ldap.model.entry.DefaultEntry; +import org.apache.directory.api.ldap.model.message.SearchScope; +import org.apache.directory.api.ldap.model.message.controls.PagedResults; +import org.apache.directory.api.ldap.model.message.controls.PagedResultsImpl; +import org.apache.directory.api.ldap.model.name.Dn; +import org.apache.directory.studio.common.core.jobs.StudioProgressMonitor; +import org.apache.directory.studio.connection.core.Connection; +import org.apache.directory.studio.connection.core.ConnectionParameter; +import org.apache.directory.studio.connection.core.ConnectionParameter.AuthenticationMethod; +import org.apache.directory.studio.connection.core.ConnectionParameter.EncryptionMethod; +import org.apache.directory.studio.connection.core.event.ConnectionEventRegistry; +import org.apache.directory.studio.ldapbrowser.core.jobs.ExportDsmlRunnable; +import org.apache.directory.studio.ldapbrowser.core.jobs.ExportDsmlRunnable.ExportDsmlJobType; +import org.apache.directory.studio.ldapbrowser.core.model.SearchParameter; +import org.apache.directory.studio.ldapbrowser.core.model.impl.BrowserConnection; +import org.apache.directory.studio.test.integration.junit5.LdapServersSource; +import org.apache.directory.studio.test.integration.junit5.SkipTestIfLdapServerIsNotAvailableInterceptor; +import org.apache.directory.studio.test.integration.junit5.TestFixture; +import org.apache.directory.studio.test.integration.junit5.TestLdapServer; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; + + +/** + * Tests the DSML export: a paged search must export all entries (DIRSTUDIO-1214) and leave the search + * parameter of the caller untouched, and a cancelled export must not write a truncated file. + */ +@ExtendWith(SkipTestIfLdapServerIsNotAvailableInterceptor.class) +public class ExportDsmlTest +{ + /** The number of entries below the base DN. */ + private static final int ENTRY_COUNT = 25; + + /** The base DN of the test entries. */ + private static final Dn BASE_DN = TestFixture.dn( "ou=dsml-paging", TestFixture.CONTEXT_DN ); + + /** The directory the DSML files are written to. */ + @TempDir + Path tempDir; + + + @BeforeAll + public static void suspendEvents() + { + ConnectionEventRegistry.suspendEventFiringInCurrentThread(); + } + + + @AfterAll + public static void resumeEvents() + { + ConnectionEventRegistry.resumeEventFiringInCurrentThread(); + } + + + /** + * Exports all entries as DSML request and as DSML response, without a paged results control and with page + * sizes smaller, equal and larger than the number of entries. + */ + @ParameterizedTest + @LdapServersSource + public void testPagedDsmlExportContainsAllEntries( TestLdapServer ldapServer ) throws Exception + { + TestFixture.createContextEntry( ldapServer ); + deleteEntries( ldapServer ); + + ConnectionParameter parameter = connectionParameter( ldapServer ); + BrowserConnection browserConnection = new BrowserConnection( new Connection( parameter ) ); + + try + { + createEntries( ldapServer ); + + for ( ExportDsmlJobType type : ExportDsmlJobType.values() ) + { + for ( int pageSize : new int[] + { 0, 4, ENTRY_COUNT, 100 } ) + { + Path file = tempDir.resolve( "export-" + type + "-" + pageSize + ".dsml" ); + + SearchParameter search = new SearchParameter(); + search.setSearchBase( BASE_DN ); + search.setFilter( "(objectClass=*)" ); + search.setScope( SearchScope.ONELEVEL ); + + PagedResults control = null; + if ( pageSize > 0 ) + { + control = new PagedResultsImpl(); + control.setSize( pageSize ); + search.getControls().add( control ); + } + byte[] cookie = control != null ? control.getCookie() : null; + + StudioProgressMonitor monitor = new StudioProgressMonitor( new NullProgressMonitor() ); + new ExportDsmlRunnable( file.toString(), browserConnection, search, type ).run( monitor ); + assertNull( monitor.getException(), type + ", page size " + pageSize ); + assertFalse( monitor.errorsReported(), type + ", page size " + pageSize ); + + String dsml = Files.readString( file ); + String element = type == ExportDsmlJobType.RESPONSE ? " { + connection.add( new DefaultEntry( BASE_DN, "objectClass", "top", "objectClass", "organizationalUnit", + "ou", "dsml-paging" ) ); + for ( int i = 1; i <= ENTRY_COUNT; i++ ) + { + connection.add( new DefaultEntry( TestFixture.dn( "ou=e" + i, BASE_DN ), "objectClass", "top", + "objectClass", "organizationalUnit", "ou", "e" + i ) ); + } + } ); + } + + + private static void deleteEntries( TestLdapServer ldapServer ) + { + ldapServer.withAdminConnection( connection -> { + if ( connection.exists( BASE_DN ) ) + { + for ( int i = 1; i <= ENTRY_COUNT; i++ ) + { + Dn child = TestFixture.dn( "ou=e" + i, BASE_DN ); + if ( connection.exists( child ) ) + { + connection.delete( child ); + } + } + connection.delete( BASE_DN ); + } + assertFalse( connection.exists( BASE_DN ) ); + } ); + } + + + private static int count( String text, String needle ) + { + int count = 0; + for ( int i = text.indexOf( needle ); i >= 0; i = text.indexOf( needle, i + needle.length() ) ) + { + count++; + } + return count; + } +}