From 0c4a9b11910e8046982710dad1c76ca64fad0c0d Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Thu, 22 Feb 2024 22:48:28 +0100 Subject: [PATCH] #3238 - Content Sync make timeouts configurable (#3254) --- CHANGELOG.md | 2 ++ .../contentsync/ConfigurationUtils.java | 6 ++++++ .../commons/contentsync/RemoteInstance.java | 20 ++++++++++++------- .../commons/contentsync/TestContentSync.java | 9 +++++++-- .../components/utilities/contentsync/POST.jsp | 3 +-- .../contentsync/configure/.content.xml | 19 ++++++++++++++++++ 6 files changed, 48 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1932de79f3..78ad64500c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com) - ## Unreleased ([details][unreleased changes details]) + +#3238 - Content Sync make timeouts configurable #3235 - Add an option to ignore selectors in the url. ## 6.3.8 - 2024-02-02 diff --git a/bundle/src/main/java/com/adobe/acs/commons/contentsync/ConfigurationUtils.java b/bundle/src/main/java/com/adobe/acs/commons/contentsync/ConfigurationUtils.java index 736d7fb9b6..c8dfd5d10f 100644 --- a/bundle/src/main/java/com/adobe/acs/commons/contentsync/ConfigurationUtils.java +++ b/bundle/src/main/java/com/adobe/acs/commons/contentsync/ConfigurationUtils.java @@ -28,6 +28,8 @@ import java.util.HashMap; import java.util.Map; +import static com.adobe.acs.commons.contentsync.RemoteInstance.CONNECT_TIMEOUT; +import static com.adobe.acs.commons.contentsync.RemoteInstance.SOCKET_TIMEOUT; import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE; import static org.apache.jackrabbit.JcrConstants.NT_UNSTRUCTURED; import static org.apache.sling.jcr.resource.api.JcrResourceConstants.NT_SLING_FOLDER; @@ -39,6 +41,8 @@ public class ConfigurationUtils { public static final String UPDATE_STRATEGY_KEY = "update-strategy"; public static final String EVENT_USER_DATA_KEY = "event-user-data"; + public static final String SO_TIMEOUT_STRATEGY_KEY = "soTimeout"; + public static final String CONNECT_TIMEOUT_KEY = "connTimeout"; private ConfigurationUtils(){ @@ -49,6 +53,8 @@ public static Resource getSettingsResource(ResourceResolver resourceResolver) th resourceProperties.put(JCR_PRIMARYTYPE, NT_UNSTRUCTURED); resourceProperties.put(UPDATE_STRATEGY_KEY, LastModifiedStrategy.class.getName()); resourceProperties.put(EVENT_USER_DATA_KEY, "changedByPageManagerCopy"); + resourceProperties.put(SO_TIMEOUT_STRATEGY_KEY, SOCKET_TIMEOUT); + resourceProperties.put(CONNECT_TIMEOUT_KEY, CONNECT_TIMEOUT); return ResourceUtil.getOrCreateResource(resourceResolver, SETTINGS_PATH, resourceProperties, NT_SLING_FOLDER, true); } diff --git a/bundle/src/main/java/com/adobe/acs/commons/contentsync/RemoteInstance.java b/bundle/src/main/java/com/adobe/acs/commons/contentsync/RemoteInstance.java index fd3af33f17..a053169e57 100644 --- a/bundle/src/main/java/com/adobe/acs/commons/contentsync/RemoteInstance.java +++ b/bundle/src/main/java/com/adobe/acs/commons/contentsync/RemoteInstance.java @@ -31,6 +31,7 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; +import org.apache.sling.api.resource.ValueMap; import javax.json.Json; import javax.json.JsonObject; @@ -45,32 +46,37 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; + +import static com.adobe.acs.commons.contentsync.ConfigurationUtils.CONNECT_TIMEOUT_KEY; +import static com.adobe.acs.commons.contentsync.ConfigurationUtils.SO_TIMEOUT_STRATEGY_KEY; import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE; /** * HTTP connection to a remote AEM instance + some sugar methods to fetch data */ public class RemoteInstance implements Closeable { - private static final int CONNECT_TIMEOUT = 5000; - private static final int SOCKET_TIMEOUT = 60000; + static final int CONNECT_TIMEOUT = 5000; + static final int SOCKET_TIMEOUT = 300000; private final CloseableHttpClient httpClient; private final SyncHostConfiguration hostConfiguration; - public RemoteInstance(SyncHostConfiguration hostConfiguration) { + public RemoteInstance(SyncHostConfiguration hostConfiguration, ValueMap generalSettings) { this.hostConfiguration = hostConfiguration; - this.httpClient = createHttpClient(); + this.httpClient = createHttpClient(hostConfiguration, generalSettings); } - private CloseableHttpClient createHttpClient() { + private CloseableHttpClient createHttpClient(SyncHostConfiguration hostConfiguration, ValueMap generalSettings) { BasicCredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(hostConfiguration.getUsername(), hostConfiguration.getPassword())); + int soTimeout = generalSettings.get(SO_TIMEOUT_STRATEGY_KEY, SOCKET_TIMEOUT); + int connTimeout = generalSettings.get(CONNECT_TIMEOUT_KEY, CONNECT_TIMEOUT); RequestConfig requestConfig = RequestConfig .custom() - .setConnectTimeout(CONNECT_TIMEOUT) - .setSocketTimeout(SOCKET_TIMEOUT) + .setConnectTimeout(connTimeout) + .setSocketTimeout(soTimeout) .setCookieSpec(CookieSpecs.STANDARD).build(); return HttpClients.custom() diff --git a/bundle/src/test/java/com/adobe/acs/commons/contentsync/TestContentSync.java b/bundle/src/test/java/com/adobe/acs/commons/contentsync/TestContentSync.java index 83bccbd9ac..a267133b14 100644 --- a/bundle/src/test/java/com/adobe/acs/commons/contentsync/TestContentSync.java +++ b/bundle/src/test/java/com/adobe/acs/commons/contentsync/TestContentSync.java @@ -25,6 +25,7 @@ import io.wcm.testing.mock.aem.junit.AemContext; import org.apache.commons.io.IOUtils; import org.apache.sling.api.resource.ModifiableValueMap; +import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ValueMap; import org.apache.sling.jcr.contentloader.ContentImporter; import org.apache.sling.jcr.contentloader.internal.ContentReaderWhiteboard; @@ -51,7 +52,10 @@ import java.util.Arrays; import java.util.List; +import static com.adobe.acs.commons.contentsync.ConfigurationUtils.CONNECT_TIMEOUT_KEY; import static com.adobe.acs.commons.contentsync.ConfigurationUtils.HOSTS_PATH; +import static com.adobe.acs.commons.contentsync.ConfigurationUtils.SETTINGS_PATH; +import static com.adobe.acs.commons.contentsync.ConfigurationUtils.SO_TIMEOUT_STRATEGY_KEY; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -59,7 +63,6 @@ import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -87,11 +90,13 @@ public void setUp() throws Exception { String configPath = HOSTS_PATH + "/host1"; context.build().resource(configPath, "host", "http://localhost:4502", "username", "", "password", ""); + context.build().resource(SETTINGS_PATH, SO_TIMEOUT_STRATEGY_KEY, 1000, CONNECT_TIMEOUT_KEY, "1000"); + ValueMap generalSettings = context.resourceResolver().getResource(configPath).getValueMap(); SyncHostConfiguration hostConfiguration = context.getService(ModelFactory.class) .createModel(context.resourceResolver().getResource(configPath), SyncHostConfiguration.class); ContentImporter contentImporter = context.registerInjectActivateService(new DefaultContentImporter()); - remoteInstance = spy(new RemoteInstance(hostConfiguration)); + remoteInstance = spy(new RemoteInstance(hostConfiguration, generalSettings)); contentSync = new ContentSync(remoteInstance, context.resourceResolver(), contentImporter); diff --git a/ui.apps/src/main/content/jcr_root/apps/acs-commons/components/utilities/contentsync/POST.jsp b/ui.apps/src/main/content/jcr_root/apps/acs-commons/components/utilities/contentsync/POST.jsp index 11c893a26f..e74639522b 100755 --- a/ui.apps/src/main/content/jcr_root/apps/acs-commons/components/utilities/contentsync/POST.jsp +++ b/ui.apps/src/main/content/jcr_root/apps/acs-commons/components/utilities/contentsync/POST.jsp @@ -71,7 +71,6 @@ String observationData = generalSettings.get(ConfigurationUtils.EVENT_USER_DATA_KEY, String.class); String strategyPid = generalSettings.get(ConfigurationUtils.UPDATE_STRATEGY_KEY, String.class); - Resource cfg = resourceResolver.getResource(cfgPath); SyncHostConfiguration hostConfig = cfg.adaptTo(SyncHostConfiguration.class); @@ -85,7 +84,7 @@ ContentReader contentReader = new ContentReader(session); UpdateStrategy updateStrategy = sling.getServices(UpdateStrategy.class, "(component.name=" + strategyPid + ")")[0]; - try(RemoteInstance remoteInstance = new RemoteInstance(hostConfig)){ + try(RemoteInstance remoteInstance = new RemoteInstance(hostConfig, generalSettings)){ ContentImporter importer = sling.getService(ContentImporter.class); ContentSync contentSync = new ContentSync(remoteInstance, resourceResolver, importer); ContentCatalog contentCatalog = new ContentCatalog(remoteInstance, catalogServlet); diff --git a/ui.apps/src/main/content/jcr_root/apps/acs-commons/content/contentsync/configure/.content.xml b/ui.apps/src/main/content/jcr_root/apps/acs-commons/content/contentsync/configure/.content.xml index e1f3aa870d..4e6e5e6ae4 100755 --- a/ui.apps/src/main/content/jcr_root/apps/acs-commons/content/contentsync/configure/.content.xml +++ b/ui.apps/src/main/content/jcr_root/apps/acs-commons/content/contentsync/configure/.content.xml @@ -239,6 +239,25 @@ sling:resourceType="granite/ui/components/coral/foundation/form/hidden" name="./jcr:primaryType" value="nt:unstructured"/> +
+ + + + +