You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by du...@apache.org on 2022/11/28 13:56:38 UTC
[sling-org-apache-sling-testing-clients] branch master updated: SLING-11679: Set more descriptive User-Agent for SlingClients (#41)
This is an automated email from the ASF dual-hosted git repository.
dulvac pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-clients.git
The following commit(s) were added to refs/heads/master by this push:
new 3d6f431 SLING-11679: Set more descriptive User-Agent for SlingClients (#41)
3d6f431 is described below
commit 3d6f431ab8c59829630a33a475349a3c13974f5e
Author: Luca <ro...@gituser.de>
AuthorDate: Mon Nov 28 14:56:34 2022 +0100
SLING-11679: Set more descriptive User-Agent for SlingClients (#41)
* add support for custom user-agents
* add test for UserAgentInterceptor
* bump package versions & licensing
* minor test refactoring
* remove codesmells
* improve it tests
* extract duplicate code into methods
* simplify data model of the user-agent
* simplify user-agent data structure
* fix whitespace user-agent
* add system-property for default user-agent
* remove unused imports
* remove whitespace & renaming
* minor coding style adjustment
Co-authored-by: Andrei Dulvac <an...@gmail.com>
Co-authored-by: Andrei Dulvac <an...@gmail.com>
---
.../apache/sling/testing/clients/SlingClient.java | 12 +-
.../testing/clients/SystemPropertiesConfig.java | 49 +++++++++
.../clients/interceptors/UserAgentHolder.java | 56 ++++++++++
.../clients/interceptors/UserAgentInterceptor.java | 48 ++++++++
.../testing/clients/interceptors/package-info.java | 2 +-
.../apache/sling/testing/clients/package-info.java | 2 +-
.../sling/testing/clients/util/UserAgentUtil.java | 53 +++++++++
.../sling/testing/clients/util/package-info.java | 2 +-
.../testing/CustomUserAgentInterceptorTest.java | 122 +++++++++++++++++++++
.../sling/testing/util/UserAgentUtilTest.java | 47 ++++++++
10 files changed, 382 insertions(+), 11 deletions(-)
diff --git a/src/main/java/org/apache/sling/testing/clients/SlingClient.java b/src/main/java/org/apache/sling/testing/clients/SlingClient.java
index 5287f09..12bd96b 100644
--- a/src/main/java/org/apache/sling/testing/clients/SlingClient.java
+++ b/src/main/java/org/apache/sling/testing/clients/SlingClient.java
@@ -47,13 +47,8 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.sling.testing.clients.exceptions.TestingValidationException;
-import org.apache.sling.testing.clients.interceptors.DelayRequestInterceptor;
-import org.apache.sling.testing.clients.interceptors.HttpRequestResponseInterceptor;
-import org.apache.sling.testing.clients.interceptors.TestDescriptionInterceptor;
-import org.apache.sling.testing.clients.util.FormEntityBuilder;
-import org.apache.sling.testing.clients.util.HttpUtils;
-import org.apache.sling.testing.clients.util.JsonUtils;
-import org.apache.sling.testing.clients.util.ServerErrorRetryStrategy;
+import org.apache.sling.testing.clients.interceptors.*;
+import org.apache.sling.testing.clients.util.*;
import org.apache.sling.testing.clients.util.poller.AbstractPoller;
import org.apache.sling.testing.clients.util.poller.Polling;
import org.apache.sling.testing.timeouts.TimeoutsProvider;
@@ -745,12 +740,13 @@ public class SlingClient extends AbstractSlingClient {
*/
private InternalBuilder setDefaults() {
httpClientBuilder.useSystemProperties();
- httpClientBuilder.setUserAgent("Java");
+ httpClientBuilder.setUserAgent(SystemPropertiesConfig.getDefaultUserAgent());
// Connection
httpClientBuilder.setMaxConnPerRoute(10);
httpClientBuilder.setMaxConnTotal(100);
// Interceptors
httpClientBuilder.addInterceptorLast(new TestDescriptionInterceptor());
+ httpClientBuilder.addInterceptorLast(new UserAgentInterceptor());
httpClientBuilder.addInterceptorLast(new DelayRequestInterceptor(SystemPropertiesConfig.getHttpDelay()));
// HTTP request strategy
diff --git a/src/main/java/org/apache/sling/testing/clients/SystemPropertiesConfig.java b/src/main/java/org/apache/sling/testing/clients/SystemPropertiesConfig.java
index e9e9862..49ab468 100644
--- a/src/main/java/org/apache/sling/testing/clients/SystemPropertiesConfig.java
+++ b/src/main/java/org/apache/sling/testing/clients/SystemPropertiesConfig.java
@@ -16,6 +16,8 @@
*/
package org.apache.sling.testing.clients;
+import org.apache.sling.testing.clients.util.UserAgentUtil;
+
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -59,6 +61,18 @@ public class SystemPropertiesConfig {
*/
public static final String HTTP_RETRIES_ERROR_CODES_PROP = "http.retriesErrorCodes";
+ /**
+ * System property for {@link SystemPropertiesConfig#getClientUserAgentName()}
+ * Prefixed by {@link SystemPropertiesConfig#CONFIG_PROP_PREFIX}
+ */
+ public static final String CLIENT_USERAGENT_NAME = "client.useragent.name";
+
+ /**
+ * System property for {@link SystemPropertiesConfig#isClientUserAgentUsingVersion()}
+ * Prefixed by {@link SystemPropertiesConfig#CONFIG_PROP_PREFIX}
+ */
+ public static final String CLIENT_USERAGENT_USEVERSION = "client.useragent.useversion";
+
public static String getPrefixedPropertyName(String prop) {
return SystemPropertiesConfig.CONFIG_PROP_PREFIX + prop;
}
@@ -132,4 +146,39 @@ public class SystemPropertiesConfig {
}
}
+ /**
+ * Returns the default user-agent name of the {@link SlingClient}
+ * @return default name
+ */
+ public static String getClientUserAgentName() {
+ String defaultName = "Java"; // for backwards compatibility this should stay "Java"
+
+ try {
+ return System.getProperty(getPrefixedPropertyName(CLIENT_USERAGENT_NAME), defaultName);
+ } catch (Exception e) {
+ return defaultName;
+ }
+ }
+
+ /**
+ * Whether the user-agent of the {@link SlingClient} should be appended by the current library version
+ * @return true if version should be appended
+ */
+ public static boolean isClientUserAgentUsingVersion() {
+ try {
+ return Boolean.getBoolean(getPrefixedPropertyName(CLIENT_USERAGENT_USEVERSION));
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * Returns the fully constructed default user-agent from system properties
+ * @return default user-agent
+ */
+ public static String getDefaultUserAgent() {
+ String name = getClientUserAgentName();
+ boolean useVersion = isClientUserAgentUsingVersion();
+ return useVersion ? UserAgentUtil.constructAgent(name, SlingClient.class.getPackage()) : name;
+ }
}
diff --git a/src/main/java/org/apache/sling/testing/clients/interceptors/UserAgentHolder.java b/src/main/java/org/apache/sling/testing/clients/interceptors/UserAgentHolder.java
new file mode 100644
index 0000000..4642cf0
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/clients/interceptors/UserAgentHolder.java
@@ -0,0 +1,56 @@
+/*******************************************************************************
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.sling.testing.clients.interceptors;
+
+import org.apache.commons.lang3.StringUtils;
+
+public class UserAgentHolder {
+
+ private UserAgentHolder() {}
+
+ private static final ThreadLocal<String> userAgent = new ThreadLocal<>();
+
+ /**
+ * Returns the current user-agent.
+ * @return the current user-agent
+ */
+ public static String get() {
+ return userAgent.get();
+ }
+
+ /**
+ * Override the current user-agent with a completely new one.
+ * @param agent the desired new user-agent (or null for default)
+ */
+ public static void set(String agent) {
+ if (StringUtils.isBlank(agent)) {
+ reset(); // don't store whitespace
+ return;
+ }
+ userAgent.set(agent);
+ }
+
+
+ /**
+ * Remove value of the user-agent
+ */
+ public static void reset() {
+ userAgent.remove();
+ }
+}
diff --git a/src/main/java/org/apache/sling/testing/clients/interceptors/UserAgentInterceptor.java b/src/main/java/org/apache/sling/testing/clients/interceptors/UserAgentInterceptor.java
new file mode 100644
index 0000000..7b1f064
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/clients/interceptors/UserAgentInterceptor.java
@@ -0,0 +1,48 @@
+/*
+ * 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.sling.testing.clients.interceptors;
+
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpRequestInterceptor;
+import org.apache.http.protocol.HttpContext;
+import org.apache.sling.testing.clients.SystemPropertiesConfig;
+import org.slf4j.LoggerFactory;
+
+public class UserAgentInterceptor implements HttpRequestInterceptor {
+
+ private final org.slf4j.Logger log = LoggerFactory.getLogger(getClass());
+
+ public static final String USER_AGENT_HEADER = "User-Agent";
+
+ public void process(HttpRequest httpRequest, HttpContext httpContext) {
+ if (UserAgentHolder.get() == null) {
+ return;
+ }
+
+ // handle existing user-agent header
+ if (httpRequest.containsHeader(USER_AGENT_HEADER)) {
+ if (!httpRequest.getFirstHeader(USER_AGENT_HEADER).getValue().equals(SystemPropertiesConfig.getDefaultUserAgent())) {
+ log.warn("User-agent of client-request changed manually; use CustomUserAgentRule instead!");
+ return;
+ }
+ httpRequest.removeHeaders(USER_AGENT_HEADER);
+ }
+
+ // add custom user agent
+ httpRequest.addHeader(USER_AGENT_HEADER, UserAgentHolder.get());
+ }
+}
diff --git a/src/main/java/org/apache/sling/testing/clients/interceptors/package-info.java b/src/main/java/org/apache/sling/testing/clients/interceptors/package-info.java
index 4279cef..fcd4341 100644
--- a/src/main/java/org/apache/sling/testing/clients/interceptors/package-info.java
+++ b/src/main/java/org/apache/sling/testing/clients/interceptors/package-info.java
@@ -17,7 +17,7 @@
* under the License.
*/
-@Version("1.2.0")
+@Version("1.3.0")
package org.apache.sling.testing.clients.interceptors;
import org.osgi.annotation.versioning.Version;
diff --git a/src/main/java/org/apache/sling/testing/clients/package-info.java b/src/main/java/org/apache/sling/testing/clients/package-info.java
index a95a378..a75b4ed 100644
--- a/src/main/java/org/apache/sling/testing/clients/package-info.java
+++ b/src/main/java/org/apache/sling/testing/clients/package-info.java
@@ -17,7 +17,7 @@
* under the License.
*/
-@Version("3.0.1")
+@Version("3.1.0")
package org.apache.sling.testing.clients;
import org.osgi.annotation.versioning.Version;
diff --git a/src/main/java/org/apache/sling/testing/clients/util/UserAgentUtil.java b/src/main/java/org/apache/sling/testing/clients/util/UserAgentUtil.java
new file mode 100644
index 0000000..90a911c
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/clients/util/UserAgentUtil.java
@@ -0,0 +1,53 @@
+/*
+ * 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.sling.testing.clients.util;
+
+public class UserAgentUtil {
+
+ private UserAgentUtil() {}
+
+ /**
+ * Create user-agent with simple class name.
+ * @param clazz the class
+ */
+ @SuppressWarnings("UnusedReturnValue")
+ public static String constructAgent(Class<?> clazz) {
+ return constructAgent(clazz.getSimpleName(), clazz.getPackage());
+ }
+
+ /**
+ * Create user-agent from name and determine version through implementation version of the provided package.
+ * In case the implementation version of the package returns null, the version info is omitted.
+ * @param name the user-agent name
+ * @param pkg the package
+ */
+ public static String constructAgent(String name, Package pkg) {
+ return constructAgent(name, pkg.getImplementationVersion());
+ }
+
+ /**
+ * Create user-agent from name and version-string [name]/[version].
+ * @param name the user-agent name
+ * @param version the user-agent version (or null to use name only)
+ */
+ public static String constructAgent(String name, String version) {
+ if (version == null) {
+ return name;
+ }
+ return name + "/" + version;
+ }
+}
diff --git a/src/main/java/org/apache/sling/testing/clients/util/package-info.java b/src/main/java/org/apache/sling/testing/clients/util/package-info.java
index 5c5d2ae..3b2a41d 100644
--- a/src/main/java/org/apache/sling/testing/clients/util/package-info.java
+++ b/src/main/java/org/apache/sling/testing/clients/util/package-info.java
@@ -17,7 +17,7 @@
* under the License.
*/
-@Version("3.0.0")
+@Version("3.1.0")
package org.apache.sling.testing.clients.util;
import org.osgi.annotation.versioning.Version;
diff --git a/src/test/java/org/apache/sling/testing/CustomUserAgentInterceptorTest.java b/src/test/java/org/apache/sling/testing/CustomUserAgentInterceptorTest.java
new file mode 100644
index 0000000..af02ff8
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/CustomUserAgentInterceptorTest.java
@@ -0,0 +1,122 @@
+/*
+ * 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.sling.testing;
+
+import org.apache.http.entity.StringEntity;
+import org.apache.sling.testing.clients.*;
+import org.apache.sling.testing.clients.interceptors.UserAgentHolder;
+import org.apache.sling.testing.clients.interceptors.UserAgentInterceptor;
+import org.junit.*;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class CustomUserAgentInterceptorTest {
+
+ private static final String PATH = "/mirror";
+
+ private static final String USER_AGENT_HEADER = "User-Agent";
+ private static final String MODIFIED_AGENT = "modified-agent";
+ private static final String CUSTOM_AGENT = "test-client";
+
+ @ClassRule
+ public static HttpServerRule httpServer = new HttpServerRule() {
+ @Override
+ protected void registerHandlers() {
+ serverBootstrap.registerHandler(PATH, (request, response, context) -> {
+ response.setEntity(new StringEntity("Success!"));
+ response.setStatusCode(200);
+ response.setHeaders(request.getHeaders(USER_AGENT_HEADER));
+ });
+ }
+ };
+ private static SlingClient client;
+
+ @BeforeClass
+ public static void beforeClass() throws ClientException {
+ client = createClient();
+ }
+
+ @After
+ public void after() {
+ UserAgentHolder.reset();
+ }
+
+ @Test
+ public void testDefault() throws ClientException {
+ assertUserAgent(client, SystemPropertiesConfig.getDefaultUserAgent());
+ }
+
+ @Test
+ public void testCustom() throws ClientException {
+ UserAgentHolder.set(CUSTOM_AGENT);
+ assertUserAgent(client, CUSTOM_AGENT);
+ }
+
+ @Test
+ public void testManualModify() throws ClientException {
+ SlingClient clientModified = createClientWithBakedInUserAgent(MODIFIED_AGENT);
+ UserAgentHolder.set(CUSTOM_AGENT);
+ assertUserAgent(clientModified, MODIFIED_AGENT);
+ }
+
+ @Test
+ public void testWhitespace() throws ClientException {
+ UserAgentHolder.set(" ");
+ assertUserAgent(client, SystemPropertiesConfig.getDefaultUserAgent());
+ }
+
+ /**
+ * Sends a dummy request to the test-server to see if the response contains a specified user-agent header
+ * to assert whether the requests contained the user-agent as well.
+ * @param client the {@link SlingClient} to be used for sending the request
+ * @param userAgent the expected user-agent as a string
+ * @throws ClientException in case of request failure
+ */
+ private static void assertUserAgent(SlingClient client, String userAgent) throws ClientException {
+ SlingHttpResponse response = client.doGet(PATH, 200);
+ assertTrue(response.containsHeader(USER_AGENT_HEADER));
+ assertEquals(userAgent, response.getFirstHeader(USER_AGENT_HEADER).getValue());
+ }
+
+ /**
+ * Creates a simple {@link SlingClient} with the {@link UserAgentInterceptor}.
+ * @return {@link SlingClient} instance
+ * @throws ClientException in case of failure during client creation
+ */
+ private static SlingClient createClient() throws ClientException {
+ return createClientWithBakedInUserAgent(null);
+ }
+
+ /**
+ * Creates a simple {@link SlingClient} with the {@link UserAgentInterceptor} and a manually baked-in user-agent.
+ * @param userAgent user-agent of the client as a {@link String} (or null to omit it)
+ * @return {@link SlingClient} instance
+ * @throws ClientException in case of failure during client creation
+ */
+ private static SlingClient createClientWithBakedInUserAgent(String userAgent) throws ClientException {
+ SlingClient.InternalBuilder<SlingClient> builder = SlingClient.Builder
+ .create(httpServer.getURI(), "user", "pass")
+ .addInterceptorLast(new UserAgentInterceptor());
+
+ if (userAgent != null) {
+ builder.httpClientBuilder().setUserAgent(MODIFIED_AGENT);
+ }
+
+ return builder.build();
+ }
+}
diff --git a/src/test/java/org/apache/sling/testing/util/UserAgentUtilTest.java b/src/test/java/org/apache/sling/testing/util/UserAgentUtilTest.java
new file mode 100644
index 0000000..28a5095
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/util/UserAgentUtilTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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.sling.testing.util;
+
+import org.apache.sling.testing.clients.util.UserAgentUtil;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class UserAgentUtilTest {
+
+ private static final String AGENT_TITLE = "test-agent";
+ private static final String AGENT_VERSION = "1.2.3";
+ private static final String AGENT = AGENT_TITLE + "/" + AGENT_VERSION;
+
+ @Test
+ public void constructAgent() {
+ String agent = UserAgentUtil.constructAgent(AGENT_TITLE, AGENT_VERSION);
+ assertEquals(AGENT, agent);
+ }
+
+ @Test
+ public void constructAgentWithoutNullVersion() {
+ String agent = UserAgentUtil.constructAgent(AGENT_TITLE, (String) null);
+ assertEquals(AGENT_TITLE, agent);
+ }
+
+ @Test
+ public void constructAgentFromClass() {
+ // just test for no exceptions
+ UserAgentUtil.constructAgent(String.class);
+ }
+}