Skip to content

Commit ffb8a4c

Browse files
author
Alessandro
committed
Added custom user-agent containing OS and client version
1 parent c23b6d4 commit ffb8a4c

9 files changed

Lines changed: 147 additions & 12 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
3+
Copyright (c) 2015, Alessandro Pieri
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
2. Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
26+
The views and conclusions contained in the software and documentation are those
27+
of the authors and should not be interpreted as representing official policies,
28+
either expressed or implied, of the FreeBSD Project.
29+
30+
*/
31+
package io.getstream.client.util;
32+
33+
import com.google.common.io.Resources;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
36+
37+
import java.io.IOException;
38+
import java.io.InputStream;
39+
import java.util.Properties;
40+
41+
/**
42+
* Read the properties carved at build time into the jar artifact.
43+
*/
44+
public class InfoUtil {
45+
46+
private static final Logger LOG = LoggerFactory.getLogger(InfoUtil.class);
47+
48+
public static final String VERSION = "version";
49+
public static final String STREAM_JAVA_INFO = "/stream-java.info";
50+
51+
/**
52+
* Read the build properties carved into the jar.
53+
* @return Build properties
54+
*/
55+
public static Properties getProperties() {
56+
Properties properties = new Properties();
57+
try (InputStream inputStream = Resources.asByteSource(InfoUtil.class.getResource(STREAM_JAVA_INFO)).openStream()) {
58+
properties.load(inputStream);
59+
} catch (IOException e) {
60+
LOG.error("Failed to read the properties file " + STREAM_JAVA_INFO + "from the jar", e);
61+
}
62+
return properties;
63+
}
64+
}

‎stream-repo-apache/pom.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,13 @@
7171
<scope>test</scope>
7272
</dependency>
7373
</dependencies>
74+
75+
<build>
76+
<resources>
77+
<resource>
78+
<directory>src/main/resources</directory>
79+
<filtering>true</filtering>
80+
</resource>
81+
</resources>
82+
</build>
7483
</project>

‎stream-repo-apache/src/main/java/io/getstream/client/repo/StreamRepositoryImpl.java‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ public class StreamRepositoryImpl implements StreamRepository {
7474

7575
static final String API_KEY = "api_key";
7676

77-
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().setPropertyNamingStrategy(
78-
/* will convert camelStyle to lower_case_style */
79-
PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
77+
/* will convert camelStyle to lower_case_style */
78+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().setPropertyNamingStrategy(
79+
PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
8080

8181
private final URI baseEndpoint;
8282
private final String apiKey;
@@ -151,15 +151,15 @@ public List<FeedFollow> getFollowers(BaseFeed feed, FeedFilter filter) throws St
151151
}
152152
}
153153

154-
@Override
155-
public void deleteActivityById(BaseFeed feed, String activityId) throws IOException, StreamClientException {
156-
streamActivityRepository.deleteActivityById(feed, activityId);
157-
}
154+
@Override
155+
public void deleteActivityById(BaseFeed feed, String activityId) throws IOException, StreamClientException {
156+
streamActivityRepository.deleteActivityById(feed, activityId);
157+
}
158158

159-
@Override
160-
public void deleteActivityByForeignId(BaseFeed feed, String foreignId) throws IOException, StreamClientException {
161-
streamActivityRepository.deleteActivityByForeignId(feed, foreignId);
162-
}
159+
@Override
160+
public void deleteActivityByForeignId(BaseFeed feed, String foreignId) throws IOException, StreamClientException {
161+
streamActivityRepository.deleteActivityByForeignId(feed, foreignId);
162+
}
163163

164164
@Override
165165
public <T extends BaseActivity> StreamResponse<T> getActivities(BaseFeed feed, Class<T> type, FeedFilter filter) throws IOException, StreamClientException {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
artifactId=${project.artifactId}
2+
groupId=${project.groupId}
3+
version=${project.version}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.getstream.client.repo.utils;
2+
3+
public class SignatureUtilsTest {
4+
5+
6+
7+
}

‎stream-repo-okhttp/pom.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,13 @@
7272
</dependency>
7373
</dependencies>
7474

75+
<build>
76+
<resources>
77+
<resource>
78+
<directory>src/main/resources</directory>
79+
<filtering>true</filtering>
80+
</resource>
81+
</resources>
82+
</build>
83+
7584
</project>

‎stream-repo-okhttp/src/main/java/client/repo/StreamRepoFactoryImpl.java‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,33 @@
3131
package client.repo;
3232

3333
import com.squareup.okhttp.ConnectionPool;
34+
import com.squareup.okhttp.Interceptor;
3435
import com.squareup.okhttp.OkHttpClient;
36+
import com.squareup.okhttp.Response;
3537
import io.getstream.client.config.AuthenticationHandlerConfiguration;
3638
import io.getstream.client.config.ClientConfiguration;
3739
import io.getstream.client.repo.StreamRepoFactory;
3840
import io.getstream.client.repo.StreamRepository;
41+
import io.getstream.client.util.InfoUtil;
3942

43+
import java.io.IOException;
4044
import java.util.concurrent.TimeUnit;
4145

4246
/**
4347
* Create a new StreamRepository using the ApacheHttpClient.
4448
*/
4549
public class StreamRepoFactoryImpl implements StreamRepoFactory {
46-
@Override
50+
51+
private static final String USER_AGENT_PREFIX = "okhttp stream-java %s v%s";
52+
53+
private final String userAgent;
54+
55+
public StreamRepoFactoryImpl() {
56+
this.userAgent = String.format(USER_AGENT_PREFIX, System.getProperty("os.name"),
57+
InfoUtil.getProperties().getProperty(InfoUtil.VERSION));
58+
}
59+
60+
@Override
4761
public StreamRepository newInstance(ClientConfiguration clientConfiguration,
4862
AuthenticationHandlerConfiguration authenticationHandlerConfiguration) {
4963
return new StreamRepositoryImpl(clientConfiguration, initClient(clientConfiguration));
@@ -55,7 +69,18 @@ private OkHttpClient initClient(final ClientConfiguration config) {
5569
client.setReadTimeout(config.getTimeout(), TimeUnit.MILLISECONDS);
5670
client.setWriteTimeout(config.getTimeout(), TimeUnit.MILLISECONDS);
5771
client.setRetryOnConnectionFailure(true);
72+
client.interceptors().add(new UserAgentInterceptor());
5873
client.setConnectionPool(new ConnectionPool(config.getMaxConnections(), config.getKeepAlive()));
5974
return client;
6075
}
76+
77+
/**
78+
* Add custom user-agent to the request.
79+
*/
80+
class UserAgentInterceptor implements Interceptor {
81+
@Override
82+
public Response intercept(Chain chain) throws IOException {
83+
return chain.proceed(chain.request().newBuilder().header("User-Agent", userAgent).build());
84+
}
85+
}
6186
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
artifactId: ${project.artifactId}
2+
groupId: ${project.groupId}
3+
version: ${project.version}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.getstream.client.util;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.any;
6+
import static org.hamcrest.CoreMatchers.is;
7+
import static org.hamcrest.MatcherAssert.assertThat;
8+
9+
public class InfoUtilTest {
10+
11+
@Test
12+
public void shouldGetProperties() {
13+
assertThat(InfoUtil.getProperties().getProperty(InfoUtil.VERSION), is(any(String.class)));
14+
}
15+
}

0 commit comments

Comments
 (0)