Permalink
Browse files

Send a proper user-agent header

  • Loading branch information...
1 parent 8642257 commit ea73bb05f37650f5fabdefcff3d536d54104d12e @shred committed Aug 21, 2016
View
@@ -29,6 +29,17 @@
<artifactId>flattr4j-core</artifactId>
<name>flattr4j core</name>
<description>Core Module of flattr4j</description>
+ <build>
+ <resources>
+ <resource>
+ <directory>src/main/resources</directory>
+ </resource>
+ <resource>
+ <directory>src/main/resources-filtered</directory>
+ <filtering>true</filtering>
+ </resource>
+ </resources>
+ </build>
<dependencies>
<dependency>
<groupId>org.json</groupId>
@@ -37,6 +37,7 @@
import java.util.Collections;
import java.util.Date;
import java.util.List;
+import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
@@ -60,6 +61,8 @@
import org.shredzone.flattr4j.oauth.AccessToken;
import org.shredzone.flattr4j.oauth.ConsumerKey;
+import android.os.Build;
+
/**
* Default implementation of {@link Connection}.
*
@@ -71,6 +74,7 @@
private static final int TIMEOUT = 10000;
private static final Pattern CHARSET = Pattern.compile(".*?charset=\"?(.*?)\"?\\s*(;.*)?", Pattern.CASE_INSENSITIVE);
private static final String BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ private static final String USER_AGENT;
private String baseUrl;
private String call;
@@ -82,6 +86,27 @@
private StringBuilder formParams;
private RateLimit limit;
+ static {
+ StringBuilder agent = new StringBuilder("flattr4j");
+ try {
+ Properties prop = new Properties();
+ prop.load(FlattrConnection.class.getResourceAsStream("/org/shredzone/flattr4j/version.properties"));
+ agent.append('/').append(prop.getProperty("version"));
+ } catch (IOException ex) {
+ // Ignore, just don't use a version
+ }
+
+ try {
+ String release = Build.VERSION.RELEASE;
+ agent.append(" Android/").append(release);
+ } catch (Throwable t) {
+ // We're not running on Android...
+ agent.append(" Java/").append(System.getProperty("java.version"));
+ }
+
+ USER_AGENT = agent.toString();
+ }
+
/**
* Creates a new {@link FlattrConnection} for the given {@link RequestType}.
*
@@ -447,7 +472,7 @@ protected HttpURLConnection createConnection(URL url) throws IOException {
conn.setConnectTimeout(TIMEOUT);
conn.setReadTimeout(TIMEOUT);
conn.setUseCaches(false);
- conn.setRequestProperty("User-Agent", "flattr4j");
+ conn.setRequestProperty("User-Agent", USER_AGENT);
return conn;
}

0 comments on commit ea73bb0

Please sign in to comment.